ProgrammingModerateProduct-company placement · Java
Which line fails to compile?
Which line fails to compile?
class Box<T extends Number> {
double d(T t) { return t.doubleValue(); }
}
// in main:
Box<Integer> a = new Box<>(); // L1
Box<String> b = new Box<>(); // L2
Box<Number> c = new Box<>(); // L3- A. L3 only
- B. L2 and L3
- C. None
- D. L2 only
Correct answer: D. L2 only
The bound T extends Number allows Number and its subclasses and lets the body call Number methods such as doubleValue(). String is not a Number.
More OOP with C++ and Java questions
- 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); }
- Which function returns a reference that is SAFE to use after the call returns?