This set of Java Multiple Choice Questions & Answers (MCQs) on “Control Statements”.
1. What would be the output of the following code snippet if variable a=10?
-
if(a<=0)
-
{
-
if(a==0)
-
{
-
System.out.println("1 ");
-
}
-
else
-
{
-
System.out.println("2 ");
-
}
-
}
-
System.out.println("3 ");
a) 1 2 Answer: d 2. The while loop repeats a set of code while the condition is not met? Answer: b 3. What is true about a break? Answer: b 4. What is true about do statement? Answer: a 5. Which of the following is used with the switch statement? Answer: c 6. What is the valid data type for variable “a” to print “Hello World”? a) int and float Answer: d 7. Which of the following is not a decision making statement? Answer: d 8. Which of the following is not a valid jump statement? Answer: b 9. From where break statement causes an exit? Answer: d 10. Which of the following is not a valid flow control statement? Answer: a
b) 2 3
c) 1 3
d) 3
Clarification: 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.
a) True
b) False
Clarification: While loop repeats a set of code only until the condition is met.
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
Clarification: Break halts the execution and forces the control out of the loop.
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
Clarification: Do statement checks the condition at the end of the loop. Hence, code gets executed at least once.
a) Continue
b) Exit
c) break
d) do
Clarification: Break is used with a switch statement to shift control out of switch.
switch(a)
{
System.out.println("Hello World");
}
b) byte and short
c) char and long
d) byte and char
Clarification: The switch condition would only meet if variable “a” is of type byte or char.
a) if
b) if-else
c) switch
d) do-while
Clarification: do-while is an iteration statement. Others are decision making statements.
a) break
b) goto
c) continue
d) return
Clarification: 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.
a) Only from innermost loop
b) Terminates a program
c) Only from innermost switch
d) From innermost loops or switches
Clarification: The break statement causes an exit from innermost loop or switch.
a) exit()
b) break
c) continue
d) return
Clarification: exit() is not a flow control statement in Java. exit() terminates the currently running JVM.