☕ Java MCQ Questions – Page 13

Questions 241–260 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q241 easy
What is the average time complexity for adding an element to the beginning or end of a `java.util.LinkedList`?
Q242 medium
In nested Java `for` loops, how does the inner loop complete its iterations relative to the outer loop?
Q243 easy
What is the typical time complexity for basic operations like `get`, `put`, and `remove` in a `TreeMap`?
Q244 medium code output
What is the content of 'chars.txt' after this code executes?
java
import java.io.FileWriter;
import java.io.IOException;

public class CharArrayWriteTest {
    public static void main(String[] args) {
        char[] buffer = {'A', 'B', 'C', 'D', 'E'};
        try (FileWriter writer = new FileWriter("chars.txt")) {
            writer.write(buffer, 1, 3);
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}
Q245 easy
What does 'data hiding' primarily refer to in the context of encapsulation?
Q246 easy
If a static method is declared `synchronized`, which lock is acquired?
Q247 medium code error
What will be the outcome when executing this Java code snippet?
java
import java.util.concurrent.LinkedBlockingQueue;

public class QueueError {
    public static void main(String[] args) throws InterruptedException {
        LinkedBlockingQueue<String> lbq = new LinkedBlockingQueue<>();
        System.out.println("Attempting to take element...");
        String element = lbq.take(); // This method blocks if the queue is empty
        System.out.println("Element taken: " + element);
    }
}
Q248 hard code error
What error occurs when attempting to compile the following Java code?
java
public class SemicolonAfterIf {
    public static void main(String[] args) {
        int value = 15;
        if (value > 10); // Empty statement
            System.out.println("Value is large.");
        else { // 'else' without 'if'
            System.out.println("Value is small.");
        }
    }
}
Q249 medium code output
What is the output of this code?
java
import java.util.LinkedList;

public class Test {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Hello");
        list.add(null);
        list.add("World");
        list.remove(null); 
        System.out.println(list.size() + " " + list.contains(null));
    }
}
Q250 medium
Which statement best describes the purpose of the ClassLoader in Java?
Q251 easy
What happens if the condition in a `while` loop is initially `false`?
Q252 easy code error
What type of error will occur when compiling this Java code?
java
public class PrimitiveSync {
    private int count = 0;

    public void increment() {
        synchronized (count) { // Attempting to synchronize on a primitive
            count++;
            System.out.println(count);
        }
    }

    public static void main(String[] args) {
        new PrimitiveSync().increment();
    }
}
Q253 easy
In which of the following contexts can the `continue` keyword be used in Java?
Q254 easy code output
What is the output of this Java code?
java
public class Main {
    public static void main(String[] args) {
        final int MAX_ATTEMPTS = 3;
        System.out.println(MAX_ATTEMPTS);
    }
}
Q255 hard code output
What is the output of this code?
java
public class StringSplitTest {
    public static void main(String[] args) {
        String data = "one,,two,three,";
        String[] parts = data.split(",", 3);
        System.out.println(parts.length);
        System.out.println(parts[2]);
    }
}
Q256 easy code error
What error will this Java code produce when executed?
java
public class ArrayError {
    public static void main(String[] args) {
        int[][] matrix = new int[1][2];
        matrix[0][0] = 10;
        matrix[0][1] = 20;
        System.out.println(matrix[0][2]);
    }
}
Q257 medium
An abstract class in Java can have all of the following EXCEPT:
Q258 hard
Which statement about Java's `Integer` wrapper class and autoboxing behavior is *true*?
Q259 medium code error
What is the primary issue with this Java code snippet when executed?
java
public class LoopError {
    public static void main(String[] args) {
        int count = 0;
        for (int i = 0; i < 3;) {
            System.out.println("Iteration: " + i);
            // Missing increment for i
        }
        System.out.println("Done");
    }
}
Q260 hard code error
What exception is thrown when the `ois.readObject()` line is executed during deserialization?
java
import java.io.*;

class ExternalizableClass implements Externalizable {
    private String data;
    private int id;

    // Missing public no-arg constructor required by Externalizable
    public ExternalizableClass(String data, int id) {
        this.data = data;
        this.id = id;
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeUTF(data);
        out.writeInt(id);
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        this.data = in.readUTF();
        this.id = in.readInt();
    }
}

public class SerializationError3 {
    public static void main(String[] args) throws Exception {
        ExternalizableClass original = new ExternalizableClass("Test Data", 123);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(original); // Serialization succeeds
        byte[] serializedBytes = bos.toByteArray();
        oos.close();

        ByteArrayInputStream bis = new ByteArrayInputStream(serializedBytes);
        ObjectInputStream ois = new ObjectInputStream(bis);
        ExternalizableClass deserialized = (ExternalizableClass) ois.readObject(); // Deserialization fails here
        ois.close();
        System.out.println("Deserialized: " + deserialized.data + ", " + deserialized.id);
    }
}
← Prev 1112131415 Next → Page 13 of 200 · 3994 questions