ProgrammingHardProduct-company placement · Java
Which constructors cause compile-time errors?
Which constructors cause compile-time errors?
class F {
final int x;
F() { } // constructor 1
F(int v) { x = v; x = v + 1; } // constructor 2
}- A. Only constructor 1
- B. Only constructor 2
- C. Both constructors
- D. Neither; x defaults to 0
Correct answer: C. Both constructors
A blank final field must be definitely assigned exactly once by the end of every constructor: constructor 1 never assigns it, constructor 2 assigns it twice.
More OOP with C++ and Java questions
- What is printed? java Map<String, Integer> m = new HashMap<>(); m.compute("a", (k, v) -> v == null ? 1 : v + 1); m.compute("a", (k, v) ->…
- What is printed? java Map<String, String> h = new HashMap<>(); h.put("k", null); System.out.print(h.size() + " "); Map<String, String> c =…
- What is printed? java List<Integer> e = Collections.emptyList(); System.out.print(e.size() + " "); e.add(1);
- What is printed? java System.out.print(String.valueOf((Object) null) + " "); System.out.print(String.valueOf(null));
- What is printed? java // static methods of the main class: static void f(int... a) { System.out.print("V"); } static void f(Integer a…
- What is printed? java // static methods of the main class: static void f(double d) { System.out.print("D"); } static void f(Object o) {…