Java Questions & Answers – Control Statements – 2

This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Control Statements”.

1. What would be the output of the following code snippet if variable a=10?

  1. if(a<=0)
  2. {
  3.    if(a==0)
  4.    {
  5.      System.out.println("1 ");
  6.    }
  7.    else 
  8.    { 
  9.       System.out.println("2 ");
  10.    }
  11. }
  12. System.out.println("3 ");

a) 1 2
b) 2 3
c) 1 3
d) 3
View Answer

Answer: d
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.
advertisement
advertisement

2. The while loop repeats a set of code while the condition is not met?
a) True
b) False
View Answer

Answer: b
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
View Answer

Answer: b
Explanation: Break halts the execution and forces the control out of the loop.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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
View Answer

Answer: a
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?
a) Continue
b) Exit
c) break
d) do
View Answer

Answer: c
Explanation: Break is used with a switch statement to shift control out of switch.
advertisement

6. What is the valid data type for variable “a” to print “Hello World”?

  1. switch(a)
  2. {
  3.    System.out.println("Hello World");
  4. }

a) int and float
b) byte and short
c) char and long
d) byte and char
View Answer

Answer: d
Explanation: The switch condition would only meet if variable “a” is of type byte or char.
advertisement

7. Which of the following is not a decision making statement?
a) if
b) if-else
c) switch
d) do-while
View Answer

Answer: d
Explanation: do-while is an iteration statement. Others are decision making statements.

8. Which of the following is not a valid jump statement?
a) break
b) goto
c) continue
d) return
View Answer

Answer: b
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
View Answer

Answer: d
Explanation: The break statement causes an exit from innermost loop or switch.

10. Which of the following is not a valid flow control statement?
a) exit()
b) break
c) continue
d) return
View Answer

Answer: a
Explanation: exit() is not a flow control statement in Java. exit() terminates the currently running JVM.

Sanfoundry Global Education & Learning Series – Java Programming Language.

To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.