☕ Java MCQ Questions – Page 48

Questions 941–960 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q941 easy code error
What error occurs when `setPriority()` is called with a value outside the valid range (1-10)?
java
class MyThread extends Thread {
    public void run() {
        System.out.println("Child thread running.");
    }
}

public class Main {
    public static void main(String[] args) {
        MyThread t = new MyThread();
        t.setPriority(11); // Priority outside the valid range
        t.start();
    }
}
Q942 hard code error
Identify the compile-time error in the given Java code.
java
public class CharArithmetic {
    public static void main(String[] args) {
        char char1 = 'X';
        char char2 = 'Y';
        char sum = char1 + char2; // Arithmetic operations on byte/short/char promote to int
        System.out.println(sum);
    }
}
Q943 hard code error
What compilation error will this Java code produce?
java
class Test {
    public static void main(String[] args) {
        int status = 0;
        CHECK_STATUS: status = 1; // Label applied to a statement, not a loop or switch
        do {
            System.out.println("Processing...");
            break CHECK_STATUS;
        } while (false);
        System.out.println("Finished with status: " + status);
    }
}
Q944 easy
`FileWriter` is a subclass of which abstract class?
Q945 hard code error
What kind of runtime error or unexpected behavior will occur when running this Java code, primarily related to thread lifecycle?
java
public class StopDanger implements Runnable {
    private int counter = 0;
    public void run() {
        while (true) {
            synchronized (this) {
                counter++;
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    return;
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        StopDanger task = new StopDanger();
        Thread t = new Thread(task);
        t.start();
        Thread.sleep(50);
        t.stop(); // Problem line: deprecated and dangerous
        Thread.sleep(50);
        System.out.println("Counter: " + task.counter);
    }
}
Q946 hard code output
What is the output of this code?
java
class A {
    public void test() {
        System.out.println("A.test()");
    }
}

class B extends A {
    @Override
    public void test() {
        System.out.println("B.test()");
        super.test();
    }
}

class C extends B {
    @Override
    public void test() {
        System.out.println("C.test()");
        super.test();
    }
}

public class Main {
    public static void main(String[] args) {
        C obj = new C();
        obj.test();
    }
}
Q947 medium code error
What error occurs when running this Java code?
java
public class MyClass {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Hello");
        sb.insert(6, " World");
        System.out.println(sb);
    }
}
Q948 easy
In Java, which of the following arithmetic operators has the highest precedence?
Q949 easy code output
What is the output of this code?
java
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<String, String> settings = new HashMap<>();
        settings.put(null, "Default Value");
        settings.put("Theme", "Dark");
        System.out.println(settings.get(null));
    }
}
Q950 hard
Which of the following statements is true regarding the support for `null` elements across various `Queue` implementations in `java.util.concurrent`?
Q951 easy code error
What will happen when this Java code is compiled?
java
public class MyClass {
    int instanceVar = 10;
    public static void main(String[] args) {
        System.out.println(instanceVar);
    }
}
Q952 easy code error
What error will occur when compiling the following Java code?
java
public class Main {
    public static void main(String[] args) {
        int x = 10;
        String result = (x > 5) ? "Greater" : 100;
        System.out.println(result);
    }
}
Q953 hard
Consider the String `s = "ababa";`. What would be the content of the resulting array after calling `s.split("a", -1)`?
Q954 hard code error
What is the error in the following Java code?
java
class Processor {
    public void process(String data) {
        System.out.println("Processing String: " + data);
    }
}

class ImageProcessor extends Processor {
    @Override
    public void process(Object data) { // Changing parameter type
        System.out.println("Processing Object: " + data);
    }
}
Q955 hard code error
What is the compilation error in the provided Java code snippet?
java
class Test {
    public static void main(String[] args) {
        int i = 0;
        do {
            i++;
        }; // Semicolon here
        while (i < 5);
        System.out.println(i);
    }
}
Q956 medium code output
What does this Java code print?
java
class InterruptibleTask implements Runnable {
    public void run() {
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            System.out.println("Task Interrupted: " + Thread.currentThread().isInterrupted());
        }
    }
}
public class Main {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(new InterruptibleTask());
        t.start();
        Thread.sleep(50);
        t.interrupt();
        t.join();
        System.out.println("Main done.");
    }
}
Q957 easy
What happens immediately after a `throw` statement is executed in a method?
Q958 hard code output
What does this code print?
java
import java.io.*;

class Outer implements Serializable {
    private static final long serialVersionUID = 1L;
    private int outerData = 10;

    class Inner implements Serializable {
        private static final long serialVersionUID = 1L;
        int innerData = 20;
        
        public String toString() {
            return "Outer:" + outerData + ", Inner:" + innerData;
        }
    }
}

public class SerializationTest {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Outer outer = new Outer();
        Outer.Inner inner = outer.new Inner();
        
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(inner);
        oos.close();

        outer.outerData = 99;

        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        Outer.Inner deserializedInner = (Outer.Inner) ois.readObject();
        ois.close();

        System.out.println(deserializedInner);
    }
}
Q959 hard code error
What is the runtime error when `main` method is executed?
java
class FaultyException extends RuntimeException {
    static {
        // This static initializer block will execute upon class loading
        if (System.getProperty("init.fail") != null) {
            throw new IllegalArgumentException("Static initialization failed!"); // This causes the error
        }
    }
    public FaultyException(String message) {
        super(message);
    }
}

public class Main {
    public static void main(String[] args) {
        System.setProperty("init.fail", "true"); // Set property to trigger failure
        // The first access to FaultyException triggers its static block
        FaultyException e = new FaultyException("Something went wrong"); // This line causes the runtime error
    }
}
Q960 easy code output
What does this code print?
java
public class Main {
    public static void main(String[] args) {
        int[][] matrix = {{10, 20}, {30, 40, 50}};
        System.out.println(matrix[1][3]);
    }
}
← Prev 4647484950 Next → Page 48 of 200 · 3994 questions