1. Which of the following is true about the final keyword in Java?
Step-by-Step Explanation
a. It can be used to prevent method overriding. - Correct. When a method is declared final, it cannot be overridden by subclasses.
b. It can be used to define abstract classes. - Incorrect. abstract is used for abstract classes.
c. It can be used to create dynamic arrays. - Incorrect. ArrayList or similar data structures are for dynamic arrays.
d. It can be used to define interfaces. - Incorrect. interface keyword is used for interfaces.
Answer: a. It can be used to prevent method overriding. ✅
2. What is the purpose of the try-catch block in Java?
Step-by-Step Explanation
c. To handle runtime exceptions. - Correct. The try-catch block is used for exception handling.
a. To define constant variables. - Incorrect. final keyword is used for constants.
b. To create threads. - Incorrect. Thread class is used for threads.
d. To declare static methods. - Incorrect. static keyword is used for static methods.
Answer: c. To handle runtime exceptions. ✅
3. Which of the following is a correct way to create an object of a class named MyClass?
Step-by-Step Explanation
b. MyClass obj = new MyClass(); - Correct. This is the standard way to create an object in Java.
d. new MyClass obj = MyClass(); - Incorrect. Syntax error.
a. MyClass = new obj(); - Incorrect. Syntax error.
c. obj = new MyClass(); - Incorrect. It misses the type declaration.
Answer: b. MyClass obj = new MyClass(); ✅
4. What is the output of the following code snippet?
int x = 5;
System.out.println(x++);
Step-by-Step Explanation
d. 5 - Correct. The x++ is a post-increment operator, so it prints the current value of x (5) and then increments it.
a. 6 - Incorrect. Post increment prints the value before incrementing.
b. 0 - Incorrect.
c. Error - Incorrect. The code compiles and runs.
Answer: d. 5 ✅
5. Which of the following is true about Java interfaces?
Step-by-Step Explanation
a. All methods in an interface are implicitly public and abstract. - Correct. This is a fundamental characteristic of interfaces.
b. Interfaces can have instance variables. - Incorrect. Interfaces can have only static final variables (constants).
c. Interfaces can have concrete methods. - Incorrect. Before Java 8 they could not. Now default and static methods can be implemented.
d. Interfaces can extend classes. - Incorrect. Interfaces extend other interfaces, classes extend other classes, and classes implement interfaces.
Answer: a. All methods in an interface are implicitly public and abstract. ✅
6. What is the purpose of the super keyword in Java?
Step-by-Step Explanation
b. To call the superclass constructor or method. - Correct. super is used to access members of the parent class.
c. To create a new thread. - Incorrect. Thread class is used for threads.
a. To define a static method. - Incorrect. static keyword is used for static methods.
d. To handle exceptions. - Incorrect. try-catch is used for exceptions.
Answer: b. To call the superclass constructor or method. ✅
7. Which of the following is a primitive data type in Java?
Step-by-Step Explanation
d. int - Correct. int is a primitive data type.
a. String - Incorrect. String is a class.
b. Object - Incorrect. Object is a class.
c. ArrayList - Incorrect. ArrayList is a class.
Answer: d. int ✅
8. What is the purpose of the static keyword in Java?
Step-by-Step Explanation
c. To create class-level variables or methods. - Correct. static members belong to the class rather than instances of the class.
d. To handle runtime exceptions. - Incorrect. try-catch is used for exceptions.
a. To define abstract classes. - Incorrect. abstract is used for abstract classes.
b. To create objects. - Incorrect. new keyword is used to create objects.
Answer: c. To create class-level variables or methods. ✅
9. Which of the following is a correct way to compare two strings in Java?
Step-by-Step Explanation
a. str1.equals(str2) - Correct. This method compares the content of the strings.
b. str1 == str2 - Incorrect. This compares the references, not the content.
c. str1.compareTo(str2) only for lexicographical order. - While correct for lexicographical order, it can be used to compare for equality if the return is 0; equals is better for simply checking equality.
d. str1.isEqual(str2) - Incorrect. This method does not exist.
Answer: a. str1.equals(str2) ✅
10. What is the purpose of the garbage collector in Java?
Step-by-Step Explanation
d. To automatically deallocate memory occupied by objects that are no longer in use. - Correct. The garbage collector handles memory management.
a. To compile Java code. - Incorrect. javac is used for compilation.
b. To execute SQL queries. - Incorrect. JDBC is used for database interaction.
c. To manage network connections. - Incorrect. Sockets or other networking libraries are used.
Answer: d. To automatically deallocate memory occupied by objects that are no longer in use. ✅
11. Which collection class does not allow duplicate elements and maintains elements in a sorted order?
Step-by-Step Explanation
b. TreeSet - Correct. TreeSet implements the SortedSet interface, ensuring elements are sorted and unique.
a. ArrayList - Incorrect. ArrayList allows duplicates and does not maintain sorted order.
c. HashMap - Incorrect. HashMap stores key-value pairs, not just elements, and does not maintain sorted order of values.
d. LinkedList - Incorrect. LinkedList allows duplicates and does not maintain sorted order.
Answer: b. TreeSet ✅
12. What is method overloading in Java?
Step-by-Step Explanation
c. Defining multiple methods with the same name but different parameters within a class. - Correct. This is the definition of method overloading.
a. Defining a method within another method. - Incorrect. This is not standard Java syntax.
b. Defining a class within another class. - Incorrect. This is called nested or inner classes.
d. Defining a method that overrides a superclass method. - Incorrect. This is method overriding, not overloading.
Answer: c. Defining multiple methods with the same name but different parameters within a class. ✅
13. Which of the following is true about abstract classes in Java?
Step-by-Step Explanation
a. They cannot be instantiated. - Correct. Abstract classes are designed to be extended, not instantiated.
b. They must have all methods implemented. - Incorrect. Abstract classes can have abstract methods (without implementation).
c. They can only have static methods. - Incorrect. Abstract classes can have instance methods.
d. They can only extend interfaces. - Incorrect. Abstract classes extend other classes.
Answer: a. They cannot be instantiated. ✅
14. What is the purpose of the instanceof operator in Java?
Step-by-Step Explanation
d. To check if an object is an instance of a particular class or interface. - Correct. This is the function of the instanceof operator.
a. To create a new instance of a class. - Incorrect. new keyword is used for object creation.
b. To compare two objects for equality. - Incorrect. equals() or == is used for equality.
c. To check if a variable is null. - Incorrect. variable == null is used for null checks.
Answer: d. To check if an object is an instance of a particular class or interface. ✅
15. Which of the following is true about Java's String class?
Step-by-Step Explanation
b. It is immutable. - Correct. Once a String object is created, its value cannot be changed.
a. It is mutable. - Incorrect. String is immutable.
c. It can be extended to create subclasses. - Incorrect. While it is technically possible to extend the String class, it is not common practice due to its immutability.
d. It is a primitive data type. - Incorrect. String is a class.
Answer: b. It is immutable. ✅
16. What is the purpose of the package keyword in Java?
Step-by-Step Explanation
a. To organize classes and interfaces into namespaces. - Correct. Packages are used for code organization and preventing naming conflicts.
b. To create threads. - Incorrect. Thread class is used for threads.
c. To handle exceptions. - Incorrect. try-catch is used for exceptions.
d. To define abstract methods. - Incorrect. abstract keyword is used for abstract methods.
Answer: a. To organize classes and interfaces into namespaces. ✅
17. What is the purpose of the finally block in a try-catch statement?
Step-by-Step Explanation
c. To execute code that should always run, regardless of whether an exception is thrown. - Correct. finally ensures cleanup code is executed.
a. To define abstract methods. - Incorrect. abstract keyword is used for abstract methods.
b. To create objects. - Incorrect. new keyword is used for object creation.
d. To declare static variables. - Incorrect. static keyword is used for static variable declaration.
Answer: c. To execute code that should always run, regardless of whether an exception is thrown. ✅
18. Which of the following is a checked exception in Java?
Step-by-Step Explanation
b. IOException - Correct. Checked exceptions must be handled in the code.
a. NullPointerException - Incorrect. This is an unchecked (runtime) exception.
c. ArrayIndexOutOfBoundsException - Incorrect. This is an unchecked (runtime) exception.
d. ArithmeticException - Incorrect. This is an unchecked (runtime) exception.
Answer: b. IOException ✅
19. What is the purpose of the Runnable interface in Java?
Step-by-Step Explanation
d. To define a task that can be executed by a thread. - Correct. Runnable is used to implement thread behavior.
a. To define abstract classes. - Incorrect. abstract keyword is used for abstract classes.
b. To handle input/output operations. - Incorrect. I/O streams are used for I/O operations.
c. To create graphical user interfaces. - Incorrect. Swing or JavaFX are used for GUIs.
Answer: d. To define a task that can be executed by a thread. ✅
20. What does the keyword transient signify when used in a class?
Step-by-Step Explanation
a. That the variable will not be serialized. - Correct. transient prevents a variable's value from being included in the serialized output.
b. That the variable is a constant. - Incorrect. final keyword is used for constants.
c. That the variable is accessible from all classes. - Incorrect. public keyword is used for global access.
d. That the variable is a static variable. - Incorrect. static keyword is used for static variables.
Answer: a. That the variable will not be serialized. ✅