Unit 7: ArrayList

How do you create an ArrayList of Strings in Java?
a) ArrayList list = new ArrayList<>();
b) ArrayList list = new ArrayList();
c) ArrayList list = new ArrayList();
d) ArrayList list = new ArrayList();

Answer: b) ArrayList list = new ArrayList();

Which method is used to add an element to an ArrayList?
a) add()
b) insert()
c) append()
d) push()

Answer: a) add()

What is the output of the following code?

ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
System.out.println(list.get(1));

a) 10
b) 20
c) 1
d) Compile-time error

Answer: b) 20

Unit 8: 2D Array

How do you declare a 2D array of integers in Java?
a) int[][] array;
b) int array[][];
c) int[] array[];
d) All of the above

Answer: d) All of the above

What is the output of the following code?

int[][] arr = { {1, 2}, {3, 4} };
System.out.println(arr[1][0]);

a) 1
b) 2
c) 3
d) 4

Answer: c) 3

Which of the following correctly initializes a 2D array with 3 rows and 4 columns?
a) int[][] arr = new int[3][4];
b) int[] arr = new int[3][4];
c) int[][] arr = new int[4][3];
d) int[] arr = new int[4][3];

Answer: a) int[][] arr = new int[3][4];

Unit 9: Inheritance

Which keyword is used to inherit a class in Java?
a) inherits
b) extends
c) implements
d) super

Answer: b) extends

What is the correct way to call a parent class’s constructor in a subclass?
a) super();
b) parent();
c) base();
d) this();

Answer: a) super();

Which of the following statements about inheritance is true?
a) A subclass can inherit private members of a superclass.
b) A subclass cannot override methods of a superclass.
c) A subclass can only have one superclass.
d) A subclass must have the same name as the superclass.

Answer: c) A subclass can only have one superclass.

Unit 10: Recursion

Which of the following is an example of a recursive method?
a) A method that calls itself
b) A method that calls another method
c) A method that runs indefinitely
d) A method that returns a value

Answer: a) A method that calls itself

What is the base case in a recursive method?
a) The condition under which the method stops calling itself
b) The first call to the recursive method
c) The last call to the recursive method
d) The condition under which the method calls itself

Answer: a) The condition under which the method stops calling itself

What will be the output of the following recursive method when called with factorial(3)?

public int factorial(int n) {
    if (n == 0) {
        return 1;
    }
    return n * factorial(n - 1);
}

a) 6
b) 3
c) 0
d) 1

Answer: a) 6