Easy Tutorial
For Competitive Exams

Java Programming Conditional Statements

1512.Which of these selection statements test only for equality?
if
switch
if & switch
None of the mentioned
Explanation:
switch statements checks for equality between the controlling variable and its constant cases.
1513.Which of these are selection statements in Java?
if()
for()
continue
break
Explanation:
continue and break are jump statements, and for is an looping statement.
1514.Which of the following loops will execute the body of loop even when condition controlling the loop is initially false?
do-while
while
for
None of the mentioned
Explanation:
None.
1515.Which of these jump statements can skip processing remainder of code in its body for a particular iteration?
break
return
exit
continue
Explanation:
None.
1516.Which of these statement is correct?
switch statement is more efficient than a set of nested ifs.
two case constants in the same switch can have identical values.
switch statement can only test for equality, whereas if statement can evaluate any type of boolean expression.
it is possible to create a nested switch statements.
Explanation:
No two case constants in the same switch can have identical values.
1517.What is the output of this program?
class selection_statements {
public static void main(String args[])
{
int var1 = 5;
int var2 = 6;
if ((var2 = 1) == var1)
System.out.print(var2);
else
System.out.print(++var2);
}
}
1
2
3
4
Explanation:
var2 is initialised to 1. The conditional statement returns false and the else part gets executed.
output:
$ javac selection_statements.java
$ java selection_statements
2

1518.What is the output of this program?
class comma_operator {
public static void main(String args[])
{
int sum = 0;
for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1)
sum += i;
System.out.println(sum);
}
}
5
6
14
compilation error
Explanation:
Using comma operator , we can include more than one statement in the initialization and iteration portion of the for loop.
Therefore both ++i and j = i + 1 is executed i gets the value – 0,1,2,3,4 & j gets the values -0,1,2,3,4,5.
output:
$ javac comma_operator.java
$ java comma_operator
6
1519.What is the output of this program?
class jump_statments {
public static void main(String args[])
{
int x = 2;
int y = 0;
for ( ; y < 10; ++y) {
if (y % x == 0)
continue;
else if (y == 8)
break;
else
System.out.print(y + " ");
}
}
}
1 3 5 7
2 4 6 8
1 3 5 7 9
1 2 3 4 5 6 7 8 9
Explanation:
Whenever y is divisible by x remainder body of loop is skipped by continue statement,
therefore if condition y == 8 is never true as when y is 8, remainder body of loop is skipped by continue statements of first if.
Control comes to print statement only in cases when y is odd.
output:
$ javac jump_statments.java
$ java jump_statments
1 3 5 7 9
1520.What is the output of this program?
class Output {
public static void main(String args[])
{
int x, y = 1;
x = 10;
if (x != 10 && x / 0 == 0)
System.out.println(y);
else
System.out.println(++y);
}
}
1
2
Runtime error owing to division by zero in if condition.
Unpredictable behavior of program.
Explanation:
Operator short circuit and, &&, skips evaluating right hand operand if left hand operand is false
thus division by zero in if condition does not give an error.
output:
$ javac Output.java
$ java Output
2
1521.What is the output of this program?
class Output {
public static void main(String args[])
{
int a = 5;
int b = 10;
first: {
second: {
third: {
if (a == b >> 1)
break second;
}
System.out.println(a.;
}
System.out.println(b.;
}
}
}
5 10
10 5
5
10
Explanation:
b >> 1 in if returns 5 which is equal to a i:e 5, therefore body of if is executed and block second is exited.
Control goes to end of the block second executing the last print statement, printing 10.
output:
$ javac Output.java
$ java Output
10
1522.Which of the following is TRUE about the switch statement in Java?
A default send execution immediately to the end of the switch statement.
A break send execution immediately to the end of the switch statement.
A case send execution immediately to the end of the switch statement.
The statements in a switch continue to execute as long as the condition at the top of the switch remains true.
1523.Which of the following is a loop construct that will always be executed once?
switch
for
while
do...while
1524.Which one is not a valid jump statement?
return
goto
continue
break
1525.Which of the following is not a valid flow control statement?
break;
continue outer;
return;
exit();
1526.The break statement causes an exit
only from the innermost loop.
only from the innermost switch.
from innermost loops or switches.
terminates a program.
Share with Friends