Consider the following Java code. What will it print when run?
public class Test { public static void main(String[] args) { int x = 5; int y = 10; int result = x > y ? x : y; System.out.println(result); } }
Look at the ternary operator and which value it selects.
The ternary operator checks if x > y. Since 5 > 10 is false, it selects y, which is 10.
Which of the following is considered a best practice to improve code readability in Java?
Think about what helps someone else understand your code easily.
Meaningful variable names make code easier to read and maintain. The other options reduce readability.
Examine the code below. What error will occur when compiling?
public class Demo { public static void main(String[] args) { final int number; System.out.println(number); } }
Consider what happens if a final variable is declared but not assigned before use.
The final variable 'number' is declared but never initialized. Java requires final variables to be assigned before use.
Which of the following is the correct syntax to declare a record named Person with fields String name and int age?
Recall the syntax introduced in Java 16 for records.
Records are declared with the record keyword followed by the name and a parameter list in parentheses.
What is the size of the map after executing the following Java code?
import java.util.*; public class MapTest { public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("a", 1); map.put("b", 2); map.put("a", 3); map.put("c", 4); map.put("b", 5); System.out.println(map.size()); } }
Remember how keys behave in a Map when duplicated.
Keys in a Map are unique. Adding a key again replaces the old value, so the final map has keys "a", "b", and "c" only.