Name four Swing components
JFrame, JPanel, JButton, JTextField, etc.
What is an advantage of using Optional<Object> over a nullable Object?
Valid answers:
-Forces users to account for the null case (no surprise runtime exceptions)
-Makes code more concise (replace if statement with .orElse())
Re-write the following function recursively:
public void countDown(int n) {
while(n > 0) {
System.out.println(n + "...");
Thread.sleep(1000);
n -= 1;
}
}
Possible answer:
public void countDown(int n) {
if(n == 0) {return;}
System.out.println(n + "...");
Thread.sleep(1000);
countDown(n-1);
}
At most, how many child components can be shown in a JFrame that uses BorderLayout?
5
Give an example of an intermediate Stream operation and a terminal Stream operation
Intermediate: map, filter
Terminal: collect, findAny, sum
What is the defining characteristic of tail recursion?
A few ways to think about it:
-The return value of every recursive call is the same
-The base case returns the final result
-The recursive call is at the very end of the function, after all the "work" is done
What is the Graphics object useful for?
It allows you to draw custom graphics on a component
What is the difference between map() and flatMap()
map returns a single object, flatMap returns a stream that is "flattened" into the current stream
Write a function that takes in a 2D array, and converts it into a 1D array, going top-to-bottom and left-to-right.
public int[] flattenByColumn(int[][] arr) {}
[Answers will vary]
What function is called whenever a Swing component re-renders?
paintComponent(Graphics g)
You have created a FancyFurniture class, and you want to allow your furniture to be sorted by price (an instance variable of type double). You decide to have your class implement Comparable<FancyFurniture>. Write a compareTo function that will make your furniture's natural ordering be from cheapest to most expensive.
Something like:
public int compareTo(FancyFurniture o) {
return this.price - o.getPrice();
}
Write a function that takes in two arrays, and creates a 2D array where the top row is 0 and then arr1, the leftmost column is 0 and then arr2, and every other cell is the sum of the top of its column and leftmost of its row
public int[][] sum(int[] arr1, int[] arr2) {}
[Answers will vary]