ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
Object o = new int[]{1};
System.out.print((o instanceof int[]) + " " + (o instanceof Object[]));- A. true true
- B. true false
- C. false false
- D. Compile-time error: arrays cannot be tested with instanceof
Correct answer: B. true false
Every array is an Object, and int[] is its own reference type. But an int[] is not an Object[]: array covariance applies only to arrays of references.
More OOP with C++ and Java questions
- Which line fails to compile? java class P { void run() throws java.io.IOException { } } class C extends P { @Override void run() { } } //…
- What is printed? java Map<String, Integer> m = new HashMap<>(); m.put("a", 1); m.keySet().remove("a"); System.out.print(m.size() + " ")…
- What is printed? java TreeSet<Integer> s = new TreeSet<>(List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); SortedSet<Integer> sub = s.subSet(3, 6)…
- What is printed? java List<String> l = Arrays.asList("b", null, "a"); l.sort(Comparator.nullsFirst(Comparator.naturalOrder()))…
- What is printed? java // static method of the main class: static String make() { System.out.print("M"); return "y"; } // in main…
- What is printed? java Stream<Integer> s = Stream.of(1, 2, 3); System.out.print(s.count()); System.out.print(s.count());