ProgrammingVery hardProduct-company placement · Java
What is printed?
What is printed?
class P { void f(int x) { System.out.print("P-int "); } }
class C extends P { void f(long x) { System.out.print("C-long "); } }
// in main:
P p = new C(); p.f(5);
C c = new C(); c.f(5);- A. C-long C-long
- B. P-int C-long
- C. C-long P-int
- D. P-int P-int
Correct answer: D. P-int P-int
C.f(long) has a different parameter type, so it OVERLOADS rather than overrides f(int). Through P only f(int) exists; through C both are visible and f(int) is the exact match for an int argument.
More OOP with C++ and Java questions
- What is printed? java BigDecimal a = new BigDecimal("1.0"), b = new BigDecimal("1.00"); Set<BigDecimal> h = new HashSet<>(List.of(a, b))…
- What is printed? java Stream.of("a", "bb", "ccc") .filter(s -> { System.out.print("f" + s.length()); return s.length() > 1; }) .map(s -> {…
- What is printed? java Stream.of(1, 2, 3).peek(System.out::print).map(x -> x 2); System.out.print("end");
- What is printed? java Map<Integer, Long> m = Stream.of("a", "bb", "cc", "ddd", "e") .collect(Collectors.groupingBy(String::length…
- What is printed? java Map<Integer, String> m = Stream.of("ab", "cd", "e") .collect(Collectors.toMap(String::length, s -> s))…
- Why is the comparator (a, b) -> a - b unsafe for sorting arbitrary int values in Java?