programming beginner 10 sample questions

Control Structures MCQ Practice Test

Exploring conditional statements (if-else) and loops (for, while).

Q1. Consider the following Python code snippet: python count = 0 for i in range(1, 11): if i % 2 == 0: count += i elif i > 7 and i < 10: count += 2 * i else: continue print(count) What will be the output of this code?

Explanation: Even values of i add themselves to count: 2 + 4 + 6 + 8 + 10 = 30. The odd value 9 satisfies i > 7 and i < 10, adding 18 more for a total of 48. All other odd values skip via continue.

Q2. What will be printed to the console after executing the following Python code snippet? python counter = 0 for i in range(1, 5): if i % 2 == 0: counter += i else: counter -= i print(counter)

Explanation: Trace: i=1 (odd) subtracts to give -1; i=2 adds up to 1; i=3 subtracts down to -2; i=4 adds up to 2. The loop ends and prints 2.

Q3. What will be printed to the console after executing the following Python code snippet? python counter = 0 for i in range(1, 5): if i % 2 == 0: counter += i else: counter -= i print(counter)

Explanation: Trace: i=1 (odd) gives -1; i=2 adds 2 giving 1; i=3 subtracts 3 giving -2; i=4 adds 4 giving 2. The final value printed is 2.

Q4. A program needs to print "Hello" five times using a counter variable i that takes the values 1, 2, 3, 4, 5. Which loop does this in Python?

Explanation: range(1, 6) yields exactly 1, 2, 3, 4, 5. range(5) yields 0-4 (the right count but the wrong values), while the while-loop options either lack initialization or run six times (i <= 5 starting from 0).

Q5. What will be printed to the console after executing the following Python code snippet? count = 0 for i in range(1, 5): if i % 2 == 0: count += i else: count -= i print(count)

Explanation: The loop iterates through numbers 1 to 4: i=1 gives -1, i=2 gives 1, i=3 gives -2, and i=4 gives 2. The final value of count is therefore 2.

Q6. Consider the following Python code snippet: python for i in range(1, 11): if i % 2 == 0: continue elif i > 7: break else: print(i) What will be the output of this code?

Explanation: The loop iterates from 1 to 10. The `continue` statement skips even numbers. The `break` statement terminates the loop when `i` is greater than 7. Therefore, only odd numbers from 1 to 7 will be printed.

Q7. What is the output of the following code snippet in a language that supports tail recursion optimization?\n\nint factorial(int n, int acc = 1)\n{\n if (n == 0)\n return acc;\n else\n return factorial(n - 1, n * acc);\n}\n\nfactorial(4);

Explanation: The code snippet is an implementation of the factorial function using tail recursion. Since the language supports tail recursion optimization, the function will not exceed the maximum recursion depth, and the output will be the correct factorial value. The calculation is as follows: factorial(4) = factorial(3, 4) = factorial(2, 12) = factorial(1, 24) = 24.

Q8. In a recursive function, what is the term for the condition that stops the recursion and returns a base case?

Explanation: A termination predicate is a condition that checks for the base case in a recursive function, stopping the recursion and returning a value.

Q9. In a recursive function, what is the term for the base case that stops the recursion when the input size reaches a predetermined threshold?

Explanation: The halting condition is the base case in a recursive function that determines when to stop the recursion, preventing infinite loops and ensuring the function terminates when the input size reaches a predetermined threshold.

Q10. In a recursive function, what is the purpose of the base case in a lambda expression?

Explanation: The base case in a recursive function is a condition that, when met, causes the function to terminate and prevent infinite recursion. This is particularly important in lambda expressions, where the recursive call is made implicitly.

That was just a sample. Sign up to unlock the full question bank with timed tests and certificates.

Sign Up Free
Control Structures MCQ Practice Test | Mind Hustle