Java Introduction and JDK Setup Interview Questions: Compile, Run and Debug

Explain what happens between a Java source file and its output. Practise terminal setup checks, trace a two-number program and repair package, classpath and version errors.

KnowledgeGate Team

Exam prep & CS education

Updated 5 Dec 20265 min read
Java Introduction and JDK Setup Interview Questions: Compile, Run and Debug

Your Java program runs in the IDE, but the terminal cannot find the compiler or load your class. As a fresher, you need to explain which stage failed before changing the code. Explain the tools, trace a two-number program and diagnose setup failures aloud without relying on the IDE's Run button.

Explain JDK, runtime and JVM without memorising a slogan

What do you install to write Java? Install a Java Development Kit (JDK). It supplies development tools, including the javac compiler, along with the runtime needed to execute Java applications.

You write source in a .java file. Compilation produces .class files containing bytecode; the Java Virtual Machine (JVM) executes that code. Runtime libraries provide facilities such as strings and console output. The term JRE describes the runtime concept, including the JVM and libraries. From JDK 11 onward, Oracle offers no standalone JRE; install the JDK, which contains the runtime.

The JDK components and JAR walkthrough covers each tool and executable JAR packaging; the command-line examples here trace argument parsing and classpath failures.

Is Java platform independent? Yes. Java bytecode runs on any platform with a JVM. The JVM and JDK are the platform-specific parts.

A clear spoken answer is: "javac compiles source; java launches the application on the runtime." This distinction supports your wider IT placement and coding preparation.

Verify the terminal setup before opening the IDE

Use Java 21 without preview features as a reproducible baseline.Java 21, without preview features, as a reproducible baseline.

shell
java -version
javac -version

Both commands should report major version 21, confirming the tools selected by this terminal.

PATH controls executable lookup. JAVA_HOME identifies a Java installation for development tools that use it. Changing JAVA_HOME alone does not guarantee that your shell selects a different java executable.

On Windows, inspect lookup with:

shell
where.exe java
where.exe javac

On macOS or Linux, use:

shell
command -v java
command -v javac

After configuration changes, reopen the terminal and repeat the version checks. If java works but javac does not, check compiler availability and PATH before editing source. The step-by-step JDK setup guide covers installation steps; the command-line examples focus on compiler/runtime boundaries, classpath and failure diagnosis.

Trace one complete compile-and-run interview problem

From two command-line integers, print their sum. What executes at each stage? In an otherwise empty working directory, save this as SumArgs.java:

java
public class SumArgs {
    public static void main(String[] args) {
        if (args.length != 2) {
            System.out.println("Usage: java SumArgs <a> <b>");
            return;
        }
        int a = Integer.parseInt(args[0]);
        int b = Integer.parseInt(args[1]);
        System.out.println(a + b);
    }
}

Compile, then launch:

shell
javac SumArgs.java
java -cp . SumArgs 7 5

Compilation creates SumArgs.class. The launch command searches the current directory, selected by -cp .. Use the case-sensitive class name SumArgs, without a .class suffix.

Trace the execution:

  1. args.length is 2, so the usage branch is skipped.

  2. args[0] is "7" and args[1] is "5".

  3. Parsing gives integers a = 7 and b = 5.

  4. a + b = 7 + 5 = 12, so the program prints 12.

The launcher passes strings; the program performs the addition. For the standard main entry point, public makes the method accessible, static means no instance is needed, void means it returns no value, and String[] holds the arguments.

Diagram showing SumArgs.java compiled by javac into SumArgs.class, then run with java -cp . SumArgs 7 5; the JVM receives two arguments and prints 12.

Try java -cp . SumArgs -3 8: -3 + 8 = 5. With no arguments, the program prints the usage line and returns. With seven 5, parsing throws NumberFormatException. Replacing the final expression with args[0] + args[1] prints 75 for 7 5, because string operands concatenate.

Repair a package and classpath problem with exact commands

Create a separate exercise directory. Save the same program at src/demo/SumArgs.java, adding package demo; as its first line. Change its usage string to Usage: java demo.SumArgs <a> <b>.

From that exercise directory, run:

shell
javac -d out src/demo/SumArgs.java
java -cp out demo.SumArgs 7 5

Oracle's Java SE 21 documentation for the javac command explains that -d selects the output directory and package names determine subdirectories. The artefact is out/demo/SumArgs.class; the output is 12.

The command java -cp out SumArgs 7 5 fails because the requested unnamed-package class is absent. The classpath root is out, not out/demo; the binary class name is demo.SumArgs. Correct it to java -cp out demo.SumArgs 7 5.

Directory diagram showing src/demo/SumArgs.java compiled to out/demo/SumArgs.class; out is the classpath root and demo.SumArgs runs with 7 and 5 to print 12.

Diagnose errors by the stage that failed

Identify the failing stage before choosing a repair.

Symptom

Reason

Repair

javac not found

Shell cannot locate compiler

Inspect JDK installation and PATH

Public SumArgs in Main.java

Compiler rejects filename mismatch

Rename to SumArgs.java

Missing semicolon

Source syntax error

Fix source and recompile

Main class not found at launch

Wrong lookup location or name

Check directory, classpath and binary name

Code compiled for Java 21 fails on Java 17 with UnsupportedClassVersionError. Run it on Java 21, or target Java 17 with javac --release 17 SumArgs.java. Release targeting restricts APIs and language features to the selected version.

For the stale-output trap, return to the original exercise and change a + b to a * b, but skip recompilation. Running the existing class with 7 5 still prints 12. Recompile successfully and rerun: 7 * 5 = 35. This is stale compiled code, distinct from a parsing exception during execution.

Practise the interview follow-ups and explain the answers

Does javac run my main method? No. It compiles this source into a class file.

What does -cp out select? The application classpath root for this packaged exercise, from which demo/SumArgs.class is located.

Can Java launch source directly? Yes. Oracle's Java SE 21 documentation for the java command describes source-file mode. Return to the original unpackaged program and restore a + b. Now java SumArgs.java 7 5 prints 12. Compilation still happens internally.

Before executing anything, explain the outcomes for 7 5, no arguments and seven 5: sum, usage message and parsing exception. Name the condition or operation responsible for each. This builds output prediction and debugging skills.

Once you can explain the compile/run boundary, continue with Java collections, exceptions and JVM questions.

Short version: prove the setup with one independent run

Check four things: intended compiler/runtime versions, matching source filename, successful fresh compilation, and correct classpath/binary name.

Compile the restored addition program again, then run java -cp . SumArgs 19 23. The calculation is 19 + 23 = 19 + 20 + 3 = 42, so the output must be 42.

For structured Java study after these exercises, inspect the Complete Java course.