Home
6.
Fill in the blanks: ____________means the state of an object cannot be changed while ____________means that it can.
  • A.
    Immutability, mutability
  • B.
    Rigidity, flexibility
  • C.
    Static, instance
  • D.
    None of the above
  • Answer & Explanation
  • Report
Answer : [A]
Explanation :
Option A is correct because mutability means the state can change and immutability means it cannot. In Option C, static means the state isn’t tied to an instance. In Option B, rigidity is not a common programming term.
Report
Name Email  
7.
Which is the first line to fail to compile?
class Tool {
void use() { } // r1
}
class Hammer extends Tool {
private void use() { } // r2
public void bang() { } // r3
}
  • A.
    r1
  • B.
    r2
  • C.
    r3
  • D.
    None of the above
  • Answer & Explanation
  • Report
Answer : [B]
Explanation :
The Hammer class is a subclass of the Tool class. Since the use() method in Hammer is intended to override the one in Tool, there are certain rules. One is that the access modifier must not be more specific. Therefore, trying to make it private is a problem. Option B is correct and r2 is the only line with a compiler error in this code.
Report
Name Email  
8.
Which of these classes properly implement(s) the singleton pattern?
class ExamAnswers {
private static ExamAnswers instance = new ExamAnswers();
private List answers = new ArrayList<>();
public static List getAnswers() {
return instance.answers;
}
}
class TestAnswers {
private static TestAnswers instance = new TestAnswers();
private List answers = new ArrayList<>();
public static TestAnswers getTestAnswers() {
return instance;
}
public List getAnswers() {
return answers;
}
}
  • A.
    ExamAnswers
  • B.
    TestAnswers
  • C.
    Both classes
  • D.
    Neither class
  • Answer & Explanation
  • Report
Answer : [D]
Explanation :
The singleton pattern requires that only one instance of the class exist. Neither of these classes meets that requirement since they have the default no-argument constructor available. There should have been a private constructor in each class. Therefore, Option D is correct. Remember that the exam doesn’t always include import statements to simplify the code you need to read.
Report
Name Email  
9.
What does the following print?
public class Transport {
static interface Vehicle {}
static class Bus implements Vehicle {}
public static void main(String[] args) {
Bus bus = new Bus();
boolean n = null instanceof Bus;
boolean v = bus instanceof Vehicle;
boolean b = bus instanceof Bus;
System.out.println(n + " " + v + " " + b);
}
}
  • A.
    true true true
  • B.
    false true true
  • C.
    false false false
  • D.
    None of the above
  • Answer & Explanation
  • Report
Answer : [B]
Explanation :
While using null with instanceof compiles, it always returns false. The other two instanceof calls show that instanceof can be used with both classes and interfaces. They both return true, making Option B correct.
Report
Name Email  
10.
What technique allows multiple variables from the same class to be shared across all instances of a class?
  • A.
    Encapsulation
  • B.
    Immutability
  • C.
    Singleton
  • D.
    Static
  • Answer & Explanation
  • Report
Answer : [D]
Explanation :
The static keyword is used to create a class-level variable, making Option D correct. Note that a singleton is where you limit a class so only one instance can be created. This means there are not multiple instances to share a variable across.
Report
Name Email