122). Which of these selection statements test only for equality?
[A]if
[B] switch
[C]if & switch
[D] none of the mentioned
Show Answer
Correct Answer: switch
Notes:
Answer: b
Explanation: Switch statements checks for equality between the controlling variable and its constant cases.
123). Which of these are selection statements in Java?
[A] if()
[B] for()
[C] continue
[D] break
Show Answer
Correct Answer: if()
Notes:
Answer: a
Explanation: Continue and break are jump statements, and for is a looping statement.
124). . Which of the following loops will execute the body of loop even when condition controlling the loop is initially false?
[A] do-while
[B] while
[C]for
[D]none of the mentioned
Show Answer
Correct Answer: do-while
Notes:
Answer: a
Explanation: None.
125). Which of these jump statements can skip processing the remainder of the code in its body for a particular iteration?
[A]break
[B] return
[C]exit
[D] continue
Show Answer
Correct Answer: continue
Notes:
Answer: d
Explanation: None.
126). Which of this statement is incorrect?
[A] switch statement is more efficient than a set of nested ifs
[B] two case constants in the same switch can have identical values
[C]switch statement can only test for equality, whereas if statement can evaluate any type of boolean expression
[D] it is possible to create a nested switch statements
Show Answer
Correct Answer: two case constants in the same switch can have identical values
Notes:
Answer: b
Explanation: No two case constants in the same switch can have identical values.
127). What will be the output of the following Java 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);
}
}
Show Answer
Correct Answer: 2
Notes:
Answer: b
Explanation: var2 is initialised to 1. The conditional statement returns false and the else part gets executed.
output:
128). What will be the output of the following Java 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);
}
}
[A]5
[B]6
[C]14
[D]compilation error
Show Answer
Correct Answer: 6
Notes:
Answer: b
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
129). What will be the output of the following Java 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 + " ");
}
}
}
[A]1 3 5 7
[B] 2 4 6 8
[C]1 3 5 7 9
[D]1 2 3 4 5 6 7 8 9
Show Answer
Correct Answer: 1 3 5 7 9
Notes:
Answer: c
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
130). What will be the output of the following Java program?
class Output
{
public static void main(String args[])
{
final int a=10,b=20;
while(a
[A]Hello
[B] run time error
[C]Hello world
[D]compile time error
Show Answer
Correct Answer: compile time error
Notes:
Answer: d
Explanation: Every final variable is compile time constant.
131). What will be the output of the following Java 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);
}
}
}
[A]5 10
[B] 10 5
[C]5
[D]10
Show Answer
Correct Answer: 10
Notes:
Answer: d
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