Flow Control Questions and Answers

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 4 of 4 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
Questions and Answers for Competitive Exams Various Entrance Test