Java collections and errors quiz

  • Admin
  • 1 min read

Nine questions on ArrayList, HashMap, exceptions, generics and file reading.

Java

Java collections and errors quiz

9 questions, instant feedback

Question 1 of 9
Question 1 of 9 What is the difference between an array and an ArrayList?
Why this is the answer

An ArrayList resizes as items are added. An array's length is fixed at creation.

Question 2 of 9
Question 2 of 9 Which method adds to an ArrayList?
Why this is the answer

add appends or inserts at an index. push exists on Deque, not List.

Question 3 of 9
Question 3 of 9 What does a HashMap store?
Why this is the answer

HashMap maps unique keys to values. Use LinkedHashMap or TreeMap when order matters.

Question 4 of 9
Question 4 of 9 What does map.get(key) return for a missing key?
Why this is the answer

get returns null when the key is absent. getOrDefault is the safer form when a default makes sense.

Question 5 of 9
Question 5 of 9 Which exception must be caught or declared?
Why this is the answer

Checked exceptions are part of the method signature. Unchecked exceptions and errors do not require handling.

Question 6 of 9
Question 6 of 9 What is the purpose of finally?
Why this is the answer

finally runs on both paths, which is where cleanup such as closing a reader belongs.

Question 7 of 9
Question 7 of 9 What does a generic type parameter such as List give you?
Why this is the answer

Generics move type errors to compile time. At runtime the type information is mostly erased.

Question 8 of 9
Question 8 of 9 Which class reads a text file line by line?
Why this is the answer

BufferedReader wraps a Reader and offers readLine. FileOutputStream is for bytes.

Question 9 of 9
Question 9 of 9 What is the modern way to ensure a file is closed?
Why this is the answer

try-with-resources closes anything implementing AutoCloseable automatically, even when an exception is thrown.

Your answers stay in this browser. Nothing is sent to a server.