Sail E0 Webinar

MCQs

Total Questions : 65 | Page 3 of 7 pages
Question 21.


What will be the output of the program?


class Test
{
static int s;
public static void main(String [] args)
{
Test p = new Test();
p.start();
System.out.println(s);
}
void start()
{
int x = 7;
twice(x);
System.out.print(x + " ");
}
void twice(int x)
{
x = x*2;
s = x;
}
}
  1.    7 7
  2.    7 14
  3.    14 0
  4.    14 14
 Discuss Question
Answer: Option B. -> 7 14

The int x in the twice() method is not the same int x as in the start() method. Start()'s x

 is not affected by the twice() method. The instance variable s is updated by twice()'s x,

 which is 14.


Question 22.


What will be the output of the program?


class BoolArray
{
boolean [] b = new boolean[3];
int count = 0;
void set(boolean [] x, int i)
{
x[i] = true;
++count;
}
public static void main(String [] args)
{
BoolArray ba = new BoolArray();
ba.set(ba.b, 0);
ba.set(ba.b, 2);
ba.test();
}
void test()
{
if ( b[0] && b[1] | b[2] )
count++;
if ( b[1] && b[(++count - 2)] )
count += 7;
System.out.println("count = " + count);
}
}
  1.    count = 0
  2.    count = 2
  3.    count = 3
  4.    count = 4
 Discuss Question
Answer: Option C. -> count = 3

The reference variables b and x both refer to the same boolean array. count is incremented 

for each call to the set() method, and once again when the first if test is true. Because of the 

&& short circuit operator, count is not incremented during the second if test.


Question 23.


What will be the output of the program?


class SC2
{
public static void main(String [] args)
{
SC2 s = new SC2();
s.start();
}
void start()
{
int a = 3;
int b = 4;
System.out.print(" " + 7 + 2 + " ");
System.out.print(a + b);
System.out.print(" " + a + b + " ");
System.out.print(foo() + a + b + " ");
System.out.println(a + b + foo());
}
String foo()
{
return "foo";
}
}
  1.    9 7 7 foo 7 7foo
  2.    72 34 34 foo34 34foo
  3.    9 7 7 foo34 34foo
  4.    72 7 34 foo34 7foo
 Discuss Question
Answer: Option D. -> 72 7 34 foo34 7foo

Because all of these expressions use the + operator, there is no precedence to worry 

about and all of the expressions will be evaluated from left to right. If either operand being

 evaluated is a String, the + operator will concatenate the two operands; if both operands 

are numeric, the + operator will add the two operands.


Question 24.


What will be the output of the program?


class Bitwise
{
public static void main(String [] args)
{
int x = 11 & 9;
int y = x ^ 3;
System.out.println( y | 12 );
}
}
  1.    0
  2.    7
  3.    8
  4.    14
 Discuss Question
Answer: Option D. -> 14

The & operator produces a 1 bit when both bits are 1. The result of the & operation is 9. 

The ^ operator produces a 1 bit when exactly one bit is 1; the result of this operation is 10. 

The | operator produces a 1 bit when at least one bit is 1; the result of this operation is 14.


Question 25.


What is the output of this program?


class Output {
public static void main(String args[])
{
int a = 1;
int b = 2;
int c = 3;
a |= 4;
b >>= 1;
c
  1.    3 1 6
  2.    2 2 3
  3.    2 3 4
  4.    3 3 6
 Discuss Question
Answer: Option A. -> 3 1 6

None.
output:
$ javac Output.java
$ java Output
3 1 6


Question 26.


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.    1
  2.    2
  3.    Runtime error owing to division by zero in if condition.
  4.    Unpredictable behavior of program.
 Discuss Question
Answer: Option B. -> 2

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


Question 27.


What will be the output of the program?


class SSBool
{
public static void main(String [] args)
{
boolean b1 = true;
boolean b2 = false;
boolean b3 = true;
if ( b1 & b2 | b2 & b3 | b2 ) /* Line 8 */
System.out.print("ok ");
if ( b1 & b2 | b2 & b3 | b2 | b1 ) /*Line 10*/
System.out.println("dokey");
}
}
  1.    ok
  2.    dokey
  3.    ok dokey
  4.    No output is produced
  5.    Compilation error
 Discuss Question
Answer: Option B. -> dokey

The & operator has a higher precedence than the | operator so that on line 8 b1 and b2 

are evaluated together as are b2 & b3. The final b1 in line 10 is what causes that if test to 

be true. Hence it prints "dokey".


Question 28.


What is the output of this program?


class Output {
public static void main(String args[])
{
int a = 1;
int b = 2;
int c;
int d;
c = ++b;
d = a++;
c++;
b++;
++a;
System.out.println(a + " " + b + " " + c);
}
}
  1.    3 2 4
  2.    3 2 3
  3.    2 3 4
  4.    3 4 4
 Discuss Question
Answer: Option D. -> 3 4 4

None.
output:
$ javac Output.java
$ java Output
3 4 4



Question 29.


What is the output of this program?


class rightshift_operator {
public static void main(String args[])
{
int x;
x = 10;
x = x >> 1;
System.out.println(x);
}
}
  1.    10
  2.    5
  3.    2
  4.    20
 Discuss Question
Answer: Option B. -> 5

Right shift operator, >>, devides the value by 2.
output:
$ javac rightshift_operator.java
$ java rightshift_operator
5


Question 30.


What is the output of this program?


class Output {
public static void main(String args[])
{
int x , y;
x = 10;
x++;
--x;
y = x++;
System.out.println(x + " " + y);
}
}
  1.    11 11
  2.    10 10
  3.    11 10
  4.    10 11
 Discuss Question
Answer: Option C. -> 11 10

x is initialized to 10 then increased by 1 by ++ operator making it 11. x
is again decreased by -

operator making it 10, next x is incremented by
post increment and intialized to y, here the value

of x obtained before
increment operator is executed, so value of y is 10 and value of x is
11.
output:
$ javac Output.java
$ java Output
11 10



Latest Videos

Latest Test Papers