☕ Java MCQ Questions – Page 141

Questions 2801–2820 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2801 hard code error
What is the compilation error in the following Java code?
java
package com.base;

public class ProtectedResource {
    protected String secret = "TopSecret";
    protected void reveal() {
        System.out.println(secret);
    }
}

package com.app;
import com.base.ProtectedResource;

public class AccessAttempt {
    public static void main(String[] args) {
        ProtectedResource res = new ProtectedResource();
        res.reveal(); // Attempt to access protected method from a different package and non-subclass
    }
}
Q2802 easy
When does a `StringBuffer` increase its capacity?
Q2803 medium code error
What will be the outcome of executing the following Java code?
java
public class Main {
    public static void main(String[] args) {
        Runnable myRunnable = () -> {
            System.out.println("Task is running.");
        };
        Thread thread = new Thread(myRunnable);
        thread.start();
        thread.start();
    }
}
Q2804 medium code error
What is the output of this Java code?
java
import java.io.*;

public class DeserializationError5 {
    public static void main(String[] args) {
        // Attempt to create ObjectInputStream from a plain text string
        String textData = "This is not serialized object data.";
        byte[] data = textData.getBytes();

        try (ByteArrayInputStream bis = new ByteArrayInputStream(data);
             ObjectInputStream ois = new ObjectInputStream(bis)) {
            Object obj = ois.readObject();
            System.out.println("Deserialized: " + obj);
        } catch (Exception e) {
            System.out.println("Error: " + e.getClass().getName() + ": " + e.getMessage());
        }
    }
}
Q2805 hard code error
What is the output of this code?
java
import java.util.LinkedList;
import java.util.Arrays;

public class MyClass {
    public static void main(String[] args) {
        LinkedList<String> data = new LinkedList<>(Arrays.asList("one", null, "three"));
        for (String item : data) {
            System.out.println(item.length()); // Dereferencing null
        }
    }
}
Q2806 easy code error
What is the error in the following Java code?
java
import java.util.TreeSet;
import java.util.Comparator;

public class Main {
    public static void main(String[] args) {
        Comparator<Integer> myComparator = (a, b) -> a.compareTo(b);
        TreeSet<String> values = new TreeSet<>(myComparator);
        values.add("One");
    }
}
Q2807 hard code error
What kind of compile-time error will you encounter with this Java code?
java
public class CharDoubleArithmetic {
    public static void main(String[] args) {
        char initialChar = 'A'; // ASCII 65
        char nextChar = initialChar + 1.0; // Adding a double literal to a char
        System.out.println(nextChar);
    }
}
Q2808 hard
If an exception is caught in a `catch` block and then explicitly re-thrown (e.g., `throw e;`), how does the `finally` block behave in relation to this re-thrown exception?
Q2809 medium code output
What is the output of the following code snippet?
java
import java.util.Date;

class DataProvider {
    Object getData() {
        return new Date();
    }
}

class SpecificDataProvider extends DataProvider {
    @Override
    String getData() { // Covariant return type
        return "Specific Data";
    }
}

public class Main {
    public static void main(String[] args) {
        DataProvider provider = new SpecificDataProvider();
        System.out.println(provider.getData());
    }
}
Q2810 hard code error
What is the compile-time error in this Java code?
java
public class InvalidRunSignature implements Runnable {
    @Override
    public int run() { // Line 3
        System.out.println("Running task.");
        return 0;
    }

    public static void main(String[] args) {
        new Thread(new InvalidRunSignature()).start();
    }
}
Q2811 medium
Which method of the `java.util.function.Function` interface can be used to compose two functions, applying the current function *after* the provided function?
Q2812 medium
In Java, if a subclass defines a `static` method with the exact same signature as a `static` method in its superclass, what concept is illustrated?
Q2813 easy code error
What is the error in the following Java code?
java
class Main {
    public static void main(String[] args) {
        TreeSet<String> data = new TreeSet<>();
        data.add("Item A");
        data.add("Item B");
    }
}
Q2814 easy code output
What is the output of this code?
java
class BaseClass {
    public final void show() {
        System.out.print("Base show");
    }
}

class ChildClass extends BaseClass {
    // This class cannot override the final show() method
}

public class Main {
    public static void main(String[] args) {
        BaseClass obj = new ChildClass();
        obj.show();
    }
}
Q2815 hard code output
What is the output of this code?
java
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        ArrayList<String> originalList = new ArrayList<>();
        originalList.add("A");
        originalList.add("B");
        originalList.add("C");
        originalList.add("D");

        List<String> subList = originalList.subList(1, 3); 
        subList.set(0, "X"); 
        originalList.remove(3); 
        System.out.println(subList.size());
    }
}
Q2816 hard
A `StringBuffer` is initialized with `new StringBuffer(10)`. If a subsequent `append` operation causes its length to exceed 10, what is the most probable *minimum* new capacity the `StringBuffer` will attempt to ensure, based on the typical `AbstractStringBuilder` growth strategy in Java?
Q2817 easy
Which keyword would you use if you want to skip only a particular iteration of a loop and move on to the next one, without terminating the loop?
Q2818 easy
What is the primary purpose of Java Lambda Expressions?
Q2819 easy
Which method is generally preferred over `add()` when adding an element to a bounded `Queue` because it doesn't throw an exception if the queue is full?
Q2820 medium code output
What is the output of this code?
java
class DataValidationException extends RuntimeException {
    public DataValidationException(String message) {
        super(message);
    }
}

public class Main {
    public static void processData(int value) {
        if (value < 0) {
            throw new DataValidationException("Negative value not allowed: " + value);
        }
        System.out.println("Data processed: " + value);
    }

    public static void main(String[] args) {
        try {
            processData(-5);
        } catch (DataValidationException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}
← Prev 139140141142143 Next → Page 141 of 200 · 3994 questions