Home
16.
Which methods compile?

private int numRakes;
public static int getNumShovels() {
return numShovels;
}
public static int getNumRakes() {
return numRakes;
}
  • A.
    Just getNumRakes()
  • B.
    Just getNumShovels()
  • C.
    Both methods
  • D.
    Neither method
  • Answer & Explanation
  • Report
Answer : [B]
Explanation :
A static method can access static variables, but not instance variables. The getNumRakes() method does not compile, so Option B is correct.
Report
Name Email  
17.
How many lines of the main method fail to compile?

11: static interface Vehicle {}
12: static class Bus implements Vehicle {}
13:
14: public static void main(String[] args) {
15: Bus bus = new Bus();
16:
17: System.out.println(null instanceof Bus);
18: System.out.println(bus instanceof Vehicle);
19: System.out.println(bus instanceof Bus);
20: System.out.println(bus instanceof ArrayList);
21: System.out.println(bus instanceof Collection);
22: }
  • A.
    one
  • B.
    two
  • C.
    three
  • D.
    four
  • Answer & Explanation
  • Report
Answer : [A]
Explanation :
You are allowed to use null with instanceof; it just prints false. The bus variable is both a Vehicle and a Bus, so lines 18 and 19 print true. Then it gets interesting. We know that bus is not an ArrayList or Collection. However, the compiler only knows that bus is not an ArrayList because ArrayList is a concrete class. Line 20 does not compile. The compiler can’t definitively state that bus is not a Collection. Some future program could create a subclass of Bus that does implement Collection, so this line compiles. Therefore, only line 20 fails to compile, and Option A is correct.
Report
Name Email  
18.
Which variable declaration is the first line not to compile?
class Building {}
class House extends Building{}
public void convert() {
Building b = new Building();
House h = new House();
Building bh = new House();
Building p = (House) b;
House q = (Building) h;
Building r = (Building) bh;
House s = (House) bh;
}
  • A.
    p
  • B.
    q
  • C.
    r
  • D.
    s
  • Answer & Explanation
  • Report
Answer : [B]
Explanation :
Building and House are both properly declared inner classes. Any House object can be stored in a Building reference, making the declarations for p and r compile. The declaration for s is also correct. It so happens that bh is a House object, so the cast works. The declaration of q is a problem though. While the cast itself is fine, a Building cannot be stored in a House reference, which means the assignment fails to compile. Option B is correct and is the only line with a compiler error in this code. Note that if the declaration of q was removed, the declaration of p would produce a ClassCastException at runtime.
Report
Name Email  
19.
Which statement is true about the code that can fill in the blank?
class Sticker {
public int hashCode() {
return 1;
}
public boolean equals(Object o) {
return____________ ;
}
}
  • A.
    It must return false.
  • B.
    It must return true.
  • C.
    It can return either true or false.
  • D.
    None of the above.
  • Answer & Explanation
  • Report
Answer : [D]
Explanation :
If two instances of a class have the same hash code, they might or might not be equal. The reverse is not true. If two objects are equal, they must have the same hash code in order to comply with the contracts of these methods. However, in this case, the answer is none of the above because the method can’t simply return true or false. Based on the rules of equals(), if null is passed in, the result must be false. If an object identity is passed in, the result must be true due to reflexivity. As a result, Option D is correct.
Report
Name Email  
20.
What change is needed to make Secret well encapsulated?


import java.util.*;
public class Secret {
private int number = new Random().nextInt(10);
public boolean guess(int candidate) {
return number == candidate;
}
}
  • A.
    Change number to use a public access modifier.
  • B.
    Declare a private constructor.
  • C.
    Remove the guess method.
  • D.
    None. It is already well encapsulated.
  • Answer & Explanation
  • Report
Answer : [D]
Explanation :
This class is a good example of encapsulation. It has a private instance variable and is accessed by a public method. No changes are needed to encapsulate it, and Option D is correct.
Report
Name Email