4. Selections - 24 Questions

# 4. Selections

24 Questions
Created by Y. Daniel Liang
Free
##### Take this quiz to improve your knowledge and crack interviews or competitive exams.
1. The "less than or equal to" comparison operator is __________.

<
<=
=<
<<

View Answer | Discuss in forum
2. The equal comparison operator is __________.

<>
!=
==
=

View Answer | Discuss in forum
3. To generate a random integer between 0 and 5, use ________________.

random.randint(0, 5)
random.randint(0, 6)
random.randrange(0, 5)
All of the mentioned

View Answer | Discuss in forum
4. random.randint(0, 1) returns ____________.

0
1
0 or 1
2

View Answer | Discuss in forum
5. random.random() returns ____________.

a float number i such that 0 < i < 1.0
a float number i such that 0 <= i < 1.0
a float number i such that 0 <= i <= 1.0
a float number i such that 0 < i < 2.0

View Answer | Discuss in forum
6. Which of the following code displays the area of a circle if the radius is positive.

if radius != 0: print(radius * radius * 3.14159)
if radius >= 0: print(radius * radius * 3.14159)
if radius > 0: print(radius * radius * 3.14159)
if radius <= 0: print(radius * radius * 3.14159)

View Answer | Discuss in forum
7. What is the output of the following code?

x = 0
if x < 4:
x = x + 1

print("x is", x)

x is 0
x is 1
x is 2
x is 3

View Answer | Discuss in forum
8. Suppose isPrime is a boolean variable, which of the following is the correct and best statement for testing if isPrime is true.

if isPrime = True:
if isPrime == True:
if isPrime:
if not isPrime = False:

View Answer | Discuss in forum
9. Analyze the following code:

even = False
if even = True:
print("It is even!")

The program has a syntax error in line 1 (even = False)
The program has a syntax error in line 2 if even = True is not a correct condition. It should be replaced by if even == True: or if even:.
The program runs, but displays nothing.
The program runs and displays It is even!.

View Answer | Discuss in forum
10. Suppose x = 1, y = -1, and z = 1. What will be displayed by the following statement?

if x > 0:
if y > 0:
print("x > 0 and y > 0")
elif z > 0:
print("x < 0 and z > 0")

x > 0 and y > 0
x < 0 and z > 0
x < 0 and z < 0
nothing displayed

View Answer | Discuss in forum
11. The following code displays ___________.

temperature = 50

if temperature >= 100:
print("too hot")
elif temperature <= 40:
print("too cold")
else:
print("just right")

too hot
too cold
just right
too hot too cold just right

View Answer | Discuss in forum
12. Analyze the following code:

Code 1:

if number % 2 == 0:
even = True
else:
even = False

Code 2:

even = number % 2 == 0

Code 1 has compile errors.
Code 2 has compile errors.
Both Code 1 and Code 2 have compile errors.
Both Code 1 and Code 2 are correct, but Code 2 is better.

View Answer | Discuss in forum
13. Suppose income is 4001, what will be displayed by f the following code?

if income > 3000:
print("Income is greater than 3000")
elif income > 4000:
print("Income is greater than 4000")

Income is greater than 3000
Income is greater than 3000 followed by Income is greater than 4000
Income is greater than 4000
Income is greater than 4000 followed by Income is greater than 3000

View Answer | Discuss in forum
14. The __________ function immediately terminates the program.

sys.terminate()
sys.halt()
sys.exit()
sys.stop()

View Answer | Discuss in forum
15. Given |x - 2| <= 4, Which of the following is true?

x - 2 <= 4 and x - 2 >= 4
x - 2 <= 4 and x - 2 > -4
x - 2 <= 4 and x - 2 >= -4
x - 2 <= 4 or x - 2 >= -4

View Answer | Discuss in forum
16. Assume x = 4 and y = 5, Which of the following is true?

x < 5 and y < 5
x < 5 or y < 5
x > 5 and y > 5
x > 5 or y > 5

View Answer | Discuss in forum
17. Assume x = 4 and y = 5, Which of the following is true?

not (x == 4)
x != 4
x == 5
x != 5

View Answer | Discuss in forum
18. Assume x = 14 and y = 15, Which of the following is true?

x % 2 == 0 and y % 2 == 0
x % 2 == 0 and y % 2 == 1
x % 2 == 0 or y % 2 == 1
x % 2 != 0 and y % 2 != 0

View Answer | Discuss in forum
19. What will be displayed by the following code?

ch = 'F'
if ch >= 'A' and ch <= 'Z':
print(ch)

F
f
nothing
F f

View Answer | Discuss in forum
20. What is y after the following statement is executed?

x = 0
y = 10 if x > 0 else -10

-10
0
10
20

View Answer | Discuss in forum
21. Analyze the following code fragments that assign a boolean value to the variable even.

Code 1:
if number % 2 == 0:
even = True
else:
even = False

Code 2:
even = True if number % 2 == 0 else False

Code 3:
even = number % 2 == 0

Code 2 has a syntax error, because you cannot have True and False literals in the conditional expression.
Code 3 has a syntax error, because you attempt to assign number to even.
All three are correct, but Code 1 is preferred.
All three are correct, but Code 3 is preferred.

View Answer | Discuss in forum
22. What will be displayed by the following code?

isCorrect = False
print("Correct" if isCorrect else "Incorrect")

Correct
Incorrect
nothing
Correct Incorrect

View Answer | Discuss in forum
23. The order of the precedence (from high to low) of the operators +, *, and, or is:

and, or, *, +
*, +, and, or
*, +, and, or
*, +, or, and

View Answer | Discuss in forum
24. Which of the following operators are right-associative?

*
+
%
=

View Answer | Discuss in forum