PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Medium
Question 60
Q540 - Operators
What is the data type of x, y, z after executing the following snippet?
- x = 23 + 42
- y = '23' + '42'
- z = '23' * 7
-
A
int, str, int
-
B
xis int,yandzare invalid declarations -
C
int, int, int
-
D
int, str, str
Reveal correct answer
Correct answer: D
Explanation
Topics: addition operator plus operator string concatenation
multiply operator string concatenation
Try it yourself:
- print(type(23 + 42)) # <class 'int'>
- print(type('23' + '42')) # <class 'str'>
- print(type('23' * 7)) # <class 'str'>
- print(23 + 42) # 65
- print('23' + '42') # 2342
- print('23' * 7) # 23232323232323
Explanation:
The first one is a normal addition.
The second one is a string concatenation by addition.
The third one is a string concatenation by multiplication.
Q540 (Please refer to this number, if you want to write me about this question.)
A. The data type of x is an integer because it results from the addition operation of two integers. The data type of y is a string because it results from the concatenation operation of two strings. However, the data type of z is not an integer, it is a string resulting from the repetition operation of a string.
B. The data type of x is an integer because it results from the addition operation of two integers. However, both y and z are valid declarations. y is a string resulting from the concatenation operation of two strings, and z is a string resulting from the repetition operation of a string.
C. The data type of x is an integer because it results from the addition operation of two integers. Both y and z are strings because y results from the concatenation operation of two strings, and z results from the repetition operation of a string.
D. The data type of x is an integer because it results from the addition operation of two integers. The data type of y is a string because it results from the concatenation operation of two strings. The data type of z is also a string because it results from the repetition operation of a string.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
