Arrays
28 questions in 8 subtopics.
Array creation and default values
What do these print?int[] a = new int[3];
String[] s = new String[2];
boolean[] f = new boolean[1];
System.out.println(a[0] + " " + s[0] + " " + f[0] + " " + a.length);
int[] b = {};
System.out.println(b.length);
Arrays utilities
Which sorting algorithms doesArrays.sort use for primitive arrays and for object arrays, and why are they different?Why does this print false and what should you call instead?int[] x = {1, 2};
int[] y = {1, 2};
System.out.println(x.equals(y) + " " + x);
Arrays.binarySearch returns a negative number when a key is missing. How do you use that value to insert the key into a sorted array, and what goes wrong if the array is not sorted?Code calls List<Integer> list = Arrays.asList(1, 2, 3); list.add(4); and fails, while another place modifies the original array and sees the list change. Explain both.Your organization's high-throughput services still pass large primitive arrays between components for performance, while most teams prefer collections. Looking ahead to the Vector API, value classes and the Foreign Function and Memory API, how do you decide what to standardise?Multi-dimensional arrays
How isint[][] grid = new int[3][]; different from new int[3][4]? Write code that creates a triangular array where row i has i + 1 elements.An image processing service stores pixels as int[height][width] and is slow when iterating column by column. What would you change?Teams doing numeric and data-heavy work in Java ask whether to standardise on arrays, a third-party matrix library, or wait for platform features such as value classes and the Vector API. How do you make this multi-year bet?Array copying
CompareSystem.arraycopy, Arrays.copyOf, Arrays.copyOfRange and clone() for copying arrays. Are the copies deep or shallow?A class returns its internal array from a getter and stores the array passed to its constructor. A caller's later changes corrupt the object's state. What review comments do you make?A shared library returns internal arrays from many public methods, and several services have corrupted its state by modifying them. Changing the return types to List would break binary compatibility. How do you fix this over several releases?Varargs
What happens with each call?static void log(String... args) {
System.out.println(args == null ? "null" : args.length);
}
log();
log("a", "b");
log((String) null);
log((String[]) null);
List.of has overloads with fixed arity.A widely used internal logging API has a method log(String format, Object... args). Profiling across the fleet shows it allocates an array on every call even when the log level is disabled. How do you change the API without breaking callers?Array covariance and ArrayStoreException
Why does this compile and then fail at run time?Object[] objs = new String[2];
objs[0] = Integer.valueOf(1);
Common array operations
Remove duplicates from a sorted int[] in place and return the new logical length, for example{1,1,2,3,3,3,4} returns 4 with the first four elements {1,2,3,4}. Then explain how you would do it for an unsorted array.Write a method that rotates an int[] to the right by k positions in place with O(1) extra space, for example {1,2,3,4,5} with k = 2 becomes {4,5,1,2,3}. Handle k larger than the length.Compare the cost of filling, searching and summing a large int[] with a for loop, Arrays.fill, Arrays.stream(a).sum() and IntStream.parallel(). When does each make sense in a service?A pricing engine keeps hot data in large primitive arrays for speed, and new team members keep introducing bugs with index arithmetic. How do you keep the performance while making the code safer?Array conversions
How do you convert between byte[] and String, and between char[] and String, correctly? Why are passwords often kept in char[] rather than String?Convert between these forms and point out the traps: int[] toList<Integer>, List<Integer> to int[], String[] to List<String> that can be resized, and List<String> to String[].Code doesObject[] raw = list.toArray();
String[] names = (String[]) raw;
ClassCastException at runtime although every element is a String. Explain why, and also why a generic method<T> T[] toArray(List<T> l) {
return (T[]) l.toArray();
}
No question matches these choices.