☕ Java MCQ Questions – Page 172

Questions 3421–3440 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3421 hard code error
Which exception is thrown when the `main` method of this Java code is executed?
java
import java.nio.charset.Charset;

public class CharsetNullConstructor {
    public static void main(String[] args) {
        byte[] bytes = {72, 101, 108, 108, 111}; // ASCII for "Hello"
        String charsetName = null;
        try {
            String s = new String(bytes, charsetName);
            System.out.println(s);
        } catch (Exception e) {
            System.out.println(e.getClass().getSimpleName());
        }
    }
}
Q3422 easy code output
What does this code print?
java
class LimitExceededException extends RuntimeException {
    public LimitExceededException() {
        // Default constructor, relies on parent's default
    }
}

public class Main {
    public static void checkLimit(int value) {
        if (value > 100) {
            throw new LimitExceededException();
        }
    }

    public static void main(String[] args) {
        try {
            checkLimit(150);
        } catch (LimitExceededException e) {
            System.out.println("Limit exceeded!");
            System.out.println(e.getClass().getSimpleName());
        }
    }
}
Q3423 easy
In which major version of Java were Lambda Expressions officially introduced?
Q3424 hard code error
What error will be thrown by the `MyTask` thread when the `main` thread interrupts it?
java
class MyTask implements Runnable {
    @Override
    public void run() {
        try {
            System.out.println("Task sleeping...");
            Thread.sleep(5000);
            System.out.println("Task woke up normally.");
        } catch (InterruptedException e) {
            // Exception caught but not handled correctly, it's just printed
            System.err.println("Task interrupted: " + e.getMessage());
        }
    }
}

public class InterruptExample {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(new MyTask());
        t.start();
        Thread.sleep(100); 
        t.interrupt(); // Interrupt the sleeping thread
    }
}
Q3425 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("Data"); // Length 4
        sb.setCharAt(sb.length(), '!'); // Attempt to set char at index 4
        System.out.println(sb);
    }
}
Q3426 medium code error
What error occurs when compiling this Java code?
java
import java.net.URISyntaxException;

public class MyResource {
    public MyResource() {
        // Simulate an operation that might throw URISyntaxException
        throw new URISyntaxException("invalid uri", "description");
    }

    public static void main(String[] args) {
        // Nothing happens here
    }
}
Q3427 hard code error
What will be the result of compiling this Java code?
java
class Base {
    private Base() {
        System.out.println("Base constructor");
    }
}
class Derived extends Base {
    public Derived() {
        // super(); // Implicit or explicit call to Base() is needed
        System.out.println("Derived constructor");
    }
    public static void main(String[] args) {
        Derived d = new Derived();
    }
}
Q3428 easy code output
What does this code print?
java
public class DirectRunCall {
    public void run() {
        System.out.println("Direct run call.");
    }

    public static void main(String[] args) {
        DirectRunCall obj = new DirectRunCall();
        obj.run();
    }
}
Q3429 easy
Can constructors in Java be overloaded?
Q3430 medium
When a thread attempts to enter a `synchronized` block or method but the monitor is currently held by another thread, what state does the attempting thread enter?
Q3431 easy
Which of the following is a common characteristic of a class designed to be immutable?
Q3432 medium code error
What error will occur when compiling this Java code?
java
public class ConditionCheck {
    public static void main(String[] args) {
        int counter = 0;
        if (counter == 0) {
            System.out.println("Starting count.");
            break;
        }
        System.out.println("Counted.");
    }
}
Q3433 medium code output
What is the output of this code?
java
class VolatileExample {
    private volatile boolean flag = false;
    private int value = 0;

    public void writer() {
        value = 123;
        flag = true;
    }

    public void reader() {
        while (!flag) {
            Thread.yield();
        }
        System.out.println("Value is: " + value);
    }
}

public class Main {
    public static void main(String[] args) throws InterruptedException {
        VolatileExample example = new VolatileExample();
        Thread writer = new Thread(example::writer);
        Thread reader = new Thread(example::reader);

        reader.start();
        Thread.sleep(100); 
        writer.start();

        writer.join();
        reader.join();
    }
}
Q3434 medium
When an object is serialized, what happens to its `static` fields?
Q3435 easy code output
What does this code print?
java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        Iterator<Integer> it = numbers.iterator();
        while (it.hasNext()) {
            System.out.print(it.next());
        }
    }
}
Q3436 easy code output
What is the output of this Java code snippet?
java
public class Test {
    public static void main(String[] args) {
        int[] data = {100};
        System.out.println(data.length - 1);
    }
}
Q3437 medium code error
What error does this Java code produce when executed?
java
public class Main {
    public static void main(String[] args) {
        String text = null;
        System.out.println(text.length());
    }
}
Q3438 easy code output
What is the output of this Java code?
java
public class MyClass {
    public static void riskyMethod() {
        System.out.println("Attempting operation...");
        throw new IllegalArgumentException("Invalid input");
    }

    public static void main(String[] args) {
        riskyMethod();
        System.out.println("Operation completed.");
    }
}
Q3439 medium code output
What does this Java code print?
java
public class Main {
    public static void main(String[] args) {
        System.out.println("Before sleep.");
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            System.out.println("Interrupted during sleep.");
        }
        System.out.println("After sleep.");
    }
}
Q3440 medium
Consider a `BufferedReader` reading from a large file. If `mark()` is called, what is a potential issue if the `readAheadLimit` specified is too small?
← Prev 170171172173174 Next → Page 172 of 200 · 3994 questions