Flow Control Questions and Answers

Take Exam

Flow control in Java determines the order in which statements, instructions, or functions are executed during program execution. It includes loops, conditional statements, and branching structures. Understanding Java flow control is essential for writing efficient and bug-free programs. In technical interviews and competitive exams, programming questions and answers related to flow control help assess a candidate’s logical thinking and problem-solving ability. Practice Java MCQs and coding challenges that involve conditional constructs, nested loops, and exception handling to strengthen your understanding. This topic is particularly relevant for companies like TCS, Infosys, and Capgemini.

Flow Control

Showing 10 of 29 questions

1. public void foo( boolean a, boolean b) {     if( a )     {         System.out.println("A"); /* Line 5 */     }     else if(a && b) /* Line 7 */     {         System.out.println( "A && B");     }     else /* Line 11 */     {         if ( !b )         {             System.out.println( "notB") ;         }         else         {             System.out.println( "ELSE" ) ;         }     } }

  • If a is true and b is true then the output is "A && B"
  • If a is true and b is false then the output is "notB"
  • If a is false and b is true then the output is "ELSE"
  • If a is false and b is false then the output is "ELSE"
Show Answer Report

2. switch(x) {     default:          System.out.println("Hello"); } Which two are acceptable types for x?     byte     long     char     float     Short     Long

  • 1 and 3
  • 2 and 4
  • 3 and 5
  • 4 and 6
Show Answer Report

3. public void test(int x) {     int odd = 1;     if(odd) /* Line 4 */     {         System.out.println("odd");     }     else     {         System.out.println("even");     } } Which statement is true?

  • Compilation fails.
  • "odd" will always be output.
  • "even" will always be output.
  • "odd" will be output for odd values of x, and "even" for even values.
Show Answer Report

4. public class While {     public void loop()     {         int x= 0;         while ( 1 ) /* Line 6 */         {             System.out.print("x plus one is " + (x + 1)); /* Line 8 */         }     } } Which statement is true?

  • There is a syntax error on line 1.
  • There are syntax errors on lines 1 and 6.
  • There are syntax errors on lines 1, 6, and 8.
  • There is a syntax error on line 6.
Show Answer Report

5. Which keyword is used to start a conditional block in Java?

  • switch
  • if
  • case
  • break
Show Answer Report

6. Which loop runs at least once even if the condition is false?

  • for
  • while
  • do-while
  • foreach
Show Answer Report

7. Which statement is used to exit from a loop immediately?

  • continue
  • break
  • return
  • stop
Show Answer Report

8. Which statement skips the current iteration of a loop?

  • skip
  • break
  • continue
  • exit
Show Answer Report

9. What is the default case used in a switch statement?

  • optional
  • mandatory
  • must be first
  • must be last
Show Answer Report

10. Which loop is best when you know the number of iterations beforehand?

  • while
  • do-while
  • for
  • recursive
Show Answer Report
Questions and Answers for Competitive Exams Various Entrance Test