Oracle Certified Professional Java Se 8 Programmer · Free Practice Question Hard
Question 43
Given code of Test.java file:
- package com.udayan.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.remove();
- chars.remove();
- System.out.println(chars);
- }
- }
What will be the result of compiling and executing Test class?
- A [A]
- B Runtime Exception
- C []
Reveal correct answer
Correct answer: B
Explanation
Deque's add() method invokes addLast(E) method and remove() method invokes removeFirst() method.
chars.add('A'); => [A],
chars.remove(); => [],
chars.remove(); => No elements left to remove() and hence java.util.NoSuchElementException is thrown at runtime.
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.
