1
What is the result of the expression 23 % 5 in Python?
Reveal Answer
Hide Answer
Correct Answer: 3
Explanation:
The modulus operator (%) returns the remainder of the division. 23 divided by 5 is 4 with a remainder of 3.
2
Which Python module must be imported to generate random numbers?
A. random
B. math
C. rand
D. stats
Reveal Answer
Hide Answer
Correct Answer: random
Explanation:
The 'random' module is the standard Python module used to generate pseudo-random numbers.
3
What is the output of the following code?
print(type(3 < 5))
A. <class 'str'>
B. <class 'int'>
C. <class 'float'>
D. <class 'bool'>
Reveal Answer
Hide Answer
Correct Answer: <class 'bool'>
Explanation:
The expression 3 < 5 evaluates to True, which is a Boolean value.
4
Which logical operator returns True only if both operands are True?
A. not
B. xor
C. or
D. and
Reveal Answer
Hide Answer
Correct Answer: and
Explanation:
The 'and' operator requires both conditions on its left and right to be True to return True.
5
What is the result of: True or False and False?
A. Error
B. True
C. False
D. None
Reveal Answer
Hide Answer
Correct Answer: True
Explanation:
The 'and' operator has higher precedence than 'or'. False and False is False. Then, True or False is True.
6
In Python, what does the function random.randint(1, 10) return?
A. A float between 1 and 10
B. An integer between 1 and 10 (inclusive)
C. An integer between 0 and 10
D. An integer between 1 and 9
Reveal Answer
Hide Answer
Correct Answer: An integer between 1 and 10 (inclusive)
Explanation:
Unlike range(), random.randint(a, b) includes both endpoints a and b.
7
What is the correct syntax for a conditional statement in Python?
A. if (x > 5) { }
B. if x > 5 then:
C. if x > 5:
D. if x > 5;
Reveal Answer
Hide Answer
Correct Answer: if x > 5:
Explanation:
Python uses the keyword 'if', followed by the condition, and ends with a colon.
8
What signifies a block of code belonging to an if-statement in Python?
A. Indentation
B. Parentheses ()
C. Curly braces {}
D. Semicolons
Reveal Answer
Hide Answer
Correct Answer: Indentation
Explanation:
Python relies on indentation (whitespace) to define scope and blocks of code.
9
What will be the output of the following code?
x = 10
if x > 5:
print('A')
elif x > 8:
print('B')
else:
print('C')
A. A
B. C
C. B
D. A and B
Reveal Answer
Hide Answer
Correct Answer: A
Explanation:
Python executes the first True condition in an if-elif chain. Since 10 > 5 is True, it prints 'A' and skips the rest.
10
Which statement allows you to check multiple conditions in a single if-block structure?
A. then
B. elseif
C. else
D. elif
Reveal Answer
Hide Answer
Correct Answer: elif
Explanation:
The 'elif' (short for else if) keyword allows checking subsequent conditions if the previous ones were False.
11
What is a nested conditional?
A. A condition that uses the 'and' operator
B. A loop inside an if statement
C. A conditional statement that returns a list
D. An if statement inside another if statement
Reveal Answer
Hide Answer
Correct Answer: An if statement inside another if statement
Explanation:
Nesting occurs when one control structure is placed inside the block of another, such as an if inside an if.
12
What is the output of -5 % 2 in Python?
Reveal Answer
Hide Answer
Correct Answer: 1
Explanation:
In Python, the sign of the remainder matches the divisor. -5 divided by 2 is -3 remainder 1 (-3 * 2 + 1 = -5).
13
Which loop is best used when the number of iterations is not known in advance?
A. for loop
B. repeat loop
C. do-while loop
D. while loop
Reveal Answer
Hide Answer
Correct Answer: while loop
Explanation:
A while loop continues running as long as a specific condition remains True, making it ideal for indefinite iteration.
14
How many times will the following loop execute?
i = 0
while i < 3:
print(i)
i += 1
A. 2 times
B. 3 times
C. 4 times
D. Infinite
Reveal Answer
Hide Answer
Correct Answer: 3 times
Explanation:
The loop runs for i=0, i=1, and i=2. When i becomes 3, the condition 3 < 3 is False.
15
What does the 'break' statement do inside a loop?
A. Restarts the loop
B. Skips the current iteration
C. Pauses the loop
D. Terminates the loop immediately
Reveal Answer
Hide Answer
Correct Answer: Terminates the loop immediately
Explanation:
The break statement exits the nearest enclosing loop immediately, skipping any remaining code in the block.
16
What is the output of the following?
for i in range(3):
print(i, end=' ')
A. 1 2
B. 1 2 3
C. 0 1 2 3
D. 0 1 2
Reveal Answer
Hide Answer
Correct Answer: 0 1 2
Explanation:
range(3) generates numbers from 0 up to (but not including) 3.
17
Which parameters does range(start, stop, step) take?
A. start, length, step
B. start, end, jump
C. start, stop, increment
D. start, stop, decrement
Reveal Answer
Hide Answer
Correct Answer: start, stop, increment
Explanation:
The third argument in range() is the step (or increment) value.
18
What is the result of range(10, 5, -2) when converted to a list?
A. [10, 8, 6, 4]
B. [10, 8, 6]
C. [10, 9, 8, 7, 6]
D. []
Reveal Answer
Hide Answer
Correct Answer: [10, 8, 6]
Explanation:
It starts at 10 and decreases by 2 until it reaches (but does not include) 5. The sequence is 10, 8, 6.
19
In a nested loop, which loop completes its iterations first?
A. It depends on the condition
B. The outer loop
C. They complete simultaneously
D. The inner loop
Reveal Answer
Hide Answer
Correct Answer: The inner loop
Explanation:
For every single iteration of the outer loop, the inner loop runs to completion.
20
What is the output of this nested loop?
for i in range(2):
for j in range(2):
print(i + j, end='')
A. 0123
B. 1234
C. 0011
D. 0112
Reveal Answer
Hide Answer
Correct Answer: 0112
Explanation:
Iterations: (i=0,j=0 -> 0), (i=0,j=1 -> 1), (i=1,j=0 -> 1), (i=1,j=1 -> 2). Result: 0112.
21
What is 'Encapsulation' in the context of basic programming structures?
A. Wrapping a piece of code (like a loop or conditional) into a function
B. Writing all code in one line
C. Deleting variables
D. Running a loop infinitely
Reveal Answer
Hide Answer
Correct Answer: Wrapping a piece of code (like a loop or conditional) into a function
Explanation:
Encapsulation involves enclosing a set of instructions within a function to isolate logic and hide complexity.
22
What does 'Generalization' mean when refactoring code?
A. Adding parameters to a function to make it applicable to a wider range of problems
B. Making code specific to one problem
C. Replacing variables with constants
D. Removing all comments
Reveal Answer
Hide Answer
Correct Answer: Adding parameters to a function to make it applicable to a wider range of problems
Explanation:
Generalization involves replacing specific values (literals) with variables (parameters) so the code can handle various inputs.
23
What happens if you forget to increment the counter variable in a standard while loop?
A. Infinite Loop
B. The loop runs once
C. Syntax Error
D. The loop will not run
Reveal Answer
Hide Answer
Correct Answer: Infinite Loop
Explanation:
If the condition remains True forever because the variable is never updated, the loop never terminates.
24
What does the function random.random() return?
A. A float between 0.0 and 1.0 (excluding 1.0)
B. An integer between 0 and 1
C. A random boolean
D. A float between 0.0 and 100.0
Reveal Answer
Hide Answer
Correct Answer: A float between 0.0 and 1.0 (excluding 1.0)
Explanation:
random.random() returns a floating-point number in the range [0.0, 1.0).
25
Which keyword is used to skip the rest of the code in the current loop iteration and return to the top?
A. return
B. continue
C. break
D. pass
Reveal Answer
Hide Answer
Correct Answer: continue
Explanation:
The 'continue' statement stops the current iteration and jumps back to the loop condition check for the next iteration.
26
What is the truth value of the integer 0 in Python Boolean expressions?
A. False
B. True
C. Undefined
D. None
Reveal Answer
Hide Answer
Correct Answer: False
Explanation:
In Python, integer 0 is considered 'falsy'. Non-zero integers are 'truthy'.
27
If x = 10 and y = 20, which expression evaluates to True?
A. x == y
B. x > y
C. not(x < y)
D. x != y
Reveal Answer
Hide Answer
Correct Answer: x != y
Explanation:
10 is not equal to 20, so x != y is True.
28
Which of the following is a correct way to generate a random choice from a list?
A. random.pick(list)
B. list.random()
C. random.select(list)
D. random.choice(list)
Reveal Answer
Hide Answer
Correct Answer: random.choice(list)
Explanation:
random.choice(sequence) returns a random element from a non-empty sequence.
29
What is the purpose of the 'pass' statement in a conditional or loop?
A. Skips to the next loop
B. Stops the program
C. Returns a value
D. It is a null operation; nothing happens
Reveal Answer
Hide Answer
Correct Answer: It is a null operation; nothing happens
Explanation:
The pass statement is a placeholder used when a statement is required syntactically but no code needs to be executed.
30
How many stars () will be printed?
for i in range(3):
for j in range(2):
print(' ')
Reveal Answer
Hide Answer
Correct Answer: 6
Explanation:
The outer loop runs 3 times. For each outer iteration, the inner loop runs 2 times. Total = 3 * 2 = 6.
31
Which operator is used to check for equality?
Reveal Answer
Hide Answer
Correct Answer: ==
Explanation:
The '==' operator compares two values for equality. The '=' operator is for assignment.
32
Which of the following loops is generally preferred for iterating over a list?
A. infinite loop
B. for loop
C. while loop
D. nested while
Reveal Answer
Hide Answer
Correct Answer: for loop
Explanation:
For loops are designed to iterate over sequences (like lists or strings) directly.
33
What is the value of x after execution?
x = 0
while x < 5:
x = x + 2
Reveal Answer
Hide Answer
Correct Answer: 6
Explanation:
Steps: 0->2, 2->4, 4->6. When x is 6, 6 < 5 is False, so the loop stops.
34
In Python, can an 'else' clause be used with a 'for' loop?
A. No, never
B. Yes, it executes only if the loop breaks
C. Yes, it executes before the loop starts
D. Yes, it executes if the loop completes normally
Reveal Answer
Hide Answer
Correct Answer: Yes, it executes if the loop completes normally
Explanation:
A loop's 'else' block runs only if the loop finishes without encountering a 'break' statement.
35
What is the output of: not (5 > 4 or 3 < 2)?
A. Error
B. False
C. True
D. None
Reveal Answer
Hide Answer
Correct Answer: False
Explanation:
(5 > 4) is True. (3 < 2) is False. True or False is True. 'not True' is False.
36
Which statement best describes the 'modulus' operator applied to a float (e.g., 5.5 % 2)?
A. It converts inputs to integers first
B. It returns the quotient
C. It returns the remainder as a float
D. It causes an error
Reveal Answer
Hide Answer
Correct Answer: It returns the remainder as a float
Explanation:
Python supports modulus with floats. 5.5 % 2 results in 1.5.
37
Using random numbers in a loop to simulate a process many times (e.g., coin flips) is often called:
A. Infinite recursion
B. Debugging
C. Monte Carlo simulation
D. Encapsulation
Reveal Answer
Hide Answer
Correct Answer: Monte Carlo simulation
Explanation:
Monte Carlo simulations rely on repeated random sampling to obtain numerical results.
38
What does random.randrange(0, 10, 3) generate?
A. 0, 1, 2, 3
B. A random number from {0, 3, 6, 9, 10}
C. A random number from {0, 3, 6, 9}
D. Any number 0-10
Reveal Answer
Hide Answer
Correct Answer: A random number from {0, 3, 6, 9}
Explanation:
randrange follows start, stop, step logic. It selects a random value from the generated sequence.
39
Consider the following:
if condition:
if nested_condition:
do_something()
This is equivalent to:
A. while condition: do_something()
B. if condition and nested_condition: do_something()
C. if condition or nested_condition: do_something()
D. if not condition: do_something()
Reveal Answer
Hide Answer
Correct Answer: if condition and nested_condition: do_something()
Explanation:
Nested ifs where the second relies on the first can often be flattened using the 'and' operator.
40
What happens if you compare a string and an integer (e.g., '5' > 3) in Python 3?
A. TypeError
B. Returns False
C. Converts string to int automatically
D. Returns True
Reveal Answer
Hide Answer
Correct Answer: TypeError
Explanation:
Python 3 does not support ordering comparisons between incompatible types like string and integer.
41
Which of these is a valid use of a boolean flag in a while loop?
A. Using 'break' inside a for loop
B. Using a random number
C. Using the modulus operator
D. Using a variable set to True/False to control the while condition
Reveal Answer
Hide Answer
Correct Answer: Using a variable set to True/False to control the while condition
Explanation:
Flags are Boolean variables used to signal a condition (like 'running = True') to control loop termination.
42
What is the correct syntax to output a random float between 2.5 and 5.5?
A. random.uniform(2.5, 5.5)
B. random.float(2.5, 5.5)
C. random.range(2.5, 5.5)
D. random.randint(2.5, 5.5)
Reveal Answer
Hide Answer
Correct Answer: random.uniform(2.5, 5.5)
Explanation:
random.uniform(a, b) returns a random floating point number N such that a <= N <= b.
43
How does a 'nested while' loop behave?
A. Both loops share the same variables automatically
B. It is not allowed in Python
C. The outer loop finishes before the inner loop starts
D. The inner loop restarts from the beginning for each iteration of the outer loop
Reveal Answer
Hide Answer
Correct Answer: The inner loop restarts from the beginning for each iteration of the outer loop
Explanation:
Just like nested for loops, the inner while loop runs its full course for every single iteration of the outer while loop.
44
Which operator implies logical negation?
Reveal Answer
Hide Answer
Correct Answer: not
Explanation:
Python uses the English word 'not' for logical negation.
45
What is the output of the following code snippet?
count = 0
while count < 5:
if count == 3:
break
print(count, end='')
count += 1
A. 3
B. 0123
C. 01234
D. 012
Reveal Answer
Hide Answer
Correct Answer: 012
Explanation:
The loop prints 0, 1, 2. When count reaches 3, the break statement executes, exiting the loop before printing 3.
46
Why might you wrap a complex conditional logic inside a function (Encapsulation)?
A. To confuse the user
B. To increase file size
C. To make the code slower
D. To improve readability and reuse
Reveal Answer
Hide Answer
Correct Answer: To improve readability and reuse
Explanation:
Encapsulation hides complexity and gives the logic a meaningful name, making the main code easier to read and the logic reusable.
47
What defines an 'infinite loop'?
A. A loop that uses random numbers
B. A loop inside another loop
C. A loop whose condition never evaluates to False
D. A loop with a high number of iterations
Reveal Answer
Hide Answer
Correct Answer: A loop whose condition never evaluates to False
Explanation:
An infinite loop lacks a functional termination condition, causing it to run indefinitely.
48
When using random numbers to test a loop logic, what is a seed used for?
A. To make the random numbers predictable and reproducible
B. To encrypt the code
C. To generate true random numbers
D. To speed up the computer
Reveal Answer
Hide Answer
Correct Answer: To make the random numbers predictable and reproducible
Explanation:
random.seed() initializes the random number generator, ensuring the same sequence of numbers is produced for debugging.
49
What is the value of (10 % 3) ** 2?
Reveal Answer
Hide Answer
Correct Answer: 1
Explanation:
Operator precedence handles modulus first, then exponentiation. 10 % 3 = 1. 1 ** 2 = 1.
50
Which variable name is used by convention for a 'throwaway' variable in a for loop?
A. temp
B. var
C. _ (underscore)
D. i
Reveal Answer
Hide Answer
Correct Answer: _ (underscore)
Explanation:
In Python, the underscore (_) is conventionally used as a loop variable when the variable's value is not actually used inside the loop.