Oracle Certified Professional Java Se 11 Developer · Free Practice Question Hard
Question 54
Question ID: UKOCP26854
Given code of Test.java file:
- package com.udayankhattry.ocp;
- import java.util.ArrayDeque;
- import java.util.Deque;
- public class Test {
- public static void main(String[] args) {
- Deque<Character> chars = new ArrayDeque<>();
- chars.add('A');
- chars.add('B');
- chars.remove();
- chars.add('C');
- chars.remove();
- System.out.println(chars);
- }
- }
What will be the result of compiling and executing Test class?
- A [A]
- B [B]
- C [C]
Reveal correct answer
Correct answer: C
Explanation
UKOCP26854:
Deque's add() method invokes addLast(E) method and remove() method invokes removeFirst() method.
chars.add('A'); => [*A], {* represents HEAD element}
chars.add('B'); => [*A,B],
chars.remove(); => [*B],
chars.add('C'); => [*B,C],
chars.remove(); => [*C],
System.out.println(chars); => Prints [C] on to the console.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
You must be logged in to post a comment.
