

















































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















































































Question 1. Which component translates Java bytecode into native machine instructions at runtime? A) JDK B) JRE C) JVM D) JIT Answer: C Explanation: The Java Virtual Machine (JVM) loads bytecode and executes it, using the JIT compiler to produce native code. Question 2. What is the default value of a local variable of type int that is not explicitly initialized? A) 0 B) null C) 0L D) Compilation error Answer: D Explanation: Local variables must be initialized before use; otherwise the code does not compile. Question 3. Which of the following statements about the String pool is true? A) All String objects are stored in the pool. B) new String("abc") always creates a new object outside the pool. C) The pool can be cleared by calling System.gc(). D) Literal strings are interned automatically. Answer: D
Explanation: String literals are automatically placed in the pool; new String(...) creates a separate object unless intern() is called. Question 4. Which access modifier allows visibility only within the same package and subclasses in other packages? A) public B) protected C) default D) private Answer: B Explanation: protected members are accessible to subclasses regardless of package and to any class in the same package. Question 5. What is the result of 5 + 2 * 3 in Java? A) 21 B) 11 C) 15 D) 9 Answer: B Explanation: Multiplication has higher precedence than addition, so 2*3=6; 5+6=11. Question 6. Which keyword is used to prevent a class from being subclassed? A) static B) final C) abstract D) sealed
Explanation: super is used to access members of the immediate superclass. Question 10. Which functional interface has a method R apply(T t)? A) Consumer B) Supplier C) Function D) Predicate Answer: C Explanation: Function represents a function that takes an argument of type T and returns a result of type R. Question 11. Which statement creates an immutable list containing the elements 1, 2, 3? A) List.of(1,2,3) B) Arrays.asList(1,2,3) C) new ArrayList<>(List.of(1,2,3)) D) Collections.unmodifiableList(Arrays.asList(1,2,3)) Answer: A Explanation: List.of (Java 9+) returns an immutable list directly. Question 12. What is the output of System.out.println(10 / 3);? A) 3 B) 3. C) 4 D) Compilation error Answer: A
Explanation: Both operands are integers, so integer division truncates the fractional part. Question 13. Which of the following correctly declares a generic method that swaps two elements in an array? A) public static void swap(T[] a, int i, int j) B) public static void swap(T[] a, int i, int j) C) public static void swap(T[] a, int i, int j) D) public static swap(T[] a, int i, int j) Answer: A Explanation: The generic type parameter `` must appear before the return type. Question 14. Which operator is used for signed right shift? A) >>> B) >> C) << D) >>> (unsigned) Answer: B Explanation: >> shifts bits to the right while preserving the sign bit. Question 15. Which method of java.util.Optional returns the contained value or throws NoSuchElementException if empty? A) orElseThrow() B) get() C) orElse(null) D) ifPresent() Answer: B
Answer: C Explanation: yield supplies the value that the switch expression evaluates to. Question 19. Which of the following correctly creates a LocalDate representing the 15th of March 2023? A) LocalDate.of(2023, 3, 15) B) LocalDate.parse("2023-03-15") C) LocalDate.ofYearDay(2023, 74) D) All of the above Answer: D Explanation: All three statements produce the same LocalDate instance. Question 20. Which statement about autoboxing is false? A) int can be automatically converted to Integer. B) Autoboxing may cause NullPointerException when unboxing a null reference. C) Autoboxing works for user-defined wrapper classes. D) Autoboxing incurs object creation overhead. Answer: C Explanation: Autoboxing is defined only for primitive types and their standard wrapper classes. Question 21. What is the effect of the volatile keyword on a variable? A) Guarantees atomicity of compound operations B) Prevents the variable from being cached thread-locally C) Makes the variable immutable
D) Allows the variable to be accessed without synchronization only on single-core CPUs Answer: B Explanation: volatile ensures visibility of changes across threads by preventing local caching. Question 22. Which method of java.util.Collections returns an unmodifiable view of a list? A) unmodifiableList(List) B) immutableList(List) C) readOnlyList(List) D) fixedList(List) Answer: A Explanation: Collections.unmodifiableList creates a view that throws UnsupportedOperationException on modification attempts. Question 23. Which of the following statements about enum types is true? A) Enums cannot implement interfaces. B) Enums can have constructors, fields, and methods. C) Each enum constant is a separate class. D) Enums are implicitly final. Answer: B Explanation: Enum constants are instances; enums can define constructors, fields, and methods. Question 24. In a try-with-resources statement, which interface must the resource implement? A) Closeable only
A) It is thread-safe. B) Its append method returns a new StringBuilder. C) It is immutable. D) It is more efficient than String for concatenation in single-threaded code. Answer: D Explanation: StringBuilder is mutable and not synchronized, making it faster for mutable concatenation in a single thread. Question 28. Which of these statements correctly declares a record in Java 16? A) record Point(int x, int y) {} B) class Point record(int x, int y) {} C) record Point {int x; int y;} D) record Point(int x, int y); Answer: A Explanation: The syntax record Name(parameters) {} defines a record with automatically generated components. Question 29. Which method of java.util.concurrent.Future blocks until the computation is complete and returns the result? A) get() B) cancel() C) isDone() D) run() Answer: A Explanation: Future.get() waits if necessary for the computation to finish and then returns the result.
Question 30. In the Stream API, which operation is intermediate and lazy? A) forEach B) collect C) filter D) count Answer: C Explanation: filter is an intermediate operation; it is evaluated only when a terminal operation is invoked. Question 31. Which of the following statements about HashMap is true? A) It maintains insertion order. B) It allows one null key and multiple null values. C) It is synchronized by default. D) It sorts entries by key. Answer: B Explanation: HashMap permits a single null key and any number of null values; it does not guarantee order or thread safety. Question 32. Which annotation indicates that a method overrides a superclass method? A) @Override B) @Super C) @Inherited D) @Overload Answer: A Explanation: @Override signals that the method is intended to override a method from a superclass or interface.
Question 36. Which method of java.time.LocalDate returns the day of the week? A) getDayOfMonth() B) getDayOfWeek() C) getChronology() D) getMonthValue() Answer: B Explanation: getDayOfWeek returns a DayOfWeek enum representing the day. Question 37. In a switch statement using a String, which case label is illegal? A) case "Hello": B) case null: C) case "World": D) All are legal Answer: B Explanation: null cannot be used as a case label; a NullPointerException would be thrown if the selector is null. Question 38. Which of the following is true about the final keyword when applied to a method parameter? A) The parameter cannot be reassigned within the method. B) The argument passed must be a constant. C) The method becomes static. D) The parameter becomes a class field. Answer: A
Explanation: Declaring a parameter final prevents reassignment inside the method body. Question 39. Which of these statements correctly creates a Path object for the file data.txt in the current directory? A) Path p = Paths.get("data.txt"); B) Path p = new Path("data.txt"); C) Path p = FileSystems.getDefault().path("data.txt"); D) Both A and C Answer: D Explanation: Both Paths.get and FileSystems.getDefault().getPath produce the same Path. Question 40. Which interface must be implemented to create a custom unchecked exception? A) Exception B) RuntimeException C) Error D) No interface is required; extend RuntimeException. Answer: D Explanation: Unchecked exceptions are subclasses of RuntimeException; no additional interface is needed. Question 41. Which of the following is a correct way to create a thread that returns a result? A) new Thread(() -> compute()) B) ExecutorService.submit(() -> compute()) C) new Runnable(() -> compute()) D) new Callable(() -> compute())
D) merge(K key, V value, BiFunction remappingFunction) Answer: A Explanation: replace(key, oldValue, newValue) performs conditional replacement based on the current mapping. Question 45. Which of the following is a correct lambda expression for a Predicate that tests if a string is empty? A) s -> s.isEmpty() B) (String s) -> { return s.isEmpty; } C) () -> s.isEmpty() D) s -> { s.isEmpty(); } Answer: A Explanation: The lambda takes a String parameter s and returns the boolean result of isEmpty(). Question 46. What is the result of Integer.valueOf("010")? A) 10 B) 8 C) 0 D) NumberFormatException Answer: A Explanation: The string is parsed as decimal by default; leading zeros are ignored. Question 47. Which of the following statements about java.io.Serializable is false? A) It is a marker interface. B) It requires implementing the writeObject method.
C) Subclasses of a serializable class are also serializable by default. D) The default serialization mechanism uses reflection. Answer: B Explanation: Serializable does not declare any methods; writeObject is optional for custom serialization. Question 48. Which of these is a correct way to declare a var-args parameter? A) void m(String... args) B) void m(String args...) C) void m(...String args) D) void m(String[] args...) Answer: A Explanation: The ellipsis follows the type, indicating a variable number of arguments. Question 49. Which statement about enum values() method is true? A) It returns a Set of the enum constants. B) It is automatically generated by the compiler. C) It can be overridden by the enum class. D) It returns the constants in alphabetical order. Answer: B Explanation: The compiler adds a static values() method that returns an array of the constants in declaration order. Question 50. Which of the following is a correct way to create a Predicate that checks for positivity using a method reference? A) Predicate p = Integer::isPositive;
A) LocalDateTime B) ZonedDateTime C) Instant D) OffsetDateTime Answer: B Explanation: ZonedDateTime combines date, time, and a time-zone. Question 54. Which of the following statements about java.util.concurrent.ConcurrentHashMap is true? A) All operations are lock-free. B) Retrieval operations do not block. C) It maintains insertion order. D) It does not allow null keys or values. Answer: D Explanation: ConcurrentHashMap rejects null keys and values; retrievals are non-blocking but may involve internal locking. Question 55. Which of the following correctly creates a Predicate that checks if a string starts with "A"? A) s -> s.startsWith("A") B) String::startsWith("A") C) s -> { s.startsWith("A"); } D) Predicate p = String::startsWith; Answer: A Explanation: The lambda s -> s.startsWith("A") matches the Predicate functional interface.
Question 56. Which method of java.util.Collections returns a synchronized (thread-safe) list backed by the specified list? A) synchronizedList(List list) B) threadSafeList(List list) C) lockedList(List list) D) safeList(List list) Answer: A Explanation: Collections.synchronizedList wraps the list with synchronized methods. Question 57. What is the result of 5 == 5.0 in Java? A) true B) false C) Compilation error D) Runtime exception Answer: true Explanation: The int literal is widened to double before comparison, so the values are equal. Question 58. Which of the following is a correct way to declare a method that never returns normally? A) void stop() throws RuntimeException B) void stop() throws Error C) void stop() { while(true); } D) void stop() { throw new AssertionError(); } Answer: D Explanation: Throwing an Error (or unchecked exception) causes the method to exit abruptly; the method does not need a throws clause.