Java Online Test - MCQ Questions and Answers

This Java Online Test simulates a real online certification exam. You will be presented with Multiple-Choice Questions (MCQs) based on Core Java Concepts, where you will be given four options. You will select the most suitable answer for each question and then proceed to the next question without wasting time. You will get your online test score after finishing the complete test.

Java remains one of the most popular programming languages worldwide, used in everything from mobile applications to large enterprise environments. Understanding its core concepts, syntax, and libraries is crucial for effective coding and professional growth.

In this post, we offer a variety of MCQs that cover fundamental and advanced topics in Java. These include the basics of the language, object-oriented programming principles, the Java Collections Framework, exception handling, file handling, JDBC, and multithreading. Each question is followed by a set of possible answers, only one of which is correct, challenging you to think and apply your Java knowledge practically.

Whether preparing for job interviews, certification exams, or just looking to test your Java programming skills, these questions will provide a comprehensive overview and a real challenge to your understanding. Happy coding, and good luck!

1. What does JDK stand for?

a) Java Development Kit
b) Java Deployment Kit
c) Java Directory Kit
d) Java Data Kit

2. What component of Java is responsible for running the compiled Java programs?

a) JDK
b) JRE
c) JVM
d) JAR

3. Which part of Java is specifically used to compile Java programs?

a) Java Compiler
b) Java Runtime Environment
c) Java Virtual Machine
d) Java API

4. What is the Java Runtime Environment (JRE) primarily responsible for?

a) Providing tools for developing Java applications
b) Compiling Java source code
c) Running Java applications
d) None of the above

5. Which of the following is not included in the JDK?

a) Java Runtime Environment
b) Java Development Tools
c) Web browser
d) Java Compiler

6. What is the purpose of the JVM?

a) To provide an environment for Java code to execute
b) To compile Java code into bytecode
c) To provide a graphical interface for Java applications
d) To manage the operating system

7. What does bytecode in Java mean?

a) The code written by a programmer
b) The code that runs on the Java Virtual Machine
c) The code compiled by the Java Compiler
d) Both b and c

8. What feature of Java allows it to run on different platforms without modification?

a) Platform independence
b) Java Virtual Machine
c) Java APIs
d) Both a and b

9. Which tool is used for documenting Java code and creating an API documentation in HTML format?

a) Javadoc
b) Java Compiler
c) Java Debugger
d) Java Profiler

10. What is the command to compile a Java program named 'Main.java'?

a) java Main.java
b) javac Main.java
c) compile Main.java
d) execute Main.java

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

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

12. How do you start a single-line comment in Java?

a) /*
b) //
c) <!--
d) #

13. 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

14. 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

15. 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

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

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

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

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

18. What is the purpose of garbage collection in Java?

a) To delete unused objects
b) To provide data security
c) To speed up the processing
d) To allocate memory

19. What is the return type of the main method in Java?

a) int
b) void
c) boolean
d) String[]

20. What is encapsulation in Java?

a) The process of wrapping code and data together into a single unit
b) A technique to inherit the properties of another class
c) A method to update Java
d) None of the above

21. What is the correct way to instantiate an object of class Car?

a) Car myCar = new Car();
b) new Car myCar = Car();
c) Car myCar = Car.new();
d) new Car() myCar;

22. Which keyword is used to import a package in Java?

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

23. Which method cannot be overridden in Java?

a) static method
b) final method
c) private method
d) All of the above

24. What is the purpose of the this keyword in Java?

a) To refer to the current object
b) To call another constructor in the same class
c) Both a and b
d) None of the above

25. How do you create a multiline comment in Java?

a) // This is a comment
b) <!-- This is a comment -->
c) /* This is a comment */
d) # This is a comment

26. What is the default constructor?

a) A constructor provided by Java if no constructors are defined in the class
b) A constructor that takes no arguments
c) Both a and b
d) None of the above

27. What is polymorphism in Java?

a) The ability of a variable, function or object to take on multiple forms
b) The concept of wrapping data and methods into a single unit
c) The division of a program into more than one class
d) None of the above

28. What keyword is used to handle exceptions in Java?

a) throw
b) throws
c) try
d) catch

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

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

30. What is the default value of an object reference declared as an instance variable?

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

31. Which method is used to compare two strings for equality in Java?

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

32. What is the default value of a char in Java?

a) 0
b) '\0'
c) ' '
d) null

33. Which of these is an immutable class in Java?

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

34. Which of the following is the smallest primitive data type in Java?

a) short
b) int
c) byte
d) long

35. Which of the following is the largest primitive data type in Java?

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

36. What does the System.exit(0); statement do in a Java program?

a) Pauses the execution
b) Exits the program normally
c) Restarts the program
d) Throws an exception

37. Which of these keywords is used to make a class's methods accessible to all the child classes?

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

38. Which of the following is the correct syntax to create an array in Java?

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

39. What is the purpose of the finally block in exception handling?

a) To execute code after a try block if no exceptions occur
b) To execute code after a catch block regardless of whether an exception occurred or not
c) To handle the exception
d) To declare exceptions

40. Which keyword is used to create a new instance of a class?

a) struct
b) new
c) class
d) this

41. Which operator is used to allocate memory for an object?

a) malloc
b) alloc
c) new
d) new memory

42. Which of these is used for deep copying of an object?

a) clone()
b) copy()
c) Object.clone()
d) System.arraycopy()

43. What does the transient keyword indicate?

a) The variable cannot be serialized
b) The variable is temporary
c) The variable is volatile
d) The variable can be modified asynchronously

44. Which keyword is used to create an abstract class in Java?

a) abstract
b) encapsulate
c) private
d) Virtual

45. How can you achieve abstraction in Java?

a) Only through abstract classes
b) Only through interfaces
c) Through both abstract classes and interfaces
d) Through private methods

46. Can we overload main() method in Java?

a) No, the main() method cannot be overloaded.
b) Yes, the main() method can be overloaded.
c) Only the return type of the main() method can be changed for overloading.
d) Overloading is not a concept applicable to Java methods.

47. What is a subclass in Java inheritance?

A. The class that inherits from another class
B. The class that is inherited from
C. The final class in the inheritance chain
D. None of the above

48. What is the parent class of all classes in Java?

A. Object
B. String
C. Class
D. System

49. Which interface represents a collection that does not allow duplicate elements?

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

50. Which class provides the resizable-array implementation of the List interface?

a) HashSet
b) HashMap
c) ArrayList
d) TreeMap

51. What is the difference between a HashSet and a TreeSet?

a) HashSet maintains elements in insertion order, while TreeSet does not
b) HashSet is faster than TreeSet
c) TreeSet maintains elements in a sorted order, while HashSet does not
d) HashSet is ordered, while TreeSet is unordered

52. What does the HashMap class use internally to store keys and values?

a) Arrays
b) Linked lists
c) Hash table
d) Trees

53. Which interface must be implemented for an object to be used as a key in a HashMap?

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

54. Which of these is a thread-safe collection?

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

55. What is the main difference between Collection and Collections in Java?

a) Collection is an interface, while Collections is a utility class
b) Collections is an interface, while Collection is a class
c) Both are classes
d) Both are interfaces

56. What is the purpose of the Iterator interface?

a) To sort collections
b) To provide a way to traverse through a collection
c) To implement collections
d) None of the above

57. Which collection does not allow null elements?

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

58. What is the initial capacity of an ArrayList when created with the default constructor?

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

59. Which of these is not a checked exception?

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

60. Which of these keywords is used to manually throw an exception?

a) try
b) catch
c) throw
d) throws

61. What does the throws keyword do?

a) It is used to declare an exception
b) It is used to throw an exception explicitly
c) It handles an exception
d) It terminates a program

62. Which exception is thrown when trying to access an index that is out of bounds for an array?

a) ArrayIndexOutOfBoundsException
b) IndexOutOfBoundsException
c) NullPointerException
d) Exception

63. Which of these is true about the try-with-resources statement?

a) It automatically closes resources used within the try block.
b) It requires explicit closure of resources.
c) It only works with file resources.
d) It does not handle exceptions.

64. If a method does not handle a checked exception, what must it do?

a) It must declare the exception using the `throw` keyword.
b) It must declare the exception using the `throws` keyword.
c) It must handle the exception with a `try-catch` block.
d) It can ignore the exception.

65. Which class is used to read text from a file in Java?

a) FileWriter
b) FileReader
c) FileInputStream
d) FileOutputSteam

66. What does JDBC stand for?

a) Java Database Connectivity
b) Java Data Connection
c) Java Development Connection
d) Java Database Control

67. What is the purpose of the DriverManager class in JDBC?

a) To manage the database drivers
b) To directly execute SQL queries
c) To handle Java security
d) To manage the database connections

68. Which method is used to establish a connection to the database?

a) DriverManager.getConnection()
b) DriverManager.connect()
c) Connection.connect()
d) Connection.getConnection()

69. Which method do you use to execute a SQL query in JDBC?

a) executeQuery()
b) execute()
c) executeSQL()
d) queryExecute()

70. What is used to execute stored procedures in a JDBC application?

a) PreparedStatement
b) CallableStatement
c) Statement
d) Connection

71. Which statement is true regarding the PreparedStatement interface?

a) It is used to execute static SQL statements.
b) It represents a precompiled SQL statement.
c) It can only execute SQL queries, not updates.
d) It does not support batch updates.

72. Which method in JDBC is used for batch processing of SQL commands?

a) executeBatch()
b) executeGroup()
c) batchExecute()
d) processBatch()

73. What does the ResultSet interface represent in JDBC?

a) A database result set obtained from a SQL query
b) A set of SQL statements for a database
c) A JDBC connection session
d) An update count confirming how many records were affected by the query

74. What is a thread in Java?

a) A standalone application
b) A lightweight process
c) A heavy-duty process
d) A type of variable

75. Which interface must a class implement to create a thread in Java?

a) Runnable
b) Threadable
c) Executor
d) Startable

76. How can a thread be started in Java?

a) By calling the start() method on a thread object
b) By calling the run() method on a thread object
c) By creating an instance of Thread class
d) By using the ExecutorService

77. What does the wait() method do in Java?

a) It immediately terminates a thread.
b) It forces the thread to run.
c) It causes the current thread to wait until another thread invokes the notify() or notifyAll() method.
d) It checks the current state of a thread.

78. Which method is NOT related to thread life cycle management in Java?

a) yield()
b) sleep()
c) exit()
d) join()

79. What is the difference between the start() and run() methods of a thread class?

a) The start() method starts a new thread, while the run() method executes in the current thread.
b) The run() method starts a new thread, while the start() method does nothing.
c) There is no difference; both methods do the same thing.
d) The start() method is not part of the Thread class.

80. How does the join() method function in the context of threads?

a) It forces all threads to stop executing.
b) It allows one thread to wait for the completion of another.
c) It permanently pauses the thread.
d) It combines two threads into one.

Comments