ProgrammingHardProduct-company placement · Java
Which line fails to compile?
Which line fails to compile?
List<String>[] a = new List<String>[2]; // L1 List<?>[] b = new List<?>[2]; // L2 List<String>[] c = new List[2]; // L3
- A. L1 and L2
- B. L3 only
- C. L1 and L3
- D. L1 only
Correct answer: D. L1 only
Creating an array of a non-reifiable generic type is forbidden, because the array could not check its element type at run time. Unbounded-wildcard arrays are allowed, and a raw array compiles with an unchecked warning.
More OOP with C++ and Java questions
- Which line fails to compile? java class Box<T extends Number> { double d(T t) { return t.doubleValue(); } } // in main: Box<Integer> a =…
- Which class compiles? java class K1 { static { throw new RuntimeException(); } } class K2 { static { if (true) throw new…
- What happens? java class W { { if (true) throw new java.io.IOException("io"); } W() throws java.io.IOException { } }
- Which line fails to compile? cpp // C++17 struct S { int v; S(int x) : v(x) { } bool operator==(const S& o) const { return v == o.v; } }…
- What is printed? cpp struct B { void run() { step(); } virtual void step() { cout << "B"; } }; struct D : B { void step() override { cout…
- What is printed? cpp class A { int x = 3; friend int peek(const A& a) { return a.x; } }; int main() { A a; cout << peek(a); }