- Home
- Programming
- OCP Java SE8
11.
Which is not a requirement for a class to be immutable?
- A.A private constructor is provided.
- B.Any instance variables are private.
- C.Methods cannot be overridden.
- D.There are no setter methods.
- Answer & Explanation
- Report
Answer : [A]
Explanation :
Explanation :
Option A is a requirement of a singleton class rather than an immutable one. The other three options are requirements of an immutable class. |
12.
Which statement is true about encapsulation while providing the broadest access
allowed?
- A.Variables are public and methods are private.
- B.Variables are public and methods are public.
- C.Variables are private and methods are public.
- D.Variables are private and methods are private.
- Answer & Explanation
- Report
Answer : [C]
Explanation :
Explanation :
If the variables are public, the class is not encapsulated because callers have direct access to them. This rules out Options A and B. Having private methods doesn’t allow the callers to use the data, making Option D an undesirable answer. Option C is correct and the classic definition of encapsulation where the data is not exposed directly. |
13.
What does the following print?
class Laptop extends Computer {
String type = "laptop";
}
public class Computer {
String type = "computer";
public static void main(String[] args) {
Computer computer = new Laptop();
Laptop laptop = new Laptop();
System.out.print(computer.type + "," + laptop.type);
}
}
class Laptop extends Computer {
String type = "laptop";
}
public class Computer {
String type = "computer";
public static void main(String[] args) {
Computer computer = new Laptop();
Laptop laptop = new Laptop();
System.out.print(computer.type + "," + laptop.type);
}
}
- A.computer,laptop
- B.laptop,computer
- C.laptop,laptop
- D.None of the above
- Answer & Explanation
- Report
Answer : [A]
Explanation :
Explanation :
While both objects are instances of Laptop, we are not calling methods in this example. Virtual method invocation only works for methods, not instance variables. For instance variables, Java actually looks at the type of the reference and calls the appropriate variable. This makes each reference call a different class’s instance variable in this example, and Option A is correct. |
14.
Which of these classes is/are immutable?
public final class Flower {
private final String name;
private final List counts;
public Flower(String name, List counts) {
this.name = name;
this.counts = counts;
}
public String getName() {
return name;
}
public List getCounts() {
return counts;
}
}
public final class Plant {
private final String name;
private final List counts;
public Plant(String name, List counts) {
this.name = name;
this.counts = new ArrayList<>(counts);
}
public String getName() {
return name;
}
public List getCounts() {
return new ArrayList<>(counts);
}
}
public final class Flower {
private final String name;
private final List
public Flower(String name, List
this.name = name;
this.counts = counts;
}
public String getName() {
return name;
}
public List
return counts;
}
}
public final class Plant {
private final String name;
private final List
public Plant(String name, List
this.name = name;
this.counts = new ArrayList<>(counts);
}
public String getName() {
return name;
}
public List
return new ArrayList<>(counts);
}
}
- A.Flower
- B.Plant
- C.Both classes
- D.Neither class
- Answer & Explanation
- Report
Answer : [B]
Explanation :
Explanation :
An immutable class must not allow the state to change. In the Flower class, the caller has a reference to the List being passed in and can change the size or elements in it. Similarly, any class with a reference to the object can get the List by calling get() and make these changes. The Flower class is not immutable. The Plant class shows how to fix these problems and is immutable. Option B is correct. |
15.
Which methods compile?
private static int numShovels;
private int numRakes;
public int getNumShovels() {
return numShovels;
}
public int getNumRakes() {
return numRakes;
}
private static int numShovels;
private int numRakes;
public int getNumShovels() {
return numShovels;
}
public int getNumRakes() {
return numRakes;
}
- A.Just getNumRakes()
- B.Just getNumShovels()
- C.Both methods
- D.Neither method
- Answer & Explanation
- Report
Answer : [C]
Explanation :
Explanation :
An instance method can access both instance variables and static variables. Both methods compile and Option C is correct. |