Flow Control Questions and Answers
Practice ModeShowing 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" ) ;
}
}
}
Answer: Option C
Q2
switch(x)
{
default:
System.out.println("Hello");
}
Which two are acceptable types for x?
byte
long
char
float
Short
Long
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?
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?
Answer: Option D
Q5
Which keyword is used to start a conditional block in Java?
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?
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?
Answer: Option B
Explanation: break stops loop execution instantly.
Q8
Which statement skips the current iteration of a loop?
Answer: Option C
Explanation: continue jumps to the next loop iteration.
Q9
What is the default case used in a switch statement?
Answer: Option A
Explanation: The default block is optional in switch.
Q10
Which loop is best when you know the number of iterations beforehand?
Answer: Option C
Explanation: for loop is ideal when iteration count is known.