ProgrammingHardAmazon/Microsoft SDE MCQ · Java placement
What is printed?
What is printed?
Object o = null; String s = (String) o; System.out.print(s + (o instanceof Object) + (s == null));
- A. nulltruetrue
- B. It throws NullPointerException
- C. It throws ClassCastException
- D. nullfalsetrue
Correct answer: D. nullfalsetrue
Casting null to any reference type succeeds and gives null, so no exception. instanceof is always false for null, even against Object, and s == null is true.
More OOP with C++ and Java questions
- What is printed? java List<String> l = new ArrayList<>(List.of("pear", "fig", "apple", "kiwi"))…
- Why does this class fail to compile? java class Util { static void f(List<String> l) { } static void f(List<Integer> l) { } }
- What is printed? java record Pt(int x, int y) { } // in main: Set<Pt> s = new HashSet<>(); s.add(new Pt(1, 2)); s.add(new Pt(1, 2))…
- Which line fails to compile? java class Animal { Animal make() { return new Animal(); } } class Dog extends Animal { Dog make() { return…
- What is printed? java PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.reverseOrder()); pq.addAll(List.of(3, 1, 4, 1, 5))…
- A Printer interface declares print(), scan(), fax() and staple(). A cheap printer class must implement all four and throws…