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?
Answer:
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?
Answer:
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?
Answer:
Explanation:
Java was initially called "Oak." It was developed by Sun Microsystems' Green Team, led by James Gosling.
4. What does JVM stand for?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
Explanation:
JRE (Java Runtime Environment) is essential for running Java programs. Without it, the Java bytecode cannot be executed.
10. Is JVM platform-independent?
Answer:
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?
Answer:
Explanation:
The & operator is used to perform a bitwise "AND" operation.
12. What does the == operator compare in Java objects?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
Explanation:
String is a reference data type, not a primitive data type.
18. What is the default value of the int data type?
Answer:
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?
Answer:
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?
Answer:
Explanation:
The char data type is used to store a single character.
21. How many bits does the long data type use?
Answer:
Explanation:
The long data type uses 64 bits to store its values.
22. What's the main difference between int and Integer in Java?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
Explanation:
StringBuilder is a mutable sequence of characters, whereas String is immutable.
39. In Java, strings are:
Answer:
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'?
Answer:
Explanation:
The length() method returns the number of characters in a string.
41. What is the result of the expression "Java" + "Programming"?
Answer:
Explanation:
The + operator concatenates two strings without adding any extra spaces.
42. Which method is used to compare two strings for equality?
Answer:
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?
Answer:
Explanation:
StringBuffer is thread-safe, whereas StringBuilder is not.
44. What is the root class for all Java classes?
Answer:
Explanation:
The Object class is the root of the Java class hierarchy. Every class has Object as a superclass.
45. What is polymorphism?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
Explanation:
The implements keyword is used by classes to implement an interface.
55. Can an interface extend another interface in Java?
Answer:
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?
Answer:
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?
Answer:
Explanation:
Variables in an interface are implicitly public, static, and final.
58. Is it possible to create an instance of an interface?
Answer:
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?
Answer:
Explanation:
A Java class can implement any number of interfaces.
60. Can an interface inherit from a class?
Answer:
Explanation:
An interface cannot inherit from a class. It can only extend other interfaces.
61. Can an interface method be declared as final?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
Explanation:
The "default" keyword is used to define a default method in an interface.
65. Are all methods in an interface abstract?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
Explanation:
From Java 8 onwards, interfaces can have default and static method implementations.
71. What is the purpose of the instanceof operator?
Answer:
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?
Answer:
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?
Answer:
Explanation:
A class declared as final cannot be subclassed or extended.
74. Which keyword is used to create an instance of a class?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
Explanation:
To create a custom-checked exception, you can extend the Exception class.
81. Which of these is an unchecked exception?
Answer:
Explanation:
ArithmeticException, like all subclasses of RuntimeException, is an unchecked exception.
82. Which keyword is used to manually throw an exception in Java?
Answer:
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?
Answer:
Explanation:
The Throwable class is the superclass of all exception and error classes.
84. What does the finally block do?
Answer:
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?
Answer:
Explanation:
In Java, the final keyword is used to declare constant variables.
86. In Java, what is the primary purpose of the Thread class?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
Explanation:
The default initial capacity of an ArrayList (when created with the no-argument constructor) is 10.
94. What does a Set guarantee?
Answer:
Explanation:
A Set guarantees no duplicate elements but does not guarantee any specific order of elements.
95. Which List implementation is synchronized?
Answer:
Explanation:
Vector is synchronized, whereas ArrayList and LinkedList are not.
96. Which interface represents a key-value pair mechanism?
Answer:
Explanation:
The Map interface represents a key-value pair mapping.
97. Which method is used to check if a Collection is empty?
Answer:
Explanation:
The isEmpty() method is used to check if a Collection is empty.
98. What does the Collections class sort() method do?
Answer:
Explanation:
The sort() method in the Collections class sorts the elements in ascending order.
99. What is the key difference between HashSet and TreeSet?
Answer:
Explanation:
HashSet does not maintain any order whereas TreeSet maintains elements in a sorted order.
100. Which Collection does not allow null values?
Answer:
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?
Answer:
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?
Answer:
Explanation:
Vector is a thread-safe implementation of the List interface.
103. Which interface provides methods to traverse through a collection?
Answer:
Explanation:
The Iterator interface provides methods to traverse through a collection.
Comments
Post a Comment
Leave Comment