☕ Java MCQ Questions – Page 84

Questions 1661–1680 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1661 hard code error
What is the output of this code?
java
class MyThread extends Thread {
    public void run() {
        System.out.println("Thread running.");
    }
}

public class Main {
    public static void main(String[] args) throws InterruptedException {
        MyThread t = new MyThread();
        t.start();
        t.join(); // Wait for thread to complete
        System.out.println("Thread finished.");
        t.start(); // Attempt to restart
    }
}
Q1662 easy
Which of the following interfaces does `java.util.LinkedList` implement?
Q1663 easy code output
What is the output of this code?
java
public class StringTest {
    public static void main(String[] args) {
        String s = "Java";
        System.out.println(s.charAt(1));
    }
}
Q1664 medium code error
What is the compilation error in the following Java code (Java 10+)?
java
public class VarNull {
    public static void main(String[] args) {
        var data = null;
        System.out.println(data);
    }
}
Q1665 hard code error
What is the most likely error message(s) printed when executing this Java code snippet?
java
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.IOException;

class DualFaultWriter extends Writer {
    @Override
    public void write(char[] cbuf, int off, int len) throws IOException {
        throw new IOException("Write operation failed!");
    }
    @Override
    public void flush() throws IOException { /* Do nothing */ }
    @Override
    public void close() throws IOException {
        throw new IOException("Close operation failed!");
    }
}

public class Test {
    public static void main(String[] args) {
        try (BufferedWriter bw = new BufferedWriter(new DualFaultWriter())) {
            bw.write("Important data."); // This throws the first exception
        } catch (IOException e) {
            System.err.println("Primary error: " + e.getMessage());
            for (Throwable t : e.getSuppressed()) {
                System.err.println("Suppressed error: " + t.getMessage());
            }
        }
    }
}
Q1666 hard code output
What is the output of this Java code, demonstrating a common encapsulation breach?
java
import java.util.ArrayList;
import java.util.List;

class Wallet {
    private final List<String> transactions;
    public Wallet(List<String> initialTransactions) {
        this.transactions = initialTransactions;
    }
    public List<String> getTransactions() {
        return transactions;
    }
}

public class Main {
    public static void main(String[] args) {
        List<String> txList = new ArrayList<>();
        txList.add("Deposit");
        Wallet myWallet = new Wallet(txList);
        List<String> retrievedTx = myWallet.getTransactions();
        retrievedTx.add("Withdrawal");
        System.out.println(myWallet.getTransactions());
    }
}
Q1667 medium code error
What error will occur when compiling and running this Java code?
java
import java.util.HashSet;
import java.util.Set;

public class HashSetError5 {
    public static void main(String[] args) {
        Set<String> data = new HashSet<>();
        data.add("Hello");
        data.add(null);
        data.add("World");

        for (String s : data) {
            if (s.equals("Hello")) { // ERROR if s is null
                System.out.println("Found Hello");
            }
        }
    }
}
Q1668 medium code error
What is the primary exception thrown at runtime when executing the following Java code snippet?
java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Test {
    public static void main(String[] args) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter("test.txt"))) {
            writer.close();
            writer.write("Data");
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
Q1669 easy
In Java, can different rows in a two-dimensional array have different numbers of columns?
Q1670 hard
What is the primary difference between a *compact constructor* and a *canonical constructor* in a Java record?
Q1671 easy code error
What type of error will occur when running this Java code?
java
public class MyTask {
    private final Object lock = new Object();

    public void performWait() throws InterruptedException {
        // Missing synchronized(lock)
        lock.wait();
        System.out.println("Waited!");
    }

    public static void main(String[] args) throws InterruptedException {
        MyTask task = new MyTask();
        task.performWait();
    }
}
Q1672 hard
What is the typical time complexity for the `insert(int offset, String str)` method in `StringBuffer` when `str` is large and `offset` is in the middle of a very long existing sequence?
Q1673 easy
Can a Java HashMap store null keys and null values?
Q1674 hard code error
What kind of error will occur when compiling or running the following Java code?
java
public class SBError {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Mutable"); // Length 7
        // Attempt to get substring from 0 to length + 1
        String sub = sb.substring(0, sb.length() + 1);
        System.out.println(sub);
    }
}
Q1675 hard code output
What does this code print?
java
class Super {
    int value = 10;
    public void show() { System.out.println("Super show: " + value); }
}
class Sub extends Super {
    int value = 20;
    public void show() { System.out.println("Sub show: " + value); }
}
public class Test {
    public static void main(String[] args) {
        Super obj = new Sub();
        System.out.println(obj.value);
        obj.show();
    }
}
Q1676 hard
When a Java object is deserialized from a byte stream, how are its constructors typically handled?
Q1677 easy
Which type of loop is most commonly used for explicit iteration with an `Iterator` object?
Q1678 medium code error
What is the reason for the compilation error in this Java class, intended to be immutable?
java
public class ConditionalImmutable {
    private final String data;

    public ConditionalImmutable(boolean condition) {
        if (condition) {
            this.data = "Initialized";
        } 
        // 'data' might not be initialized if condition is false
    }
}
Q1679 hard
Consider the declaration: `final int[] myNumbers = {1, 2, 3};`. Which of the following operations is *not* permitted?
Q1680 easy code error
Which error will occur when running this Java code?
java
import java.io.FileReader;
import java.io.IOException;

public class FileReaderIssue {
    public static void main(String[] args) throws IOException {
        FileReader reader = new FileReader(""); // Empty string filename
        int data = reader.read();
        reader.close();
    }
}
← Prev 8283848586 Next → Page 84 of 200 · 3994 questions