☕ Java MCQ Questions – Page 51

Questions 1001–1020 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1001 easy code error
What is the compilation error in the following Java code snippet?
java
import java.util.ArrayList;
import java.util.List;

public class Question3 {
    public static void main(String[] args) {
        List<String> myNames = new List<>();
        myNames.add("Bob");
        System.out.println(myNames);
    }
}
Q1002 easy code error
What compile-time error does the following Java code exhibit?
java
class SimpleOperations {
    public void perform(String operationName) {
        System.out.println("Performing " + operationName);
    }

    public void perform(String operationName) {
        System.out.println("Executing " + operationName);
    }
}
Q1003 hard code error
What is the compilation error in the following Java code snippet?
java
import java.util.function.BiFunction;

class Calculator {
    public static int add(int a, int b) { return a + b; }
    public static long add(long a, long b) { return a + b; }
}

public class Main {
    public static void main(String[] args) {
        BiFunction<Integer, Integer, Integer> adder = Calculator::add;
    }
}
Q1004 hard
How do `sealed` classes and interfaces (introduced in Java 17) enhance or modify the concept of abstraction in Java?
Q1005 hard code error
What is the compile-time error in the `if` statement?
java
class Gadget {}
class Device {}
public class TypeChecker {
    public static void main(String[] args) {
        Gadget g = new Gadget();
        if (g instanceof Device) { // Line 6
            System.out.println("g is a Device");
        }
    }
}
Q1006 easy code error
What kind of error will occur when compiling or running the following Java code snippet?
java
import java.io.File;

public class FileError10 {
    public static void main(String[] args) {
        File root = new File("/"); // Represents the root directory on Unix-like systems
        File parentOfRoot = root.getParentFile();
        System.out.println(parentOfRoot.getName());
    }
}
Q1007 medium code error
What is the compilation error in the given Java code snippet?
java
public class LogicalOperatorError {
    public static void main(String[] args) {
        int value = 5;
        if (value && true) {
            System.out.println("This won't compile");
        }
    }
}
Q1008 hard code output
What is the output of this code?
java
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;

public class Test {
    public static void main(String[] args) {
        StringWriter sw = new StringWriter();
        BufferedWriter bw = new BufferedWriter(sw);
        try {
            bw.write("First");
            bw.close();
            bw.write("Second");
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        } finally {
            System.out.println("Content: " + sw.toString());
        }
    }
}
Q1009 easy
Which `StringBuilder` method can be used to remove a sequence of characters from the builder?
Q1010 medium code error
What kind of exception will this Java code throw during execution?
java
import java.util.Comparator;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        Comparator<Object> comp = (o1, o2) -> ((String) o1).compareTo((String) o2);
        TreeMap<Object, String> map = new TreeMap<>(comp);
        map.put("hello", "World");
        map.put(123, "Number");
    }
}
Q1011 medium code error
What compile-time error will occur in this Java code snippet?
java
public class LoopError {
    public static void main(String[] args) {
        final int x = 0;
        for (int i = 0; i < 2; i++) {
            x = i;
            System.out.println(x);
        }
    }
}
Q1012 medium
When a static method is declared `synchronized`, which lock is acquired?
Q1013 easy code error
What kind of error will occur when compiling this Java code?
java
public class Main {
    public static void main(String[] args) {
        int x = 5;
        if (x = 10) {
            System.out.println("X is 10");
        }
    }
}
Q1014 hard code output
What is the output of this code? (Assuming Java 14+)
java
public class SwitchTest {
    enum Day { MONDAY, TUESDAY, WEDNESDAY }
    public static void main(String[] args) {
        Day today = Day.MONDAY;
        String type = switch (today) {
            case MONDAY -> "Start";
            case TUESDAY -> "Mid";
            // Missing WEDNESDAY case and no default
        };
        System.out.println(type);
    }
}
Q1015 medium
Which operation is generally more efficient for `LinkedList` compared to `ArrayList` in Java, assuming an unsorted list?
Q1016 hard code error
What kind of error will this Java code produce when compiled?
java
final class Settings {
    private final String theme;

    public Settings(String theme) {
        this.theme = theme;
    }

    public void setTheme(String newTheme) {
        this.theme = newTheme; // Attempt to reassign a final field
    }
}

public class ImmutableViolation {
    public static void main(String[] args) {
        Settings appSettings = new Settings("Dark");
        appSettings.setTheme("Light");
    }
}
Q1017 hard code output
What is the likely output of this code?
java
public class SimpleDeadlock {
    private static final Object lock1 = new Object();
    private static final Object lock2 = new Object();

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            synchronized (lock1) {
                System.out.println("T1: Acquired lock1");
                try { Thread.sleep(50); } catch (InterruptedException e) {}
                synchronized (lock2) {
                    System.out.println("T1: Acquired lock2");
                }
            }
        }).start();

        new Thread(() -> {
            synchronized (lock2) {
                System.out.println("T2: Acquired lock2");
                try { Thread.sleep(50); } catch (InterruptedException e) {}
                synchronized (lock1) {
                    System.out.println("T2: Acquired lock1");
                }
            }
        }).start();
        Thread.sleep(200);
        System.out.println("Main: Done waiting.");
    }
}
Q1018 easy code error
What is the error in the following Java code?
java
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        TreeSet<String> names = new TreeSet<Integer>();
        names.add("Charlie");
    }
}
Q1019 hard code error
What will be the result of compiling this Java code?
java
interface MyInterface {
    void doSomething();
}
public class Anomaly {
    public static void main(String[] args) {
        MyInterface mi = new MyInterface() {
            public static int count = 0; // Problematic line
            @Override
            public void doSomething() {
                System.out.println("Doing something");
            }
        };
        mi.doSomething();
    }
}
Q1020 medium
When using `wait()` and `notify()` methods for inter-thread communication, what is a crucial requirement for the calling thread?
← Prev 4950515253 Next → Page 51 of 200 · 3994 questions