100 Java MCQ (Multiple Choice Questions) with Answers

1) What is the extension of compiled Java class files?
  1. .java
  2. .class
  3. .txt
  4. .js
Show Answer
Answer: b


2) Who developed the Java programming language?

  1. Dennis Ritchie
  2. James Gosling
  3. Bjarne Stroustrup
  4. Guido van Rossum
Show Answer
Answer: b


3) Which component of Java is responsible for executing the bytecode?

  1. JDK
  2. JRE
  3. JVM
  4. JIT
Show Answer
Answer: c


4) What is the entry point method in a Java application?

  1. public void main(String[] args)
  2. public static void main(String[] args)
  3. private static void main(String[] args)
  4. public static int main(String[] args)
Show Answer
Answer: b


5) Which keyword is used to inherit a class in Java?

  1. implements
  2. extends
  3. inherits
  4. super
Show Answer
Answer: b


6) What is the default value of a boolean variable in Java?

  1. true
  2. false
  3. 0
  4. null
Show Answer
Answer: b


7) Which of these is a primitive data type in Java?

  1. String
  2. Array
  3. int
  4. Class
Show Answer
Answer: c


8) Which keyword is used to create an instance of a class?

  1. alloc
  2. new
  3. instance
  4. create
Show Answer
Answer: b


9) What is the size of an int variable in Java?

  1. 16 bits
  2. 32 bits
  3. 64 bits
  4. 8 bits
Show Answer
Answer: b


10) Which keyword is used to make a variable’s value unchangeable?

  1. static
  2. abstract
  3. final
  4. const
Show Answer
Answer: c


11) Which method is used to find the length of a String in Java?

  1. length()
  2. len()
  3. size()
  4. getLength()
Show Answer
Answer: a


12) How do you declare an array in Java?

  1. int arr[]
  2. int[] arr
  3. int arr = new int[5]
  4. Both a and b
Show Answer
Answer: d


13) Which keyword is used to handle exceptions in Java?

  1. catch
  2. throw
  3. try
  4. All of the above
Show Answer
Answer: d


14) What package is automatically imported into every Java program?

  1. java.util
  2. java.io
  3. java.lang
  4. java.net
Show Answer
Answer: c


15) Which of the following is NOT a Java access modifier?

  1. protected
  2. private
  3. internal
  4. public
Show Answer
Answer: c


16) What is the default value of an object reference variable in Java?

  1. 0
  2. false
  3. null
  4. undefined
Show Answer
Answer: c


17) Which keyword is used to access members of the parent class?

  1. this
  2. super
  3. parent
  4. base
Show Answer
Answer: b


18) What exception is thrown when an array is accessed with an invalid index?

  1. IndexOutOfBoundsException
  2. ArrayIndexOutOfBoundsException
  3. NullPointerException
  4. IllegalArgumentException
Show Answer
Answer: b


19) Which operator is used to compare two primitives for equality in Java?

  1. =
  2. ==
  3. equals()
  4. ===
Show Answer
Answer: b


20) What is the superclass of all classes in Java?

  1. System
  2. Object
  3. Class
  4. Root
Show Answer
Answer: b


21) Which keyword is used to declare an interface in Java?

  1. interface
  2. implements
  3. abstract class
  4. contract
Show Answer
Answer: a


22) Can a class implement multiple interfaces in Java?

  1. Yes
  2. No
  3. Only if they are abstract
  4. Only up to two
Show Answer
Answer: a


23) Which method in the Object class is used to check for structural equality?

  1. sameAs()
  2. equals()
  3. compareTo()
  4. isEqual()
Show Answer
Answer: b


24) Which loop is guaranteed to execute at least once?

  1. for
  2. while
  3. do-while
  4. foreach
Show Answer
Answer: c


25) What is the result of 10 / 4 using integer division in Java?

  1. 2.5
  2. 2
  3. 3
  4. 2.0
Show Answer
Answer: b


26) Which of the following classes is immutable in Java?

  1. StringBuilder
  2. StringBuffer
  3. String
  4. ArrayList
Show Answer
Answer: c


27) What keyword is used to define a constant method that cannot be overridden?

  1. static
  2. final
  3. abstract
  4. strictfp
Show Answer
Answer: b


28) Which keyword is used to invoke a constructor of the current class?

  1. this()
  2. super()
  3. new()
  4. self()
Show Answer
Answer: a


29) What is the size of a char data type in Java?

  1. 8 bits
  2. 16 bits
  3. 32 bits
  4. 64 bits
Show Answer
Answer: b


30) Which memory area in JVM stores class structures like runtime constant pool, field, and method data?

  1. Stack
  2. Heap
  3. Method Area
  4. Native Method Stack
Show Answer
Answer: c


31) Which exception occurs when dividing an integer by zero in Java?

  1. ArithmeticException
  2. NullPointerException
  3. NumberFormatException
  4. IllegalArgumentException
Show Answer
Answer: a


32) What keyword is used to define a class that cannot be instantiated?

  1. interface
  2. abstract
  3. static
  4. final
Show Answer
Answer: b


33) Which collection class allows duplicate elements in Java?

  1. HashSet
  2. TreeSet
  3. ArrayList
  4. HashMap
Show Answer
Answer: c


34) Which collection does NOT allow duplicate keys?

  1. List
  2. Set
  3. Map
  4. Vector
Show Answer
Answer: c


35) Which keyword is used to prevent serializing a field in Java?

  1. transient
  2. volatile
  3. static
  4. final
Show Answer
Answer: a


36) Which thread method is used to start execution of a thread?

  1. run()
  2. execute()
  3. start()
  4. init()
Show Answer
Answer: c


37) Which interface must be implemented to create a thread by passing it to the Thread class?

  1. Callable
  2. Runnable
  3. Process
  4. Executable
Show Answer
Answer: b


38) What is the default initial capacity of an ArrayList in Java?

  1. 5
  2. 10
  3. 16
  4. 20
Show Answer
Answer: b


39) What is the output of “Hello”.charAt(1)?

  1. H
  2. e
  3. l
  4. o
Show Answer
Answer: b


40) Which statement is used to exit from a loop prematurely?

  1. continue
  2. return
  3. break
  4. exit
Show Answer
Answer: c


41) Which statement skips the current iteration of a loop?

  1. break
  2. continue
  3. skip
  4. pass
Show Answer
Answer: b


42) Which class is synchronized and thread-safe?

  1. ArrayList
  2. HashMap
  3. Vector
  4. StringBuilder
Show Answer
Answer: c


43) Which class is preferred for mutable strings in a non-multithreaded environment?

  1. String
  2. StringBuffer
  3. StringBuilder
  4. StringTokenizer
Show Answer
Answer: c


44) Which keyword is used to declare a variable that may be modified by different threads asynchronously?

  1. transient
  2. volatile
  3. synchronized
  4. atomic
Show Answer
Answer: b


45) Which block is guaranteed to execute regardless of whether an exception occurs or not?

  1. try
  2. catch
  3. finally
  4. final
Show Answer
Answer: c


46) What does garbage collection in Java do?

  1. Cleans up unreferenced objects on the heap
  2. Frees stack memory
  3. Deletes unused class files
  4. Optimizes network calls
Show Answer
Answer: a


47) What method of Garbage Collector can be suggested to run manually?

  1. Runtime.getRuntime().gc()
  2. System.gc()
  3. Both a and b
  4. None of the above
Show Answer
Answer: c


48) What is method overriding?

  1. Defining multiple methods in the same class with the same name but different parameters
  2. Redefining a parent class method in a child class with the same signature
  3. Hiding a class member
  4. Converting one data type to another
Show Answer
Answer: b


49) What is method overloading?

  1. Methods with the same name and parameter list in different classes
  2. Methods with the same name but different parameter list in the same class
  3. Overriding a static method
  4. Calling a method recursively
Show Answer
Answer: b


50) Can static methods be overridden in Java?

  1. Yes
  2. No
  3. Only in abstract classes
  4. Only in interfaces
Show Answer
Answer: b


51) What is the return type of a constructor in Java?

  1. void
  2. int
  3. Object
  4. No return type, not even void
Show Answer
Answer: d


52) Which keyword is used to throw an explicit exception?

  1. throws
  2. throw
  3. try
  4. catch
Show Answer
Answer: b


53) Which keyword is used in a method signature to declare exceptions that the method might throw?

  1. throw
  2. throws
  3. exception
  4. raises
Show Answer
Answer: b


54) What type of exception is NullPointerException?

  1. Checked Exception
  2. Unchecked Exception (RuntimeException)
  3. Compile-time Error
  4. Fatal Error
Show Answer
Answer: b


55) Which exception is a Checked Exception in Java?

  1. IOException
  2. ArithmeticException
  3. IndexOutOfBoundsException
  4. ClassCastException
Show Answer
Answer: a


56) What does the static keyword mean when applied to a field?

  1. The field is constant
  2. The field belongs to the class, not instances
  3. The field is hidden
  4. The field cannot be accessed outside the package
Show Answer
Answer: b


57) Which wrapper class corresponds to the float primitive type?

  1. Float
  2. Floating
  3. Double
  4. Real
Show Answer
Answer: a


58) What is autoboxing in Java?

  1. Converting a primitive type to its corresponding wrapper class automatically
  2. Converting a wrapper class to its primitive type
  3. Casting an object to a subclass
  4. Converting a string to an integer
Show Answer
Answer: a


59) What is unboxing in Java?

  1. Converting a primitive to a wrapper
  2. Converting a wrapper object to its corresponding primitive type
  3. Unpacking a ZIP file
  4. Parsing a String
Show Answer
Answer: b


60) Which operator is used to instantiate an object or array?

  1. alloc
  2. new
  3. malloc
  4. create
Show Answer
Answer: b


61) What is the default value of a double variable in Java?

  1. 0
  2. 0.0
  3. 0.0d
  4. Both b and c
Show Answer
Answer: d


62) Which method converts a string representation of a number to an integer?

  1. Integer.parseInt()
  2. Integer.valueOf()
  3. int.parse()
  4. Both a and b
Show Answer
Answer: d


63) Which collection class maintains insertion order?

  1. HashSet
  2. LinkedHashSet
  3. TreeSet
  4. HashMap
Show Answer
Answer: b


64) Which map class maintains keys in sorted order?

  1. HashMap
  2. LinkedHashMap
  3. TreeMap
  4. Hashtable
Show Answer
Answer: c


65) What is the scope of a private member of a class?

  1. Within the package
  2. Within the class only
  3. Within subclasses only
  4. Everywhere
Show Answer
Answer: b


66) What is the default access modifier if none is specified?

  1. public
  2. private
  3. protected
  4. package-private (default)
Show Answer
Answer: d


67) Which feature was introduced in Java 8 to support functional programming?

  1. Generics
  2. Lambda Expressions
  3. Annotations
  4. Enums
Show Answer
Answer: b


68) What interface represents a function that takes one argument and produces a result in Java 8?

  1. Supplier
  2. Consumer
  3. Function
  4. Predicate
Show Answer
Answer: c


69) Which functional interface returns a boolean value given an argument?

  1. Function
  2. Consumer
  3. Predicate
  4. Supplier
Show Answer
Answer: c


70) Which functional interface takes no arguments and returns a result?

  1. Supplier
  2. Consumer
  3. Runnable
  4. Function
Show Answer
Answer: a


71) What does the Stream.filter() method do in Java Streams API?

  1. Transforms elements
  2. Filters elements based on a Predicate
  3. Reduces elements to a single value
  4. Sorts elements
Show Answer
Answer: b


72) Which of the following is a terminal operation in Java Streams?

  1. map()
  2. filter()
  3. collect()
  4. sorted()
Show Answer
Answer: c


73) What is the purpose of Optional class introduced in Java 8?

  1. To optimize performance
  2. To avoid NullPointerExceptions
  3. To represent threads
  4. To handle file operations
Show Answer
Answer: b


74) How can you define a default implementation for a method inside an interface (Java 8+)?

  1. Using the default keyword
  2. Using the static keyword
  3. Interfaces cannot have implementation
  4. Both a and b
Show Answer
Answer: d


75) What is the size of byte primitive type in Java?

  1. 1 byte (8 bits)
  2. 2 bytes (16 bits)
  3. 4 bytes (32 bits)
  4. 8 bytes (64 bits)
Show Answer
Answer: a


76) What is the range of a byte data type in Java?

  1. -128 to 127
  2. -32768 to 32767
  3. 0 to 255
  4. -2147483648 to 2147483647
Show Answer
Answer: a


77) What happens if you try to compile a Java class without a main method?

  1. Compile-time error
  2. It compiles fine, but fails at runtime when executed as a standalone app
  3. Syntax error
  4. Warning
Show Answer
Answer: b


78) Which statement about static initializer blocks is true?

  1. They execute when an object is instantiated
  2. They execute when the class is loaded by the JVM
  3. They execute after the main method completes
  4. They require manual invocation
Show Answer
Answer: b


79) What is marker interface in Java?

  1. An interface with exactly one abstract method
  2. An interface with no methods or fields
  3. An interface with default methods
  4. An interface marked as final
Show Answer
Answer: b


80) Which of the following is a built-in marker interface in Java?

  1. Serializable
  2. Cloneable
  3. Remote
  4. All of the above
Show Answer
Answer: d


81) What is the default capacity multiplier of an ArrayList when it grows?

  1. Doubles (2x)
  2. 1.5 times
  3. 3 times
  4. Fixed size increase of 10
Show Answer
Answer: b


82) Which of the following allows null keys in Java?

  1. Hashtable
  2. ConcurrentHashMap
  3. HashMap
  4. TreeMap
Show Answer
Answer: c


83) What is the result of “Java”.concat(“SE”)?

  1. Java SE
  2. JavaSE
  3. SEJava
  4. Error
Show Answer
Answer: b


84) What is the output of Math.abs(-5.5)?

  1. -5.5
  2. 5.5
  3. 5
  4. -5
Show Answer
Answer: b


85) Which class can be used to read input from the console easily?

  1. ConsoleReader
  2. Scanner
  3. InputReader
  4. SystemReader
Show Answer
Answer: b


86) Which keyword is used to access package members from another package?

  1. export
  2. import
  3. package
  4. include
Show Answer
Answer: b


87) Can an abstract class have constructors in Java?

  1. Yes
  2. No
  3. Only private constructors
  4. Only static constructors
Show Answer
Answer: a


88) Can an interface have instance fields in Java?

  1. Yes
  2. No, fields are implicitly public, static, and final
  3. Yes, if marked protected
  4. Yes, using default keyword
Show Answer
Answer: b


89) What is the difference between == and .equals() when comparing String objects?

  1. == checks value, .equals() checks memory address
  2. == checks reference memory address, .equals() checks string content
  3. They are identical
  4. == works for primitives, .equals() causes compile error
Show Answer
Answer: b


90) What is String Pool in Java?

  1. A heap memory area storing String literals for memory reuse
  2. A thread pool handling String operations
  3. A stack area for local string variables
  4. A file cache for strings
Show Answer
Answer: a


91) What method is used to manually add a String object to the String Pool?

  1. pool()
  2. intern()
  3. addToPool()
  4. save()
Show Answer
Answer: b


92) What is the bitwise XOR operator in Java?

  1. &
  2. |
  3. ^
  4. ~
Show Answer
Answer: c


93) What is the output of 5 & 3 in Java?

  1. 1
  2. 7
  3. 0
  4. 15
Show Answer
Answer: a


94) Which right shift operator preserves the sign bit (arithmetic shift)?

  1. >>
  2. >>>
  3. <<
  4. <<<
Show Answer
Answer: a


95) What is the unsigned right shift operator in Java?

  1. >>
  2. >>>
  3. <<
  4. <<<
Show Answer
Answer: b


96) Which annotation indicates that a method is intended to override a method in a superclass?

  1. @Override
  2. @Overload
  3. @Super
  4. @Inherited
Show Answer
Answer: a


97) What annotation suppresses compiler warnings?

  1. @Deprecated
  2. @SuppressWarnings
  3. @SafeVarargs
  4. @Ignore
Show Answer
Answer: b


98) Which class is used to measure elapsed time with nanosecond precision?

  1. System.currentTimeMillis()
  2. System.nanoTime()
  3. Date.getTime()
  4. Instant.now()
Show Answer
Answer: b


99) Which enum method returns an array of all defined enum constants?

  1. getValues()
  2. values()
  3. toArray()
  4. list()
Show Answer
Answer: b


100) What is the result of 1.0 / 0.0 in Java floating-point arithmetic?

  1. Throws ArithmeticException
  2. Double.NaN
  3. Double.POSITIVE_INFINITY
  4. 0.0
Show Answer
Answer: c

 

MCQ Tutorial
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment