Advanced Programming – Sample Exam Questions
Q. You are designing a graphical user interface (GUI) for a music player application. The player allows users to visualize album cover, song information, sound waves, and to control playback (play, pause, next, volume, etc.). Consider each visualization and control element (sound wave, song info, play/pause button, volume slider) as a separate Java class. Which design pattern would be most suitable to structure these elements and allow them to be dynamically added/removed from the main player window?
a. Composite
b. Observer
c. Decorator
Q. You have been asked to develop a text editor application in Java. The editor provides basic functionalities like editing text, but users can install plugins to extend its capabilities (e.g., spell checking, syntax highlighting, code formatting). Which of the following design patterns would be most suitable to allow users to extend the editor's functionality with plugins without modifying the core editor code?
a. Composite
b. Observer
c. Decorator
Q. Consider an ArrayList named cities that contains the following elements:
[Belfast, Cardiff, Dublin, Edinburgh]
What will be the contents of the colours list after the following code is executed?
cities.remove(1);
a. [Belfast, Cardiff, Dublin, Edinburgh]
b. [Belfast, Cardiff, Edinburgh]
c. [Belfast, Dublin, Edinburgh]
d. [Cardiff, Dublin, Edinburgh]
e. An error will occur (out of bounds exception)
Q. Consider a HashMap of type where the following products are added in the order specified:
products.put("Shirt", 30);
products.put("Hat", 15);
products.put("Shirt", 45);
products.put("Socks", 8);
products.put("Socks", 15);
Which of the following statements would be true after all entries are added? Select all that apply.
a. The map will contain 5 entries.
b. The map will contain 4 entries.
c. The map will only contain the objects with unique keys.
d. The map will only contain the objects with unique values.
e. The map will throw an error about comparing non-numerical objects.
f. Java will crash due to duplicate entries.
Q. Suppose class Sheep extends class Animal. Each of these classes declares two public instance fields (so Sheep has four publicly accessible fields in total). What are the lengths of Sheep.class.getFields() and Sheep.class.getDeclaredFields()?
a. 2 and 2
b. 2 and 4
c. 4 and 2
d. 4 and 4
Q. What is the purpose of the following method? Explain in 30 words or fewer.
private static Long doSomething(final String string) {
var X = Arrays.stream(string.split("\\."));
return X.map(
Z -> Arrays.stream(Z.split(" "))
.map(String::length).max(Comparator.naturalOrder()).get()
).filter(L -> L > 10).count();
}
(Free text answer)