C - Control Statements Practice Test.2

  1. What is the primary difference between an "if" statement and a "switch-case" statement in C?

    A) "if" statements are used for conditional execution, while "switch-case" statements are used for loop control.

    B) "if" statements can only compare integers, while "switch-case" statements can compare various data types.

    C) "if" statements allow for more complex conditional expressions, while "switch-case" statements are limited to simple equality checks.

    D) "if" statements are used for loop control, while "switch-case" statements are used for conditional execution.

    Answer: C) "if" statements allow for more complex conditional expressions, while "switch-case" statements are limited to simple equality checks.

  2. In terms of code readability and maintainability, which statement is generally preferred for handling multiple conditions?

    A) "if" statement

    B) "switch-case" statement

    C) Both are equally preferred.

    D) It depends on the specific use case.

    Answer: B) "switch-case" statement

  3. When should you choose to use an "if" statement instead of a "switch-case" statement?

    A) When there are only a few possible values to compare

    B) When the condition involves complex expressions or ranges

    C) When you want to perform multiple operations based on a single condition

    D) When you want to create a sequence of conditions with fall-through behavior

    Answer: B) When the condition involves complex expressions or ranges

  4. Which of the following statements is true about "if-else" statements compared to "switch-case" statements?

    A) "if-else" statements are more efficient in terms of execution speed.

    B) "if-else" statements are limited to comparing integer values.

    C) "if-else" statements are generally easier to read and understand.

    D) "if-else" statements can only handle simple equality checks.

    Answer: C) "if-else" statements are generally easier to read and understand.

  5. In a "switch-case" statement, which data type can be used as the control expression?

    A) Integer

    B) Float

    C) String

    D) Array

    Answer: A) Integer

  6. Which statement allows you to evaluate multiple conditions sequentially and execute code based on the first true condition?

    A) "if" statement

    B) "else-if" statement

    C) "switch-case" statement

    D) "while" statement

    Answer: A) "if" statement

  7. In a "switch-case" statement, what happens if none of the case values match the value of the control expression?

    A) It results in a compilation error.

    B) It throws a runtime error.

    C) It executes the default case (if present) or continues with the code after the switch statement.

    D) It goes into an infinite loop.

    Answer: C) It executes the default case (if present) or continues with the code after the switch statement.

  8. Which statement provides more flexibility when dealing with complex conditions and expressions?

    A) "if" statement

    B) "switch-case" statement

    C) Both statements offer equal flexibility.

    D) It depends on the specific use case.

    Answer: A) "if" statement

  9. When might you choose to use a "switch-case" statement over an "if" statement?

    A) When you want to execute different code blocks for different conditions

    B) When you want to create nested conditions

    C) When you want to handle exceptions

    D) When you want to loop through an array

    Answer: A) When you want to execute different code blocks for different conditions

  10. Which statement is commonly used for handling non-integer values and complex conditions?

    A) "if" statement

    B) "switch-case" statement

    C) "while" statement

    D) "for" statement

    Answer: A) "if" statement

  11. What is the primary purpose of a "while" loop in C?

    A) To perform a specific task a fixed number of times.

    B) To execute a block of code repeatedly as long as a condition is true.

    C) To exit the program.

    D) To declare and initialize variables.

    Answer: B) To execute a block of code repeatedly as long as a condition is true.

  12. In a "while" loop, when is the loop condition checked?

    A) Before each iteration.

    B) After each iteration.

    C) Only once before entering the loop.

    D) Only once after exiting the loop.

    Answer: A) Before each iteration.

  13. What happens if the loop condition in a "while" loop is initially false?

    A) The loop is skipped, and the program proceeds to the next statement after the loop.

    B) An error is thrown, and the program terminates.

    C) The loop executes at least once before checking the condition.

    D) The loop runs indefinitely.

    Answer: A) The loop is skipped, and the program proceeds to the next statement after the loop.

  14. How can you ensure that a "while" loop will eventually terminate?

    A) By using an infinite loop.

    B) By not specifying a loop condition.

    C) By making sure the loop condition becomes false at some point.

    D) By using the "break" statement.

    Answer: C) By making sure the loop condition becomes false at some point.

  15. Which statement is used to exit a "while" loop prematurely?

    A) "return"

    B) "break"

    C) "continue"

    D) "exit"

    Answer: B) "break"

  16. What is the minimum number of times a "while" loop will execute if the loop condition is initially true?

    A) 0 times

    B) 1 time

    C) 2 times

    D) It depends on the loop body.

    Answer: B) 1 time

  17. In a "while" loop, what happens if the loop condition is always true?

    A) The loop executes once and terminates.

    B) The loop becomes an infinite loop.

    C) The loop condition is ignored.

    D) The loop executes only if the condition changes.

    Answer: B) The loop becomes an infinite loop.

  18. Which of the following statements correctly defines the structure of a "while" loop in C?

    A) `for (initialization; condition; update) { // code }`

    B) `do { // code } while (condition);`

    C) `while (condition) { // code }`

    D) `if (condition) { // code }`

    Answer: C) `while (condition) { // code }`

  19. What is the difference between a "while" loop and a "for" loop in C?

    A) There is no difference; they are interchangeable.

    B) "while" loops are used for iteration, while "for" loops are used for decision-making.

    C) "for" loops require an initialization, condition, and update expression, while "while" loops do not.

    D) "while" loops can only run a fixed number of times.

    Answer: C) "for" loops require an initialization, condition, and update expression, while "while" loops do not.

  20. Which statement is used to skip the current iteration and continue with the next iteration of a loop?

    A) "return"

    B) "break"

    C) "exit"

    D) "continue"

    Answer: D) "continue"

  21. What is the key characteristic of a "do-while" loop in C?

    A) It always executes the loop body at least once.

    B) It checks the loop condition before executing the loop body.

    C) It can only execute the loop body a fixed number of times.

    D) It doesn't require a loop condition.

    Answer: A) It always executes the loop body at least once.

  22. In a "do-while" loop, when is the loop condition checked?

    A) Before each iteration.

    B) After each iteration.

    C) Only once before entering the loop.

    D) Only once after exiting the loop.

    Answer: B) After each iteration.

  23. How is a "do-while" loop different from a "while" loop?

    A) They are the same; there is no difference.

    B) "do-while" loops can only execute once.

    C) In a "do-while" loop, the loop body is executed before checking the condition.

    D) "do-while" loops don't require a loop condition.

    Answer: C) In a "do-while" loop, the loop body is executed before checking the condition.

  24. What happens if the loop condition in a "do-while" loop is initially false?

    A) The loop is skipped, and the program proceeds to the next statement after the loop.

    B) An error is thrown, and the program terminates.

    C) The loop executes at least once before checking the condition.

    D) The loop runs indefinitely.

    Answer: C) The loop executes at least once before checking the condition.

  25. Which statement is used to exit a "do-while" loop prematurely?

    A) "return"

    B) "break"

    C) "continue"

    D) "exit"

    Answer: B) "break"

  26. What is the primary purpose of using a "do-while" loop?

    A) To perform a specific task a fixed number of times.

    B) To execute a block of code repeatedly as long as a condition is true.

    C) To exit the program.

    D) To declare and initialize variables.

    Answer: B) To execute a block of code repeatedly as long as a condition is true.

  27. In a "do-while" loop, can you guarantee that the loop body will execute at least once?

    A) Yes, always.

    B) No, it depends on the loop condition.

    C) Only if the loop condition is true initially.

    D) No, "do-while" loops never execute.

    Answer: A) Yes, always.

  28. Which of the following statements correctly defines the structure of a "do-while" loop in C?

    A) `for (initialization; condition; update) { // code }`

    B) `do { // code } while (condition);`

    C) `while (condition) { // code }`

    D) `if (condition) { // code }`

    Answer: B) `do { // code } while (condition);`

  29. What is the role of the "do" keyword in a "do-while" loop?

    A) It signifies the end of the loop.

    B) It is used to declare variables.

    C) It marks the beginning of the loop body.

    D) It has no specific role in the loop.

    Answer: C) It marks the beginning of the loop body.

  30. When might you prefer to use a "do-while" loop instead of a "while" loop?

    A) When you want the loop to run an unknown number of times.

    B) When you want the loop to execute exactly once.

    C) When you want to exit the program early.

    D) When you want to declare multiple variables.

    Answer: B) When you want the loop to execute exactly once.

  31. What is the key difference between a "while" loop and a "do-while" loop in C?

    A) The "while" loop always executes at least once.

    B) The "do-while" loop always executes at least once.

    C) There is no difference; they are interchangeable.

    D) The "do-while" loop doesn't require a loop condition.

  32. In a "while" loop, when is the loop condition checked?

    A) Before each iteration.

    B) After each iteration.

    C) Only once before entering the loop.

    D) Only once after exiting the loop.

  33. In a "do-while" loop, when is the loop condition checked?

    A) Before each iteration.

    B) After each iteration.

    C) Only once before entering the loop.

    D) Only once after exiting the loop.

  34. Which loop, "while" or "do-while," is more suitable when you want to ensure that the loop body executes at least once?

    A) "while" loop

    B) "do-while" loop

    C) Both loops achieve this equally.

    D) Neither loop can guarantee this.

  35. What happens if the loop condition in a "while" loop is initially false?

    A) The loop is skipped, and the program proceeds to the next statement after the loop.

    B) An error is thrown, and the program terminates.

    C) The loop executes at least once before checking the condition.

    D) The loop runs indefinitely.

  36. In which loop, "while" or "do-while," is it more common to check the loop condition after executing the loop body?

    A) "while" loop

    B) "do-while" loop

    C) Both loops check the condition before executing the body.

    D) Neither loop checks the condition after executing the body.

  37. Which of the following statements correctly defines the structure of a "while" loop in C?

    A) `for (initialization; condition; update) { // code }`

    B) `do { // code } while (condition);`

    C) `while (condition) { // code }`

    D) `if (condition) { // code }`

  38. Which of the following statements correctly defines the structure of a "do-while" loop in C?

    A) `for (initialization; condition; update) { // code }`

    B) `do { // code } while (condition);`

    C) `while (condition) { // code }`

    D) `if (condition) { // code }`

  39. When might you prefer to use a "do-while" loop instead of a "while" loop?

    A) When you want the loop to run an unknown number of times.

    B) When you want the loop to execute exactly once.

    C) When you want to exit the program early.

    D) When you want to declare multiple variables.

  40. Which loop, "while" or "do-while," is more commonly used when validating user input in C programs?

    A) "while" loop

    B) "do-while" loop

    C) Both loops are equally suitable for validation.

    D) Neither loop is suitable for validation.

  41. 1. What is the basic structure of a "for" loop in C?

    A) if (condition) { // code }

    B) while (condition) { // code }

    C) for (initialization; condition; update) { // code }

    D) do { // code } while (condition);

  42. 2. In a "for" loop, where is the initialization statement typically placed?

    A) Before the loop body.

    B) After the loop body.

    C) Inside the loop body.

    D) It can be placed anywhere.

  43. 3. How many times will the following "for" loop execute?

    
                for (int i = 0; i < 5; i++) {
                    // code
                }
                

    A) Zero times

    B) Once

    C) Five times

    D) Indefinitely

  44. 4. What is the purpose of the loop condition in a "for" loop?

    A) It determines the number of iterations.

    B) It initializes the loop variable.

    C) It specifies the loop body.

    D) It is optional and not necessary.

  45. 5. Which part of a "for" loop is executed after each iteration of the loop body?

    A) Initialization

    B) Condition

    C) Update

    D) Loop body

  46. 6. What is the advantage of using a "for" loop over a "while" loop for iteration?

    A) "for" loops are more concise.

    B) "for" loops can execute indefinitely.

    C) "for" loops can't be used for iteration.

    D) There is no advantage; they are equivalent.

  47. 7. In a "for" loop, can you omit the initialization statement?

    A) Yes, it's optional.

    B) No, it's required.

    C) Only if the condition is omitted.

    D) Only if the update statement is omitted.

  48. 8. What is the typical usage of the "for" loop's initialization statement?

    A) To specify the loop condition.

    B) To set the loop variable to a starting value.

    C) To end the loop.

    D) To check if the loop is done.

  49. 9. In a "for" loop, when is the loop condition checked?

    A) Before each iteration.

    B) After each iteration.

    C) Only once before entering the loop.

    D) Only once after exiting the loop.

  50. 10. What happens if the loop condition in a "for" loop is initially false?

    A) The loop is skipped, and the program proceeds to the next statement after the loop.

    B) An error is thrown, and the program terminates.

    C) The loop executes at least once before checking the condition.

    D) The loop runs indefinitely.