Docsity

Java Programming Arrays and Lists Practice Exam, Exams of Technology

Toggle sidebar
bg1
Java Programming Arrays Lists and Structured
Data Certificate Practice Exam
**Question 1.** Which statement correctly creates an array of 10 integers in Java?
A) `int[] numbers = new int[10];`
B) `int numbers = new int[10];`
C) `int[10] numbers = new int;`
D) `int numbers[10] = new int;`
Answer: A
Explanation: The syntax `type[] name = new type[size];` creates an array; option A follows this
pattern.
**Question 2.** After executing `int[] a = {3, 5, 7};`, what is the value of `a[1]`?
A) 3
B) 5
C) 7
D) ArrayIndexOutOfBoundsException
Answer: B
Explanation: Arrays are zero‑based; index 1 refers to the second element, which is 5.
**Question 3.** Which property gives the number of elements in a Java array?
A) `size()`
B) `length`
C) `count`
D) `capacity`
Answer: B
Explanation: The public field `length` of an array returns its size.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57

Partial preview of the text

Download Java Programming Arrays and Lists Practice Exam and more Exams Technology in PDF only on Docsity!

Data Certificate Practice Exam

Question 1. Which statement correctly creates an array of 10 integers in Java? A) int[] numbers = new int[10]; B) int numbers = new int[10]; C) int[10] numbers = new int; D) int numbers[10] = new int; Answer: A Explanation: The syntax type[] name = new type[size]; creates an array; option A follows this pattern. Question 2. After executing int[] a = {3, 5, 7};, what is the value of a[1]? A) 3 B) 5 C) 7 D) ArrayIndexOutOfBoundsException Answer: B Explanation: Arrays are zero‑based; index 1 refers to the second element, which is 5. Question 3. Which property gives the number of elements in a Java array? A) size() B) length C) count D) capacity Answer: B Explanation: The public field length of an array returns its size.

Data Certificate Practice Exam

Question 4. Which loop correctly iterates over an array int[] arr using the enhanced for‑loop? A) for (int i = 0; i < arr.length; i++) {} B) for (int val : arr) {} C) while (arr.hasNext()) {} D) for (int i = 0; i <= arr.length; i++) {} Answer: B Explanation: The enhanced for‑loop syntax is for (type var : array). Question 5. What is the output of the following code?

int[] a = {2,4,6}; System.out.println(a.length); 

A) 2 B) 3 C) 4 D) Compilation error Answer: B Explanation: The array contains three elements; length returns 3. Question 6. Which statement creates a two‑dimensional array of 5 rows and 3 columns? A) int[][] matrix = new int[5][3]; B) int[5][3] matrix = new int; C) int matrix[5][3] = new int;

Data Certificate Practice Exam

B) asString() C) format() D) print() Answer: A Explanation: Arrays.toString(array) creates a readable string like [1, 2, 3]. Question 10. What does Arrays.sort(intArray); do? A) Sorts the array in descending order. B) Sorts the array in ascending order. C) Randomly shuffles the array. D) Does nothing; sort is unsupported for primitives. Answer: B Explanation: Arrays.sort implements a stable, ascending order sort for primitive arrays. Question 11. After executing int[] a = {4,2,9,1}; Arrays.sort(a);, what is a[2]? A) 2 B) 4 C) 9 D) 1 Answer: B Explanation: Sorted array becomes [1,2,4,9]; index 2 holds 4. Question 12. Which method performs a binary search on a sorted array? A) Arrays.find() B) Arrays.search()

Data Certificate Practice Exam

C) Arrays.binarySearch() D) Arrays.lookup() Answer: C Explanation: Arrays.binarySearch(array, key) returns the index of the key in a sorted array. Question 13. What is the time complexity of Arrays.binarySearch on a sorted array of size n? A) O(n) B) O(log n) C) O(n log n) D) O(1) Answer: B Explanation: Binary search repeatedly halves the search space, yielding logarithmic time. Question 14. Which of the following statements about array copying using System.arraycopy is true? A) It creates a deep copy of nested arrays. B) It copies elements bit‑by‑bit and is faster than a manual loop. C) It can only copy primitive arrays. D) It automatically resizes the destination array. Answer: B Explanation: System.arraycopy performs a native memory copy, making it faster than element‑by‑element loops. Question 15. Which operation is NOT directly supported by Java arrays? A) Adding an element at the end.

Data Certificate Practice Exam

B) ArrayList<String> list = new ArrayList[0]; C) ArrayList<String> list = new ArrayList(0); D) ArrayList<String> list = new ArrayList<String>(0); Answer: A Explanation: The generic diamond operator <> infers the type; new ArrayList<>() creates an empty list. Question 19. What is the amortized time complexity of adding an element to the end of an ArrayList? A) O(1) B) O(log n) C) O(n) D) O(n log n) Answer: A Explanation: Most adds are O(1); occasional resizing incurs O(n) but the amortized cost stays constant. Question 20. Which method inserts an element at index 2 in an ArrayList<String> list? A) list.add(2, "item"); B) list.insert(2, "item"); C) list.put(2, "item"); D) list.set(2, "item"); Answer: A Explanation: add(int index, E element) inserts at the specified position, shifting subsequent elements.

Data Certificate Practice Exam

Question 21. After executing ArrayList<Integer> nums = new ArrayList<>(Arrays.asList(1,2,3)); nums.remove(1);, what is the content of nums? A) [1, 2, 3] B) [1, 3] C) [2, 3] D) [1, 2] Answer: B Explanation: remove(int index) removes the element at index 1 (value 2), leaving [1,3]. Question 22. Which method replaces the element at index 0 with "Java" in an ArrayList<String> list? A) list.add(0, "Java"); B) list.set(0, "Java"); C) list.replace(0, "Java"); D) list.put(0, "Java"); Answer: B Explanation: set(int index, E element) overwrites the existing element. Question 23. How can you convert an ArrayList<String> to a plain String[] array? A) String[] arr = list.toArray(new String[0]); B) String[] arr = (String[]) list; C) String[] arr = list.asArray(); D) String[] arr = list.getArray(); Answer: A Explanation: toArray(T[] a) returns an array of the runtime type; passing a zero‑length array is idiomatic.

Data Certificate Practice Exam

Question 27. Which interface provides the iterator() method for traversing a collection? A) Collection B) Iterable C) List D) Iterator Answer: B Explanation: Iterable defines Iterator<T> iterator(); enabling enhanced for‑loops. Question 28. Which statement correctly removes the element "cat" from a List<String> animals while iterating with an Iterator? A) while (it.hasNext()) { if (it.next().equals("cat")) it.remove(); } B) for (String a : animals) { if (a.equals("cat")) animals.remove(a); } C) animals.removeIf(s - > s.equals("cat")); D) animals.delete("cat"); Answer: A Explanation: Only the iterator’s remove() method safely deletes the current element during iteration. Question 29. Which method of ListIterator allows traversing the list in reverse order? A) previous() B) back() C) rewind() D) reverse() Answer: A

Data Certificate Practice Exam

Explanation: ListIterator provides previous() to move backward and retrieve the preceding element. Question 30. Which statement about ArrayList's internal resizing is true? A) It doubles its capacity each time it runs out of space. B) It increases capacity by 10% each time. C) It never resizes; capacity is fixed at construction. D) It halves its capacity after each removal. Answer: A Explanation: The default growth strategy roughly doubles the internal array size when needed. Question 31. Which collection guarantees no duplicate elements? A) ArrayList B) LinkedList C) HashSet D) Vector Answer: C Explanation: Set implementations, such as HashSet, enforce uniqueness. Question 32. Which method adds an element to a HashSet<String>? A) addElement("x") B) insert("x") C) add("x") D) push("x") Answer: C

Data Certificate Practice Exam

Explanation: Tree‑based structures provide logarithmic performance for fundamental operations. Question 36. Which interface represents a collection of key‑value pairs? A) Set B) List C) Map D) Queue Answer: C Explanation: Map<K,V> stores unique keys associated with values. Question 37. Which method inserts a mapping into a HashMap<String,Integer>? A) put("key", 5) B) add("key", 5) C) insert("key", 5) D) set("key", 5) Answer: A Explanation: Map.put(K key, V value) adds or replaces a mapping. Question 38. After executing map.put("a", 1); map.put("b", 2); map.put("a", 3);, what is map.get("a")? A) 1 B) 2 C) 3 D) null Answer: C

Data Certificate Practice Exam

Explanation: The second put with the same key overwrites the previous value. Question 39. Which method returns a Set view of the keys contained in a Map? A) keySet() B) keys() C) getKeys() D) entrySet() Answer: A Explanation: Map.keySet() yields a Set<K> of all keys. Question 40. Which of the following is required for a custom class to be used reliably as a key in a HashMap? A) Override toString() only. B) Implement Comparable. C) Override both hashCode() and equals(). D) Extend HashMap. Answer: C Explanation: Correct hashing and equality semantics ensure proper bucket placement and lookup. Question 41. What does the values() method of a Map return? A) A Collection of all keys. B) A Set of all entries. C) A Collection of all values. D) An array of values. Answer: C

Data Certificate Practice Exam

Explanation: hasNext() returns true if the iteration has more elements. Question 45. Which statement correctly creates an immutable list containing the elements 1, 2, and 3? A) List<Integer> list = List.of(1,2,3); B) List<Integer> list = Collections.unmodifiableList(Arrays.asList(1,2,3)); C) Both A and B are correct. D) Neither A nor B is correct. Answer: C Explanation: Both List.of (Java 9+) and Collections.unmodifiableList produce unmodifiable lists. Question 46. Which class provides a thread‑safe variant of ArrayList? A) Vector B) CopyOnWriteArrayList C) SynchronizedArrayList D) Both A and B Answer: D Explanation: Vector is synchronized; CopyOnWriteArrayList offers thread‑safety with copy‑on‑write semantics. Question 47. Which method of StringBuilder appends a character array char[] data? A) append(data) B) add(data) C) insert(data) D) concat(data)

Data Certificate Practice Exam

Answer: A Explanation: StringBuilder.append(char[]) appends the entire character array. Question 48. What is the advantage of using StringBuilder over String concatenation inside a loop? A) StringBuilder is immutable. B) StringBuilder reduces the number of temporary objects, improving performance. C) StringBuilder automatically sorts characters. D) There is no advantage; both are equivalent. Answer: B Explanation: StringBuilder modifies a mutable buffer, avoiding repeated creation of intermediate String objects. Question 49. Which method of String returns a substring from index 2 (inclusive) to index 5 (exclusive)? A) substring(2,5) B) subString(2,5) C) slice(2,5) D) cut(2,5) Answer: A Explanation: String.substring(int beginIndex, int endIndex) follows the described semantics. Question 50. Which call extracts the character at position 4 from the string s = "hello"? A) s.charAt(4) B) s.get(4) C) s[4]

Data Certificate Practice Exam

D) IndexOutOfBoundsException Answer: B Explanation: Direct array accesses trigger ArrayIndexOutOfBoundsException. Question 54. Which method of Arrays returns a copy of the original array truncated to a new length? A) copyOf(original, newLength) B) slice(original, newLength) C) subArray(original, newLength) D) trim(original, newLength) Answer: A Explanation: Arrays.copyOf creates a new array of the specified length, copying elements as needed. Question 55. Which statement correctly checks whether an ArrayList<String> list is empty? A) list.isEmpty() B) list.size() == 0 C) Both A and B are correct. D) Neither A nor B is correct. Answer: C Explanation: Both isEmpty() and a size check of zero determine emptiness. Question 56. Which of the following is a characteristic of a LinkedHashMap? A) It sorts entries by key. B) It preserves insertion order.

Data Certificate Practice Exam

C) It provides O(1) random access by index. D) It is thread‑safe by default. Answer: B Explanation: LinkedHashMap maintains a linked list of entries in insertion order. Question 57. Which method of Map.Entry<K,V> returns the key? A) getKey() B) key() C) get() D) keySet() Answer: A Explanation: Map.Entry defines K getKey() and V getValue(). Question 58. Which collection is best suited for storing a large number of unique identifiers where order does not matter? A) ArrayList B) LinkedList C) HashSet D) TreeMap Answer: C Explanation: HashSet provides constant‑time insertion and ensures uniqueness without ordering. Question 59. Which method of Collections can be used to sort a List<Integer> in descending order? A) Collections.sort(list, Collections.reverseOrder());