This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Control Statements”.
1. What would be the output of the following codesnippet if variable a=10?
a) 1 2
b) 2 3
c) 1 3
Explanation: Since the first if condition is not met, control would not go inside if statement and hence only statement after the entire if block will be executed.
2. The while loop repeats a set of code while the condition is not met?
Explanation: While loop repeats a set of code only until the condition is met.
3. What is true about a break?
a) Break stops the execution of entire program
b) Break halts the execution and forces the control out of the loop
c) Break forces the control out of the loop and starts the execution of next iteration
d) Break halts the execution of the loop for certain time frame
Explanation: Break halts the execution and forces the control out of the loop.
4. What is true about do statement?
a) do statement executes the code of a loop at least once
b) do statement does not get execute if condition is not matched in the first iteration
c) do statement checks the condition at the beginning of the loop
d) do statement executes the code more than once always
Explanation: Do statement checks the condition at the end of the loop. Hence, code gets executed at least once.
5. Which of the following is used with the switch statement?
Explanation: Break is used with a switch statement to shift control out of switch.
6. What is the valid data type for variable “a” to print “Hello World”?
a) int and float
b) byte and short
c) char and long
d) byte and char
Explanation: The switch condition would only meet if variable “a” is of type byte or char.
7. Which of the following is not a decision making statement?
Explanation: do-while is an iteration statement. Others are decision making statements.
8. Which of the following is not a valid jump statement?
Explanation: break, continue and return transfer control to another part of the program and returns back to caller after execution. However, goto is marked as not used in Java.
9. From where break statement causes an exit?
a) Only from innermost loop
b) Terminates a program
c) Only from innermost switch
d) From innermost loops or switches
Explanation: The break statement causes an exit from innermost loop or switch.
10. Which of the following is not a valid flow control statement?
Explanation: exit() is not a flow control statement in Java. exit() terminates the currently running JVM.
Sanfoundry Global Education & Learning Series – Java Programming Language.