☕ Java MCQ Questions – Page 24

Questions 461–480 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q461 hard code error
Which exception is thrown when the `main` method of this Java code is executed?
java
public class SubstringBoundsError {
    public static void main(String[] args) {
        String text = "Java";
        try {
            String sub = text.substring(text.length(), text.length() + 1);
            System.out.println(sub);
        } catch (Exception e) {
            System.out.println(e.getClass().getSimpleName());
        }
    }
}
Q462 medium code error
What is the output of this Java code?
java
import java.io.*;

public class DeserializationError8 {
    public static void main(String[] args) {
        try (FileInputStream fis = new FileInputStream("nonExistentFile.ser");
             ObjectInputStream ois = new ObjectInputStream(fis)) {
            Object obj = ois.readObject();
            System.out.println("Deserialized: " + obj);
        } catch (Exception e) {
            System.out.println("Error: " + e.getClass().getName() + ": " + e.getMessage());
        }
    }
}
Q463 medium
If a class `MyClass` explicitly defines only a parameterized constructor `MyClass(int id)`, and no other constructors, what happens when you try to create an instance using `new MyClass()`?
Q464 medium
In a `do-while` loop, when is the loop's boolean condition evaluated?
Q465 easy
Can a constructor in Java be declared with the 'static' keyword?
Q466 easy code error
What is the compilation or runtime error in the following Java code?
java
public class Main {
    public static void main(String[] args) {
        String sentence = "apple,banana,cherry";
        String[] parts = sentence.split(null);
        System.out.println(parts.length);
    }
}
Q467 medium code output
What is the output of the following Java code?
java
import java.util.TreeMap;
import java.util.SortedMap;

public class TreeMapSubMap {
    public static void main(String[] args) {
        TreeMap<Integer, String> map = new TreeMap<>();
        map.put(1, "one");
        map.put(5, "five");
        map.put(3, "three");
        map.put(7, "seven");
        SortedMap<Integer, String> subMap = map.subMap(2, 6);
        System.out.println(subMap.size() + ":" + subMap.firstKey() + ":" + subMap.lastKey());
    }
}
Q468 medium code error
What error will this Java code produce when executed?
java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> messages = new ArrayList<>();
        Iterator<String> it = messages.iterator();
        String firstMessage = it.next(); // Attempt to retrieve from an empty list
        System.out.println(firstMessage);
    }
}
Q469 hard
Consider a class `A` with a public instance field `x` and a class `B` extending `A` that also declares a public instance field `x`. If a variable of type `A` references an object of type `B`, which field `x` is accessed when using `variable.x`?
Q470 hard code output
What does this code print?
java
public class ThisInLambda {
    private String member = "ClassMember";

    public Runnable createLambda() {
        return () -> System.out.println(this.member);
    }

    public static void main(String[] args) {
        ThisInLambda instance = new ThisInLambda();
        instance.member = "ChangedMember";
        instance.createLambda().run();
    }
}
Q471 easy code error
What is the error in the following Java code snippet?
java
public class ArrayError {
    public static void main(String[] args) {
        String[] names = new String[3];
        names[0] = "Alice";
        names[1] = "Bob";
        names[2] = "Charlie";
        names[3] = "David";
    }
}
Q472 medium code error
What is the compile-time or runtime error in the following Java code snippet when attempting to deserialize the object?
java
import java.io.*;

class Profile implements Externalizable {
    private String name;

    public Profile(String name) { // Only a parameterized constructor
        this.name = name;
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeUTF(name);
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        this.name = in.readUTF();
    }
}

public class SerializationQuestion6 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Profile profile = new Profile("John Doe");
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("profile.ser"))) {
            oos.writeObject(profile);
        }

        // Deserialization will fail because Profile lacks a public no-arg constructor
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("profile.ser"))) {
            Profile loadedProfile = (Profile) ois.readObject(); 
        }
    }
}
Q473 easy
What keyword or property is used to find the number of elements in a Java array named `myArray`?
Q474 medium
What is the primary purpose of the `default` case in a Java `switch` statement or expression?
Q475 hard code error
What is the output of this code?
java
import java.io.File;
import java.io.IOException;

public class TempFileDirectoryError {
    public static void main(String[] args) {
        File nonExistentParent = new File("unlikely/to/exist/parent");
        File tempFile = null;
        try {
            // The 'directory' argument MUST exist and be a directory.
            tempFile = File.createTempFile("prefix", ".tmp", nonExistentParent);
            System.out.println("Temp file created: " + tempFile.getAbsolutePath());
        } catch (IOException e) {
            System.out.println("Error: " + e.getClass().getSimpleName() + ": " + e.getMessage());
        } finally {
            if (tempFile != null) tempFile.delete();
        }
    }
}
Q476 hard code output
What is the output of this code?
java
public class Test {
    public static void main(String[] args) {
        String result = "";
        for (int i = 0; i < 3; i++) {
            try {
                if (i == 1) {
                    continue;
                }
                result += i;
            } finally {
                result += "F";
            }
        }
        System.out.print(result);
    }
}
Q477 easy code output
What is the output of the following code snippet?
java
public class MyClass {
    public static void main(String[] args) {
        int value = 10;
        switch (value) {
            case 1: System.out.print("One"); break;
            case 2: System.out.print("Two"); break;
            case 3: System.out.print("Three"); break;
        }
        System.out.print("End");
    }
}
Q478 hard
When initializing a `HashSet` with a very low load factor (e.g., 0.1) compared to the default (0.75), what is the primary trade-off in terms of resource utilization and performance?
Q479 hard
According to the Java Memory Model (JMM), which happens-before guarantee is established by exiting a `synchronized` block?
Q480 medium
What is the primary purpose of the `throw` keyword in Java?
← Prev 2223242526 Next → Page 24 of 200 · 3994 questions