Unit 2 - Practice Quiz

INT108 50 Questions
0 Correct 0 Wrong 50 Left
0/50

1 What is the result of the expression 23 % 5 in Python?

A. 4.6
B. 2
C. 4
D. 3

2 Which Python module must be imported to generate random numbers?

A. random
B. math
C. stats
D. rand

3 What is the output of the following code?
print(type(3 < 5))

A. <class 'int'>
B. <class 'float'>
C. <class 'bool'>
D. <class 'str'>

4 Which logical operator returns True only if both operands are True?

A. xor
B. or
C. and
D. not

5 What is the result of: True or False and False?

A. False
B. Error
C. True
D. None

6 In Python, what does the function random.randint(1, 10) return?

A. A float between 1 and 10
B. An integer between 0 and 10
C. An integer between 1 and 9
D. An integer between 1 and 10 (inclusive)

7 What is the correct syntax for a conditional statement in Python?

A. if x > 5;
B. if x > 5:
C. if x > 5 then:
D. if (x > 5) { }

8 What signifies a block of code belonging to an if-statement in Python?

A. Semicolons
B. Parentheses ()
C. Indentation
D. Curly braces {}

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. C
B. A and B
C. A
D. B

10 Which statement allows you to check multiple conditions in a single if-block structure?

A. else
B. elseif
C. then
D. elif

11 What is a nested conditional?

A. An if statement inside another if statement
B. A conditional statement that returns a list
C. A loop inside an if statement
D. A condition that uses the 'and' operator

12 What is the output of -5 % 2 in Python?

A. 0
B. -1
C. 1
D. Error

13 Which loop is best used when the number of iterations is not known in advance?

A. repeat loop
B. for loop
C. while loop
D. do-while loop

14 How many times will the following loop execute?
i = 0
while i < 3:
print(i)
i += 1

A. 2 times
B. 4 times
C. 3 times
D. Infinite

15 What does the 'break' statement do inside a loop?

A. Skips the current iteration
B. Restarts the loop
C. Terminates the loop immediately
D. Pauses the loop

16 What is the output of the following?
for i in range(3):
print(i, end=' ')

A. 0 1 2 3
B. 0 1 2
C. 1 2 3
D. 1 2

17 Which parameters does range(start, stop, step) take?

A. start, end, jump
B. start, stop, increment
C. start, stop, decrement
D. start, length, step

18 What is the result of range(10, 5, -2) when converted to a list?

A. [10, 8, 6]
B. []
C. [10, 8, 6, 4]
D. [10, 9, 8, 7, 6]

19 In a nested loop, which loop completes its iterations first?

A. It depends on the condition
B. The inner loop
C. They complete simultaneously
D. The outer loop

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. 0112
C. 0011
D. 1234

21 What is 'Encapsulation' in the context of basic programming structures?

A. Running a loop infinitely
B. Deleting variables
C. Writing all code in one line
D. Wrapping a piece of code (like a loop or conditional) into a function

22 What does 'Generalization' mean when refactoring code?

A. Making code specific to one problem
B. Replacing variables with constants
C. Removing all comments
D. Adding parameters to a function to make it applicable to a wider range of problems

23 What happens if you forget to increment the counter variable in a standard while loop?

A. The loop will not run
B. Infinite Loop
C. Syntax Error
D. The loop runs once

24 What does the function random.random() return?

A. A random boolean
B. An integer between 0 and 1
C. A float between 0.0 and 100.0
D. A float between 0.0 and 1.0 (excluding 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. pass
B. break
C. continue
D. return

26 What is the truth value of the integer 0 in Python Boolean expressions?

A. True
B. False
C. Undefined
D. None

27 If x = 10 and y = 20, which expression evaluates to True?

A. not(x < y)
B. x > y
C. x != y
D. x == y

28 Which of the following is a correct way to generate a random choice from a list?

A. random.pick(list)
B. random.select(list)
C. list.random()
D. random.choice(list)

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. It is a null operation; nothing happens
D. Returns a value

30 How many stars () will be printed?
for i in range(3):
for j in range(2):
print('
')

A. 6
B. 3
C. 5
D. 9

31 Which operator is used to check for equality?

A. ===
B. ==
C. =
D. <>

32 Which of the following loops is generally preferred for iterating over a list?

A. nested while
B. infinite loop
C. while loop
D. for loop

33 What is the value of x after execution?
x = 0
while x < 5:
x = x + 2

A. 4
B. 5
C. 6
D. 8

34 In Python, can an 'else' clause be used with a 'for' loop?

A. Yes, it executes before the loop starts
B. Yes, it executes if the loop completes normally
C. No, never
D. Yes, it executes only if the loop breaks

35 What is the output of: not (5 > 4 or 3 < 2)?

A. None
B. Error
C. True
D. False

36 Which statement best describes the 'modulus' operator applied to a float (e.g., 5.5 % 2)?

A. It returns the remainder as a float
B. It causes an error
C. It returns the quotient
D. It converts inputs to integers first

37 Using random numbers in a loop to simulate a process many times (e.g., coin flips) is often called:

A. Monte Carlo simulation
B. Debugging
C. Infinite recursion
D. Encapsulation

38 What does random.randrange(0, 10, 3) generate?

A. Any number 0-10
B. 0, 1, 2, 3
C. A random number from {0, 3, 6, 9}
D. A random number from {0, 3, 6, 9, 10}

39 Consider the following:
if condition:
if nested_condition:
do_something()
This is equivalent to:

A. if condition or nested_condition: do_something()
B. while condition: do_something()
C. if not condition: do_something()
D. if condition and nested_condition: do_something()

40 What happens if you compare a string and an integer (e.g., '5' > 3) in Python 3?

A. TypeError
B. Returns False
C. Returns True
D. Converts string to int automatically

41 Which of these is a valid use of a boolean flag in a while loop?

A. Using the modulus operator
B. Using a random number
C. Using 'break' inside a for loop
D. Using a variable set to True/False to control the while condition

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.range(2.5, 5.5)
C. random.float(2.5, 5.5)
D. random.randint(2.5, 5.5)

43 How does a 'nested while' loop behave?

A. The inner loop restarts from the beginning for each iteration of the outer loop
B. Both loops share the same variables automatically
C. It is not allowed in Python
D. The outer loop finishes before the inner loop starts

44 Which operator implies logical negation?

A. not
B. !
C. ~
D. neg

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. 01234
B. 0123
C. 012
D. 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 improve readability and reuse
D. To make the code slower

47 What defines an 'infinite loop'?

A. A loop inside another loop
B. A loop with a high number of iterations
C. A loop that uses random numbers
D. A loop whose condition never evaluates to False

48 When using random numbers to test a loop logic, what is a seed used for?

A. To encrypt the code
B. To make the random numbers predictable and reproducible
C. To generate true random numbers
D. To speed up the computer

49 What is the value of (10 % 3) ** 2?

A. 0
B. 3
C. 1
D. 9

50 Which variable name is used by convention for a 'throwaway' variable in a for loop?

A. var
B. i
C. _ (underscore)
D. temp