☕ Java MCQ Questions – Page 120

Questions 2381–2400 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2381 easy code output
What is the output of this code?
java
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<Integer, String> numbers = new HashMap<>();
        numbers.put(1, "One");
        numbers.put(2, "Two");
        numbers.put(1, "Uno"); // Overwrites
        System.out.println(numbers.size());
    }
}
Q2382 easy code error
What is the output or error of the following Java code?
java
class MyClass {
    void riskyMethod() throws java.io.IOException {
        throw new java.io.IOException("File error");
    }

    public static void main(String[] args) {
        MyClass obj = new MyClass();
        obj.riskyMethod();
    }
}
Q2383 hard code output
What is the output of this code?
java
import java.util.TreeSet;

public class App {
    static class SimpleData implements Comparable<SimpleData> {
        int id; String name;
        public SimpleData(int id, String name) { this.id = id; this.name = name; }
        @Override public int compareTo(SimpleData o) { return Integer.compare(this.id, o.id); }
        @Override public String toString() { return id + ":" + name; }
    }
    public static void main(String[] args) {
        TreeSet<SimpleData> set = new TreeSet<>();
        set.add(new SimpleData(10, "Apple"));
        set.add(new SimpleData(20, "Banana"));
        set.add(new SimpleData(10, "Cherry")); // Same ID, different object
        System.out.println(set.size());
    }
}
Q2384 hard code error
What error will this Java code produce when compiled and run?
java
public class EnhancedForNullArray {
    public static void main(String[] args) {
        String[] words = null;
        int count = 0;
        // Attempt to iterate over a null array using an enhanced for loop
        for (String s : words) {
            count++;
        }
        System.out.println("Count: " + count);
    }
}
Q2385 hard
Consider the following code: `StringBuffer sb = new StringBuffer("HelloWorld"); String sub = sb.substring(5, 10);`. Which statement about the relationship between `sub` and `sb`'s internal character array is true after this execution?
Q2386 medium
Which of the following pairs correctly represents an immutable class and its common mutable counterpart in Java?
Q2387 hard code error
What compile-time error occurs when attempting to compile this Java code?
java
class Parent {
    public final void display() {
        System.out.println("Parent's display");
    }
}

class Child extends Parent {
    @Override
    public void display() {
        System.out.println("Child's display");
    }
}
Q2388 easy
What is the main purpose of a constructor in Java?
Q2389 medium code output
What is the output of this code?
java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<String> items = new ArrayList<>(List.of("A", "B"));
        Iterator<String> it = items.iterator();
        try {
            System.out.println(it.next()); // A
            System.out.println(it.next()); // B
            System.out.println(it.next()); // This will throw an exception
        } catch (Exception e) {
            System.out.println(e.getClass().getSimpleName());
        }
    }
}
Q2390 hard code error
What is the error encountered when running this Java code (assuming the system has a low file descriptor limit)?
java
import java.io.FileReader;
import java.io.IOException;
import java.io.File;

public class FileReaderError8 {
    public static void main(String[] args) {
        File file = new File("temp_fr_many.txt");
        try {
            file.createNewFile();
            for (int i = 0; i < 10000; i++) { // Attempt to open many FileReaders without closing them
                new FileReader(file); // Each FileReader opens a file descriptor
            }
            System.out.println("Opened many files.");
        } catch (IOException e) {
            System.err.println(e.getClass().getSimpleName() + ": " + e.getMessage());
        } finally {
            file.delete();
        }
    }
}
Q2391 hard code output
What is the output of this code?
java
import java.util.function.Function;

public class LambdaCheckedException {
    public static void process(Function<String, String> func, String data) {
        System.out.println(func.apply(data));
    }

    public static void main(String[] args) {
        Function<String, String> unsafeFunc = s -> {
            if (s.equals("error")) {
                throw new RuntimeException("Simulated Runtime Error");
            }
            return "Processed: " + s;
        };

        try {
            process(unsafeFunc, "test");
            process(unsafeFunc, "error");
        } catch (RuntimeException e) {
            System.out.println("Caught: " + e.getMessage());
        }
    }
}
Q2392 medium code error
What error will occur when compiling and running this Java code?
java
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class HashSetError6 {
    public static void main(String[] args) {
        Set<String> items = new HashSet<>();
        items.add("A");
        items.add("B");
        items.add("C");

        Iterator<String> it = items.iterator();
        it.remove(); // ERROR line
        System.out.println(items);
    }
}
Q2393 medium code output
What does this code print?
java
class Counter {
    private int count = 0;

    public synchronized void increment() {
        count++;
    }

    public int getCount() {
        return count;
    }
}

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();
        Runnable task = () -> {
            for (int i = 0; i < 10000; i++) {
                counter.increment();
            }
        };

        Thread t1 = new Thread(task);
        Thread t2 = new Thread(task);

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println(counter.getCount());
    }
}
Q2394 medium code output
What is the output of this code?
java
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileSize {
    public static void main(String[] args) throws IOException {
        File file = new File("data.txt");
        FileOutputStream fos = new FileOutputStream(file);
        fos.write("Hello World".getBytes());
        fos.close();
        long size = file.length();
        file.delete(); // Cleanup
        System.out.println(size);
    }
}
Q2395 easy code output
What is the output of this code?
java
public class MyClass {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("computer");
        sb.deleteCharAt(0);
        System.out.print(sb);
    }
}
Q2396 hard
Consider the following method declarations within the same class: java class GenericOverload { void processList(List<String> stringList) { System.out.println("List of Strings"); } void processList(List<Integer> integerList) { System.out.println("List of Integers"); } } What is the result of attempting to compile this class?
Q2397 hard code output
What is the output of this code?
java
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        ArrayList rawList = new ArrayList();
        rawList.add("Hello");
        rawList.add(123);
        rawList.add(true);

        ArrayList<String> stringList = rawList; 
        String s1 = stringList.get(0); 
        String s2 = stringList.get(1); 
        System.out.println(s1);
    }
}
Q2398 medium code error
What is the compile-time error in this Java code?
java
interface MyInterface {
    void method1();
    void method2();
}

public class LambdaError {
    public static void main(String[] args) {
        MyInterface myLambda = () -> {
            System.out.println("Hello");
        };
        myLambda.method1();
    }
}
Q2399 medium code error
What is the output of this Java code?
java
public class ExceptionFlow10 {
    public static void main(String[] args) {
        testMethod(0);
    }

    public static void testMethod(int type) {
        try {
            System.out.print("Try ");
            if (type == 0) {
                int x = 1 / type; // ArithmeticException
            } else if (type == 1) {
                String s = null;
                s.length(); // NullPointerException
            }
        } catch (NullPointerException e) {
            System.out.print("Catch NPE ");
        } catch (ArithmeticException e) {
            System.out.print("Catch AE ");
        } catch (Exception e) {
            System.out.print("Catch Generic ");
        } finally {
            System.out.println("Finally");
        }
    }
}
Q2400 easy code output
What is the result of running this Java code?
java
public class Main {
    public static void main(String[] args) {
        int x = 0;
        while (true) {
            System.out.print(x + ".");
            x++;
            if (x > 2) {
                break;
            }
        }
    }
}
← Prev 118119120121122 Next → Page 120 of 200 · 3994 questions