Flow Control Questions and Answers

Practice Mode
Showing 10 of 29 questions
Q1
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" ) ;         }     } }
  • A If a is true and b is true then the output is "A && B"
  • B If a is true and b is false then the output is "notB"
  • C If a is false and b is true then the output is "ELSE"
  • D If a is false and b is false then the output is "ELSE"
Answer: Option C
Q2
switch(x) {     default:          System.out.println("Hello"); } Which two are acceptable types for x?     byte     long     char     float     Short     Long
  • A 1 and 3
  • B 2 and 4
  • C 3 and 5
  • D 4 and 6
Answer: Option A
Q3
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?
  • A Compilation fails.
  • B "odd" will always be output.
  • C "even" will always be output.
  • D "odd" will be output for odd values of x, and "even" for even values.
Answer: Option A
Q4
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?
  • A There is a syntax error on line 1.
  • B There are syntax errors on lines 1 and 6.
  • C There are syntax errors on lines 1, 6, and 8.
  • D There is a syntax error on line 6.
Answer: Option D
Q5
Which keyword is used to start a conditional block in Java?
  • A switch
  • B if
  • C case
  • D break
Answer: Option B
Explanation: The if keyword begins a conditional decision-making block.
Q6
Which loop runs at least once even if the condition is false?
  • A for
  • B while
  • C do-while
  • D foreach
Answer: Option C
Explanation: do-while executes its body first, then checks the condition.
Q7
Which statement is used to exit from a loop immediately?
  • A continue
  • B break
  • C return
  • D stop
Answer: Option B
Explanation: break stops loop execution instantly.
Q8
Which statement skips the current iteration of a loop?
  • A skip
  • B break
  • C continue
  • D exit
Answer: Option C
Explanation: continue jumps to the next loop iteration.
Q9
What is the default case used in a switch statement?
  • A optional
  • B mandatory
  • C must be first
  • D must be last
Answer: Option A
Explanation: The default block is optional in switch.
Q10
Which loop is best when you know the number of iterations beforehand?
  • A while
  • B do-while
  • C for
  • D recursive
Answer: Option C
Explanation: for loop is ideal when iteration count is known.
Questions and Answers for Competitive Exams Various Entrance Test