Core Java MCQs

1 min read

Core Java forms the fundamental foundation of the Java programming language, encompassing standard syntax, object-oriented programming paradigms, and essential APIs required for standalone and backend application development. It covers crucial runtime components like the Java Virtual Machine (JVM), memory management, exception handling, multithreading, and the Collections Framework. A strong grip on core Java concepts is vital for debugging applications efficiently, writing reusable code, and clearing technical interviews across software engineering roles.

1. What is the size of the int data type in Java?

a) 2 bytes
b) 4 bytes
c) 8 bytes
d) Depends on the system architecture
Correct Answer: b) 4 bytes
Explanation:
In Java, the int data type is always 4 bytes (32 bits).

2. Which of the following is not a feature of Java?

a) Object-Oriented
b) Platform Independent
c) Use of Pointers
d) Multithreaded
Correct Answer: c) Use of Pointers
Explanation:
Java does not support explicit pointers to ensure security and robustness.

3. What does JVM stand for?

a) Java Virtual Machine
b) Java Variable Machine
c) Java Virtual Memory
d) Java Verified Machine
Correct Answer: a) Java Virtual Machine
Explanation:
JVM stands for Java Virtual Machine, which executes Java bytecode.

4. What is the default value of a boolean variable in Java?

a) true
b) false
c) null
d) 0
Correct Answer: b) false
Explanation:
The default value of a boolean instance variable is false.

5. Which class is the superclass of all classes in Java?

a) java.lang.Class
b) java.lang.Object
c) java.util.Super
d) java.lang.System
Correct Answer: b) java.lang.Object
Explanation:
The Object class is the root of the class hierarchy in Java.

6. Does Java support multiple inheritance using classes?

a) Yes always
b) No never
c) Yes with abstract classes
d) Yes with concrete classes
Correct Answer: b) No never
Explanation:
Java does not support multiple inheritance with classes to avoid the diamond problem.

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

a) static
b) abstract
c) final
d) const
Correct Answer: c) final
Explanation:
The final keyword prevents a class from being subclassed.

8. What is a static block in Java?

a) Block to initialize instance variables
b) Block to initialize static variables
c) Block executed after constructor
d) Block that cannot run
Correct Answer: b) Block to initialize static variables
Explanation:
A static block executes once when the class is loaded, primarily to initialize static variables.

9. What is the return type of a constructor?

a) void
b) Object
c) None
d) Class name
Correct Answer: c) None
Explanation:
Constructors do not have a return type, not even void.

10. What does the this keyword refer to in Java?

a) The parent class
b) The current object instance
c) The static class context
d) A new object created in memory
Correct Answer: b) The current object instance
Explanation:
The this keyword refers to the current object in a method or constructor.

11. What is the primary use of the super keyword?

a) To refer to the child class object
b) To refer to the immediate parent class object
c) To call static methods
d) To break a loop
Correct Answer: b) To refer to the immediate parent class object
Explanation:
The super keyword is used to access methods, constructors, and variables of the parent class.

12. What is Method Overriding in Java?

a) Same name but different parameters in same class
b) Same name and parameters in parent and child class
c) Same name but different return types
d) Different names in same class
Correct Answer: b) Same name and parameters in parent and child class
Explanation:
Overriding happens when a subclass provides a specific implementation for a method defined in its superclass.

13. What is Method Overloading?

a) Same name but different parameters in same class
b) Same name and parameters in parent and child
c) Throwing different exceptions
d) Changing the access modifier
Correct Answer: a) Same name but different parameters in same class
Explanation:
Overloading occurs when multiple methods in the same class have the same name but different parameters.

14. Can we override a final method in Java?

a) Yes
b) No
c) Yes if we use super
d) Yes if child is abstract
Correct Answer: b) No
Explanation:
Final methods cannot be overridden by subclasses.

15. What happens if a variable is declared as final?

a) It can be modified only once
b) Its value cannot be changed once initialized
c) It is automatically initialized to zero
d) It can only be accessed statically
Correct Answer: b) Its value cannot be changed once initialized
Explanation:
A final variable acts as a constant and cannot be reassigned.

16. Which keyword is used to skip serialization of a field?

a) volatile
b) transient
c) static
d) final
Correct Answer: b) transient
Explanation:
The transient keyword tells the JVM to ignore the field during serialization.

17. Which keyword guarantees visibility of changes to variables across threads?

a) volatile
b) synchronized
c) transient
d) strictfp
Correct Answer: a) volatile
Explanation:
The volatile keyword ensures that changes to a variable are always visible to other threads.

18. What is a synchronized method in Java?

a) Runs in multiple threads simultaneously
b) Can be accessed by only one thread at a time
c) Used to synchronize databases
d) Cannot throw exceptions
Correct Answer: b) Can be accessed by only one thread at a time
Explanation:
Synchronized methods prevent thread interference by enforcing mutual exclusion.

19. What is the correct signature of the main method in Java?

a) public void main(String args[])
b) public static void main(String[] args)
c) static void main(String args[])
d) public static int main(String[] args)
Correct Answer: b) public static void main(String[] args)
Explanation:
The JVM looks for exactly public static void main(String[] args) to start the program.

20. Which access modifier provides the widest accessibility?

a) private
b) default
c) protected
d) public
Correct Answer: d) public
Explanation:
Public members are accessible from anywhere in the application.

21. If no access modifier is specified, what is the default accessibility?

a) public
b) private
c) protected
d) package-private
Correct Answer: d) package-private
Explanation:
No modifier means package-private, which limits access to classes within the same package.

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

a) exit
b) continue
c) break
d) return
Correct Answer: c) break
Explanation:
The break statement exits the current loop entirely.

23. What is the role of the continue statement?

a) Stops the program execution
b) Skips the current iteration and proceeds to the next
c) Exits the loop entirely
d) Pauses the thread
Correct Answer: b) Skips the current iteration and proceeds to the next
Explanation:
The continue statement bypasses the remaining code in the current iteration and moves to the next loop evaluation.

24. Which data type is NOT supported by a switch statement in Java 8?

a) int
b) String
c) enum
d) boolean
Correct Answer: d) boolean
Explanation:
Switch statements do not support boolean values.

25. What is the difference between while and do-while loops?

a) while executes at least once
b) do-while executes at least once
c) There is no difference
d) while is for infinite loops
Correct Answer: b) do-while executes at least once
Explanation:
A do-while loop evaluates its condition after execution, ensuring it runs at least one time.

26. What is the instanceof operator used for?

a) To create a new instance
b) To check whether an object belongs to a specific class
c) To copy an instance
d) To delete an instance
Correct Answer: b) To check whether an object belongs to a specific class
Explanation:
The instanceof operator tests whether an object is an instance of a specified type.

27. What is the difference between == and equals in Java?

a) == compares references, equals compares content
b) == compares content, equals compares references
c) Both compare references
d) Both compare content
Correct Answer: a) == compares references, equals compares content
Explanation:
The == operator checks memory addresses, while equals() evaluates logical equivalence.

28. Which of these classes is thread-safe?

a) StringBuilder
b) StringBuffer
c) String
d) None of the above
Correct Answer: b) StringBuffer
Explanation:
StringBuffer is synchronized and thread-safe, unlike StringBuilder.

29. What happens when you modify a String object?

a) Original String changes
b) A new String object is created
c) Reference becomes null
d) Throws exception
Correct Answer: b) A new String object is created
Explanation:
Strings are immutable in Java; modifying them creates a new object.

30. What is the String Pool in Java?

a) Database for strings
b) Storage area in heap memory for unique string literals
c) Thread pool
d) Collection of StringBuilders
Correct Answer: b) Storage area in heap memory for unique string literals
Explanation:
The String Pool optimizes memory by storing one copy of each unique string literal.

31. When does a NullPointerException occur?

a) Accessing array out of bounds
b) Dividing by zero
c) Calling a method on a null reference
d) Parsing invalid string
Correct Answer: c) Calling a method on a null reference
Explanation:
A NullPointerException is thrown when attempting to dereference a null object.

32. What is the root class of the Java Exception hierarchy?

a) Exception
b) Error
c) Throwable
d) RuntimeException
Correct Answer: c) Throwable
Explanation:
Throwable is the superclass of all errors and exceptions in Java.

33. What is the main difference between an Error and an Exception?

a) Errors can be caught, Exceptions cannot
b) Exceptions are severe, Errors are mild
c) Errors represent severe JVM problems, Exceptions can be handled
d) There is no difference
Correct Answer: c) Errors represent severe JVM problems, Exceptions can be handled
Explanation:
Errors like OutOfMemoryError are unrecoverable, while Exceptions are designed to be caught and handled.

34. Which keyword is used to explicitly throw an exception?

a) throws
b) throw
c) catch
d) try
Correct Answer: b) throw
Explanation:
The throw keyword is used to throw an exception explicitly from within a method.

35. What is the purpose of the throws keyword?

a) To throw an exception explicitly
b) To catch exceptions
c) To declare exceptions a method might throw
d) To ignore errors
Correct Answer: c) To declare exceptions a method might throw
Explanation:
The throws clause in a method signature indicates that the method may throw specific checked exceptions.

36. What is the purpose of the finally block?

a) To catch exceptions
b) To execute only if an error occurs
c) To execute essential cleanup code regardless of exceptions
d) To return values
Correct Answer: c) To execute essential cleanup code regardless of exceptions
Explanation:
The finally block always executes, making it ideal for closing resources like file streams.

37. Can a try block exist without a catch block?

a) No, catch is mandatory
b) Yes, if followed by a finally block
c) Yes, it can stand alone
d) Yes, if it throws an Error
Correct Answer: b) Yes, if followed by a finally block
Explanation:
A try block must be paired with either a catch block or a finally block.

38. Which of these is a Checked Exception?

a) NullPointerException
b) ArithmeticException
c) IOException
d) ArrayIndexOutOfBoundsException
Correct Answer: c) IOException
Explanation:
IOException is checked by the compiler. The others are unchecked runtime exceptions.

39. What exception is thrown when an array is accessed with an invalid index?

a) IndexOutOfBoundsException
b) ArrayIndexOutOfBoundsException
c) InvalidIndexException
d) ArrayException
Correct Answer: b) ArrayIndexOutOfBoundsException
Explanation:
ArrayIndexOutOfBoundsException is specifically thrown for illegal array access.

40. What exception is thrown by Integer.parseInt(abc)?

a) ClassCastException
b) NumberFormatException
c) IllegalArgumentException
d) ParseException
Correct Answer: b) NumberFormatException
Explanation:
NumberFormatException is thrown when a string does not contain a parsable integer.

41. Which interface is the root of the Java Collections hierarchy?

a) List
b) Collection
c) Iterable
d) Map
Correct Answer: c) Iterable
Explanation:
Iterable is the root interface, which is extended by the Collection interface.

42. Does the Map interface extend the Collection interface?

a) Yes
b) No
c) Yes in Java 8
d) Yes indirectly
Correct Answer: b) No
Explanation:
Map operates on key-value pairs and has a separate hierarchy from Collection.

43. What is the main difference between List and Set?

a) Set allows duplicates, List does not
b) List allows duplicates, Set does not
c) List is synchronized
d) Set maintains insertion order
Correct Answer: b) List allows duplicates, Set does not
Explanation:
A List is an ordered collection that permits duplicate elements, while a Set does not.

44. What is the difference between ArrayList and LinkedList?

a) ArrayList uses a doubly linked list
b) ArrayList is faster for insertion
c) ArrayList uses a dynamic array, LinkedList uses a doubly linked list
d) ArrayList does not allow nulls
Correct Answer: c) ArrayList uses a dynamic array, LinkedList uses a doubly linked list
Explanation:
ArrayList is backed by an array making random access fast, while LinkedList uses nodes making insertions faster.

45. Which of these collections is synchronized by default?

a) ArrayList
b) Vector
c) LinkedList
d) HashSet
Correct Answer: b) Vector
Explanation:
Vector is a legacy class and its methods are synchronized by default.

46. Which Set implementation stores elements in a sorted order?

a) HashSet
b) LinkedHashSet
c) TreeSet
d) SortedSet
Correct Answer: c) TreeSet
Explanation:
TreeSet stores elements in their natural ordering or according to a provided Comparator.

47. What is the key difference between HashMap and Hashtable?

a) HashMap is synchronized
b) Hashtable allows null keys
c) HashMap allows one null key, Hashtable allows none
d) Hashtable maintains insertion order
Correct Answer: c) HashMap allows one null key, Hashtable allows none
Explanation:
Hashtable is synchronized and strictly prohibits null keys or values, while HashMap permits one null key and multiple null values.

48. Which class provides thread-safe operations on maps without locking the entire map?

a) Hashtable
b) SynchronizedMap
c) ConcurrentHashMap
d) ThreadSafeMap
Correct Answer: c) ConcurrentHashMap
Explanation:
ConcurrentHashMap uses lock striping, allowing concurrent reads and writes without locking the whole map.

49. What is the difference between Iterator and ListIterator?

a) Iterator traverses backward
b) ListIterator traverses in both directions
c) Iterator is for Sets only
d) There is no difference
Correct Answer: b) ListIterator traverses in both directions
Explanation:
ListIterator extends Iterator and provides bi-directional traversal for Lists.

50. What is a fail-fast iterator?

a) Ignores modifications
b) Throws ConcurrentModificationException on structural modification
c) Optimized for speed
d) Fails on null values
Correct Answer: b) Throws ConcurrentModificationException on structural modification
Explanation:
Fail-fast iterators instantly throw an exception if the underlying collection is modified during iteration.

51. Which interface should be implemented for natural sorting order?

a) Comparator
b) Sortable
c) Comparable
d) Orderable
Correct Answer: c) Comparable
Explanation:
The Comparable interface provides the compareTo method to define the natural sorting behavior of objects.

52. Which interface is used to provide custom sorting logic?

a) Comparable
b) Comparator
c) Sortable
d) CustomSort
Correct Answer: b) Comparator
Explanation:
The Comparator interface provides the compare method to implement custom, external sorting logic.

53. Which method must be implemented by a class implementing Runnable?

a) start()
b) execute()
c) run()
d) main()
Correct Answer: c) run()
Explanation:
The run() method defines the entry point and execution logic for a thread.

54. What is the correct way to start a new thread using the Thread class?

a) thread.run()
b) thread.start()
c) thread.init()
d) thread.execute()
Correct Answer: b) thread.start()
Explanation:
Calling start() instructs the JVM to allocate a new call stack and invoke the run() method asynchronously.

55. What does Thread.sleep() do?

a) Terminates the thread
b) Pauses execution for a specified time without releasing locks
c) Releases locks
d) Wakes a thread
Correct Answer: b) Pauses execution for a specified time without releasing locks
Explanation:
sleep() pauses the current thread but retains any synchronized locks it holds.

56. What is the difference between wait() and sleep()?

a) wait() is a Thread method
b) wait() releases the lock, sleep() does not
c) sleep() releases the lock
d) They are exactly the same
Correct Answer: b) wait() releases the lock, sleep() does not
Explanation:
wait() belongs to the Object class and releases the monitor lock, whereas sleep() does not release locks.

57. What does the thread.join() method do?

a) Joins strings
b) Merges threads
c) Causes current thread to wait until the joined thread completes
d) Wakes the thread
Correct Answer: c) Causes current thread to wait until the joined thread completes
Explanation:
join() halts the current thread's execution until the thread on which join() was called has finished.

58. What is a daemon thread in Java?

a) A high-priority thread
b) A database thread
c) A background thread that provides services to user threads
d) A malicious thread
Correct Answer: c) A background thread that provides services to user threads
Explanation:
Daemon threads run in the background (like Garbage Collector) and do not prevent the JVM from exiting.

59. What is the default priority of a thread in Java?

a) 1
b) 5
c) 10
d) 0
Correct Answer: b) 5
Explanation:
Thread priority ranges from 1 to 10. The default priority is 5 (NORM_PRIORITY).

60. What is a deadlock in multithreading?

a) A thread throws an exception
b) Two or more threads are blocked forever waiting on each other
c) Infinite loop
d) Out of memory
Correct Answer: b) Two or more threads are blocked forever waiting on each other
Explanation:
Deadlock occurs when multiple threads wait indefinitely for locks held by one another.

61. Which method notifies a thread waiting on an object's monitor?

a) wakeUp()
b) resume()
c) notify()
d) start()
Correct Answer: c) notify()
Explanation:
The notify() method wakes up a single thread waiting on the object's monitor.

62. What is Garbage Collection in Java?

a) Deleting temp files
b) Automatic reclamation of memory by destroying unreferenced objects
c) Code cleanup
d) Killing threads
Correct Answer: b) Automatic reclamation of memory by destroying unreferenced objects
Explanation:
Java automatically manages memory by reclaiming space from objects that are no longer reachable.

63. How can you request the JVM to run the Garbage Collector?

a) System.gc()
b) Object.collect()
c) JVM.clean()
d) Memory.free()
Correct Answer: a) System.gc()
Explanation:
System.gc() or Runtime.getRuntime().gc() suggests that the JVM perform garbage collection.

64. Which method is called by the Garbage Collector before destroying an object?

a) destroy()
b) clean()
c) finalize()
d) exit()
Correct Answer: c) finalize()
Explanation:
The finalize() method is invoked by the garbage collector before the object's memory is reclaimed.

65. What is the purpose of Wrapper classes?

a) Package management
b) To convert primitive data types into objects
c) Data encryption
d) GUI wrapping
Correct Answer: b) To convert primitive data types into objects
Explanation:
Wrapper classes encapsulate primitives so they can be used in collections that require objects.

66. What is Autoboxing in Java?

a) Object to primitive
b) Automatic conversion of primitives to wrapper objects
c) GUI generation
d) Casting superclass to subclass
Correct Answer: b) Automatic conversion of primitives to wrapper objects
Explanation:
Autoboxing is the compiler automatically converting a primitive type into its corresponding wrapper class.

67. What is Unboxing in Java?

a) Extracting JAR files
b) Converting wrapper object to primitive value
c) String to integer
d) Removing from collection
Correct Answer: b) Converting wrapper object to primitive value
Explanation:
Unboxing is the reverse of autoboxing, converting a wrapper object back to a primitive type.

68. Can Enums implement interfaces in Java?

a) Yes
b) No
c) Only empty interfaces
d) Only abstract enums
Correct Answer: a) Yes
Explanation:
Enums can implement interfaces, but they cannot extend classes because they implicitly extend java.lang.Enum.

69. What is an Annotation in Java?

a) A comment type
b) Metadata about a class, method, or field
c) GUI framework
d) Alternative to switch
Correct Answer: b) Metadata about a class, method, or field
Explanation:
Annotations provide metadata for the compiler or runtime environments, such as @Override.

70. What is the primary benefit of Generics?

a) Execution speed
b) Compile-time type safety and eliminating casts
c) Memory reduction
d) Multiple inheritance
Correct Answer: b) Compile-time type safety and eliminating casts
Explanation:
Generics provide strong type checking at compile time and eliminate the need for manual type casting.

71. What is Type Erasure in Generics?

a) Deleting unreferenced objects
b) Replacing generic types with bounds or Object at compile time
c) Casting to Object
d) Compiler bug
Correct Answer: b) Replacing generic types with bounds or Object at compile time
Explanation:
Type erasure removes generic type parameters during compilation to maintain backward compatibility with older Java versions.

72. What does the wildcard ? represent in Generics?

a) Unknown type
b) Primitive type
c) Object class
d) Syntax error
Correct Answer: a) Unknown type
Explanation:
The question mark (?) represents an unknown type parameter in generics.

73. What is Serialization in Java?

a) Converting object state to byte stream
b) Byte stream to object
c) Assigning serial numbers
d) Sorting objects
Correct Answer: a) Converting object state to byte stream
Explanation:
Serialization converts an object into a byte stream for storage or transmission.

74. Which interface enables serialization?

a) java.io.Serializable
b) java.lang.Cloneable
c) java.util.Serialize
d) java.io.Externalizable
Correct Answer: a) java.io.Serializable
Explanation:
The Serializable interface acts as a marker to indicate that a class can be serialized.

75. What is the purpose of serialVersionUID?

a) Database primary key
b) Identify a thread
c) Verify sender and receiver class compatibility during deserialization
d) Limit file size
Correct Answer: c) Verify sender and receiver class compatibility during deserialization
Explanation:
It ensures that the serialized object matches the loaded class structure during deserialization.

76. What is the difference between Externalizable and Serializable?

a) They are exactly the same
b) Serializable requires methods, Externalizable is marker
c) Externalizable is marker, Serializable provides control
d) Externalizable provides custom control over serialization
Correct Answer: d) Externalizable provides custom control over serialization
Explanation:
Externalizable requires implementing readExternal and writeExternal methods to manually control serialization.

77. Which stream is used to read character data from a file?

a) FileInputStream
b) FileReader
c) BufferedInputStream
d) DataInputStream
Correct Answer: b) FileReader
Explanation:
FileReader reads streams of characters, whereas FileInputStream reads raw bytes.

78. Why is BufferedReader preferred over FileReader for text?

a) Can read images
b) Buffers data for efficiency and provides readLine()
c) FileReader is Windows only
d) FileReader is deprecated
Correct Answer: b) Buffers data for efficiency and provides readLine()
Explanation:
BufferedReader reads chunks into memory, speeding up reading and allowing line-by-line reads.

79. Which class represents a file or directory path?

a) java.io.Directory
b) java.io.PathName
c) java.io.File
d) java.util.File
Correct Answer: c) java.io.File
Explanation:
The File class is an abstract representation of file and directory pathnames.

80. Which class parses primitive types and strings using regex?

a) StringTokenizer
b) RegexParser
c) Scanner
d) Pattern
Correct Answer: c) Scanner
Explanation:
The Scanner class parses input into primitive types and strings using delimiters.

81. How do you find the length of an array in Java?

a) array.length()
b) array.size()
c) array.length
d) array.size
Correct Answer: c) array.length
Explanation:
For arrays, length is a final property, not a method, so no parentheses are used.

82. Which represents a 2D array declaration?

a) int[] array[]
b) int array[][]
c) int[][] array
d) All of the above
Correct Answer: d) All of the above
Explanation:
All listed syntaxes are syntactically valid for 2D arrays in Java.

83. What is Cloning in Java?

a) Creating an exact copy of an object
b) Creating a new class
c) Copying references only
d) Deleting duplicates
Correct Answer: a) Creating an exact copy of an object
Explanation:
Cloning creates a new instance with the same state as the original object.

84. Which exception is thrown if clone() is called without implementing Cloneable?

a) CloneNotSupportedException
b) NotCloneableException
c) IllegalCloneException
d) RuntimeException
Correct Answer: a) CloneNotSupportedException
Explanation:
The Object clone() method throws this exception if the object's class does not implement the Cloneable interface.

85. What is a Shallow Copy?

a) Copies new instances for all objects
b) Copies primitives but object references point to the same memory
c) Copies only public fields
d) Network copy
Correct Answer: b) Copies primitives but object references point to the same memory
Explanation:
In a shallow copy, nested objects are not duplicated; their references are shared.

86. What is the Reflection API used for?

a) Console output reflection
b) Examine or modify runtime behavior of classes in JVM
c) Server mirroring
d) Array duplication
Correct Answer: b) Examine or modify runtime behavior of classes in JVM
Explanation:
Reflection allows dynamic inspection and manipulation of classes, methods, and fields at runtime.

87. What does Class.forName() do?

a) Initializes named object
b) Dynamically loads a class at runtime
c) Renames a class
d) Checks class existence
Correct Answer: b) Dynamically loads a class at runtime
Explanation:
It locates, loads, and links a class dynamically during execution.

88. What is the Singleton pattern?

a) Ensures a class has only one instance and a global point of access
b) Single thread creation
c) Hiding single variables
d) Restricting class to one method
Correct Answer: a) Ensures a class has only one instance and a global point of access
Explanation:
Singleton is a creational pattern that limits object instantiation to a single object.

89. Which of these is a Creational Design Pattern?

a) Observer Pattern
b) Factory Pattern
c) Decorator Pattern
d) Strategy Pattern
Correct Answer: b) Factory Pattern
Explanation:
The Factory Pattern abstracts the object creation process, classifying it as a creational pattern.

90. What does MVC stand for?

a) Model View Controller
b) Main Visual Class
c) Method Variable Constructor
d) Module View Center
Correct Answer: a) Model View Controller
Explanation:
MVC separates application logic into Data (Model), UI (View), and Input logic (Controller).

91. What is the role of the ClassLoader?

a) Execute bytecode
b) Load class files into the JVM at runtime
c) Compile Java source
d) Clean unreferenced objects
Correct Answer: b) Load class files into the JVM at runtime
Explanation:
The ClassLoader is responsible for dynamically finding and loading Java class files into the JVM.

92. What is the JIT compiler?

a) Just In Time compiler, compiles bytecode to native code for performance
b) Internet Translator
c) Converts Java to JS
d) Runs before javac
Correct Answer: a) Just In Time compiler, compiles bytecode to native code for performance
Explanation:
JIT improves performance by compiling bytecode into native machine code at runtime.

93. What is Bytecode in Java?

a) Machine code for Intel
b) Intermediate code generated by Java compiler for the JVM
c) Source code in byte arrays
d) Encrypted code
Correct Answer: b) Intermediate code generated by Java compiler for the JVM
Explanation:
Bytecode is a platform-independent instruction set executed by the Java Virtual Machine.

94. What does WORA stand for?

a) Write Once, Run Anywhere
b) Write Object, Read Array
c) Windows Or Run Apple
d) Wide Object Request Architecture
Correct Answer: a) Write Once, Run Anywhere
Explanation:
WORA emphasizes Java's cross-platform capabilities via bytecode.

95. What is the size of the char data type in Java?

a) 1 byte
b) 2 bytes
c) 4 bytes
d) 8 bytes
Correct Answer: b) 2 bytes
Explanation:
Java uses 16-bit Unicode characters, so a char takes 2 bytes.

96. Which data type is more precise?

a) float
b) double
c) Same precision
d) Depends on OS
Correct Answer: b) double
Explanation:
Double is 64-bit and offers more precision than the 32-bit float.

97. What is casting a larger primitive type to a smaller one called?

a) Widening casting
b) Narrowing casting
c) Autoboxing
d) Type promotion
Correct Answer: b) Narrowing casting
Explanation:
Narrowing requires an explicit cast and may result in loss of precision or data.

98. Can a static nested class access non-static variables of the outer class directly?

a) Yes
b) No
c) Only protected variables
d) Only if extended
Correct Answer: b) No
Explanation:
Static nested classes do not have an implicit reference to an outer class instance.

99. What is an Inner Class?

a) A non-static class defined within another class
b) A nameless class
c) A method class only
d) An interface class
Correct Answer: a) A non-static class defined within another class
Explanation:
Inner classes have access to all members of the outer class, including private ones.

100. What is an Anonymous Inner Class?

a) Class without access modifier
b) Nameless inner class instantiated once
c) Hidden JVM class
d) Class without methods
Correct Answer: b) Nameless inner class instantiated once
Explanation:
It allows declaring and instantiating a class at the same time to make code more concise.

101. What restriction applies to local variables accessed by a local inner class?

a) Must be static
b) Must be public
c) Must be final or effectively final
d) Cannot be accessed
Correct Answer: c) Must be final or effectively final
Explanation:
Local variables must be final or effectively final to be used inside a local inner class.

102. Which keyword defines a namespace for a Java class?

a) import
b) namespace
c) package
d) module
Correct Answer: c) package
Explanation:
The package keyword organizes classes into namespaces, preventing naming conflicts.
← Previous: C++ Variables & Data Types MCQs
Next →: Important Java 8 Features MCQs
No Image

Latest C++ Functions MCQs

1. What is the size of the int data type in Java? a) 2 bytes b) 4 bytes c) 8…

By MCQs Generator
important java 8 features mcqs

Important Java 8 Features MCQs

Released in March 2014, Java 8 represents one of the most significant upgrades in the history of the Java programming…

By MCQs Generator
Java Multithreading & Concurrency MCQs

Java Multithreading & Concurrency MCQs

Java multithreading and concurrency are core pillars for building high-performance, responsive applications that execute multiple tasks simultaneously. Utilizing the java.util.concurrent…

By MCQs Generator