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));
banana
null
IndexOutOfBoundsException
apple
Which method is used to remove an element at a specific index from an ArrayList?
extract()
remove()
delete()
discard()
What is the primary purpose of a method in Java?
To store data
To control program flow
To perform a specific task
To define a class
If a method in Java is declared 'private,' where can it be accessed?
From any class in the application
From any class within the same package
Only from within the same class
From subclasses, even in different packages
What is the primary purpose of exception handling in Java?
To completely prevent runtime errors from occurring.
To improve the performance of Java applications.
To enforce strict coding standards and prevent compilation errors.
To provide a mechanism for handling unexpected events during program execution.
What is the output of the following code snippet?
int x = 5; if (x > 10) { System.out.println("Greater than 10"); } else { System.out.println("Less than or equal to 10"); }
x is 5
Greater than 10
Error
Less than or equal to 10
What is the core principle behind Object-Oriented Programming (OOP) in Java?
Code reusability
Data security
Faster execution speed
Improved memory management
Can a class have multiple constructors?
Yes, but they must have the same parameter list.
Yes, as long as they have different parameter lists.
No, a class can only have one constructor.
Constructors are optional, so a class may or may not have any.
What is the parent class of all exception classes in Java?
Exception
RuntimeException
Throwable
What is the fundamental difference between Java and JavaScript?
Java is a scripting language, while JavaScript is a compiled language.
They are essentially the same; the names are interchangeable.
Java is a statically-typed language, while JavaScript is dynamically-typed.
Java is primarily used for client-side web development, while JavaScript is used for server-side applications.