MCQs
class A{
public void printValue(){
System.out.println("A");
}
}
class B extends A{
public void printValue(){
System.out.println("B");
}
}
1. public class Test{
2. public static void main(String... args){
3. A b = new B();
4. newValue(b);
5. }
6. public static void newValue(A a){
7. if(a instanceof B){
8. ((B)a).printValue();
9. }
10. }
11. }
(2) and (4) are correct. (2) and (4) both evaluate to 32. (2) is shifting bits right then left
using the signed bit shifters >>and <<. (4) is shifting bits using the unsigned operator
>>>, but since the beginning number is positive the sign is maintained.
(1) evaluates to 8, (3) looks like 2 to the 5th power, but ^ is the Exclusive OR operator
so (3) evaluates to 7. (5) evaluates to 0 (2 >> 5 is not 2 to the 5th).
import java.awt.Button;
class CompareReference
{
public static void main(String [] args)
{
float f = 42.0f;
float [] f1 = new float[2];
float [] f2 = new float[2];
float [] f3 = f1;
long x = 42;
f1[0] = 42.0f;
}
}
which three statements are true?
1. f1 == f2
2. f1 == f3
3. f2 == f1[1]
4. x == f1[0]
f == f1[0]
(2) is correct because the reference variables f1 and f3 refer to the same array object.
(4) is correct because it is legal to compare integer and floating-point types.
(5) is correct because it is legal to compare a variable with an array element.
(3) is incorrect because f2 is an array object and f1[1] is an array element.
(1) is wrong. 3/2 = 1 (integer arithmetic).
(2) is wrong. 3 < 2 = false.
(3) is correct. 3 * 4 = 12.
(4) is correct. 3 <<2= 12. In binary 3 is 11, now shift the bits two places to the left and we get 1100 which is 12 in binary (3*2*2).
Statements (1), (2), (3), and (4) are correct. (1) is correct because when a floating-
point number (a double in this case) is cast to an int, it simply loses the digits after
the decimal.
(2) and (4) are correct because a long can be cast into a byte. If the long is over 127,
it loses its most significant (leftmost) bits.
(3) actually works, even though a cast is not necessary, because a long can store a byte.
(2) is correct. 16 >> 2 = 4
(4) is correct. 16 >>> 2 = 4
(1) is wrong. 16 * 4 = 64
(3) is wrong. 16/2 ^ 2 = 10
import java.awt.*;
class Ticker extends Component
{
public static void main (String [] args)
{
Ticker t = new Ticker();
/* Missing Statements ? */
}
}
which two of the following statements, inserted independently, could legally be inserted into missing section of this code?
1. boolean test = (Component instanceof t);
2. boolean test = (t instanceof Ticker);
3. boolean test = t.instanceof(Ticker);
4. boolean test = (t instanceof Component);
(2) is correct because class type Ticker is part of the class hierarchy of t; therefore it is
a legal use of the instanceof operator. (4) is also correct because Component is part of
the hierarchy of t, because Ticker extends Component.
(1) is incorrect because the syntax is wrong. A variable (or null) always appears before
the instanceof operator, and a type appears after it. (3) is incorrect because the statement
is used as a method (t.instanceof(Ticker);), which is illegal.
Java only ever passes arguments to a method by value (i.e. a copy of the variable)
and never by reference. Therefore the value of the variable i remains unchanged in
the main method.
If you are clever you will spot that 16 is 4 multiplied by 2 twice, (4 * 2 * 2) = 16. If you
had 16 left shifted by three bits then 16 * 2 * 2 * 2 = 128. If you had 128 right shifted by
2 bits then 128 / 2 / 2 = 32. Keeping these points in mind, you don't have to go converting
to binary to do the left and right bit shifts.
What will be the output of the program?
class Two
{
byte x;
}
class PassO
{
public static void main(String [] args)
{
PassO p = new PassO();
p.start();
}
void start()
{
Two t = new Two();
System.out.print(t.x + " ");
Two t2 = fix(t);
System.out.println(t.x + " " + t2.x);
}
Two fix(Two tt)
{
tt.x = 42;
return tt;
}
}
In the fix() method, the reference variable tt refers to the same object (class Two) as the
t reference variable. Updating tt.x in the fix() method updates t.x (they are one in the same
object). Remember also that the instance variable x in the Two class is initialized to 0.