Courseiva

CCNA What is Java Questions

39 questions · What is Java · All types, answers revealed

1
Matchingmedium

Match each OOP concept to its Java implementation.

Drag a concept onto its matching description — or click a concept then click the description.

Concepts
Matches

Using private fields with public getters/setters

Using extends keyword to derive a class

Method overriding and overloading

Using abstract classes and interfaces

Using fields that reference other objects

Why these pairings

Correct matches: Encapsulation with private fields/getters; Inheritance with extends; Polymorphism with method overriding. Common confusions include mixing abstraction and polymorphism.

2
MCQmedium

A developer is tasked with deploying a Java application to a customer's server. The customer has only a JRE installed and no internet access. The application uses Java NIO and requires reading configuration files from the classpath. The application compiles and runs fine on the developer's machine which has JDK 11. However, when deploying the compiled JAR to the customer's JRE 11, it throws a 'NoClassDefFoundError' for a class that is part of the JDK's internal API (e.g., com.sun.nio.file.SensitivityWatchEventModifier). The developer is confused because the class is in the standard library. Which action should be taken to resolve the issue?

A.Recompile with a lower target version
B.Add the missing JAR file to the classpath
C.Modify the code to use a public API from the standard library
D.Install the JDK on the customer's server
AnswerC

Internal APIs are not accessible; use public alternatives.

Why this answer

The issue is that the code uses an internal JDK API (com.sun.nio.file.SensitivityWatchEventModifier), which is not guaranteed to be available in all JRE distributions and is encapsulated in Java 9+. The proper fix is to modify the code to use a public, supported API from the standard library (e.g., java.nio.file.StandardWatchEventKinds). Option A is incorrect because recompiling with a lower target version would still use the internal API, and the target version does not affect encapsulation.

Option B is incorrect because the missing class is not a separate JAR; it's part of the JDK internal modules and not available in the JRE's classpath. Option D is incorrect because installing the JDK would expose the internal class but is not a proper solution; it ties the application to a specific JDK implementation and may violate encapsulation in future releases.

3
MCQhard

A large enterprise application is experiencing intermittent crashes on a production server running Java 8. The crash logs show 'java.lang.OutOfMemoryError: Metaspace'. The application heavily uses frameworks like Hibernate and JasperReports, which generate many classes dynamically at runtime. The server is configured with default JVM options except for -Xmx2g. A junior administrator suggests increasing -Xmx to 4g. What is the most effective solution to prevent these crashes?

A.Enable -XX:+UseCompressedOops
B.Increase -Xmx to 4g
C.Switch to -XX:+UseParallelGC
D.Set -XX:MaxMetaspaceSize to a higher value
AnswerD

Directly increases the limit for class metadata storage.

Why this answer

The crash is caused by Metaspace exhaustion due to dynamic class generation by frameworks like Hibernate and JasperReports. The default MaxMetaspaceSize is unlimited, but the JVM may still hit native memory limits. Setting -XX:MaxMetaspaceSize to a higher value (or leaving it unlimited but with more available memory) directly addresses the issue.

Option B (Increasing -Xmx) only increases heap size, which does not affect Metaspace. Option A (UseCompressedOops) optimizes object references in the heap, not class metadata. Option C (UseParallelGC) changes the garbage collector but does not prevent Metaspace growth.

Therefore, Option D is the most effective solution.

4
MCQmedium

Given the javap output of a class file, which statement is correct about the Java version used to compile it?

A.Compiled with Java 11
B.Compiled with Java 8
C.This is a normal class file but does not indicate a specific Java version
D.Compiled with Java 9
AnswerA

Major version 55 indicates Java 11.

Why this answer

The major version number in the javap output is 55, which corresponds to Java 11. Java 8 uses major version 52, Java 9 uses 53, and Java 10 uses 54. Therefore, the class file was compiled with Java 11.

5
MCQhard

In the Java memory model, where are primitive local variables declared inside a method stored?

A.Heap
B.Native method area
C.Stack
D.Method area
AnswerC

Local variables, including primitives, live on the stack.

Why this answer

Primitive local variables declared inside a method are stored on the stack, which handles method invocation and local variable storage. Option A (heap) is incorrect because the heap stores objects and arrays, not local primitives. Option B (native method area) is incorrect; the native method stack is used for native method calls, not regular Java local variables.

Option D (method area) is incorrect because the method area stores class metadata and static fields, not local variables.

6
MCQeasy

A company wants to develop a Java application that can run on Windows, Linux, and macOS without any code changes. Which Java feature makes this possible?

A.Multithreading
B.Platform Independence via JVM
C.Garbage Collection
D.Object-Oriented Programming
AnswerB

The JVM allows bytecode to run on any device with a compatible JVM.

Why this answer

Java achieves platform independence through the Java Virtual Machine (JVM), which interprets compiled bytecode. Option A is wrong because multithreading is a concurrency feature, not responsible for platform independence. Option C is wrong because garbage collection manages memory but does not enable platform independence.

Option D is wrong because object-oriented programming is a paradigm, not responsible for cross-platform execution.

7
MCQmedium

What is the most likely cause of this error?

A.There is a memory leak in native code outside the heap.
B.The heap size is insufficient for the objects being created.
C.There is a stack overflow in the method being called.
D.Too many threads are running concurrently.
AnswerB

Heap space error occurs when object allocations exceed heap capacity.

Why this answer

OutOfMemoryError: Java heap space indicates the heap is full. Option A (memory leak in native code) would typically cause a different error, such as a native memory error. Option C (stack overflow) would be StackOverflowError.

Option D (too many threads) would cause an error like 'unable to create new native thread'.

8
MCQeasy

A developer says Java is platform-independent because of the JVM. Which statement best explains this?

A.Java source code is compiled directly into native machine code for each platform.
B.Java source code is compiled into bytecode, which runs on the Java Virtual Machine (JVM).
C.The JVM is written in platform-independent code, allowing it to run anywhere.
D.Java uses an interpreter only, so the source code is interpreted directly on any platform.
AnswerB

Bytecode is platform-independent and executed by the JVM.

Why this answer

Java source code is compiled into bytecode, which runs on the JVM, making it platform-independent at the source level. Option A is incorrect because Java does not compile to native code for each platform; it compiles to bytecode. Option C is incorrect because Java uses both a compiler and interpreter/JIT.

Option D is incorrect because the JVM itself is platform-specific, but bytecode is not.

9
Matchingmedium

Match each Java term to its correct definition.

Drag a concept onto its matching description — or click a concept then click the description.

Concepts
Matches

Java Virtual Machine that executes bytecode

Runtime environment including JVM and core libraries

Development kit including JRE and tools like javac

Just-In-Time compiler that optimizes bytecode at runtime

Garbage Collector that automatically manages memory

Why these pairings

Correct matches: JDK is the development kit; JRE is the runtime; JVM executes bytecode; bytecode is the intermediate format. Common confusions: mixing JDK and JRE or JVM with JDK.

10
MCQhard

A team is designing a new system that requires deploying independent services communicating over a network. Which Java technology is most suitable for this architecture?

A.Java Applets
B.Java EE with EJB
C.Java EE with JAX-RS RESTful Web Services
D.Java RMI
AnswerC

Correct. Java EE with JAX-RS is the standard Java technology for RESTful web services, enabling independent services to communicate over a network via HTTP. JAX-RS annotations like @Path and @GET simplify service creation.

Why this answer

Java EE with JAX-RS (Java API for RESTful Web Services) enables building RESTful services that communicate over HTTP, which is ideal for independent services in a microservices architecture. Option A is incorrect: Java Applets are client-side components that run in a browser and are not suitable for server-side network communication. Option B is incorrect: Java EE with EJB is a heavyweight, monolithic approach that is less suited for modern microservices architectures.

Option D is incorrect: Java RMI is a remote method invocation protocol for tightly coupled distributed systems, not designed for lightweight RESTful services.

Exam trap

Candidates may mistakenly choose Spring Boot because it is popular, but the question specifically asks for a 'Java technology' from the core Java ecosystem. JAX-RS is the correct standard choice.

11
Multi-Selecteasy

Which THREE of the following are key features of the Java programming language?

Select 3 answers
A.Use of pointers for direct memory access
B.Support for multiple inheritance of classes
C.Platform independence through bytecode and JVM
D.Automatic memory management (garbage collection)
E.Strong type checking at compile time
AnswersC, D, E

Java source code is compiled to bytecode, which runs on any JVM.

Why this answer

Java is platform independent via bytecode, has automatic garbage collection, and is strongly typed. It does not support multiple inheritance for classes (only interfaces) and does not have pointers.

12
Multi-Selecteasy

Which TWO statements are true about the Java programming language?

Select 2 answers
A.It is a purely procedural language.
B.It supports direct pointer manipulation for memory access.
C.It is a strongly-typed language.
D.It allows multiple inheritance of classes.
E.It provides automatic memory management through garbage collection.
AnswersC, E

All variables must have a declared type.

Why this answer

Options C and E are correct. Java is a strongly-typed language (C) that provides automatic memory management through garbage collection (E). Option A is wrong because Java is object-oriented, not purely procedural.

Option B is wrong because Java does not support direct pointer manipulation; it uses references. Option D is wrong because Java does not allow multiple inheritance of classes; it uses interfaces to achieve a form of multiple inheritance.

13
Multi-Selecteasy

Which TWO are true about the Java Runtime Environment (JRE)? (Choose two.)

Select 2 answers
A.It is larger than the JDK
B.It includes the Java compiler
C.It includes the JVM
D.It includes development tools
E.It is required to run Java applications
AnswersC, E

The JVM is a core component of the JRE.

Why this answer

The JRE (Java Runtime Environment) includes the JVM (Java Virtual Machine) and core libraries necessary to run Java applications. Therefore, option C is correct. Option E is correct because the JRE is required to run any Java application; without it, Java bytecode cannot be executed.

Option A is incorrect: the JRE is smaller than the JDK (Java Development Kit) because the JDK contains additional tools like the compiler and debugger. Option B is incorrect: the Java compiler is part of the JDK, not the JRE. Option D is incorrect: development tools are included in the JDK, not the JRE.

14
Multi-Selectmedium

A developer has written a Java program that uses third-party libraries. Which TWO actions are necessary to run the program on a different machine? (Choose two.)

Select 2 answers
A.Copy only the .class files of the program
B.Install the JDK
C.Set the PATH environment variable to include the java executable
D.Install the JRE
E.Copy the .java source files
AnswersC, D

The system must find the java command.

Why this answer

Options C and D are correct. To run a Java program, the JRE must be installed (D) because it provides the Java Runtime Environment including the JVM. The PATH must include the java executable's directory (C) so that the 'java' command can be invoked.

Option A is incorrect because .class files alone are insufficient; third-party libraries (JARs) are also needed and must be accessible. Option B is incorrect because the JDK (Java Development Kit) is only needed for compilation and development, not for running. Option E is incorrect because source files are not required to run the compiled program.

15
Multi-Selecthard

Which THREE are benefits of Java's platform independence? (Choose three.)

Select 3 answers
A.Ability to run on any device with a JVM
B.Write once, run anywhere
C.Faster execution compared to native code
D.Automatic memory management
E.Enhanced security through sandboxing
AnswersA, B, E

Platform independence allows the same bytecode to run on any device that has a JVM implementation.

Why this answer

Options A, B, and E are correct. Java's platform independence allows code to run on any device with a JVM (A), enables the 'write once, run anywhere' principle (B), and enhances security through sandboxing, which isolates untrusted code (E). Option C is incorrect because faster execution compared to native code is not a benefit; Java often runs slower due to interpretation or JIT compilation.

Option D is incorrect because automatic memory management (garbage collection) is a feature of the JVM but not directly a benefit of platform independence; it is a separate advantage of Java.

16
MCQmedium

Refer to the exhibit. A developer runs the command java -version on a system. Which statement about this Java installation is correct?

A.This is a Java SE 17 installation.
B.This is a Java Runtime Environment (JRE) installation.
C.This is a Java SE 8 installation.
D.This is a Java Development Kit (JDK) installation.
AnswerB

The output explicitly says 'Runtime Environment' and lacks compiler information.

Why this answer

The output shows "Runtime Environment" and no compiler information, indicating a JRE. Version 11.0.12 is Java SE 11 LTS. Option C and D are wrong because the version is 11, not 8 or 17.

Option A is incorrect because JDK would include the compiler.

17
Drag & Dropmedium

Arrange the steps to overload a method in Java in the correct order.

Drag steps to the numbered slots on the right, or tap a step then tap a slot.

Steps
Order
1Step 1
2Step 2
3Step 3
4Step 4

Why this order

First define one method, then define another with same name but different parameters, ensure unique signatures, and then call with matching arguments.

18
MCQhard

A team deploys a Java application and observes frequent Full GC pauses. Which garbage collector is designed to minimize pause times?

A.Garbage-First (G1GC) (-XX:+UseG1GC)
B.Concurrent Mark Sweep (CMS) (-XX:+UseConcMarkSweepGC)
C.Serial GC (-XX:+UseSerialGC)
D.Parallel GC (-XX:+UseParallelGC)
AnswerA

G1GC aims to limit pause times and reduce Full GC frequency.

Why this answer

G1GC (Garbage-First) is designed to provide predictable pause times and minimize Full GC pauses by dividing the heap into regions and collecting the regions with the most garbage first. Option B (CMS) was also designed for low pauses but is deprecated and can cause fragmentation. Option C (Serial) is for single-threaded environments and has long pauses.

Option D (Parallel) focuses on throughput, not pause times.

19
MCQeasy

A junior developer writes a simple 'Hello World' program and saves it as HelloWorld.java. He compiles it successfully with 'javac HelloWorld.java', confirming that HelloWorld.class is created in the current directory. When he tries to run it with the command 'java HelloWorld', the system returns 'Error: Could not find or load main class HelloWorld'. The current directory is indeed the one containing HelloWorld.class. He has JAVA_HOME set to the JDK installation directory and has verified that java is in the PATH. What is the most likely cause?

A.The class file is corrupt
B.The classpath does not include the current directory
C.The main method is missing or incorrectly declared
D.The java command is not in the PATH
AnswerB

Java does not automatically search the current directory; use '-cp .' to include it.

Why this answer

By default, the java command does not include the current directory in the classpath, so even though HelloWorld.class exists in the current directory, the JVM cannot find it. The developer must explicitly add the current directory using -cp . or set the CLASSPATH environment variable. Option A is incorrect because a corrupt class file would typically cause a ClassFormatError, not 'Could not find or load main class'.

Option C is incorrect because a missing or incorrect main method would yield 'Main method not found in class HelloWorld' error. Option D is incorrect because the error message indicates that the java command executed (it found the java executable), so it is in the PATH.

20
MCQhard

Based on the command, which garbage collector is configured for this application?

A.Parallel GC
B.Garbage-First (G1GC)
C.Serial GC
D.Concurrent Mark Sweep (CMS)
AnswerB

The flag -XX:+UseG1GC enables G1GC.

Why this answer

XX:+UseG1GC explicitly sets the Garbage-First (G1GC) garbage collector. Option A is incorrect; Parallel GC would be enabled with -XX:+UseParallelGC. Option C is incorrect; Serial GC would be enabled with -XX:+UseSerialGC.

Option D is incorrect; CMS would be enabled with -XX:+UseConcMarkSweepGC.

21
Multi-Selecthard

Which TWO are characteristics of the Java Runtime Environment (JRE)?

Select 2 answers
A.It includes the Java compiler (javac) for compiling source code.
B.It is smaller in size compared to the JDK.
C.It provides a debugger for troubleshooting code.
D.It is necessary to run any Java application.
E.It contains the JVM and core class libraries.
AnswersD, E

JRE provides the runtime required to execute Java programs.

Why this answer

Options D and E are correct. The JRE (Java Runtime Environment) is necessary to run any Java application (D) and it contains the JVM and core class libraries (E). Option A is incorrect because the Java compiler (javac) is part of the JDK, not the JRE.

Option B is incorrect because although the JRE is smaller than the JDK, this is a relative comparison and not a defining characteristic of the JRE itself. Option C is incorrect because the debugger is included in the JDK, not the JRE.

22
Multi-Selecthard

Which TWO statements correctly describe the Java language? (Choose two.)

Select 2 answers
A.Java supports multiple inheritance of implementation.
B.Java supports operator overloading.
C.Java supports object-oriented programming.
D.Java supports multiple inheritance of classes.
E.Java is a statically typed language.
AnswersC, E

Java is primarily object-oriented.

Why this answer

Java is fundamentally an object-oriented programming language that supports encapsulation, inheritance, and polymorphism. Java's design revolves around classes and objects, and all code must be written inside a class, making OOP a core principle of the language.

Exam trap

Oracle often tests the distinction between multiple inheritance of implementation (not supported) and multiple inheritance of type (supported via interfaces), and candidates mistakenly think Java supports operator overloading because of the + operator for strings.

23
MCQhard

A company is developing a Java-based inventory management system. The system runs on a single server and processes up to 1000 concurrent requests. The development team has implemented the code using multiple threads to handle requests. Recently, the system has been experiencing intermittent data corruption in the inventory counts. After reviewing the code, the team suspects that the issue is related to thread safety. The team is considering the following solutions: (A) Use the 'synchronized' keyword on all methods that update inventory counts. (B) Use 'volatile' keyword on the inventory count variables. (C) Use 'AtomicInteger' for inventory counts. (D) Increase the number of threads to handle requests faster. Which solution should the team implement to fix the data corruption issue with minimal performance impact?

A.Use 'AtomicInteger' for inventory counts.
B.Increase the number of threads to handle requests faster.
C.Use 'volatile' keyword on the inventory count variables.
D.Use the 'synchronized' keyword on all methods that update inventory counts.
AnswerA

AtomicInteger provides lock-free, thread-safe operations with good performance.

Why this answer

AtomicInteger provides thread-safe atomic operations (like incrementAndGet) without requiring synchronization, ensuring consistent inventory counts under concurrent access with minimal performance overhead compared to full method synchronization.

Exam trap

Oracle often tests the distinction between visibility (volatile) and atomicity (AtomicInteger), trapping candidates who think volatile alone solves read-modify-write race conditions.

How to eliminate wrong answers

Option B is wrong because increasing the number of threads does not fix thread safety issues; it can worsen data corruption by increasing race conditions. Option C is wrong because volatile only ensures visibility of changes across threads but does not provide atomicity for compound operations like read-modify-write (e.g., count++), which is the root cause of corruption. Option D is wrong because using synchronized on all methods that update inventory counts would fix the issue but introduces significant performance impact due to thread contention, making it less optimal than AtomicInteger.

24
Multi-Selectmedium

Which THREE are valid components of the Java Virtual Machine (JVM)?

Select 3 answers
A.Java source code (.java files)
B.Java compiler (javac)
C.Heap memory area
D.Execution engine
E.Class loader subsystem
AnswersC, D, E

Heap is where all objects are allocated.

Why this answer

The correct components of the JVM listed here are: Heap memory area (C), Execution engine (D), and Class loader subsystem (E). The Java compiler (B) is part of the JDK, not the JVM. Java source code (A) is input to the compiler, not a JVM component.

The class loader loads bytecode into the JVM, the heap stores objects, and the execution engine executes bytecode.

25
MCQmedium

A company is developing a security-sensitive banking application. Which Java feature most directly enhances security?

A.Automatic garbage collection
B.Platform independence via bytecode
C.Built-in multithreading support
D.Bytecode verification
AnswerD

Bytecode verification checks code for security violations before execution.

Why this answer

(Bytecode verification) is correct because it performs a series of checks on the bytecode, such as ensuring type safety, no stack overflows, and no illegal jumps, which prevents malicious code from compromising the JVM. Option A (garbage collection) manages memory automatically but does not directly enforce security. Option B (platform independence via bytecode) allows Java to run on any platform but does not inherently provide security.

Option C (built-in multithreading support) facilitates concurrent execution but is not a security feature.

26
MCQmedium

A company is upgrading from Java 8 to Java 11. Which advantage does the module system introduced in Java 9 provide?

A.Better multithreading
B.Faster garbage collection
C.Stronger encapsulation of internal APIs
D.Improved lambda syntax
AnswerC

The module system allows hiding internal packages from external access.

Why this answer

The module system (Project Jigsaw) enforces stronger encapsulation of internal APIs, preventing accidental access. Options A, B, and D are incorrect: multithreading improvements are not provided by the module system, garbage collection enhancements are unrelated, and lambda syntax was introduced in Java 8.

27
MCQmedium

A developer compiles a Java program successfully but gets 'ClassNotFoundException' when running it. What is the most likely cause?

A.The Java version is incompatible
B.The main method signature is incorrect
C.The program has multiple classes
D.The classpath does not include the directory containing the .class file
AnswerD

ClassNotFoundException is thrown when the classpath does not contain the class.

Why this answer

ClassNotFoundException occurs when the JVM cannot find the class definition, typically due to the classpath not including the location of the .class file. Option A is wrong because Java version incompatibility leads to UnsupportedClassVersionError, not ClassNotFoundException. Option B is wrong because an incorrect main method signature causes NoSuchMethodError.

Option C is wrong because having multiple classes does not cause ClassNotFoundException unless one of them is missing from the classpath.

28
MCQeasy

Given the output, which statement is true about this Java installation?

A.It includes a Just-In-Time (JIT) compiler.
B.It runs only in interpreted mode without JIT.
C.It is a debug build of the JVM.
D.It is a Java Micro Edition (Java ME) runtime.
AnswerA

HotSpot VM always includes JIT.

Why this answer

The output shows 'mixed mode', meaning the JVM uses both an interpreter and a Just-In-Time (JIT) compiler. In a standard Java SE installation, HotSpot VM includes a JIT compiler for performance optimization. Option B is incorrect because interpreted-only mode would require the -Xint flag.

Option C is incorrect because a debug build typically includes 'debug' in the version string. Option D is incorrect because Java ME is for embedded devices and would not show a standard HotSpot VM.

29
Drag & Dropmedium

Arrange the steps to compile and run a Java program from the command line in the correct order.

Drag steps to the numbered slots on the right, or tap a step then tap a slot.

Steps
Order
1Step 1
2Step 2
3Step 3
4Step 4

Why this order

The correct order is to first write the code, then open terminal, navigate to the file location, compile with javac, and run with java.

30
MCQeasy

A team is designing a Java application that needs to run on different operating systems without modification. Which Java feature makes this possible?

A.The Java Virtual Machine
B.Just-in-time compilation
C.Garbage collection
D.The Java compiler
AnswerA

JVM interprets bytecode on any platform, providing portability.

Why this answer

The Java Virtual Machine (JVM) is the key enabler of Java's 'write once, run anywhere' capability. When you compile Java source code, the Java compiler produces bytecode, which is platform-independent. This bytecode is then executed by the JVM, which is implemented specifically for each operating system (Windows, Linux, macOS, etc.), translating the bytecode into native machine instructions.

Therefore, the same compiled .class file can run on any OS that has a compatible JVM, without requiring any modifications to the application code.

Exam trap

Oracle often tests the misconception that the Java compiler or JIT compilation is responsible for platform independence, but the correct answer is always the JVM because it is the runtime environment that abstracts away the underlying operating system.

How to eliminate wrong answers

Option B is wrong because Just-in-time (JIT) compilation is an optimization technique used by the JVM to improve runtime performance by compiling bytecode into native machine code at runtime; it does not provide platform independence. Option C is wrong because Garbage collection is an automatic memory management feature that reclaims memory from objects no longer in use; it has no role in enabling cross-platform portability. Option D is wrong because The Java compiler (javac) translates Java source code into bytecode, but the bytecode itself is platform-independent only because the JVM interprets it; the compiler does not handle execution or OS-specific adaptation.

31
MCQeasy

What is the primary purpose of Java bytecode?

A.To be executed by the Java Virtual Machine on any platform
B.To be compiled into native code once and reused
C.To be human-readable source code
D.To be directly executed by the operating system
AnswerA

Bytecode is platform-independent and executed by JVM.

Why this answer

Java bytecode is designed to be executed by the Java Virtual Machine (JVM), which interprets or compiles it to native code for the underlying platform, enabling platform independence. Option B is incorrect because bytecode is an intermediate representation that is compiled or interpreted at runtime, not compiled once into native code for reuse across platforms. Option C is incorrect because bytecode is not human-readable source code; it is a binary format.

Option D is incorrect because bytecode cannot be directly executed by the operating system; it requires the JVM to run.

32
MCQhard

A development team is building a modular Java application using Java 17. They have defined a module named com.myapp with a module-info.java that includes 'requires com.thirdparty.lib;'. The com.thirdparty.lib module is a third-party library packaged as a modular JAR with its own module-info.class. The application compiles successfully using javac with the module path pointing to the directory containing the JAR. However, when starting the application with java --module-path <path> --module com.myapp, a NoClassDefFoundError occurs for a class from com.thirdparty.lib. The error message indicates the class is not found. The team has confirmed that the JAR file is present in the specified module path and that the class exists in the JAR. No other errors or warnings are displayed. The team is puzzled because the code compiles without issues. What is the most likely cause of this runtime error?

A.The module path order is incorrect, causing a different version of the library to be loaded.
B.The library is not compatible with Java 17.
C.The application's module requires the wrong version of the library.
D.The module com.thirdparty.lib does not export the package containing the required class in its module-info.java.
AnswerD

Even though the module is on the module path, its packages are not accessible without an 'exports' directive.

Why this answer

In modular Java, a module must explicitly export a package using the 'exports' directive in its module-info.java for that package to be accessible by other modules. If com.thirdparty.lib does not export the package containing the required class, the class is present in the module but not accessible at runtime, leading to a NoClassDefFoundError even though compilation succeeded. Option D is correct because the missing exports directive causes this issue.

Option A is incorrect because module path order affects module resolution, not accessibility of exported packages; if the module is resolved, the correct JAR is used. Option B is incorrect because if the library were incompatible with Java 17, compilation would typically fail or generate different errors (e.g., UnsupportedClassVersionError). Option C is incorrect because version requirements in 'requires' are optional and generally produce compile-time warnings/errors, not a runtime NoClassDefFoundError for a class that exists.

33
MCQeasy

An application throws 'java.lang.OutOfMemoryError: Java heap space'. Which JVM option can help generate diagnostic information to identify the cause?

A.-version
B.-verbose:gc
C.-XX:+HeapDumpOnOutOfMemoryError
D.-Xmx
AnswerC

This option produces a heap dump when OutOfMemoryError occurs, useful for analysis.

Why this answer

XX:+HeapDumpOnOutOfMemoryError instructs the JVM to generate a heap dump file when an OutOfMemoryError occurs, which can be analyzed to identify memory leaks or other causes. Option A (-version) prints the Java version and does not help with diagnostics. Option B (-verbose:gc) prints garbage collection details but does not produce a heap dump at the time of failure.

Option D (-Xmx) sets the maximum heap size, which may delay or prevent the error but does not provide diagnostic information about the cause.

34
MCQeasy

What is the primary role of the Java Development Kit (JDK) compared to the JRE?

A.JDK provides only a debugger and profiler.
B.JRE includes the JDK and additional libraries.
C.JDK is required to run Java applications, whereas JRE is not.
D.JDK includes compilers and tools for developing Java applications.
AnswerD

JDK contains javac, debugger, etc., which JRE lacks.

Why this answer

The JDK (Java Development Kit) is a software development environment used for developing Java applications. It includes the JRE (Java Runtime Environment) plus development tools such as the Java compiler (javac), debugger, and other utilities. In contrast, the JRE provides only the runtime environment needed to run Java applications.

Therefore, option D is correct because the JDK includes compilers and tools for development. Option A is incorrect because the JDK provides far more than just a debugger and profiler; it includes the compiler and other essential tools. Option B is incorrect because the JDK includes the JRE, not the other way around.

Option C is incorrect because the JRE is required to run Java applications, while the JDK is needed for development; the JDK is not required to run applications.

35
MCQhard

A developer writes a multi-threaded application that runs on Windows. To ensure the same bytecode runs without modification on Linux and macOS, which Java feature is essential?

A.Bytecode verification
B.Platform independence via JVM
C.Just-in-Time (JIT) compilation
D.Thread synchronization
AnswerB

The JVM abstracts the underlying OS, allowing the same bytecode to run anywhere.

Why this answer

Platform independence via JVM. The Java Virtual Machine (JVM) abstracts the underlying operating system and hardware, so bytecode compiled on any platform can run on any other platform that has a compatible JVM. This allows the same bytecode to run on Windows, Linux, and macOS without modification.

Option A (Bytecode verification) is a security check that validates bytecode before execution, but it does not provide cross-platform portability. Option C (Just-in-Time compilation) optimizes performance by compiling bytecode to native code at runtime, but it is not essential for platform independence. Option D (Thread synchronization) is a concurrency control mechanism that prevents race conditions, but it does not affect cross-platform execution.

36
MCQhard

A company wants to run existing Java SE application code on an embedded device with limited resources. Which Java edition is designed for such environments?

A.Java Card
B.Java FX
C.Java EE (Enterprise Edition)
D.Java ME (Micro Edition)
AnswerD

Java ME is tailored for embedded and mobile devices.

Why this answer

Java ME (Micro Edition) is specifically designed for embedded devices and mobile devices with constrained resources. Option A (Java Card) is for smart cards and very small devices like SIM cards. Option B (Java FX) is a UI framework for rich client applications, not an edition of Java.

Option C (Java EE) is for enterprise server applications.

37
MCQmedium

During execution, the JVM uses Just-In-Time (JIT) compilation. What is its primary benefit?

A.Translates bytecode into an intermediate language for interpretation.
B.Improves execution speed by compiling frequently used bytecode to native code.
C.Converts Java source code directly into bytecode.
D.Enhances security by verifying bytecode integrity.
AnswerB

JIT identifies hot spots and compiles them for faster execution.

Why this answer

JIT compilation improves execution speed by compiling frequently used bytecode into native machine code at runtime. Option A is incorrect because JIT does not translate bytecode into an intermediate language; it compiles to native code. Option C is incorrect because JIT compiles bytecode, not source code.

Option D is incorrect because JIT's primary purpose is performance, not security.

38
MCQmedium

A developer is writing a Java application that processes a large number of transactions. The application must ensure that each transaction is committed only if all steps complete successfully, otherwise the entire transaction should be rolled back. Which Java concept should the developer use to implement this requirement?

A.Exception handling
B.Inheritance
C.Multithreading
D.Encapsulation
AnswerA

Exception handling can catch failures and trigger rollback.

Why this answer

Exception handling in Java allows the developer to catch runtime failures (e.g., SQLException, IOException) within a try block and, in the catch block, invoke a rollback on the transaction (e.g., Connection.rollback()). If all steps succeed, the transaction is committed via Connection.commit(). This ensures atomicity — the 'all-or-nothing' property required for transaction processing.

Exam trap

Oracle often tests whether candidates confuse 'transaction management' with 'multithreading' — the trap here is assuming that concurrent execution (Option C) is needed for atomicity, when in fact atomicity is enforced by exception handling and explicit commit/rollback calls, not by running steps in parallel.

How to eliminate wrong answers

Option B is wrong because inheritance is a mechanism for code reuse and establishing type hierarchies (e.g., extends), not for controlling transactional commit/rollback behavior. Option C is wrong because multithreading deals with concurrent execution of tasks (e.g., using Thread or Runnable), not with ensuring atomicity of a single transaction's steps. Option D is wrong because encapsulation hides internal state and exposes methods via access modifiers (e.g., private fields with public getters/setters), which does not provide any mechanism for conditional commit or rollback.

39
MCQmedium

A developer runs the command shown in the exhibit. The developer wants to ensure the application uses the latest available language features. Which action should the developer take?

A.Download and install a newer version of the JDK.
B.Enable lambda expressions by setting the -enable-lambdas flag.
C.Use the -source and -target flags to compile for a newer version.
D.Upgrade the JVM to the latest version.
AnswerA

A newer JDK includes both compiler and runtime with latest features.

Why this answer

The latest available language features (e.g., pattern matching, sealed classes, records) are tied to the JDK version. Downloading and installing a newer JDK provides both the compiler (javac) and runtime (JVM) that support those features. Simply upgrading the JVM (Option D) or using -source/-target flags (Option C) does not enable new language syntax in the compiler if the JDK itself is outdated.

Exam trap

The trap here is that candidates confuse upgrading the JVM (runtime) with upgrading the JDK (development kit), or think that compiler flags like -source and -target can retroactively add new language features to an older JDK.

How to eliminate wrong answers

Option B is wrong because there is no -enable-lambdas flag in Java; lambda expressions were introduced in Java 8 and are enabled by default when using a JDK 8 or later. Option C is wrong because the -source and -target flags only control the version of source code accepted and the class file format produced, but they do not add new language features to an older JDK; you need a newer JDK to compile with newer syntax. Option D is wrong because upgrading only the JVM (runtime) does not give the compiler access to new language features; the JDK (which includes javac) must also be updated.

Ready to test yourself?

Try a timed practice session using only What is Java questions.