☕ Java MCQ Questions – Page 97

Questions 1921–1940 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1921 hard code error
What is the result of running this Java code?
java
import java.io.*;
import java.util.Comparator;
import java.util.TreeMap;

public class TreeMapError7 {
    public static void main(String[] args) {
        Comparator<Integer> customComparator = (i1, i2) -> i2.compareTo(i1); 

        TreeMap<Integer, String> map = new TreeMap<>(customComparator);
        map.put(1, "A");
        map.put(2, "B");

        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("map.ser"))) {
            oos.writeObject(map); 
        } catch (NotSerializableException e) {
            System.out.println("Serialization Error: " + e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Q1922 medium
How can you instantiate a `FileWriter` to append data to an existing file instead of overwriting its content?
Q1923 easy code error
What will happen when this Java code is compiled and executed?
java
class MyClass {
    public void invalidThrow() {
        throw "This is not an exception";
    }
}
Q1924 easy code error
What is the error in the following Java code? java class Animal {} class Dog extends Animal {} class Cat extends Animal {}
java
public class Main {
    public static void main(String[] args) {
        Animal a = new Dog();
        Cat c = (Cat) a;
        System.out.println(c);
    }
}
Q1925 medium code error
What is the compilation error in the following code?
java
abstract class Shape {
    abstract void draw();
    void getArea() {
        System.out.println("Calculating area...");
    }
}

class Circle extends Shape {
    // Missing implementation of draw()
}
Q1926 hard code output
What is the output of this code?
java
public class ThreadLocalIsolation {
    private static ThreadLocal<String> userSession = new ThreadLocal<>();

    public static void main(String[] args) throws InterruptedException {
        userSession.set("MainUser");

        Thread t1 = new Thread(() -> {
            userSession.set("User1");
            System.out.println("T1: " + userSession.get());
        });

        Thread t2 = new Thread(() -> {
            userSession.set("User2");
            System.out.println("T2: " + userSession.get());
        });

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

        System.out.println("Main: " + userSession.get());
    }
}
Q1927 hard
Regarding `java.util.PriorityQueue`, which statement is true concerning its behavior and properties?
Q1928 medium code output
What is the output of this code?
java
import java.io.File;
import java.io.IOException;

public class FileCreation {
    public static void main(String[] args) throws IOException {
        File file = new File("myFile.txt");
        boolean createdFirst = file.createNewFile();
        boolean createdSecond = file.createNewFile(); // Attempt to create again
        file.delete(); // Clean up
        System.out.println(createdFirst + ", " + createdSecond);
    }
}
Q1929 easy code error
What is the compile-time error in this Java code snippet?
java
public class ArrayTest {
    public static void main(String[] args) {
        int arr[5];
        arr[0] = 1;
    }
}
Q1930 easy code error
What is the outcome when compiling and running this Java code?
java
import java.util.ArrayList;
import java.util.List;
public class FinalVarListReassignment {
    public static void main(String[] args) {
        final var list = new ArrayList<String>();
        list.add("Item1");
        list = new ArrayList<String>();
        System.out.println(list.size());
    }
}
Q1931 hard
Why is `Thread.stop()` considered inherently unsafe and deprecated?
Q1932 hard code output
What is the output of this code?
java
import java.util.TreeSet;

public class App {
    static class MutableElement implements Comparable<MutableElement> {
        int value;
        public MutableElement(int value) { this.value = value; }
        public void setValue(int value) { this.value = value; }
        @Override public int compareTo(MutableElement other) { return Integer.compare(this.value, other.value); }
        @Override public String toString() { return String.valueOf(value); }
    }
    public static void main(String[] args) {
        TreeSet<MutableElement> set = new TreeSet<>();
        MutableElement e1 = new MutableElement(10);
        MutableElement e2 = new MutableElement(20);
        set.add(e1);
        set.add(e2);
        e1.setValue(30); // Mutate e1, changing its ordering
        System.out.println(set.first().value);
        System.out.println(set.last().value);
    }
}
Q1933 hard
How does declaring a `private` field as `final` contribute to encapsulation, especially when dealing with mutable object references?
Q1934 easy code error
What is the compilation error in the `Main` class?
java
class MyClass {
    private String name = "Test";
}

public class Main {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        System.out.println(obj.name);
    }
}
Q1935 easy
What is the underlying data structure used by `java.util.HashSet` for storing its elements?
Q1936 medium code error
What is the compilation error in the provided Java code?
java
abstract class Shape {
    public abstract void draw();
}

public class Main {
    public static void main(String[] args) {
        Shape s = new Shape();
        s.draw();
    }
}
Q1937 easy
What happens if you call the `run()` method directly on a `Thread` object instead of `start()`?
Q1938 easy
When using an `if-else if-else` ladder, what is true about the execution of its blocks?
Q1939 easy code error
What is the error in the following Java code?
java
public class Main {
    public static void main(String[] args) {
        String str = "123";
        Integer num = (Integer) str;
        System.out.println(num);
    }
}
Q1940 hard
What is "false sharing" in the context of multi-threaded programming and how can it impact performance?
← Prev 9596979899 Next → Page 97 of 200 · 3994 questions