ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
List<Character> l = new ArrayList<>(List.of('a', 'b'));
System.out.print(l.contains('a') + " " + l.contains(97) + " " + l.indexOf((char) 98));- A. true true 1
- B. true false 1
- C. true false 0
- D. false false 1
Correct answer: B. true false 1
contains and indexOf take Object: 'a' boxes to Character (found), 97 boxes to Integer (never equal to a Character), and (char) 98 is 'b' boxed to Character at index 1.
More OOP with C++ and Java questions
- What is printed? java Function<Integer, Integer> f = x -> x + 1; Function<Integer, Integer> g = x -> x 2…
- What is printed? java List<String> l = new ArrayList<>(List.of("ccc", "ee", "a", "bb", "d"))…
- What is printed? java int[] c = {0}; Runnable r = () -> c[0]++; r.run(); r.run(); System.out.print(c[0]);
- What is printed? java List<Integer> a = new ArrayList<>(List.of(1, 2)); List<Integer> b = new LinkedList<>(List.of(1, 2))…
- What is printed? java Queue<Integer> q = new ArrayDeque<>(); q.add(1); q.add(2); q.offer(3); q.poll(); System.out.print(q.peek() + " " +…
- What is printed? java System.out.print(Stream.of(1, 2, 3).reduce(10, Integer::sum));