Core Java MCQ - Top 100 Questions and Answers

Welcome to this comprehensive guide featuring 100 Multiple-Choice Questions (MCQs) on Core Java. Java remains one of the most popular, versatile, and widely-used programming languages in the world, and understanding its core concepts is essential for anyone aspiring to become proficient in it. Whether you are a beginner, an intermediate programmer, or even an expert looking for a quick refresher, this blog post is your go-to resource.

We've curated these questions to cover all the important Core Java topics such as variables, data types, operators, control statements, loops, arrays, methods, inheritance, polymorphism, interfaces, packages, exception handling, multithreading, and much more. This ensures a rounded understanding of Java’s fundamentals.

But we didn’t stop there! Each question is followed by the correct answer as well as an explanation. These explanations are designed to reinforce your knowledge and help clarify any doubts you may have.

Are you ready to level up your Java skills? Let's dive into the quiz!

1. Who developed the Java programming language?

a) Microsoft
b) Oracle
c) Sun Microsystems
d) Google

Answer:

c) Sun Microsystems

Explanation:

Java was developed by Sun Microsystems. Later, Oracle acquired Sun Microsystems in 2010.

2. In which year was the first version of Java released?

a) 1991
b) 1995
c) 1998
d) 2000

Answer:

b) 1995

Explanation:

The first version of Java, Java 1.0, was officially released by Sun Microsystems in 1995.

3. What was the original name for Java?

a) C++++
b) Oak
c) Pine
d) Maple

Answer:

b) Oak

Explanation:

Java was initially called "Oak." It was developed by Sun Microsystems' Green Team, led by James Gosling.

4. What does JVM stand for?

a) Java Version Machine
b) Java Virtual Mechanism
c) Java Verified Module
d) Java Virtual Machine

Answer:

d) Java Virtual Machine

Explanation:

JVM stands for Java Virtual Machine. It is a virtualization engine that enables Java applications to run on various hardware platforms without modification.

5. Which of the following is responsible for converting bytecode into machine code?

a) JDK
b) JRE
c) JVM
d) Java Compiler

Answer:

c) JVM

Explanation:

JVM is responsible for converting bytecode (intermediate code) into machine code, ensuring Java's "Write Once, Run Anywhere" philosophy.

6. What does JDK include?

a) Only a compiler
b) Only a runtime environment
c) Both a compiler and a runtime environment
d) None of the above

Answer:

c) Both a compiler and a runtime environment

Explanation:

JDK (Java Development Kit) includes the Java Runtime Environment (JRE) and other tools like the Java compiler (javac).

7. Which of the following is NOT a part of the JRE?

a) Bytecode verifier
b) Classloader
c) Java Compiler
d) Java API classes

Answer:

c) Java Compiler

Explanation:

The Java compiler (javac) is a part of the JDK (Java Development Kit) and not the JRE (Java Runtime Environment).

8. What is the primary function of JRE?

a) Compilation
b) Debugging
c) Execution
d) Development

Answer:

c) Execution

Explanation:

JRE (Java Runtime Environment) is responsible for providing the runtime environment where Java programs can be executed.

9. Can you run a Java program without JRE?

a) Yes
b) No

Answer:

b) No

Explanation:

JRE (Java Runtime Environment) is essential for running Java programs. Without it, the Java bytecode cannot be executed.

10. Is JVM platform-independent?

a) Yes
b) No

Answer:

b) No

Explanation:

While Java code (bytecode) is platform-independent, JVMs are not. Each operating system has its own JVM.

11. Which operator is used to perform bitwise "AND" operation?

a) &&
b) &
c) |
d) ||

Answer:

b) &

Explanation:

The & operator is used to perform a bitwise "AND" operation.

12. What does the == operator compare in Java objects?

a) Values
b) References
c) Hash codes
d) Fields

Answer:

b) References

Explanation:

For objects, the == operator compares references, not the content of the objects. To compare the content, you would typically use the .equals() method.

13. Which operator is used for logical "AND" operation?

a) &&
b) &
c) ||
d) |

Answer:

a) &&

Explanation:

The && operator is used for the logical "AND" operation and is short-circuiting, meaning it won't evaluate the second operand if the first one is false.

14. Which of the following is a unary operator?

a) +
b) -
c) !
d) All of the above

Answer:

d) All of the above

Explanation:

Unary operators are operators that act on a single operand. +, -, and ! are all unary operators in Java.

15. Which operator has the highest precedence?

a) +
b) *
c) ()
d) &&

Answer:

c) ()

Explanation:

Parentheses () have the highest precedence in Java and are used to explicitly specify the order of evaluation in expressions.

16. What is the output of the expression true || false?

a) true
b) false
c) null
d) 0

Answer:

a) true

Explanation:

The || operator is a logical OR. The result of true || false is true.

17. Which of the following is not a primitive data type in Java?

a) byte
b) String
c) double
d) short

Answer:

b) String

Explanation:

String is a reference data type, not a primitive data type.

18. What is the default value of the int data type?

a) 0
b) 1
c) null
d) Undefined

Answer:

a) 0

Explanation:

The default value of the int data type is 0. In Java, each primitive data type has a default value.

19. Which of the following data types can store a floating-point number?

a) int
b) byte
c) double
d) char

Answer:

c) double

Explanation:

The double data type can store floating-point numbers. There's also float, but it wasn't listed among the options.

20. Which data type can store a single character?

a) String
b) byte
c) char
d) int

Answer:

c) char

Explanation:

The char data type is used to store a single character.

21. How many bits does the long data type use?

a) 8
b) 16
c) 32
d) 64

Answer:

d) 64

Explanation:

The long data type uses 64 bits to store its values.

22. What's the main difference between int and Integer in Java?

a) No difference
b) Integer can store larger values than int
c) int is a primitive data type, while Integer is a class
d) int can be null, while Integer cannot

Answer:

c) int is a primitive data type, while Integer is a class

Explanation:

int is a primitive data type, whereas Integer is a wrapper class that provides methods to operate on the int data type.

23. Which loop construct in Java is best suited when the number of iterations is known?

a) for loop
b) while loop
c) do-while loop
d) break statement

Answer:

 a) for loop

Explanation:

The for loop in Java is best suited when the number of iterations is known.

24. What is the purpose of the continue statement in a loop?

a) To exit the loop immediately
b) To skip the current iteration and move to the next iteration
c) To terminate the program
d) To execute a specific block of code

Answer:

b) To skip the current iteration and move to the next iteration

Explanation:

The continue statement in Java is used to skip the current iteration of a loop and move to the next iteration.

25. Which loop construct in Java is best suited when the number of iterations is unknown?

a) for loop
b) while loop
c) do-while loop
d) none

Answer:

b) while loop

Explanation:

The while loop in Java is used when the number of iterations is unknown or depends on a certain condition.

26. What is the key difference between a while loop and a do-while loop in Java?

a) The syntax used to define the loop
b) The number of iterations performed
c) The condition check timing
d) The ability to use the break statement

Answer:

c) The condition check timing

Explanation:

The key difference between a while loop and a do-while loop in Java is the timing of the condition check. In a while loop, the condition is checked before the loop body is executed, whereas in a do-while loop, the condition is checked after the loop body is executed.

27. Which loop construct guarantees that the loop body is executed at least once?

a) for loop
b) while loop
c) do-while loop
d) continue statement

Answer:

c) do-while loop

Explanation:

The do-while loop in Java guarantees that the loop body is executed at least once, as the condition is checked after the loop body is executed.

28. What is an infinite loop?

a) A loop that executes only once
b) A loop that never terminates naturally
c) A loop that contains an unreachable code block
d) A loop that uses the continue statement

Answer:

b) A loop that never terminates naturally

Explanation:

An infinite loop in Java is a loop that never terminates naturally unless interrupted externally or using a break statement.

29. Which statement is used to exit a loop prematurely?

a) return statement
b) continue statement
c) break statement
d) exit statement

Answer:

c) break statement

Explanation:

The break statement in Java is used to exit a loop prematurely and continue with the execution of the code outside the loop.

30. Which loop construct is best suited for iterating over an array or a collection?

a) for loop
b) while loop
c) do-while loop
d) continue statement

Answer:

a) for loop

Explanation:

The for loop in Java is best suited for iterating over an array or a collection, as it provides a convenient way to control the iteration using an index or an iterator.

31. How do you declare an array in Java?

a) int arrayName;
b) int[] arrayName;
c) int arrayName[];
d) Both b and c

Answer:

d) Both b and c

Explanation:

Arrays in Java can be declared using either int[] arrayName; or int arrayName[];.

32. Which method is used to get the length of an array in Java?

a) length()
b) size()
c) getLength()
d) length

Answer:

d) length

Explanation:

The length property is used to get the number of elements in an array, e.g., arrayName.length.

33. How do you initialize an array in Java?

a) int[] arr = (1,2,3,4,5);
b) int arr = {1,2,3,4,5};
c) int[] arr = new int[]{1,2,3,4,5};
d) int arr = new int(5);

Answer:

c) int[] arr = new int[]{1,2,3,4,5};

Explanation:

Arrays can be initialized using the new keyword followed by the type, and values enclosed in curly braces.

34. What happens when you try to access an array element with an index that is out of bounds?

a) It returns -1
b) It returns a null value
c) It throws an exception
d) It initializes a new array

Answer:

c) It throws an exception

Explanation:

Accessing an element outside the array's range will throw an ArrayIndexOutOfBoundsException.

35. How can you check if two arrays are equal in Java?

a) Use the == operator
b) Use the .equals() method
c) Use Arrays.equals() method
d) Compare each element one by one

Answer:

c) Use Arrays.equals() method

Explanation:

The Arrays.equals() method is the correct way to check if two arrays are equal. Using the == operator checks for reference equality, not the content.

36. How do you access the fourth element of an array named numbers?

a) numbers[4];
b) numbers(3);
c) numbers[3];
d) numbers.get(3);

Answer:

c) numbers[3];

Explanation:

Arrays use zero-based indexing, which means the fourth element is accessed using index 3.

37. Which of the following operators is used for concatenation of two strings?

a) +
b) *
c) &
d) +=

Answer:

a) +

Explanation:

In Java, the + operator is overloaded for string concatenation. When used between two strings, it concatenates them.

38. Which of the following creates a mutable string?

a) String
b) StringBuilder
c) StringChar
d) StringMutable

Answer:

b) StringBuilder

Explanation:

StringBuilder is a mutable sequence of characters, whereas String is immutable.

39. In Java, strings are:

a) Primitive data types
b) Immutable objects
c) Mutable objects
d) Arrays of characters

Answer:

b) Immutable objects

Explanation:

Once a String object is created, its content cannot be modified. Hence, it's considered immutable.

40. How do you find the length of a string named 'example'?

a) example.size()
b) example.length()
c) example.getLength()
d) example.len()

Answer:

b) example.length()

Explanation:

The length() method returns the number of characters in a string.

41. What is the result of the expression "Java" + "Programming"?

a) JavaProgramming
b) Java Programming
c) Java-Programming
d) JavaProgramming-

Answer:

a) JavaProgramming

Explanation:

The + operator concatenates two strings without adding any extra spaces.

42. Which method is used to compare two strings for equality?

a) ==
b) equals()
c) compare()
d) isEqual()

Answer:

b) equals()

Explanation:

The equals() method compares the content of two strings. The == operator compares the memory addresses, not the content.

43. Which class can create a string that is thread-safe?

a) String
b) StringBuffer
c) StringBuilder
d) StringSafe

Answer:

b) StringBuffer

Explanation:

StringBuffer is thread-safe, whereas StringBuilder is not.

44. What is the root class for all Java classes?

a) Object
b) Class
c) Superclass
d) Root

Answer:

a) Object

Explanation:

The Object class is the root of the Java class hierarchy. Every class has Object as a superclass.

45. What is polymorphism?

a) Multiple forms
b) Single form
c) No form
d) Static form

Answer:

a) Multiple forms

Explanation:

Polymorphism allows objects to be treated as instances of their parent class, leading to simplified code and a way to use objects dynamically.

46. What is encapsulation in Java?

a) The process of combining data and methods into a single unit
b) The process of hiding data and methods within a class
c) The process of creating multiple instances of a class
d) The process of reusing code from existing classes

Answer:

a) The process of combining data and methods into a single unit

Explanation:

Encapsulation is one of the four fundamental Object-Oriented Programming (OOP) concepts. The main idea behind encapsulation is to bind together the data (attributes) and the methods (functions) that operate on the data into a single unit or class. It also serves to hide the internal state of an object and requires the use of methods to access the object's data. This ensures that unwanted or unexpected modifications don't occur.

47. What is inheritance in Java?

a) The process of creating multiple instances of a class
b) The process of hiding data and methods within a class
c) The process of reusing code from existing classes
d) The process of combining data and methods into a single unit

Answer:

c) The process of reusing code from existing classes

Explanation:

Inheritance is a mechanism in Java that allows a class to inherit properties and behaviors from another class. It promotes code reuse by enabling the creation of subclasses that inherit the attributes and methods of a superclass. Subclasses can also add their own unique attributes and methods.

48. What is polymorphism in Java?

a) The ability of a class to inherit properties and behaviors from another class
b) The process of hiding data and methods within a class
c) The process of creating multiple instances of a class
d) The ability of an object to take on many forms

Answer:

d) The ability of an object to take on many forms

Explanation:

Polymorphism refers to the ability of an object to take on many forms or have multiple behaviors. In Java, polymorphism is achieved through method overriding and method overloading. It allows objects of different classes to be treated as objects of a common superclass, providing flexibility and extensibility.

49. What are abstract classes in Java?

a) Classes that cannot be instantiated
b) Classes that can be used as blueprints for creating objects
c) Classes that only contain abstract methods
d) All of the above

Answer:

d) All of the above

Explanation:

Abstract classes in Java cannot be instantiated directly and are typically used as blueprints for creating objects. They can contain abstract methods (methods without implementation) and regular methods. Abstract classes provide a way to define common behavior and enforce specific methods to be implemented by subclasses.

50. What is the purpose of the "super" keyword in Java?

a) To refer to the current object
b) To invoke the superclass constructor or methods
c) To create multiple instances of a class
d) To hide data and methods within a class

Answer:

b) To invoke the superclass constructor or methods

Explanation:

The "super" keyword in Java is used to refer to the superclass (or parent class) of the current object. It is commonly used to invoke the superclass constructor or methods within the subclass. The "super" keyword allows for code reuse and accessing superclass members that may be overridden in the subclass.

51. What is the purpose of the "this" keyword in Java?

a) To refer to the superclass
b) To create multiple instances of a class
c) To hide data and methods within a class
d) To refer to the current object

Answer:

d) To refer to the current object

Explanation:

The "this" keyword in Java is used to refer to the current object within an instance method or constructor. It is often used to distinguish between instance variables and method parameters or to access methods and variables of the current object.

52. What is the purpose of the "final" keyword in Java?

a) To prevent the inheritance of a class
b) To prevent overriding of a method
c) To prevent modification of a variable's value
d) All of the above

Answer:

d) All of the above

Explanation:

The "final" keyword in Java can be used to prevent the inheritance of a class, overriding of a method, or modification of a variable's value. When a class, method, or variable is declared as final, it cannot be further extended, overridden, or modified, respectively.

53. What is an interface in Java?

a) A class
b) A data type
c) A blueprint for a class
d) A data structure

Answer:

c) A blueprint for a class

Explanation:

An interface in Java is a blueprint that can be used to implement classes. It can have methods and variables, but the methods are abstract by default.

54. Which keyword is used to implement an interface?

a) extends
b) new
c) interface
d) implements

Answer:

d) implements

Explanation:

The implements keyword is used by classes to implement an interface.

55. Can an interface extend another interface in Java?

a) No
b) Yes

Answer:

b) Yes

Explanation:

Interfaces can extend other interfaces in Java, allowing an interface to inherit abstract methods from another interface.

56. Can an interface have a constructor?

a) Yes
b) No

Answer:

b) No

Explanation:

Interfaces cannot have constructors because they cannot be instantiated.

57. Which of the following access modifiers are implicitly applied to variables in an interface?

a) private
b) protected
c) public
d) default

Answer:

c) public

Explanation:

Variables in an interface are implicitly public, static, and final.

58. Is it possible to create an instance of an interface?

a) Yes
b) No

Answer:

b) No

Explanation:

We cannot instantiate an interface directly. However, we can create reference variables of an interface type.

59. How many interfaces can a Java class implement?

a) None
b) Only one
c) Two
d) As many as needed

Answer:

d) As many as needed

Explanation:

A Java class can implement any number of interfaces.

60. Can an interface inherit from a class?

a) Yes
b) No

Answer:

b) No

Explanation:

An interface cannot inherit from a class. It can only extend other interfaces.

61. Can an interface method be declared as final?

a) Yes
b) No

Answer:

b) No

Explanation:

Methods in an interface are implicitly abstract, and abstract methods cannot be final.

62. In Java 9, which type of methods can be added to interfaces to share code between methods?

a) Static
b) Private
c) Final
d) Protected

Answer:

b) Private

Explanation:

Starting from Java 9, interfaces can have private methods, which can help in sharing code between methods without exposing them to external classes.

63. An interface with no methods is known as?

a) Abstract Interface
b) Marker Interface
c) Empty Interface
d) Functional Interface

Answer:

b) Marker Interface

Explanation:

An interface with no defined methods is known as a marker interface. It is used to mark classes that support certain capabilities.

64. Which keyword is used to define a default method in an interface?

a) static
b) default
c) final
d) abstract

Answer:

b) default

Explanation:

The "default" keyword is used to define a default method in an interface.

65. Are all methods in an interface abstract?

a) Yes
b) No
c)
d)

Answer:

b) No

Explanation:

Prior to Java 8, all methods in an interface were implicitly abstract. However, with Java 8 and onwards, interfaces can have default and static methods.

66. Which of these can be contained in an interface?

a) Abstract methods
b) Constants
c) Default and static methods
d) All of the above

Answer:

d) All of the above

Explanation:

An interface can contain abstract methods, constants (public, static, final variables), static methods, and default methods.

67. Which Java feature helps achieve multiple inheritance?

a) Abstract classes
b) Static methods
c) Interfaces
d) Enums

Answer:

c) Interfaces

Explanation:

In Java, multiple inheritance is achieved through interfaces. A class can implement multiple interfaces, thereby inheriting the abstract methods of all the interfaces.

68. What is the default access modifier of a method in an interface in Java?

a) private
b) protected
c) public
d) None of the above

Answer:

c) public.

Explanation:

Interface methods are public by default since the idea is for them to be implemented by other classes.

69. Why were default methods introduced in Java 8 interfaces?

a) To provide multiple inheritance
b) To add utility functions
c) To provide backward compatibility with older interface versions
d) For better performance

Answer:

c) To provide backward compatibility with older interface versions.

Explanation:

Default methods allow developers to add new methods to interfaces with an implementation without affecting classes that already use this interface.

70. Starting from which Java version can an interface contain method implementations?

a) Java 5
b) Java 7
c) Java 8
d) Java 9

Answer:

c) Java 8

Explanation:

From Java 8 onwards, interfaces can have default and static method implementations.

71. What is the purpose of the instanceof operator?

a) Multiply instances
b) Compare two object references
c) Check if an object is an instance of a specific class or interface
d) To create a new instance of a class

Answer:

c) Check if an object is an instance of a specific class or interface

Explanation:

The instanceof operator is used to check if an object belongs to a particular class or implements a particular interface.

72. Which keyword is used to declare a class variable?

a) volatile
b) transient
c) static
d) final

Answer:

c) static

Explanation:

The static keyword is used to declare a class variable which is common to all instances of a class.

73. Which keyword is used to prevent a class from being inherited?

a) final
b) abstract
c) class
d) extends

Answer:

a) final

Explanation:

A class declared as final cannot be subclassed or extended.

74. Which keyword is used to create an instance of a class?

a) new
b) return
c) this
d) create

Answer:

a) new

Explanation:

The new keyword is used to instantiate an object from a class.

75. Which keyword is used to inherit the properties and methods from another class?

a) import
b) package
c) extends
d) implements

Answer:

c) extends

Explanation:

The extends keyword is used to inherit properties and methods from another class.

76. Which keyword is used to refer to the current instance of a class?

a) class
b) this
c) instance
d) object

Answer:

b) this

Explanation:

The this keyword is used to refer to the current instance of a class.

77. Which keyword in Java is used for importing packages into a program?

a) import
b) package
c) include
d) requires

Answer:

a) import

Explanation:

The import keyword is used to import a package or a class into a Java program.

78. What does the transient keyword indicate in a Java class?

a) The variable can change over time
b) The variable cannot be serialized
c) The variable is thread-safe
d) The variable is volatile

Answer:

b) The variable cannot be serialized

Explanation:

The transient keyword indicates that the variable should not be serialized when the class instance is persisted.

79. Which of these is a checked exception?

a) NullPointerException
b) ArithmeticException
c) IOException
d) IndexOutOfBoundsException

Answer:

c) IOException

Explanation:

IOException is a checked exception. Checked exceptions need to be either caught or declared in the method signature using the throws keyword.

80. Which of the following can be used to create a custom checked exception?

a) Extending the Exception class
b) Extending the Error class
c) Extending the RuntimeException class
d) None of the above

Answer:

a) Extending the Exception class

Explanation:

To create a custom-checked exception, you can extend the Exception class.

81. Which of these is an unchecked exception?

a) ClassNotFoundException
b) SQLException
c) IOException
d) ArithmeticException

Answer:

d) ArithmeticException

Explanation:

ArithmeticException, like all subclasses of RuntimeException, is an unchecked exception.

82. Which keyword is used to manually throw an exception in Java?

a) new
b) throw
c) throws
d) Throwable

Answer:

b) throw

Explanation:

The throw keyword is used to explicitly throw an exception.

83. Which of these classes is the superclass of all Exception and Error classes?

a) Exception
b) Error
c) Throwable
d) Object

Answer:

c) Throwable

Explanation:

The Throwable class is the superclass of all exception and error classes.

84. What does the finally block do?

a) Catches any exception
b) Executes whether an exception is thrown or not
c) Executes only when an exception is thrown
d) Executes only when an exception is not thrown

Answer:

b) Executes whether an exception is thrown or not

Explanation:

The finally block is executed irrespective of whether an exception is thrown or caught.

85. Which keyword in Java is used for constant variables?

a) const
b) static
c) constant
d) final

Answer:

d) final

Explanation:

In Java, the final keyword is used to declare constant variables.

86. In Java, what is the primary purpose of the Thread class?

a) File handling
b) String operations
c) Network operations
d) Creating and executing threads

Answer:

d) Creating and executing threads

Explanation:

The Thread class in Java is primarily used for creating and executing threads.

87. Which method is used to start the execution of a thread?

a) run()
b) start()
c) execute()
d) go()

Answer:

b) start()

Explanation:

The start() method is used to initiate the execution of a thread. It internally calls the run() method.

88. What does the join() method do when called on a thread object?

a) Terminates the thread
b) Pauses the thread
c) Forces the current executing thread to wait until the thread it's called on completes
d) Checks the status of a thread

Answer:

c) Forces the current executing thread to wait until the thread it's called on completes

Explanation:

The join() method makes the currently executing thread wait until the thread on which it's called completes its execution.

89. Which method can be used to momentarily pause the execution of the current thread?

a) sleep()
b) wait()
c) pause()
d) stop()

Answer:

a) sleep()

Explanation:

The sleep() method is used to pause the execution of the current thread for a specified period.

90. Which interface provides an alternative to extending the Thread class?

a) Runnable
b) Callable
c) Executor
d) Parallel

Answer:

a) Runnable

Explanation:

The Runnable interface provides an alternative way to define thread execution behavior without the need to extend the Thread class.

91. What is a daemon thread in Java?

a) A thread that monitors and logs other threads
b) A thread that runs continuously in the background
c) A thread that manages memory usage
d) A thread that is part of the Java virtual machine

Answer:

b) A thread that runs continuously in the background

Explanation:

Daemon threads are background threads that usually run continuously, performing tasks like garbage collection.

92. Which interface represents a collection of objects in which duplicate values can be stored?

a) List
b) Set
c) Map
d) ListMap

Answer:

b) List

Explanation:

The List interface in Java allow the addition of duplicate elements.

93. What will be the initial capacity of an ArrayList if it is created with the no-argument constructor?

a) 0
b) 5
c) 10
d) 16

Answer:

c) 10

Explanation:

The default initial capacity of an ArrayList (when created with the no-argument constructor) is 10.

94. What does a Set guarantee?

a) Order
b) No duplicates
c) Both
d) None of the above

Answer:

b) No duplicates

Explanation:

A Set guarantees no duplicate elements but does not guarantee any specific order of elements.

95. Which List implementation is synchronized?

a) ArrayList
b) Vector
c) LinkedList
d) None of the above

Answer:

b) Vector

Explanation:

Vector is synchronized, whereas ArrayList and LinkedList are not.

96. Which interface represents a key-value pair mechanism?

a) List
b) Set
c) Queue
d) Map

Answer:

d) Map

Explanation:

The Map interface represents a key-value pair mapping.

97. Which method is used to check if a Collection is empty?

a) isEmpty()
b) isNull()
c) checkEmpty()
d) hasElements()

Answer:

a) isEmpty()

Explanation:

The isEmpty() method is used to check if a Collection is empty.

98. What does the Collections class sort() method do?

a) Sorts elements in descending order
b) Sorts elements in ascending order
c) Randomizes the order of elements
d) Removes duplicates

Answer:

b) Sorts elements in ascending order

Explanation:

The sort() method in the Collections class sorts the elements in ascending order.

99. What is the key difference between HashSet and TreeSet?

a) HashSet allows duplicates; TreeSet doesn't
b) HashSet is ordered; TreeSet is unordered
c) HashSet is unordered; TreeSet is ordered
d) HashSet is synchronized; TreeSet is not

Answer:

c) HashSet is unordered; TreeSet is ordered

Explanation:

HashSet does not maintain any order whereas TreeSet maintains elements in a sorted order.

100. Which Collection does not allow null values?

a) ArrayList
b) HashMap
c) Hashtable
d) LinkedList

Answer:

c) Hashtable

Explanation:

Hashtable does not allow null keys or null values.

101. Which method is used to insert an object at a specific position in a List?

a) put()
b) set()
c) insert()
d) add()

Answer:

d) add()

Explanation:

The add(index, element) method is used to insert an element at a specific position in a List.

102. Which class provides a thread-safe implementation of the List interface?

a) ArrayList
b) Vector
c) HashMap
d) LinkedHashMap

Answer:

b) Vector

Explanation:

Vector is a thread-safe implementation of the List interface.

103. Which interface provides methods to traverse through a collection?

a) Iterator
b) Enumerator
c) Traverser
d) Mover

Answer:

a) Iterator

Explanation:

The Iterator interface provides methods to traverse through a collection.


Comments