What will be the output of the following code snippet?
ArrayList<String> list = new ArrayList<>(); list.add("apple"); list.add("banana"); System.out.println(list.get(1));
null
banana
apple
IndexOutOfBoundsException
What are fields in a Java class?
Methods that belong to the class
Variables that hold the state of an object
Keywords reserved by the Java language
Constructors used to create objects
Which method is used to remove an element at a specific index from an ArrayList?
extract()
delete()
remove()
discard()
What is the primary difference between an array and an ArrayList in Java?
Arrays can store primitive data types, while ArrayLists cannot.
Arrays are fixed in size, while ArrayLists are dynamically sized.
Arrays are part of the Java Collections Framework, while ArrayLists are not.
Arrays can store objects of different classes, while ArrayLists can only store objects of the same class.
What is the role of methods in a Java class?
To perform actions or operations on an object's data
To control the visibility of class members
To store data associated with an object
To define the blueprint for creating objects
Which keyword is used to define a block of code that might throw an exception?
finally
try
catch
throw
What is the primary purpose of a constructor in Java?
To define methods
To declare variables
To control access to class members
To initialize object attributes
What is the parent class of all exception classes in Java?
Exception
Throwable
RuntimeException
Error
Which access modifier allows a class's members to be accessed from anywhere?
public
default
private
protected
What is the time complexity of the get() method for an ArrayList?
get()
O(n)
O(log n)
O(1)
O(n log n)