MCQs
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.
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);
}
}
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.
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";
}
}
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.
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.
None.
output:
$ javac Output.java
$ java Output
3 1 6
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
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");
}
}
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".
None.
output:
$ javac Output.java
$ java Output
3 4 4
Right shift operator, >>, devides the value by 2.
output:
$ javac rightshift_operator.java
$ java rightshift_operator
5
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