Control structures are fundamental in programming as they control the flow of execution based on certain conditions. This chapter covers key control structures like conditional statements, looping constructs, and loop control mechanisms. Here are 30 MCQs on control structures in Java.
Conditional Statements
Which of the following is a valid conditional statement in Java? a) if-else b) for-else c) loop-else d) while-else
What does the “if” statement do in Java? a) Repeats a block of code b) Executes a block of code if the condition is true c) Skips a block of code d) Exits a block of code
How does an “else” statement function in Java? a) It executes only if the preceding “if” condition is false b) It executes if the preceding condition is true c) It is used for loop control d) It terminates the program
What is the purpose of the “else if” statement in Java? a) To check for multiple conditions after an initial “if” condition b) To break out of the loop c) To terminate the program d) To execute the block of code without a condition
What is the output if the condition in an “if” statement is false? a) It will skip the code inside the block b) It will execute the code inside the block c) It will jump to the next “if” statement d) It will print an error
Switch Statement
Which of the following is true about the “switch” statement in Java? a) It only works with integers b) It can handle multiple conditions based on a single variable c) It allows looping through values d) It can only compare strings
What is required in each case statement of a “switch” block? a) A “return” statement b) A variable to compare against c) A break statement d) A constant expression
How does the “break” statement work inside a switch case? a) It stops the switch and continues the execution after the switch block b) It continues with the next case c) It returns the value to the calling function d) It throws an exception
What will happen if a “switch” statement does not have a “break” statement? a) It will throw a compile-time error b) It will execute all subsequent cases until it finds a “break” c) It will stop after the first case d) It will skip all cases
What types of variables can be used in a “switch” statement? a) Only integers b) Only strings c) Any data type d) Integers, characters, and enumerated types
Looping Statements
What is the purpose of a “for” loop in Java? a) To repeat a block of code a specific number of times b) To execute a block of code based on a condition c) To handle user input d) To jump between multiple blocks of code
Which of the following is the correct syntax for a “for” loop? a) for (int i=0; i<10; i++) { } b) for (int i=0; i<=10; i–) { } c) for int i=0 to 10 { } d) for (i=0; i<10; i++) { }
What does the “while” loop do in Java? a) Loops a specific number of times b) Loops as long as the condition is true c) Loops until a “break” statement is encountered d) Loops until a condition becomes false
Which statement is used to exit a “while” loop prematurely? a) continue b) break c) return d) exit
What is the difference between a “for” loop and a “while” loop in Java? a) “for” loops execute only when a condition is true, while “while” loops run until a condition becomes true b) “for” loops are used for indefinite looping, while “while” loops are used for definite looping c) “for” loops have a specified number of iterations, while “while” loops do not d) “while” loops are more efficient than “for” loops
do-while Loop
How does a “do-while” loop differ from a “while” loop in Java? a) A “do-while” loop runs at least once, even if the condition is false b) A “do-while” loop runs only when the condition is true c) A “do-while” loop requires an explicit “break” statement d) A “do-while” loop can only execute once
What is the correct syntax for a “do-while” loop? a) do { } while (condition); b) do { while (condition) } c) do while { condition } d) do (condition) while { }
What happens if the condition in a “do-while” loop is false? a) The loop is skipped entirely b) The loop runs at least once before checking the condition c) The loop runs infinitely d) The loop stops immediately without executing
Can a “do-while” loop be used in place of a “while” loop? a) Yes, if you want to ensure the loop executes at least once b) No, because “while” loops cannot execute the code inside the loop c) Yes, if you want the loop to run indefinitely d) No, because “do-while” loops do not work with conditions
Which of the following is an advantage of using a “do-while” loop? a) It guarantees that the loop will run at least once, regardless of the condition b) It provides better performance than other loops c) It always executes a fixed number of times d) It simplifies nested loops
Break and Continue
What is the purpose of the “break” statement in Java loops? a) It skips the current iteration and continues with the next one b) It exits the loop entirely and continues execution after the loop c) It forces the loop to run indefinitely d) It pauses the loop until the condition is true
Which statement is used to skip the current iteration of a loop in Java? a) continue b) break c) exit d) stop
In which type of loops can the “continue” statement be used? a) Only “for” loops b) Only “while” loops c) In all loop types (“for”, “while”, “do-while”) d) Only in “do-while” loops
What happens when “continue” is used inside a loop? a) The loop is completely terminated b) The loop moves to the next iteration without executing remaining statements c) The loop runs indefinitely d) The loop executes the remaining code in the current iteration
Which statement will terminate a “while” loop and continue execution after the loop? a) return b) break c) continue d) exit
Nested Loops
What is a “nested loop” in Java? a) A loop that runs infinitely b) A loop inside another loop c) A loop that skips over elements d) A loop that controls the flow of execution
How can a “for” loop be nested inside a “while” loop in Java? a) by simply writing one inside the other b) by using a “continue” statement c) by using a “break” statement d) Nested loops are not possible in Java
What is the maximum number of loops that can be nested in Java? a) 1 b) 2 c) There is no strict limit d) 5
What is the primary concern when using nested loops? a) Efficiency and performance issues b) Simplicity of code c) It can only be done in “while” loops d) Managing input/output operations
How does the performance of nested loops compare to single loops? a) Nested loops have a higher time complexity and may be less efficient b) Nested loops perform better than single loops c) Nested loops always execute faster than single loops d) There is no difference in performance
Answers
Qno
Answer
1
a) if-else
2
b) Executes a block of code if the condition is true
3
a) It executes only if the preceding “if” condition is false
4
a) To check for multiple conditions after an initial “if” condition
5
a) It will skip the code inside the block
6
b) It can handle multiple conditions based on a single variable
7
b) A variable to compare against
8
a) It stops the switch and continues the execution after the switch block
9
b) It will execute all subsequent cases until it finds a “break”
10
d) Integers, characters, and enumerated types
11
a) To repeat a block of code a specific number of times
12
a) for (int i=0; i<10; i++) { }
13
b) Loops as long as the condition is true
14
b) break
15
c) “for” loops have a specified number of iterations, while “while” loops do not
16
a) A “do-while” loop runs at least once, even if the condition is false
17
a) do { } while (condition);
18
b) The loop runs at least once before checking the condition
19
a) Yes, if you want to ensure the loop executes at least once
20
a) It guarantees that the loop will run at least once, regardless of the condition
21
b) It exits the loop entirely and continues execution after the loop
22
a) continue
23
c) In all loop types (“for”, “while”, “do-while”)
24
b) The loop moves to the next iteration without executing remaining statements
25
b) break
26
b) A loop inside another loop
27
a) by simply writing one inside the other
28
c) There is no strict limit
29
a) Efficiency and performance issues
30
a) Nested loops have a higher time complexity and may be less efficient