Oracle Certified Professional Java Se 11 Developer · Free Practice Question Hard
Question 8
Question ID: UKOCP70946
Given code:
- package com.udayankhattry.ocp;
- import java.security.*;
- import java.util.*;
- import java.util.stream.Collectors;
- class Attacker {
- private final List<Integer> attackCoordinates = new ArrayList<>(List.of(100, 276, 190));
- public final List<Integer> getCoord() {
- PrivilegedAction<List<Integer>> pa = () -> {
- /*Do some processing and update attack co-ordinates. */
- return attackCoordinates;
- };
- return AccessController.doPrivileged(pa);
- }
- }
Does above code expose internal data?
- A Yes
- B No
Reveal correct answer
Correct answer: A
Explanation
UKOCP70946:
Even though 'attackCoordinates' is private and final, but ArrayList object it refers is not constant.
return attackCoordinates; exposes this ArrayList object, whose contents may be changed by the calling code.
return attackCoordinates; should be written as return Collectionss.unmodifiableList(attackCoordinates);
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.
