3.2 - Loops.


Back Button Home

3.2.1 - For Loop, While Loop and Do-while Loop.


1. What is the purpose of a loop in programming?
  • (A) To execute a set of instructions once
  • (B) To execute a set of instructions multiple times
  • (C) To terminate the program
  • (D) To skip certain instructions
Correct Answer: (B) To execute a set of instructions multiple times
2. Which of the following loops is a pre-tested loop?
  • (A) For loop
  • (B) While loop
  • (C) Do-while loop
  • (D) Both A and B
Correct Answer: (D) Both A and B
3. What is the correct syntax for a for loop in C?
  • (A) for(condition; initialization; increment/decrement)
  • (B) for(initialization; condition; increment/decrement)
  • (C) for(increment/decrement; condition; initialization)
  • (D) for(initialization; increment/decrement; condition)
Correct Answer: (B) for(initialization; condition; increment/decrement)
4. What happens when the condition in a while loop becomes false?
  • (A) The loop continues to execute
  • (B) The loop terminates
  • (C) The loop skips the current iteration
  • (D) The loop restarts
Correct Answer: (B) The loop terminates
5. In a do-while loop, when is the condition checked?
  • (A) Before the loop executes
  • (B) After the loop executes
  • (C) Before and after the loop executes
  • (D) It depends on the initialization
Correct Answer: (B) After the loop executes
6. What happens if the loop control variable is not incremented or decremented in a for loop?
  • (A) The loop will terminate after one iteration
  • (B) The loop will become an infinite loop
  • (C) The program will crash
  • (D) The loop will execute only once
Correct Answer: (B) The loop will become an infinite loop
7. Which loop guarantees that the loop body will execute at least once, even if the condition is false initially?
  • (A) For loop
  • (B) While loop
  • (C) Do-while loop
  • (D) Nested loop
Correct Answer: (C) Do-while loop
8. Which of the following loops is best suited for a situation where the number of iterations is not known in advance?
  • (A) For loop
  • (B) While loop
  • (C) Do-while loop
  • (D) Nested loop
Correct Answer: (B) While loop
9. Which statement is true about nested loops?
  • (A) The outer loop must always be a for loop
  • (B) Any number of loops can be nested within another loop
  • (C) The inner loop always executes fewer times than the outer loop
  • (D) Nested loops must have the same type (e.g., both for loops or both while loops)
Correct Answer: (B) Any number of loops can be nested within another loop
10. What happens if the condition of a for loop is omitted?
  • (A) The loop will never execute
  • (B) The loop will execute infinitely
  • (C) The loop will execute only once
  • (D) The loop will cause an error
Correct Answer: (B) The loop will execute infinitely
11. Which loop type is used when a condition needs to be tested after executing the loop body?
  • (A) For loop
  • (B) While loop
  • (C) Do-while loop
  • (D) None of the above
Correct Answer: (C) Do-while loop
12. What will happen in the following code?
            int i = 10;
            while(i > 5) 
            {
                printf("%d", i);
            }
                
  • (A) The loop will execute infinitely
  • (B) The loop will never execute
  • (C) The loop will execute once
  • (D) The loop will cause a compilation error
Correct Answer: (A) The loop will execute infinitely
13. What is the default step value in a for loop if it is not explicitly specified?
  • (A) 0
  • (B) 1
  • (C) -1
  • (D) Undefined
Correct Answer: (B) 1
14. Which of the following conditions can result in an infinite loop?
  • (A) Forgetting to initialize the loop counter
  • (B) Forgetting to update the loop counter
  • (C) Writing a condition that is always true
  • (D) All of the above
Correct Answer: (D) All of the above
15. What should be established at the outset of a loop in order to prevent infinite execution?
  • (A) Initialization and condition
  • (B) Condition and termination condition
  • (C) Initialization, condition, and step value
  • (D) Just the step value
Correct Answer: (C) Initialization, condition, and step value
16. What is the primary difference between a while loop and a for loop?
  • (A) A while loop has no condition
  • (B) A for loop checks the condition at the end
  • (C) A for loop is typically used when the number of iterations is known
  • (D) A while loop cannot be infinite
Correct Answer: (C) A for loop is typically used when the number of iterations is known
17. What is the condition for a loop to stop executing?
  • (A) The loop counter reaches the starting value
  • (B) The loop counter exceeds the step value
  • (C) The loop condition becomes false
  • (D) The loop executes a specific number of times
Correct Answer: (C) The loop condition becomes false

3.2.2 - Nested Loop.


18. What is a "nested loop" in programming?
  • (A) A loop that runs indefinitely
  • (B) A loop placed inside another loop
  • (C) A loop that terminates after a single iteration
  • (D) A loop that runs backward
Correct Answer: (B) A loop placed inside another loop
19. Which of the following is an example of a scenario where nested loops are typically used?
  • (A) Reversing a string
  • (B) Iterating through a list
  • (C) Working with two-dimensional data structures like matrices
  • (D) Reading input from the user
Correct Answer: (C) Working with two-dimensional data structures like matrices
20. In the following structure of a nested loop, which loop runs first?
    for (initialization; condition; increment/decrement) {
    // Outer loop code
    for (initialization; condition; increment/decrement) {
        // Inner loop code
    }
    }
                
  • (A) Inner loop
  • (B) Both loops run simultaneously
  • (C) Outer loop
  • (D) Neither loop runs
Correct Answer: (C) Outer loop
21. What does the following nested loop in C print?
            for (int i = 1; i <= 2; i++) {
                for (int j = 1; j <= 2; j++) {
                    printf("%d %d\n", i, j);
                }
            }
                
  • (A) Pairs of numbers from 1 to 2
  • (B) A multiplication table
  • (C) Numbers from 1 to 2
  • (D) Reversed numbers
Correct Answer: (A) Pairs of numbers from 1 to 2
22. In the example of printing a multiplication table, what is printed by the inner loop?
  • (A) Rows of the table
  • (B) Columns of the table
  • (C) Both rows and columns
  • (D) Only headers of the table
Correct Answer: (B) Columns of the table
23. What is the purpose of the printf("\n"); statement in the multiplication table example?
  • (A) It prints the multiplication result
  • (B) It ends the program
  • (C) It moves to the next line after printing each row
  • (D) It increments the loop variable
Correct Answer: (C) It moves to the next line after printing each row
24. How many times will the inner loop execute if the outer loop runs 10 times and the inner loop also runs 10 times?
  • (A) 10
  • (B) 100
  • (C) 20
  • (D) 50
Correct Answer: (B) 100
25. In the multiplication table example, what does the expression i * j represent?
  • (A) The sum of i and j
  • (B) The difference between i and j
  • (C) The product of i and j
  • (D) The division of i by j
Correct Answer: (C) The product of i and j
26. Which of the following is NOT a correct description of the use of nested loops?
  • (A) They can be used to create grids or tables
  • (B) They always result in infinite loops
  • (C) They can iterate over combinations of elements from two sets
  • (D) They allow more complex looping structures
Correct Answer: (B) They always result in infinite loops
27. What is the output of the following code snippet?
            for (int i = 1; i <= 3; i++) {
                for (int j = 1; j <= 2; j++) {
                    printf("%d ", i);
                }
            }
                
  • (A) 1 1 2 2 3 3
  • (B) 1 2 3 1 2 3
  • (C) 1 2 1 2 1 2
  • (D) 1 2 2 3 3
Correct Answer: (A) 1 1 2 2 3 3