Oracle Certified Professional Java Se 11 Developer · Free Practice Question Hard
Question 75
Question ID: UKOCP34034
Consider below code of Test.java file:
- package com.udayankhattry.ocp;
- import java.util.Arrays;
- public class Test {
- public static void main(String[] args) {
- char [] arr1 = {'A'};
- char [] arr2 = {'A', 'A', 'A', 'A'};
- System.out.println(Arrays.compare(arr1, arr2));
- }
- }
What will be the result of compiling and executing above code?
- A 0
- B 1
- C 2
- D 3
- E -1
- F -2
-
G
-3
Reveal correct answer
Correct answer: G
Explanation
UKOCP34034:
Arrays class has following overloaded compare methods for primitive type arrays:
int compare(boolean[] a, boolean[] b)
int compare(byte[] a, byte[] b)
int compare(short[] a, short[] b)
int compare(char[] a, char[] b)
int compare(int[] a, int[] b)
int compare(long[] a, long[] b)
int compare(float[] a, float[] b)
int compare(double[] a, double[] b)
For comparing the array contents, these methods take help of static compare(x, y) method defined in respective Wrapper classes. For example, Arrays.compare(char[] a, char[] b) uses Character.compare(char x, char y) to compare array contents.
So, for the exam, you need to have an idea of how static compare(...) methods are implemented in these wrapper classes:
For Character, Byte & Short; compare method returns x - y.
For Integer and Long; compare method returns -1 if x < y, it returns 1 if x > y and it returns 0 if x == y.
For Float and Double; logic is almost same as Integer type but logic is bit complex for finding equality of two floats or two doubles (not part of the exam).
For Boolean; compare method returns 0 if x == y, 1 if x is true and -1 if x is false.
All of these compare methods of Arrays class compare the arrays lexicographically and return below values:
0: if the first and second array are equal and contain the same elements in the same order
<0: if the first array is lexicographically less than the second array
>0: if the first array is lexicographically greater than the second array
Consider following scenarios for lexicographic comparison of arrays:
A. If 1st array is null and 2nd array is not null, return -1.
E.g., Arrays.compare(null, new char[] {'A'}) returns -1
B. If 2nd array is null and 1st array is not null, return 1.
E.g., Arrays.compare(new boolean[] {false}, null) returns 1
C. If both the arrays are null, return 0.
E.g., Arrays.compare((int []) null, (int []) null) returns 0
D. If both the arrays are of equal lengths and contain same elements in the same order, return 0.
E.g., Arrays.compare(new char[] {'A', 'C', 'E'}, new char[] {'A', 'C', 'E'}) returns 0
E. If one array is the proper prefix of the other, then return a.length - b.length, where a refers to 1st array and b refers to 2nd array.
E.g., Arrays.compare(new char[] {'A', 'C', 'E'}, new char[] {'A'}) returns 3 - 1, which is 2. Please note that 2nd array is a proper prefix of 1st array.
F. For unequal arrays, respective compare methods of wrapper classes are invoked and it returns non-zero value (positive or negative) based on the array element comparison:
E.g.,
Arrays.compare(new char[] {'A', 'C', 'E'}, new char[] {'E', 'Y'}) returns -4, which is the result of Character.compare('A', 'E').
Arrays.compare(new int[] {5, 10, 15, 20}, new int[] {5, 100, 150, 200}) returns -1, which is the result of Integer.compare(10, 100).
Arrays.compare(new byte[] {5, 10, 100, 20}, new byte[] {5, 10, 15}) returns 85, which is the result of Byte.compare((byte)100, (byte)15).
For the given code,
char [] arr1 = {'A'};
char [] arr2 = {'A', 'A', 'A', 'A'};
arr1 is a proper prefix of arr2.
Hence, Arrays.compare(arr1, arr2) = arr1.length - arr2.length = 1 - 4 = -3
Output of the given code is: -3.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
