PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Medium
Question 9
Q208 - Operators
What is the expected output of the following code?
- x = True
- y = False
- x = x or y
- y = x and y
- x = x or y
- print(x, y)
-
A
False False -
B
False True -
C
True True -
D
True False
Reveal correct answer
Correct answer: D
Explanation
Topics: logical operators boolean
Try it yourself:
- x = True
- y = False
- x = x or y # True or False -> True
- y = x and y # True and False -> False
- x = x or y # True or False -> True
- print(x, y) # True False
Explanation:
Nothing changes here.
The variables always get assigned the value they had before.
Q208 (Please refer to this number, if you want to write me about this question.)
A. The code first assigns the value of x to True and y to False. Then, it updates the value of x by performing a logical OR operation between x and y, resulting in x being True. Next, it updates the value of y by performing a logical AND operation between the updated x (True) and the original y (False), resulting in y being False. Finally, it updates the value of x again by performing a logical OR operation between the updated x (True) and the updated y (False), resulting in x being True. Therefore, the output will be True False.
B. The code first assigns the value of x to True and y to False. Then, it updates the value of x by performing a logical OR operation between x and y, resulting in x being True. Next, it updates the value of y by performing a logical AND operation between the updated x (True) and the original y (False), resulting in y being False. Finally, it prints the values of x and y, which are True and False respectively, leading to the output False True.
C. The code first assigns the value of x to True and y to False. Then, it updates the value of x by performing a logical OR operation between x and y, resulting in x being True. Next, it updates the value of y by performing a logical AND operation between the updated x (True) and the original y (False), resulting in y being False. Finally, it updates the value of x again by performing a logical OR operation between the updated x (True) and the updated y (False), resulting in x being True. Therefore, the output will be True False.
D. The code first assigns the value of x to True and y to False. Then, it updates the value of x by performing a logical OR operation between x and y, resulting in x being True. Next, it updates the value of y by performing a logical AND operation between the updated x (True) and the original y (False), resulting in y being False. Finally, it updates the value of x again by performing a logical OR operation between the updated x (True) and the updated y (False), resulting in x being True and y being False, hence the output True False.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
