Quiz: Java
Java questions from real interviews as a quiz, with an explanation for every option.
Try it: 5 questions
Loading…
Sample questions
What is the primary role of
HandlerMappingin Spring MVC?- Check whether the user is authorized to make the request
- Convert a controller's response into an HTML page
- Invoke the selected controller method
- Map an incoming HTTP request to the appropriate handler
Which mechanisms can enforce a database constraint that depends on values from multiple columns? Select all that apply.
- Checking values only in Java before saving
- A
CHECKconstraint whose condition evaluates multiple columns - A composite
UNIQUEconstraint over a combination of columns - A composite
PRIMARY KEYmade up of multiple columns - A separate
UNIQUEconstraint on each column
What does
flatMapdo when processing a stream of lists, such asStream<List<String>>?- Processes each list in a separate parallel thread.
- Converts each list into another list while preserving the nested structure.
- Keeps only lists that contain elements of a specified type.
- Converts each list to a stream and combines the elements into one flat stream.
Why does the JVM create and store class metadata in Metaspace when the bytecode is already in a JAR file?
- Metaspace preserves classes after the JVM exits so they can be reused on the next run without loading them from the JAR.
- The JVM executes classes directly from Metaspace to read bytecode faster, without creating runtime structures.
- The JVM uses a runtime representation of classes, such as method metadata and the runtime constant pool; the JAR remains an archive on disk.
- A JAR contains only compiled bytecode, while Metaspace contains only the classes' static fields.
How is a linked list organized, and how does accessing its elements differ from accessing array elements?
- Nodes store data and links to neighboring nodes; accessing an element by index usually requires traversing the list.
- Each node is linked to every other node, making access to any element take O(1).
- Each node stores the index of the next element, while the data itself is kept in a separate array.
- Elements occupy a contiguous block of memory, so access by index takes O(1).
Which command can obtain a thread dump from a JVM process when its PID is known?
javac <PID>jstack <PID>jmap <PID>jstat <PID>
Which statements accurately describe how most modern LLMs work? Select all that apply.
- Text is processed as a sequence of tokens, represented internally as numbers.
- During generation, the model repeatedly chooses the next token based on a probability distribution.
- Each token has a fixed meaning that the model processes independently of context.
- During training, the model learns to predict the next token given the preceding context.
- The model looks up every answer in an explicit fact database stored inside it.
What is the correct general order for running tasks using a
ThreadPoolExecutor-based pool?- Create the pool, call
shutdown, then submit tasks withsubmit. - Pass tasks to the pool constructor, then call
executewith no arguments. - Create the pool, submit tasks with
executeorsubmit, then callshutdown. - Create a separate thread for each task, and use the pool only to stop them.
- Create the pool, call
Which standard Java exception most specifically indicates that a network connection could not be established?
InterruptedExceptionNullPointerExceptionFileNotFoundExceptionConnectException
Which statement correctly describes the main difference between a binary tree and a B-tree?
- A binary-tree node has at most two children, while a B-tree node can hold multiple keys and have more than two children.
- A binary-tree node can hold multiple keys, while a B-tree node has at most two children.
- A binary tree is always balanced, while a B-tree can become unbalanced after insertions.
- Both trees limit each node to two children, but only a B-tree stores keys in sorted order.