Java Basics & JVM Fundamentals Quiz
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.
Question 1
What is the foundation of Java's 'Write Once, Run Anywhere' principle?
Question 2
Which component contains the javac compiler?
Question 3
What does this command actually do?
javac -d build src/com/company/app/*.javaQuestion 4
What is the complete output of this classic first program?
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!");
}
}Question 5
What file extension contains executable Java bytecode?
Question 6
What will be printed by this program demonstrating polymorphism?
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());
}
}Question 7
Which command correctly executes a class inside a package?
java com.example.demo.AppQuestion 8
What is printed by this StringBuilder example?
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);
}
}Question 9
What is the exact role of the JVM?
Question 10
What output do you expect from this array and loop combination?
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");
}
}Question 11
Where are object instances stored in the JVM?
Question 12
What is the result of this multi-file program?
// 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();
}
}Question 13
What does this enhanced for-loop output?
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() + ")");
}
}
}
}Question 14
Which memory area stores static variables?
Question 15
What is printed by this nested loop pattern?
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();
}
}
}Question 16
What is garbage collection?
Question 17
What happens when you run this program with command-line arguments?
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]);
}
}
}Question 18
What will this program output when compiled and run?
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);
}
}Question 19
Which part of the JVM verifies bytecode safety?
Question 20
What is the output of this switch expression (Java 14+)?
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);
}
}Question 21
Why does Java require the main method to be public static void?
Question 22
What is printed by this recursive countdown?
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);
}
}Question 23
What makes bytecode truly platform-independent?
Question 24
What is the result of this String immutability demo?
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);
}
}Question 25
What happens during the linking phase of class loading?
Question 26
What is printed by this labeled break example?
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");
}
}Question 27
What is the role of the JIT compiler?
Question 28
What is the output of this record (Java 16+) example?
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));
}
}Question 29
Why can you not instantiate an abstract class?
Question 30
What is the final output of this complex first program?
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());
}
}Question 31
What is the classpath?
Question 32
What is printed by this method-local inner class?
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());
}
}Question 33
What does the JVM do when it runs out of heap memory?
Question 34
What is the output of this text block (Java 15+)?
public class TextBlockDemo {
public static void main(String[] args) {
String json = """
{
"name": "Java",
"year": 1995
}
""";
System.out.println(json.trim());
}
}Question 35
What is the bootstrap class loader responsible for?
Question 36
What is printed by this final variable demo?
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());
}
}Question 37
What is the PC Register in the JVM?
Question 38
What is the output of this sealed class preview?
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());
}
}Question 39
What does the JVM do first when you run java MyApp?
Question 40
What is the final output of this complete beginner program?
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");
}
}Question 41
Why is Java considered safe?
Question 42
What is printed by this enum example?
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");
}
}
}Question 43
What is the native method stack used for?
Question 44
What is the output of this pattern matching preview?
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() + "!");
}
}
}Question 45
What is the execution engine composed of?
Question 46
What is printed by this virtual thread preview (Java 21+)?
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");
}
}Question 47
Why has Java survived for nearly 30 years?
Question 48
What is the output of this final comprehensive program?
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!");
}
}Question 49
What is the single most important thing to remember about Java?
Question 50
Why does this simple program represent the entire Java platform?
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
}
}