Java Basics & JVM Fundamentals Quiz

Java
0 Passed
0% acceptance

50 in-depth questions (30 with code) covering Java platform independence, JVM architecture, compilation, bytecode, class loading, memory areas, garbage collection, and your first real Java program.

50 Questions
~100 minutes
1

Question 1

What is the foundation of Java's 'Write Once, Run Anywhere' principle?

A
Compilation to platform-independent bytecode executed by the JVM
B
Direct compilation to native code for each OS
C
Source code interpretation by the operating system
D
Java runs only on Oracle hardware
2

Question 2

Which component contains the javac compiler?

A
JDK (Java Development Kit)
B
JRE only
C
JVM
D
java command
3

Question 3

What does this command actually do?

shell
javac -d build src/com/company/app/*.java
A
Compiles all .java files and places .class files in build/ preserving package structure
B
Runs the program
C
Creates an executable JAR
D
Only checks syntax without generating bytecode
4

Question 4

What is the complete output of this classic first program?

java
public class Welcome {
    public static void main(String[] args) {
        String lang = "Java";
        int year = 1995;
        System.out.printf("%s was released in %d!%n", lang, year);
        System.out.println("Still going strong in 2025!");
    }
}
A
Java was released in 1995! Still going strong in 2025!
B
Java was released in 1995!Still going strong in 2025!
C
Compilation error
D
No output
5

Question 5

What file extension contains executable Java bytecode?

A
.class
B
.java
C
.jar
D
.exe
6

Question 6

What will be printed by this program demonstrating polymorphism?

java
class Vehicle {
    String move() { return "moving"; }
}
class Car extends Vehicle {
    String move() { return "driving on road"; }
}
public class Test {
    public static void main(String[] args) {
        Vehicle v = new Car();
        System.out.println(v.move());
    }
}
A
driving on road
B
moving
C
Compilation error
D
Runtime exception
7

Question 7

Which command correctly executes a class inside a package?

shell
java com.example.demo.App
A
Uses the fully qualified class name from the classpath root
B
Requires the .class extension
C
Only works inside the package directory
D
Automatically recompiles source
8

Question 8

What is printed by this StringBuilder example?

java
public class BuilderTest {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        sb.append("Java ");
        sb.append(17);
        sb.insert(5, "SE ");
        sb.delete(8, 10);
        System.out.println(sb);
    }
}
A
Java SE 17
B
Java 17
C
Java SE 7
D
Exception
9

Question 9

What is the exact role of the JVM?

A
Load, verify, and execute bytecode while managing memory and threads
B
Compile .java to .class
C
Edit source code
D
Create JAR files
10

Question 10

What output do you expect from this array and loop combination?

java
public class ArrayDemo {
    public static void main(String[] args) {
        int[] primes = {2, 3, 5, 7, 11, 13, 17, 19};
        for (int i = 0; i < primes.length; i += 2) {
            System.out.print(primes[i] + " ");
        }
        System.out.println("\nTotal: " + primes.length + " primes");
    }
}
A
2 5 11 17 Total: 8 primes
B
2 3 5 7 11 13 17 19 Total: 8 primes
C
IndexOutOfBoundsException
D
No output
11

Question 11

Where are object instances stored in the JVM?

A
Heap memory
B
Java Stack
C
Method Area
D
PC Register
12

Question 12

What is the result of this multi-file program?

java
// File: Greeting.java
public class Greeting {
    private String message;
    public Greeting(String msg) { this.message = msg; }
    public void print() { System.out.println(message); }
}

// File: Main.java
public class Main {
    public static void main(String[] args) {
        new Greeting("Hello from multi-file Java!").print();
    }
}
A
Hello from multi-file Java!
B
Compilation error
C
No output
D
NullPointerException
13

Question 13

What does this enhanced for-loop output?

java
public class LanguageLoop {
    public static void main(String[] args) {
        String[] jvmLangs = {"Java", "Kotlin", "Scala", "Groovy", "Clojure"};
        for (String lang : jvmLangs) {
            if (lang.length() >= 5) {
                System.out.println(lang + " (" + lang.length() + ")");
            }
        }
    }
}
A
Kotlin (6) Scala (5) Groovy (6) Clojure (7)
B
Java (4) Kotlin (6) Scala (5) Groovy (6) Clojure (7)
C
Only Java
D
Compilation error
14

Question 14

Which memory area stores static variables?

A
Metaspace (JDK 8+) or PermGen (older)
B
Heap
C
Stack
D
Native Method Stack
15

Question 15

What is printed by this nested loop pattern?

java
public class Pattern {
    public static void main(String[] args) {
        for (int i = 1; i <= 4; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}
A
* * * * * * * * * *
B
Four lines of four stars each
C
Compilation error
D
Infinite loop
16

Question 16

What is garbage collection?

A
Automatic memory reclamation of unreachable objects
B
Manual memory freeing like C's free()
C
Bytecode optimization
D
Class loading mechanism
17

Question 17

What happens when you run this program with command-line arguments?

java
public class ArgsDemo {
    public static void main(String[] args) {
        System.out.println("Arguments count: " + args.length);
        for (int i = 0; i < args.length; i++) {
            System.out.println("arg[" + i + "] = " + args[i]);
        }
    }
}
A
Prints number of arguments and their values
B
Always prints 0
C
Compilation error
D
NullPointerException
18

Question 18

What will this program output when compiled and run?

java
public class VarDemo {
    public static void main(String[] args) {
        var number = 42;
        var text = "The answer";
        var result = text + " is " + number;
        System.out.println(result);
    }
}
A
The answer is 42
B
Compilation error before Java 10
C
Runtime exception
D
var is not allowed in main
19

Question 19

Which part of the JVM verifies bytecode safety?

A
The Bytecode Verifier
B
JIT Compiler
C
Garbage Collector
D
Class Loader only
20

Question 20

What is the output of this switch expression (Java 14+)?

java
public class SwitchDemo {
    public static void main(String[] args) {
        int day = 3;
        String type = switch (day) {
            case 1, 2, 3, 4, 5 -> "Weekday";
            case 6, 7 -> "Weekend";
            default -> "Invalid";
        };
        System.out.println(type);
    }
}
A
Weekday
B
Weekend
C
Compilation error in older Java
D
Invalid
21

Question 21

Why does Java require the main method to be public static void?

A
So the JVM can invoke it without creating an instance
B
It's optional in modern Java
C
Only for backward compatibility
D
To allow multiple mains
22

Question 22

What is printed by this recursive countdown?

java
public class Countdown {
    static void count(int n) {
        if (n <= 0) {
            System.out.println("Blast off!");
            return;
        }
        System.out.print(n + "...");
        count(n - 1);
    }
    public static void main(String[] args) {
        count(5);
    }
}
A
5...4...3...2...1...Blast off!
B
Blast off!...1...2...3...4...5
C
StackOverflowError
D
No output
23

Question 23

What makes bytecode truly platform-independent?

A
It's a standardized instruction set defined by the Java Virtual Machine Specification
B
It's encrypted
C
It's compiled to machine code at compile time
D
It's human-readable
24

Question 24

What is the result of this String immutability demo?

java
public class ImmutableDemo {
    public static void main(String[] args) {
        String a = "hello";
        String b = a;
        a = a + " world";
        System.out.println("a = " + a);
        System.out.println("b = " + b);
    }
}
A
a = hello world b = hello
B
a = hello b = hello world
C
Both show hello world
D
NullPointerException
25

Question 25

What happens during the linking phase of class loading?

A
Verification, preparation (allocate statics), and resolution of symbolic references
B
Reading .class file from disk
C
Executing static initializers
D
Running the main method
26

Question 26

What is printed by this labeled break example?

java
public class LabeledBreak {
    public static void main(String[] args) {
        outer: for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 3; j++) {
                if (i == 2 && j == 2) {
                    break outer;
                }
                System.out.print("(" + i + "," + j + ") ");
            }
        }
        System.out.println("Done");
    }
}
A
(1,1) (1,2) (1,3) (2,1) Done
B
All 9 combinations then Done
C
Only (1,1) then Done
D
Compilation error
27

Question 27

What is the role of the JIT compiler?

A
Compile hot bytecode to native machine code at runtime for speed
B
Compile .java to .class
C
Verify bytecode safety
D
Manage garbage collection
28

Question 28

What is the output of this record (Java 16+) example?

java
public record Point(int x, int y) {
    public static void main(String[] args) {
        var p1 = new Point(10, 20);
        var p2 = new Point(10, 20);
        System.out.println(p1);
        System.out.println(p1.equals(p2));
    }
}
A
Point[x=10, y=20] true
B
Different hash codes false
C
Compilation error before Java 16
D
NullPointerException
29

Question 29

Why can you not instantiate an abstract class?

A
The JVM prevents it — abstract classes may have abstract methods with no implementation
B
It's optional in Java
C
Only interfaces cannot be instantiated
D
Only in older Java versions
30

Question 30

What is the final output of this complex first program?

java
public class EverythingDemo {
    static String version = "Java 21";
    
    public static void main(String[] args) {
        var builder = new StringBuilder();
        builder.append("Welcome to ").append(version).append("!");
        
        for (int i = 0; i < 3; i++) {
            builder.append(" ").append(i * 7);
        }
        
        System.out.println(builder.reverse());
        System.out.println("Length: " + builder.length());
    }
}
A
!12 avaJ ot emocleW 14 7 0 Length: 22
B
Welcome to Java 21! 0 7 14 Length: 24
C
Compilation error
D
Different length
31

Question 31

What is the classpath?

A
A list of locations the JVM searches for .class files and JARs
B
The location of javac.exe
C
Only the current directory
D
A Java keyword
32

Question 32

What is printed by this method-local inner class?

java
public class InnerDemo {
    public static void main(String[] args) {
        class Helper {
            String greet() { return "Hello from inner class!"; }
        }
        Helper h = new Helper();
        System.out.println(h.greet());
    }
}
A
Hello from inner class!
B
Compilation error
C
Only anonymous classes allowed
D
No output
33

Question 33

What does the JVM do when it runs out of heap memory?

A
Throws OutOfMemoryError
B
Automatically expands infinitely
C
Restarts the program
D
Switches to disk
34

Question 34

What is the output of this text block (Java 15+)?

java
public class TextBlockDemo {
    public static void main(String[] args) {
        String json = """
            {
                "name": "Java",
                "year": 1995
            }
            """;
        System.out.println(json.trim());
    }
}
A
{ "name": "Java", "year": 1995 }
B
Compilation error before Java 15
C
Empty string
D
With extra spaces
35

Question 35

What is the bootstrap class loader responsible for?

A
Loading core Java classes (java.lang.*, java.util.*)
B
Loading application classes
C
Loading third-party libraries
D
Loading user-written classes only
36

Question 36

What is printed by this final variable demo?

java
public class FinalDemo {
    public static void main(String[] args) {
        final int MAX = 100;
        final var list = java.util.List.of(1, 2, 3);
        // list.add(4); // Would not compile
        System.out.println("Max: " + MAX + ", List size: " + list.size());
    }
}
A
Max: 100, List size: 3
B
Compilation error
C
Runtime exception
D
Null output
37

Question 37

What is the PC Register in the JVM?

A
Stores the address of the current executing instruction per thread
B
Stores program code
C
Holds object references
D
Manages garbage collection
38

Question 38

What is the output of this sealed class preview?

java
sealed interface Vehicle permits Car, Bike {}
non-sealed class Car implements Vehicle {}
final class Bike implements Vehicle {}
public class SealedDemo {
    public static void main(String[] args) {
        Vehicle v = new Car();
        System.out.println(v.getClass().getSimpleName());
    }
}
A
Car
B
Compilation error before Java 17
C
Vehicle
D
Anonymous
39

Question 39

What does the JVM do first when you run java MyApp?

A
Loads the specified class using the bootstrap class loader and starts execution from main
B
Compiles MyApp.java
C
Runs garbage collection
D
Waits for user input
40

Question 40

What is the final output of this complete beginner program?

java
public class UltimateHello {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
        System.out.println("Java runs on " + Runtime.getRuntime().availableProcessors() + " CPU cores");
        System.out.println("Max memory: " + (Runtime.getRuntime().maxMemory() / 1024 / 1024) + " MB");
    }
}
A
Prints greeting plus real system information
B
Compilation error
C
Only 'Hello, World!'
D
SecurityException
41

Question 41

Why is Java considered safe?

A
Bytecode verification, security manager, no pointer arithmetic, automatic memory management
B
It runs in a sandbox only in browsers
C
All code is open source
D
It prevents all bugs
42

Question 42

What is printed by this enum example?

java
enum Level { LOW, MEDIUM, HIGH }
public class EnumDemo {
    public static void main(String[] args) {
        Level l = Level.HIGH;
        switch (l) {
            case LOW -> System.out.println("Easy");
            case MEDIUM -> System.out.println("Normal");
            case HIGH -> System.out.println("Hard");
        }
    }
}
A
Hard
B
HIGH
C
Compilation error
D
No output
43

Question 43

What is the native method stack used for?

A
Executing native (C/C++) code called via JNI
B
Storing Java bytecode
C
Garbage collection
D
Class loading
44

Question 44

What is the output of this pattern matching preview?

java
public class PatternMatch {
    public static void main(String[] args) {
        Object obj = "Hello";
        if (obj instanceof String s && s.length() > 3) {
            System.out.println(s.toUpperCase() + "!");
        }
    }
}
A
HELLO!
B
Compilation error before Java 16
C
ClassCastException
D
No output
45

Question 45

What is the execution engine composed of?

A
Interpreter and JIT compiler(s)
B
Only interpreter
C
Only AOT compiler
D
Garbage collector
46

Question 46

What is printed by this virtual thread preview (Java 21+)?

java
public class VirtualThreads {
    public static void main(String[] args) throws Exception {
        try (var executor = java.util.concurrent.Executors.newVirtualThreadPerTaskExecutor()) {
            for (int i = 0; i < 10; i++) {
                executor.submit(() -> System.out.print(Thread.currentThread() + " "));
            }
        }
        System.out.println("Done");
    }
}
A
Prints 10 virtual threads then Done
B
Compilation error before Java 21
C
Only one thread
D
Deadlock
47

Question 47

Why has Java survived for nearly 30 years?

A
Backward compatibility, vast ecosystem, enterprise stability, and continuous evolution
B
It never changes
C
Only used in Android
D
Fastest language available
48

Question 48

What is the output of this final comprehensive program?

java
public class JavaJourney {
    public static void main(String[] args) {
        System.out.println("Java: Born 1995, Still #1 in 2025");
        var jvm = Runtime.getRuntime();
        System.out.printf("Running on %,d cores with %,d MB max heap%n",
            jvm.availableProcessors(),
            jvm.maxMemory() / 1024 / 1024);
        System.out.println("Write Once, Run Anywhere — proven for 30 years!");
    }
}
A
Prints system-specific performance info with the message
B
Compilation error
C
Only the first line
D
Security exception
49

Question 49

What is the single most important thing to remember about Java?

A
Everything happens inside the JVM — your code never runs on bare metal directly
B
Java is interpreted only
C
You must manage memory manually
D
Java only runs on Windows
50

Question 50

Why does this simple program represent the entire Java platform?

java
public class TheEssence {
    public static void main(String[] args) {
        System.out.println("Hello from the JVM!");
        // This single line triggered:
        // • Class loading
        // • Bytecode verification
        // • Memory allocation
        // • JIT compilation
        // • Garbage collection readiness
        // • And 30 years of engineering excellence
    }
}
A
Even the smallest program uses the full power and safety of the Java Virtual Machine
B
It's the only program that works
C
It bypasses the JVM
D
It proves Java is slow

QUIZZES IN Java