ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
class N {
public Object clone() throws CloneNotSupportedException { return super.clone(); }
}
// in main:
try { new N().clone(); System.out.print("ok"); }
catch (CloneNotSupportedException e) { System.out.print("CNSE"); }- A. ok
- B. Compile-time error: N must implement Cloneable
- C. CNSE
- D. A ClassCastException is thrown
Correct answer: C. CNSE
Overriding clone() compiles, but Object.clone checks at run time that the class implements the Cloneable marker interface and otherwise throws CloneNotSupportedException.
More OOP with C++ and Java questions
- What is printed? java System.out.print((List.of(1, 2).hashCode() == List.of(2, 1).hashCode()) + " " + (Set.of(1, 2).hashCode() ==…
- What is printed? java // static methods of the main class: static void f(Object o) { System.out.print("O"); } static void f(Object[] a) {…
- What is printed? java class Range implements Iterable<Integer> { public Iterator<Integer> iterator() { return List.of(1, 2, 3).iterator()…
- Which line fails to compile (C++17)? cpp struct A { A() = default; A(const A&) = delete; A(A&&) = default; }; A make() { A a; return a; }…
- What is printed? cpp int main() { const string s = "x"; string t = std::move(s); cout << s << t; }
- Which line fails to compile? cpp int main() { int x = 1; const int& r = x; auto a = r; // L1 a++; // L2 decltype(r) b = x; // L3 b++; //…