132). What would be the output of the following code snippet if variable a=10?
if(a<=0)
{
if(a==0)
{
System.out.println("1 ");
}
else
{
System.out.println("2 ");
}
}
System.out.println("3 ");
[A]1 2
[B] 2 3
[C]1 3
[D]3
Show Answer
Correct Answer: 3
Notes:
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.
133). The while loop repeats a set of code while the condition is not met?
if(a<=0)
{
if(a==0)
{
System.out.println("1 ");
}
else
{
System.out.println("2 ");
}
}
System.out.println("3 ");
[A]True
[B]False
[C]none
[D]none
Show Answer
Correct Answer: False
Notes:
Answer: b
Explanation: While loop repeats a set of code only until the condition is met.
134). What is true about a break?
if(a<=0)
{
if(a==0)
{
System.out.println("1 ");
}
else
{
System.out.println("2 ");
}
}
System.out.println("3 ");
[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
Show Answer
Correct Answer: Break halts the execution and forces the control out of the loop
Notes:
Answer: b
Explanation: Break halts the execution and forces the control out of the loop.
135). What is true about do statement?
if(a<=0)
{
if(a==0)
{
System.out.println("1 ");
}
else
{
System.out.println("2 ");
}
}
System.out.println("3 ");
[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
Show Answer
Correct Answer: do statement executes the code of a loop at least once
Notes:
Answer: a
Explanation: Do statement checks the condition at the end of the loop. Hence, code gets executed at least once.
136). Which of the following is used with the switch statement?
if(a<=0)
{
if(a==0)
{
System.out.println("1 ");
}
else
{
System.out.println("2 ");
}
}
System.out.println("3 ");
[A]Continue
[B]Exit
[C]break
[D]do
Show Answer
Correct Answer: break
Notes:
Answer: c
Explanation: Break is used with a switch statement to shift control out of switch.
137). What is the valid data type for variable “a” to print “Hello World”?
switch(a)
{
System.out.println("Hello World");
}
[A] int and float
[B]byte and short
[C] char and long
[D]byte and char
Show Answer
Correct Answer: byte and char
Notes:
Answer: d
Explanation: The switch condition would only meet if variable “a” is of type byte or char.
138). Which of the following is not a decision making statement?
switch(a)
{
System.out.println("Hello World");
}
[A]if
[B]if-else
[C]switch
[D]do-while
Show Answer
Correct Answer: do-while
Notes:
Answer: d
Explanation: do-while is an iteration statement. Others are decision making statements.
139). Which of the following is not a valid jump statement?
switch(a)
{
System.out.println("Hello World");
}
[A]break
[B]goto
[C]continue
[D]return
Show Answer
Correct Answer: goto
Notes:
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.
140). From where break statement causes an exit?
switch(a)
{
System.out.println("Hello World");
}
[A]Only from innermost loop
[B]Terminates a program
[C]Only from innermost switch
[D]From innermost loops or switches
Show Answer
Correct Answer: From innermost loops or switches
Notes:
Answer: d
Explanation: The break statement causes an exit from innermost loop or switch.
141). Which of the following is not a valid flow control statement?
switch(a)
{
System.out.println("Hello World");
}
[A]exit()
[B]break
[C]continue
[D]return
Show Answer
Correct Answer: exit()
Notes:
Answer: a
Explanation: exit() is not a flow control statement in Java. exit() terminates the currently running JVM.