ProgrammingEasyAccenture · Cognizant pattern
In Java, final applied to a class, a method and a variable means, respectively:
- A. Cannot be instantiated; cannot be overridden; cannot be reassigned
- B. Cannot be instantiated; cannot be overloaded; cannot be read
- C. Cannot be subclassed; cannot be overloaded; must be static
- D. Cannot be subclassed; cannot be overridden; cannot be reassigned
Correct answer: D. Cannot be subclassed; cannot be overridden; cannot be reassigned
A final class (like String) has no subclasses, a final method cannot be overridden (it can still be overloaded), and a final variable is assigned exactly once. Preventing instantiation is what abstract or a private constructor does.
More OOP with C++ and Java questions
- What is printed? java List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(1, 5); System.out.print(list);
- What is printed? java Set<String> s = new HashSet<>(); s.add("a"); s.add("b"); s.add("a"); System.out.print(s.size() + " " + s.add("b"));
- What is printed? java try { System.out.print("T"); throw new RuntimeException(); } catch (Exception e) { System.out.print("C"); } finally…
- Why must a C++ copy constructor take its parameter by reference (A(const A &other)) rather than by value?
- What is printed? java System.out.print("placement".substring(2, 5) + "placement".indexOf('e'));
- What is the time complexity of get(i) on a Java ArrayList and on a LinkedList?