☕ Java MCQ Questions – Page 50

Questions 981–1000 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q981 hard code error
What error will this code produce during compilation?
java
public class UnreachableCodeLoop {
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            System.out.println("Iteration " + i);
            break;
            System.out.println("This message is unreachable.");
        }
    }
}
Q982 medium
Consider a two-dimensional array declared as `int[][] matrix = new int[3][];`. Which statement is true regarding this declaration?
Q983 easy
Which methods, inherited from `java.lang.Object`, do NOT count towards the single abstract method rule in a functional interface?
Q984 easy code error
What compile-time error will occur in the `main` method when calling `performAction()`?
java
import java.io.IOException;

class CustomProcessException extends Exception {
    public CustomProcessException(String message) { super(message); }
}

public class Main {
    public static void performAction() throws CustomProcessException {
        throw new CustomProcessException("Action failed");
    }

    public static void main(String[] args) {
        try {
            performAction();
        } catch (IOException e) { // This catch block is for IOException, not CustomProcessException
            System.out.println("Caught IO Exception: " + e.getMessage());
        }
    }
}
Q985 hard
In a `do-while` loop where the `while` condition involves a method call with side effects (e.g., `do { ... } while (resource.hasNext() && processResource());`), when are these side effects of `processResource()` executed?
Q986 hard code error
What is the compilation error in the following Java code?
java
package model;

public class SecretData {
    private SecretData() { // Private constructor
        System.out.println("SecretData instance created.");
    }

    public static SecretData createInstance() {
        return new SecretData();
    }
}

package client;
import model.SecretData;

public class App {
    public static void main(String[] args) {
        SecretData data = new SecretData(); // Attempt to call private constructor
    }
}
Q987 hard code error
What is the most likely error when executing this Java code snippet?
java
import java.io.BufferedWriter;
import java.io.StringWriter;
import java.io.IOException;

public class Test {
    public static void main(String[] args) {
        StringWriter sw = new StringWriter();
        try (BufferedWriter bw = new BufferedWriter(sw)) {
            char[] data = {'H', 'e', 'l', 'l', 'o'};
            bw.write(data, 0, 10); // Attempt to write more chars than available
            bw.flush();
        } catch (IOException e) {
            System.err.println("IO Error: " + e.getMessage());
        } catch (IndexOutOfBoundsException e) {
            System.err.println("Array Access Error: " + e.getClass().getName());
        }
        System.out.println("Content: " + sw.toString());
    }
}
Q988 hard
The `BufferedReader.ready()` method returns `true` if the next `read()` or `readLine()` would not block. However, which of the following scenarios highlights a potential subtlety or limitation of relying solely on `ready()` for non-blocking `readLine()` operations?
Q989 medium
Consider a method where an exception occurs in the `try` block, is caught by a `catch` block, and the `catch` block contains a `return` statement. If the `finally` block also contains a `return` statement, what will be returned?
Q990 easy code error
What exception will be thrown when executing the following Java code?
java
import java.util.HashSet;
import java.util.Set;

public class MyClass {
    public static void main(String[] args) {
        Set<Object> data = new HashSet<>();
        data.add("String Data");
        data.add(123);
        
        for (Object item : data) {
            Integer value = (Integer) item; // This line will cause an error for "String Data"
            System.out.println(value);
        }
    }
}
Q991 hard code output
What is the output of this code?
java
import java.io.*;

class Singleton implements Serializable {
    private static final long serialVersionUID = 1L;
    private static Singleton INSTANCE = new Singleton();
    private int value = 0;

    private Singleton() {
        System.out.println("Singleton constructor called.");
    }

    public static Singleton getInstance() {
        return INSTANCE;
    }

    public void setValue(int v) { this.value = v; }
    public int getValue() { return value; }

    protected Object readResolve() {
        System.out.println("readResolve called.");
        return INSTANCE;
    }
}

public class SerializationTest {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Singleton original = Singleton.getInstance();
        original.setValue(100);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(original);
        oos.close();

        Singleton originalAfterSerialization = Singleton.getInstance();
        originalAfterSerialization.setValue(200);

        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        Singleton deserialized = (Singleton) ois.readObject();
        ois.close();

        System.out.println("Original Value: " + original.getValue());
        System.out.println("Deserialized Value: " + deserialized.getValue());
        System.out.println("Are same instances: " + (original == deserialized));
    }
}
Q992 hard code output
What is the output of this code?
java
import java.io.IOException;

class Super {
    void method() throws IOException {
        System.out.println("Super method");
    }
}

class Sub extends Super {
    @Override
    void method() throws Exception {
        System.out.println("Sub method");
    }
}

public class Main {
    public static void main(String[] args) {
        Super s = new Sub();
        try {
            s.method();
        } catch (Exception e) {
            System.out.println("Caught exception");
        }
    }
}
Q993 hard code output
What is the output of this code?
java
public class Test {
    public static int getValue() {
        try {
            System.out.print("Try ");
            return 10;
        } finally {
            System.out.print("Finally ");
            return 20;
        }
    }
    public static void main(String[] args) {
        System.out.println(getValue());
    }
}
Q994 medium code output
What does this code print?
java
import java.time.LocalDate;

public class LocalDateImmutability {
    public static void main(String[] args) {
        LocalDate date1 = LocalDate.of(2023, 1, 15);
        LocalDate date2 = date1.plusDays(10);
        System.out.println(date1.getDayOfMonth());
    }
}
Q995 medium
When using `this()` for constructor chaining within a constructor, what is the mandatory rule regarding its placement?
Q996 easy
Method overriding in Java is an example of which type of polymorphism?
Q997 hard code error
What is the error encountered when running this Java code?
java
import java.io.FileReader;
import java.io.IOException;
import java.io.File;

public class FileReaderError1 {
    public static void main(String[] args) {
        File dir = new File("temp_directory_fr");
        dir.mkdir(); // Ensure directory exists

        try (FileReader fr = new FileReader(dir)) { // Attempt to open a directory as a file
            int data = fr.read();
            System.out.println("Read: " + (char)data);
        } catch (IOException e) {
            System.err.println(e.getClass().getSimpleName());
        } finally {
            dir.delete(); // Clean up
        }
    }
}
Q998 hard
A thread `T` has been created but `T.start()` has not yet been invoked. What is the state of `T`, and what happens if `T.interrupt()` is called at this point?
Q999 easy code output
What is the output of this code?
java
class EmptyInputException extends RuntimeException {
    // No constructor explicitly defined
}

public class Main {
    public static void main(String[] args) {
        try {
            throw new EmptyInputException();
        } catch (EmptyInputException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}
Q1000 medium code error
What error occurs when compiling this Java code?
java
public class LoopTest {
    public static void main(String[] args) {
        int i = 0;
        do {
            i++;
        } while (i < 2);
        break;
    }
}
← Prev 4849505152 Next → Page 50 of 200 · 3994 questions