Oracle Certified Professional Java Se 11 Developer · Free Practice Question Medium
Question 34
Question ID: UKOCP83821
Given code of Test.java file:
- package com.udayankhattry.ocp;
- import java.util.List;
- import java.util.Map;
- public class Test {
- public static void main(String[] args) {
- var map1 = Map.of("1", "2", "3", "4");
- var map2 = Map.ofEntries(Map.entry("3", "4"), Map.entry("1", "2"));
- var list1 = List.of("1", "2", "3", "4");
- var list2 = List.of("4", "3", "2", "1");
- System.out.println(map1.equals(map2) + ":" + list1.equals(list2));
- }
- }
What is the result?
- A true:true
- B false:false
- C true:false
- D false:true
Reveal correct answer
Correct answer: C
Explanation
UKOCP83821:
There are 11 overloaded of methods in Map interface:
Map.of()
Map.of(K k1, V v1)
Map.of(K k1, V v1, K k2, V v2)
Map.of(K k1, V v1, K k2, V v2, K k3, V v3)
Map.of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
Map.of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)
Map.of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6)
Map.of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7)
Map.of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8)
Map.of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9)
Map.of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10)
These create an unmodifiable map containing specified keys and values.
These methods throw NullPointerException - if any key or value is null or IllegalArgumentException - if the keys are duplicates
Please note interfaces List and Set contain 12 overloaded of methods each, whereas interface Map contains 11 overloaded of methods. Varargs version is not available in Map interface.
To provide more than 10 key-value pairs use, Map.ofEntries(Map.Entry<? extends K,? extends V>... entries) method.
Variable 'map1' refers to Map object containing 2 key-value pairs. Variable 'map2' refers to different Map object containing same 2 key-value pairs.
According to the Javadoc of equals(Object) method of Map interface, it compares the specified object with this map for equality. Returns true if the given object is also a map and the two maps represent the same mappings. More formally, two maps m1 and m2 represent the same mappings if m1.entrySet().equals(m2.entrySet()).
Hence, map1.equals(map2) evaluates to true.
There are 12 overloaded of methods in List interface:
List.of()
List.of(E e1)
List.of(E e1, E e2)
List.of(E e1, E e2, E e3)
List.of(E e1, E e2, E e3, E e4)
List.of(E e1, E e2, E e3, E e4, E e5)
List.of(E e1, E e2, E e3, E e4, E e5, E e6)
List.of(E e1, E e2, E e3, E e4, E e5, E e6, E e7)
List.of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8)
List.of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9)
List.of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10)
List.of(E... elements)
These create an unmodifiable list containing specified elements.
These methods throw NullPointerException - if an element is null
Variable 'list1' refers to List object containing elements "1", "2" , "3" & "4" in order. Variable 'list2' refers to List object containing elements "4", "3", "2" & "1" in order.
According to the Javadoc of equals(Object) method of Map interface, it compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if Objects.equals(e1, e2).) In other words, two lists are defined to be equal if they contain the same elements in the same order.
Hence, list1.equals(list2) evaluates to false.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
