Java - Programming MCQ Questions and Answers

Test your knowledge of Java Programming Language with these interactive multiple-choice questions.

« First 1 2 3 4 Last »
« Previous Page Next Page »

1. Which of these is not a Java keyword?

  • a) abstract
  • b) assert
  • c) main
  • d) volatile
Answer: C - "main" is not a Java keyword, though it's used as the method name where Java program execution begins.

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

  • a) true
  • b) false
  • c) null
  • d) 0
Answer: B - The default value of a boolean variable in Java is false.

3. Which collection class implements a FIFO (first-in-first-out) behavior?

  • a) ArrayList
  • b) LinkedList
  • c) PriorityQueue
  • d) ArrayDeque
Answer: D - ArrayDeque can be used as a FIFO queue when using add() and remove() methods.

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

  • a) 16 bits
  • b) 32 bits
  • c) 64 bits
  • d) Platform dependent
Answer: B - In Java, int is always 32 bits regardless of the operating system or processor architecture.

5. Which method must be implemented by all threads in Java?

  • a) start()
  • b) init()
  • c) run()
  • d) execute()
Answer: C - The run() method must be implemented as it contains the code that constitutes the thread's task.

6. What is the superclass of all exception classes in Java?

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

7. Which of these is not a valid Java identifier?

  • a) _variable
  • b) $value
  • c) 2ndPlace
  • d) finalValue
Answer: C - Identifiers cannot start with a digit in Java.

8. What does the 'final' keyword indicate when applied to a method?

  • a) The method cannot be overridden
  • b) The method cannot throw exceptions
  • c) The method must be implemented immediately
  • d) The method cannot be accessed from other classes
Answer: A - A final method cannot be overridden by subclasses.

9. Which interface should be implemented for sorting objects of a class?

  • a) Serializable
  • b) Cloneable
  • c) Comparable
  • d) Runnable
Answer: C - The Comparable interface provides natural ordering for objects of a class.

10. What is the output of: System.out.println(5 + 3 + "Java");

  • a) 8Java
  • b) 53Java
  • c) Compilation error
  • d) Runtime error
Answer: A - The + operator performs addition first (5+3=8) then string concatenation.

11. Which of these is not a feature of Java?

  • a) Platform independence
  • b) Multiple inheritance
  • c) Automatic garbage collection
  • d) Strong type checking
Answer: B - Java doesn't support multiple inheritance of classes (though it supports multiple interface implementation).

12. What is the purpose of the 'static' keyword?

  • a) To prevent method overriding
  • b) To indicate class-level members
  • c) To make variables constant
  • d) To allow dynamic binding
Answer: B - The static keyword is used for class-level members that belong to the class rather than instances.

13. Which package is automatically imported in every Java program?

  • a) java.util
  • b) java.io
  • c) java.lang
  • d) java.awt
Answer: C - The java.lang package is automatically imported and contains fundamental classes.

14. What is the correct way to create an ArrayList that can store String objects?

  • a) ArrayList list = new ArrayList();
  • b) ArrayList list = new ArrayList();
  • c) ArrayList list = new ArrayList();
  • d) Both A and C
Answer: D - Both syntaxes are correct in Java, though the diamond operator (<>) is preferred in newer versions.

15. Which of these is a checked exception?

  • a) NullPointerException
  • b) ArrayIndexOutOfBoundsException
  • c) IOException
  • d) ArithmeticException
Answer: C - IOException is a checked exception that must be either caught or declared in the method signature.

16. What is the output of: System.out.println(Math.floor(-7.4));

  • a) -7
  • b) -7.0
  • c) -8
  • d) -8.0
Answer: D - Math.floor() returns the largest double value that is less than or equal to the argument (-8.0 in this case).

17. Which access modifier provides the most restrictive access level?

  • a) public
  • b) protected
  • c) default (no modifier)
  • d) private
Answer: D - Private provides the most restrictive access, limiting visibility to the declaring class only.

18. What is the purpose of the 'transient' keyword?

  • a) To indicate a variable should not be serialized
  • b) To make a variable thread-safe
  • c) To prevent method overriding
  • d) To make a variable constant
Answer: A - The transient keyword indicates that a variable should not be included in serialization.

19. Which of these is not a valid Java comment?

  • a) // Single-line comment
  • b) /* Multi-line comment */
  • c) /** Documentation comment */
  • d)
Answer: D - HTML-style comments are not valid in Java source code.

20. What is the parent class of all Java classes?

  • a) Object
  • b) Class
  • c) Root
  • d) Super
Answer: A - The Object class is the superclass of all other classes in Java.
« First 1 2 3 4 Last »
« Previous Page Next Page »

Learn Computer Skills with PCBooks

Master computer skills with PCBooks! Explore interactive tutorials, MCQ tests, and online exams for programming, web development, and IT fundamentals. Perfect for beginners and advanced learners.

Online MCQ Tests for Programming

Practice programming MCQ questions and improve your coding skills with our online quizzes. Whether you're learning Python, Java, or C programming, PCBooks has you covered.

Free Web Development Tutorials

Learn HTML, CSS, and JavaScript with our free web development tutorials. Test your knowledge with interactive quizzes and online exams.