☕ Java MCQ Questions – Page 126

Questions 2501–2520 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2501 medium
What is the primary purpose of the `synchronized` keyword in Java?
Q2502 medium code error
What is the compile-time error in the following Java code?
java
class MyClass {
    int value;

    public MyClass(int value) {
        this.value = value;
    }

    private MyClass(int value) { // Duplicate signature with different access modifier
        this.value = value * 2;
    }

    public static void main(String[] args) {
        MyClass obj = new MyClass(10);
    }
}
Q2503 hard code output
What is the output of this code, given that `serializedData` was generated from a `MyObjectV1` class (an inner class used to simulate version 1 of `MyObject`) which had `serialVersionUID = 1L`?
java
import java.io.*;

class MyObject implements Serializable {
    private static final long serialVersionUID = 2L; // Current version
    String data = "Hello";
}

public class SerializationTest {
    private static byte[] getSerializedDataFromV1() throws IOException {
        class MyObjectV1 implements Serializable {
            private static final long serialVersionUID = 1L; // Old version
            String data = "Hello";
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(new MyObjectV1());
        oos.close();
        return bos.toByteArray();
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        byte[] serializedData = getSerializedDataFromV1();

        try (ByteArrayInputStream bis = new ByteArrayInputStream(serializedData);
             ObjectInputStream ois = new ObjectInputStream(bis)) {
            MyObject deserializedObj = (MyObject) ois.readObject();
            System.out.println("Deserialized: " + deserializedObj.data);
        } catch (InvalidClassException e) {
            System.out.println("Error: " + e.getMessage().split(";")[0]);
        }
    }
}
Q2504 hard code output
What does this code print?
java
String text = "  Hello World  ";
text.trim().replace("o", "X").toLowerCase();

System.out.println(text);
Q2505 hard code error
What is the most likely error when executing this Java code snippet?
java
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.IOException;

public class Test {
    public static void main(String[] args) {
        Writer underlyingWriter = null;
        try {
            BufferedWriter bw = new BufferedWriter(underlyingWriter);
            bw.write("Test");
            bw.flush();
        } catch (IOException e) {
            System.err.println("IO Error: " + e.getMessage());
        } catch (NullPointerException e) {
            System.err.println("Initialization Error: " + e.getClass().getName());
        }
    }
}
Q2506 hard code error
What is the compile-time error in the following Java code?
java
public class FloatingPointTest {
    public static void main(String[] args) {
        float piValue = 3.1415926535; // Double literal assigned to float
        System.out.println(piValue);
    }
}
Q2507 easy code output
What does this Java code print?
java
public class ImmutableTest {
    public static void main(String[] args) {
        String s = "Hello";
        s.concat(" World");
        System.out.println(s);
    }
}
Q2508 easy
`BufferedWriter` typically wraps which type of stream?
Q2509 easy code error
What compile-time error will occur in the `main` method?
java
class MyClass {
    private MyClass() {
        System.out.println("Private Constructor");
    }
}
public class Main {
    public static void main(String[] args) {
        MyClass obj = new MyClass(); // Attempting to instantiate a private constructor
    }
}
Q2510 medium
If a `BufferedWriter` is closed without explicitly calling `flush()`, what happens to any data still residing in its internal buffer?
Q2511 medium code output
What is the output of the following code?
java
public class StringBufferTest {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("abcdefg");
        String sub1 = sb.substring(2);
        String sub2 = sb.substring(1, 4);
        System.out.print(sub1 + " " + sub2);
    }
}
Q2512 easy
Which of the following is NOT considered a common component of a Java class definition?
Q2513 medium code output
What is the output of this Java code?
java
import java.util.TreeSet;

public class Test {
    public static void main(String[] args) {
        TreeSet<String> letters = new TreeSet<>();
        letters.add("B");
        letters.add("A");
        letters.add("D");
        letters.add("C");
        letters.add("E");
        String first = letters.pollFirst();
        String last = letters.pollLast();
        System.out.println(first + ", " + last + ", " + letters);
    }
}
Q2514 easy
If a class has a `private final` field that holds a reference to a mutable object (e.g., an `ArrayList`), is the class guaranteed to be immutable?
Q2515 hard
Consider a complex `if` condition: `if (methodA() || (methodB() && methodC()))`. If `methodA()` returns `true`, what is the guaranteed execution behavior of `methodB()` and `methodC()`?
Q2516 hard code output
What is the output of this code?
java
public class StringInternTest {
    public static void main(String[] args) {
        String s1 = new String("hello");
        String s2 = "hello";
        String s3 = s1.intern();
        System.out.println(s1 == s2);
        System.out.println(s2 == s3);
    }
}
Q2517 hard
Which statement best describes the atomicity of operations performed by the `java.io.File` class methods in relation to concurrent access by multiple threads or processes?
Q2518 easy code output
What is the output of this code?
java
import java.io.BufferedWriter;
import java.io.StringWriter;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        StringWriter stringWriter = new StringWriter();
        try (BufferedWriter writer = new BufferedWriter(stringWriter)) {
            writer.write("PartA");
            writer.flush();
            writer.write("PartB");
            writer.flush();
            System.out.print(stringWriter.toString());
        } catch (IOException e) {
            System.out.print("Error");
        }
    }
}
Q2519 medium
What is the fundamental principle behind the execution of intermediate operations in the Java Stream API?
Q2520 hard
What is the specific contract of `java.io.File.createNewFile()` regarding its return value and underlying file system operations, particularly in a multi-threaded or concurrent environment?
← Prev 124125126127128 Next → Page 126 of 200 · 3994 questions