ProgrammingModerateProduct-company placement · Java
What is printed?
What is printed?
// static generic method of the main class:
static <T extends Comparable<T>> T max(T a, T b) {
return a.compareTo(b) >= 0 ? a : b;
}
// in main:
System.out.print(max("pear", "apple") + " " + max(3, 7) + " " + max('z', 'a'));- A. apple 7 z
- B. pear 3 a
- C. pear 7 a
- D. pear 7 z
Correct answer: D. pear 7 z
T is inferred as String, Integer and Character in turn; each compares by its natural order: "pear" > "apple" lexicographically, 7 > 3 and 'z' > 'a'.
More OOP with C++ and Java questions
- What is printed? java class A { private int v = 1; boolean same(A other) { return v == other.v; } void set(A other) { other.v = 9; } } //…
- What is printed? cpp struct M { M(const char s) { cout << s; } }; struct S { M x, y; S() : y("Y"), x("X") { cout << "S"; } }; int main() {…
- What is printed? cpp struct B { virtual ~B() { hi(); } virtual void hi() { cout << "B"; } }; struct D : B { ~D() { cout << "~D "; } void…
- What is printed? cpp struct E { virtual const char what() const { return "E"; } }; struct F : E { const char what() const override {…
- What is printed? cpp struct E { }; struct F : E { }; int main() { try { throw F(); } catch (E&) { cout << "E"; } catch (F&) { cout << "F"…
- What is printed? cpp struct E { virtual void id() const { cout << "E"; } }; struct F : E { void id() const override { cout << "F"; } }…