1. What is the correct way to declare a 2D array in Java?
2. Which of the following is true about classes in Java?
3. What is encapsulation in Java?
4. Which keyword is used to implement inheritance in Java?
5. What is polymorphism in Java?
6. Which of the following is true about ArrayList in Java?
7. What is recursion in Java?
8. Which of the following is a correct way to initialize a 2D array in Java?
9. What is the purpose of the 'private' access modifier in Java?
10. Which of the following is true about method overriding in Java?
11. What is the output of the following code?
ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
list.add(1, "D");
System.out.println(list);
12. Which of the following is true about abstract classes in Java?
13. What is the purpose of the 'super' keyword in Java?
14. Which of the following is true about the 'final' keyword in Java?
15. What is the correct way to declare a method that throws an exception in Java?
16. Which of the following is NOT a valid way to iterate over an ArrayList in Java?
17. What is the output of the following recursive method call?
public static int mystery(int n) {
if (n == 0) return 0;
return n + mystery(n - 1);
}
System.out.println(mystery(5));
18. Which of the following is true about interfaces in Java?
19. What is the purpose of the 'instanceof' operator in Java?
20. Which of the following is true about method overloading in Java?
Free Response Question 1: Diagonal Sum
Problem Statement
Write a Java method that takes a 2D array of integers and returns the sum of all elements on the main diagonal (top-left to bottom-right).
Here's the starter code:
public class DiagonalSum {
public static int sumDiagonal(int[][] array) {
// Your code here
return 0;
}
public static void main(String[] args) {
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
System.out.println(sumDiagonal(matrix)); // Should output 15 (1 + 5 + 9)
}
}
Write your code here
Make it print 15
Did it work?
Your program should print this:
15
Free Response Question 2: Fibonacci Sequence
Problem Statement
Implement a recursive method to calculate the nth Fibonacci number.
Here's the starter code:
public class Fibonacci {
public static int fibonacci(int n) {
// Your code here
return 0;
}
public static void main(String[] args) {
System.out.println(fibonacci(10)); // Should print the 10th Fibonacci number
}
}