





















































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This exam assesses the foundational knowledge and practical skills required for Java development. It covers object-oriented programming, core libraries, exception handling, multithreading, and Java tools and frameworks.
Typology: Exams
1 / 93
This page cannot be seen from the preview
Don't miss anything!






















































































Question 1. Which component of the Java platform is responsible for converting bytecode into native machine instructions at runtime? A) JDK B) JRE C) JVM D) JIT Compiler Answer: C Explanation: The Java Virtual Machine (JVM) loads class files, verifies bytecode, and executes it by interpreting or using the Just‑In‑Time (JIT) compiler to translate bytecode to native code. Question 2. What is the primary difference between the JRE and the JDK? A) JRE contains development tools, JDK does not. B) JDK includes the compiler, JRE does not. C) JRE can create .class files, JDK cannot. D) JDK is only for web applications. Answer: B Explanation: The Java Development Kit (JDK) includes the Java compiler (javac) and other development tools, whereas the Java Runtime Environment (JRE) only provides the libraries and JVM needed to run Java programs. Question 3. In the JVM memory model, which area stores class metadata such as field and method information? A) Heap B) Stack C) Method Area D) Native Method Stack
Answer: C Explanation: The Method Area (also called Metaspace in newer JVMs) holds class definitions, constant pool, field and method data, and method bytecode. Question 4. Which of the following primitive types occupies the least amount of memory? A) short B) byte C) int D) long Answer: B Explanation: byte uses 8 bits (1 byte), the smallest primitive storage size in Java. Question 5. Which wrapper class corresponds to the primitive type double? A) Double B) Float C) Decimal D) RealNumber Answer: A Explanation: The wrapper class for double is java.lang.Double, providing object methods and utilities for the primitive. Question 6. What is the result of the following code?
Integer i = 127; Integer j = 127; ## Exam **Explanation:** A variable declared in the loop header (`for (int i = 0; …)`) is limited to the loop’s block. **Question 9.** Which operator has the highest precedence in Java? A) `+` (addition) B) `&&` (logical AND) C) `*` (multiplication) D) `=` (assignment) **Answer:** C **Explanation:** Multiplication (`*`) has higher precedence than addition, logical AND, and assignment. **Question 10.** What does the ternary operator `?:` evaluate? A) It selects one of two statements to execute. B) It returns a value based on a boolean condition. C) It loops until a condition is false. D) It throws an exception if the condition is true. **Answer:** B **Explanation:** The ternary operator returns `condition? expr1 : expr2`, choosing between two values. **Question 11.** Which `switch` statement syntax is valid in Java 14 and later? A) `switch (value) - > { … }` B) `switch (value) { case 1 => … }` C) `switch (value) { case 1: … break; }` only D) `switch (value) { case 1 - > … }` ## Exam **Answer:** D **Explanation:** Java 14 introduced the “switch expression” with the arrow `->` syntax. **Question 12.** Which loop guarantees that its body is executed at least once? A) `for` loop B) `while` loop C) `do‑while` loop D) Enhanced `for` loop **Answer:** C **Explanation:** The `do‑while` loop checks its condition after executing the body, ensuring one execution. **Question 13.** What does the `continue` statement do inside a loop? A) Terminates the loop entirely. B) Skips the remaining statements in the current iteration and proceeds to the next iteration. C) Jumps to a labeled statement outside the loop. D) Causes a compile‑time error if used inside a `switch`. **Answer:** B **Explanation:** `continue` skips to the loop’s next iteration, bypassing any remaining code in the current iteration. **Question 14.** Which of the following correctly declares a static block? A) `static void init() { … }` B) `static { System.out.println("Init"); }` C) `static int x = 10;` ## Exam C) `final` D) `sealed` **Answer:** C **Explanation:** Declaring a class as `final` forbids inheritance. **Question 18.** Which statement about method overriding is true? A) The overriding method can have a more restrictive access modifier. B) The overriding method must have the same return type or a covariant type. C) The overriding method can change the method’s `throws` clause arbitrarily. D) The overriding method can be declared `static`. **Answer:** B **Explanation:** Overridden methods must retain the same (or covariant) return type and cannot reduce visibility. **Question 19.** What does the `super` keyword refer to in a subclass? A) The subclass’s own fields. B) The immediate parent class’s members. C) Any ancestor class in the hierarchy. D) The Java runtime environment. **Answer:** B **Explanation:** `super` accesses members (methods, fields, constructors) of the direct superclass. **Question 20.** Which of the following is a correct way to call another constructor in the same class? A) `this();` ## Exam B) `super();` C) `this(ClassName.this);` D) `self();` **Answer:** A **Explanation:** `this(arguments);` invokes another constructor of the same class and must be the first statement in the constructor. **Question 21.** Which statement about abstract classes is FALSE? A) An abstract class can have concrete methods. B) An abstract class cannot be instantiated directly. C) All methods in an abstract class must be declared abstract. D) An abstract class can have constructors. **Answer:** C **Explanation:** Abstract classes may contain both abstract and concrete methods. **Question 22.** Which of the following is a functional interface in Java? A) `java.util.List` B) `java.lang.Runnable` C) `java.io.Serializable` D) `java.util.Comparator` (prior to Java 8) **Answer:** B **Explanation:** `Runnable` has a single abstract method (`run`) and is a functional interface. **Question 23.** What is the effect of declaring a method as `default` inside an interface? A) It must be overridden by all implementing classes. ## Exam **Question 26.** Which generic declaration restricts a type parameter to subclasses of `Number`? A) `<T super Number>` B) `<T extends Number>` C) `<T implements Number>` D) `<T Number>` **Answer:** B **Explanation:** The `extends` keyword bounds the type parameter to `Number` or its subclasses. **Question 27.** Which wildcard is used to represent an unknown type that can be any subclass of `Number`? A) `? super Number` B) `? extends Number` C) `? Number` D) `?` (unbounded) **Answer:** B **Explanation:** `? extends Number` denotes an unknown type that is a subclass of `Number`. **Question 28.** Which collection interface provides no duplicate elements and no guaranteed order? A) `List` B) `Queue` C) `Set` D) `Map` **Answer:** C ## Exam **Explanation:** `Set` enforces uniqueness; ordering depends on implementation (e.g., `HashSet` is unordered). **Question 29.** Which `List` implementation is synchronized by default? A) `ArrayList` B) `LinkedList` C) `Vector` D) `CopyOnWriteArrayList` **Answer:** C **Explanation:** `Vector` methods are synchronized, making it thread‑safe (though performance‑wise it is less efficient). **Question 30.** Which `Set` implementation maintains insertion order? A) `HashSet` B) `TreeSet` C) `LinkedHashSet` D) `EnumSet` **Answer:** C **Explanation:** `LinkedHashSet` preserves the order in which elements were inserted. **Question 31.** Which `Map` implementation sorts its keys according to their natural ordering or a provided comparator? A) `HashMap` B) `LinkedHashMap` C) `TreeMap` D) `WeakHashMap` ## Exam D) `ArithmeticException` **Answer:** C **Explanation:** `IOException` must be declared or caught; it is a checked exception. **Question 35.** Which keyword is used to rethrow a caught exception without handling it? A) `throw` B) `throws` C) `try` D) `finally` **Answer:** A **Explanation:** Inside a `catch` block, `throw e;` rethrows the exception. **Question 36.** Which method must be overridden when extending `java.lang.Exception` to provide a custom exception message? A) `toString()` B) `getMessage()` C) `printStackTrace()` D) No method is required; the constructor can accept a message. **Answer:** D **Explanation:** You typically provide a constructor that calls `super(message)`. Overriding `getMessage()` is not required. **Question 37.** What is the purpose of the try‑with‑resources statement? A) To catch multiple exceptions in a single block. B) To automatically close resources that implement `AutoCloseable`. ## Exam C) To execute code after an exception is thrown, regardless of handling. D) To lock a resource for thread‑safe access. **Answer:** B **Explanation:** Resources declared in the try‑with‑resources header are closed automatically at the end of the block. **Question 38.** Which class is the superclass of all input streams for reading bytes? A) `Reader` B) `InputStream` C) `FileInputStream` D) `BufferedInputStream` **Answer:** B **Explanation:** `java.io.InputStream` is the abstract base class for byte‑oriented input streams. **Question 39.** Which stream class should be used for reading characters efficiently? A) `FileInputStream` B) `BufferedReader` C) `DataInputStream` D) `ObjectInputStream` **Answer:** B **Explanation:** `BufferedReader` wraps a `Reader` and buffers input, providing efficient character reading and convenient methods like `readLine()`. **Question 40.** Which NIO.2 class represents a file system path? A) `File` ## Exam **Question 43.** Which method transitions a thread from the `NEW` state to the `RUNNABLE` state? A) `run()` B) `start()` C) `sleep()` D) `yield()` **Answer:** B **Explanation:** Calling `start()` causes the JVM to schedule the thread for execution, moving it to `RUNNABLE`. **Question 44.** Which keyword is used to acquire an intrinsic lock on an object? A) `lock` B) `synchronized` C) `volatile` D) `atomic` **Answer:** B **Explanation:** The `synchronized` keyword obtains the monitor lock of the specified object (or class). **Question 45.** Which of the following conditions can cause a deadlock? A) Two threads waiting for the same lock. B) A thread waiting for a lock it already holds. C) Two threads each holding a lock the other needs. D) A thread sleeping while holding a lock. **Answer:** C ## Exam **Explanation:** Deadlock occurs when each thread holds a lock the other thread is waiting for, creating a circular wait. **Question 46.** Which class from `java.util.concurrent` provides a thread pool that can schedule tasks to run after a delay? A) `ExecutorService` B) `ScheduledThreadPoolExecutor` C) `ForkJoinPool` D) `ThreadPoolExecutor` **Answer:** B **Explanation:** `ScheduledThreadPoolExecutor` implements `ScheduledExecutorService`, allowing delayed or periodic task execution. **Question 47.** Which functional interface represents a function that takes an argument and returns a result? A) `Consumer<T>` B) `Supplier<T>` C) `Function<T,R>` D) `Predicate<T>` **Answer:** C **Explanation:** `Function<T,R>` has the method `R apply(T t)`. **Question 48.** Which lambda expression correctly implements a `Predicate<String>` that returns true for non‑empty strings? A) `s - > s.length() > 0` B) `() - > s.length() > 0` ## Exam **Question 51.** Which class from `java.time` represents a date without a time‑zone and without a time-of‑day? A) `LocalDateTime` B) `ZonedDateTime` C) `LocalDate` D) `Instant` **Answer:** C **Explanation:** `LocalDate` stores only the year, month, and day. **Question 52.** Which method creates an immutable `LocalTime` representing 14:30? A) `LocalTime.of(14, 30)` B) `LocalTime.parse("14:30")` C) Both A and B D) Neither A nor B **Answer:** C **Explanation:** Both `of` and `parse` produce a `LocalTime` instance of 14:30. **Question 53.** Which annotation indicates that a method overrides a method declared in a superclass? A) `@Deprecated` B) `@Override` C) `@SuppressWarnings` D) `@FunctionalInterface` **Answer:** B **Explanation:** `@Override` signals the compiler to verify that the method overrides a superclass method. ## Exam **Question 54.** Which built‑in annotation can be used to mark a method as deprecated? A) `@Override` B) `@Deprecated` C) `@SuppressWarnings` D) `@SafeVarargs` **Answer:** B **Explanation:** `@Deprecated` indicates that the element should no longer be used. **Question 55.** Which class enables inspection of fields, methods, and constructors at runtime? A) `ClassLoader` B) `Method` C) `Field` D) `java.lang.reflect.Class` **Answer:** D **Explanation:** `Class` provides reflection capabilities such as `getDeclaredMethods()`, `getFields()`, etc. **Question 56.** Which method of `java.lang.reflect.Method` invokes the underlying method on a given object? A) `execute(Object obj, Object... args)` B) `call(Object obj, Object... args)` C) `invoke(Object obj, Object... args)` D) `run(Object obj, Object... args)` **Answer:** C