Java - Programming MCQ Questions and Answers

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

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

81. What is the purpose of the 'var' keyword introduced in Java 10?

  • a) To declare variables with dynamic types
  • b) To reduce boilerplate code for local variables
  • c) To make variables immutable
  • d) To support functional programming
Answer: B - var allows local variable type inference while maintaining static typing.

82. Which method reference syntax is equivalent to (String s) -> s.length()?

  • a) String::length
  • b) s::length
  • c) length::String
  • d) String.length
Answer: A - ClassName::methodName is the syntax for instance method reference.

83. What is the purpose of the 'Records' feature (Java 14+)?

  • a) To create immutable data carriers
  • b) To record method executions
  • c) To implement the Observer pattern
  • d) To store database records
Answer: A - Records provide a compact syntax for declaring classes that are transparent holders for immutable data.

84. Which Stream operation transforms elements without changing their count?

  • a) filter()
  • b) map()
  • c) distinct()
  • d) sorted()
Answer: B - map() transforms each element one-to-one without changing stream size.

85. What is the purpose of the 'Sealed Classes' feature (Java 15+)?

  • a) To prevent class inheritance
  • b) To restrict which classes can extend them
  • c) To make classes thread-safe
  • d) To optimize class loading
Answer: B - Sealed classes control which other classes may extend or implement them.

86. Which is not a valid way to create a Stream in Java?

  • a) Stream.of()
  • b) Arrays.stream()
  • c) Collection.stream()
  • d) new Stream()
Answer: D - Streams are created through factory methods, not direct instantiation.

87. What is the purpose of the 'Pattern Matching for instanceof' (Java 16+)?

  • a) To simplify conditional extractions
  • b) To implement regular expressions
  • c) To optimize pattern recognition
  • d) To replace switch statements
Answer: A - It eliminates the need for explicit casting after instanceof checks.

88. Which is not a valid terminal operation in Stream API?

  • a) forEach()
  • b) peek()
  • c) reduce()
  • d) collect()
Answer: B - peek() is an intermediate operation used for debugging.

89. What is the purpose of the 'Text Blocks' feature (Java 15+)?

  • a) To handle binary data
  • b) To simplify multiline string literals
  • c) To implement template engines
  • d) To optimize string concatenation
Answer: B - Text blocks provide a clean way to work with multiline strings.

90. Which functional interface represents a predicate (boolean-valued function)?

  • a) Function<T,R>
  • b) Consumer<T>
  • c) Predicate<T>
  • d) Supplier<T>
Answer: C - Predicate's test() method returns a boolean.

91. What is the purpose of the 'CompletableFuture' class?

  • a) To handle completed collections
  • b) To compose asynchronous operations
  • c) To implement final classes
  • d) To optimize future allocations
Answer: B - CompletableFuture allows chaining and combining async operations.

92. Which is not a valid Collectors method?

  • a) toList()
  • b) toSet()
  • c) toArray()
  • d) groupingBy()
Answer: C - There's toCollection() but no toArray() in Collectors.

93. What is the purpose of the 'Optional.orElseThrow()' method?

  • a) To always throw an exception
  • b) To throw if value is present
  • c) To throw if value is absent
  • d) To throw a NullPointerException
Answer: C - Throws specified exception if no value is present.

94. Which is not a valid Stream source?

  • a) List
  • b) Map
  • c) Array
  • d) Set
Answer: B - You need to use entrySet(), keySet(), or values() to stream a Map.

95. What is the purpose of the 'takeWhile()' stream operation?

  • a) To take elements while a condition is true
  • b) To take elements until a condition is true
  • c) To take all elements
  • d) To take alternating elements
Answer: A - Stops when predicate first becomes false (Java 9+).

96. Which functional interface has a 'get()' method?

  • a) Function
  • b) Supplier
  • c) Consumer
  • d) Predicate
Answer: B - Supplier's get() provides a value without inputs.

97. What is the purpose of the 'Files.readString()' method (Java 11+)?

  • a) To read binary files
  • b) To read a file into a String
  • c) To read file metadata
  • d) To read file permissions
Answer: B - Simplifies reading text files into a String.

98. Which is not a valid method in the 'java.time' package?

  • a) LocalDate.now()
  • b) LocalTime.today()
  • c) Instant.ofEpochSecond()
  • d) Duration.between()
Answer: B - LocalTime doesn't have a today() method (LocalDate does).

99. What is the purpose of the 'var' keyword in lambda parameters (Java 11+)?

  • a) To allow dynamic typing
  • b) To enable type inference
  • c) To make parameters final
  • d) var cannot be used in lambdas
Answer: D - var is not permitted in lambda parameter declarations.

100. Which is not a valid switch expression feature (Java 14+)?

  • a) Multiple case labels
  • b) Arrow syntax (->)
  • c) Returning values
  • d) Pattern matching
Answer: D - Pattern matching in switch came later (Java 17+).
« First 4 5 6 7 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.