ProgrammingVery hardProduct-company placement · Java
What is printed?
What is printed?
// static methods of the main class:
static void f(Object o) { System.out.print("O"); }
static void f(Object[] a) { System.out.print("A"); }
// in main:
f(new String[]{"a"});
f((Object) new String[]{"a"});
f(new int[]{1});- A. AAA
- B. AOO
- C. AOA
- D. OOO
Correct answer: B. AOO
String[] fits both, and Object[] is more specific than Object. The cast fixes the static type to Object. An int[] is not an Object[] (primitive arrays are not covariant), so only f(Object) applies.
More OOP with C++ and Java questions
- 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++; //…
- Which line fails to compile? cpp struct A { virtual ~A() { } }; struct B { virtual ~B() { } }; struct C : A, B { }; int main() { A a = new…
- What does the C++ standard say about this code? cpp const int x = 5; int p = constcast<int>(&x); p = 6;