Java - Programming MCQ Questions and Answers

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

« First 3 4 5 6 Last »
« Previous Page Next Page »

61. What is the purpose of the 'transient' keyword in Java?

  • a) To make variables thread-safe
  • b) To exclude fields from serialization
  • c) To mark temporary variables
  • d) To optimize variable access
Answer: B - transient variables are not serialized when an object is persisted.

62. Which method is used to create a deep copy of an object?

  • a) clone()
  • b) copy()
  • c) deepCopy()
  • d) There's no built-in method
Answer: D - Java doesn't provide a built-in deep copy mechanism; must be implemented manually.

63. What is the output of: System.out.println(0.1 + 0.2 == 0.3);

  • a) true
  • b) false
  • c) Compilation error
  • d) Runtime exception
Answer: B - Due to floating-point precision issues, 0.1 + 0.2 doesn't exactly equal 0.3.

64. Which collection provides thread-safe operations without explicit synchronization?

  • a) ArrayList
  • b) Vector
  • c) LinkedList
  • d) HashSet
Answer: B - Vector methods are synchronized by default.

65. What is the purpose of the 'Comparator' interface?

  • a) To compare two objects
  • b) To define custom sorting orders
  • c) To implement natural ordering
  • d) Both A and B
Answer: D - Comparator is used for both comparison and defining custom sorting.

66. Which annotation is used to suppress compiler warnings?

  • a) @SuppressWarnings
  • b) @SafeVarargs
  • c) @Deprecated
  • d) @Override
Answer: A - @SuppressWarnings tells the compiler to ignore specific warnings.

67. What is the default value of a double variable?

  • a) 0
  • b) 0.0
  • c) null
  • d) undefined
Answer: B - The default value of double is 0.0d.

68. Which interface should be implemented for objects to be used as keys in HashMap?

  • a) Serializable
  • b) Cloneable
  • c) Comparable
  • d) Both equals() and hashCode()
Answer: D - HashMap keys must properly implement equals() and hashCode().

69. What is the purpose of the 'synchronized' keyword?

  • a) To make variables immutable
  • b) To prevent method overriding
  • c) To provide thread safety
  • d) To optimize method execution
Answer: C - synchronized provides mutually exclusive access to resources.

70. Which of these is not a valid Java 8 feature?

  • a) Lambda expressions
  • b) Stream API
  • c) var keyword
  • d) Default methods
Answer: C - 'var' was introduced in Java 10, not Java 8.

71. What is the output of: System.out.println(Math.min(Double.MIN_VALUE, 0.0d));

  • a) 0.0
  • b) Double.MIN_VALUE
  • c) Negative infinity
  • d) Positive infinity
Answer: A - Double.MIN_VALUE is the smallest positive value, so 0.0 is smaller.

72. Which method is used to get the class object of a primitive type?

  • a) getClass()
  • b) class()
  • c) TYPE
  • d) getType()
Answer: C - Each wrapper class has a TYPE field (e.g., Integer.TYPE).

73. What is the purpose of the 'try-with-resources' statement?

  • a) To handle multiple exceptions
  • b) To automatically close resources
  • c) To optimize resource usage
  • d) To test resource availability
Answer: B - It ensures resources implementing AutoCloseable are closed automatically.

74. Which class is used to format dates in Java?

  • a) DateFormatter
  • b) SimpleDateFormat
  • c) DateTimeFormatter
  • d) Both B and C
Answer: D - SimpleDateFormat (legacy) and DateTimeFormatter (Java 8+) are used.

75. What is the purpose of the 'volatile' keyword in multithreading?

  • a) To make variables immutable
  • b) To ensure visibility across threads
  • c) To provide atomic operations
  • d) To synchronize method access
Answer: B - volatile ensures changes are visible to all threads immediately.

76. Which method is used to convert a primitive array to a List?

  • a) Arrays.asList()
  • b) Arrays.toList()
  • c) List.fromArray()
  • d) CollectionUtils.toList()
Answer: A - Arrays.asList() converts an array to a fixed-size List.

77. What is the purpose of the 'default' keyword in interfaces?

  • a) To provide default access modifier
  • b) To implement methods in interfaces
  • c) To set default parameter values
  • d) To mark default implementations
Answer: B - default methods allow interface method implementations (Java 8+).

78. Which of these is not a valid functional interface?

  • a) Runnable
  • b) Callable
  • c) Action
  • d) Consumer
Answer: C - Action is not a standard functional interface in Java.

79. What is the purpose of the 'Optional' class?

  • a) To handle null values more elegantly
  • b) To provide optional method parameters
  • c) To make fields optional in serialization
  • d) To mark optional dependencies
Answer: A - Optional is a container object that may or may not contain a value.

80. Which stream operation is not terminal?

  • a) forEach
  • b) collect
  • c) map
  • d) reduce
Answer: C - map() is an intermediate operation; others are terminal.
« First 3 4 5 6 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.