☕ Java MCQ Questions – Page 136

Questions 2701–2720 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2701 easy code output
What does this Java code print?
java
public class MutableTest {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Java");
        sb.append(" is fun");
        System.out.println(sb);
    }
}
Q2702 medium code output
What is the result of concatenating the substrings?
java
public class StringBuilderTest {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Java Programming");
        String s1 = sb.substring(0, 4);
        String s2 = sb.substring(5);
        System.out.println(s1 + " " + s2);
    }
}
Q2703 easy code output
What does this code print?
java
import java.io.FileReader;
import java.io.IOException;

public class FileReaderTest {
    public static void main(String[] args) {
        // Assume test.txt exists and contains "HelloWorld"
        char[] buffer = new char[5];
        try (FileReader reader = new FileReader("test.txt")) {
            int firstRead = reader.read(buffer); 
            int secondRead = reader.read(buffer); 
            System.out.print(firstRead + "," + secondRead);
        } catch (IOException e) {
            System.out.print("Error");
        }
    }
}
Q2704 easy code output
What is the output of this Java code snippet?
java
import java.io.*;

class MyObject implements Serializable {
    private String name;
    public MyObject(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
}

public class Main {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        MyObject obj = new MyObject("TestName");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(obj);
        oos.close();

        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        MyObject deserializedObj = (MyObject) ois.readObject();
        ois.close();

        System.out.println(deserializedObj.getName());
    }
}
Q2705 medium
If a variable is declared and initialized within the block of an `if` statement in Java, what is the scope of that variable?
Q2706 medium code error
What is the compile-time error in the following Java code?
java
public class Test {
    public static void main(String[] args) {
        int counter = 0;
        System.out.println("Before break");
        break;
        System.out.println("After break");
    }
}
Q2707 hard
A `while` loop's termination condition involves several complex boolean expressions. Which of the following conditions is most likely to introduce subtle bugs, such as infinite loops or incorrect termination, if not handled with extreme care?
Q2708 medium code error
What kind of error will this Java code produce?
java
public class LoopTest {
    public static void main(String[] args) {
        boolean active = false;
        while (active) {
            System.out.println("Looping...");
        }
        System.out.println("End");
    }
}
Q2709 easy code error
What is wrong with this Java code?
java
import java.util.function.Consumer;

public class Test {
    public static void main(String[] args) {
        Consumer<String> consumer = (s, t) -> System.out.println(s + t);
    }
}
Q2710 medium code error
What compilation error will occur in the `main` method?
java
public class MyCheckedException extends Exception {
    public MyCheckedException(String message) {
        super(message);
    }
}

public class LogicProcessor {
    public void processValue(int value) {
        if (value < 0) {
            new MyCheckedException("Value must be positive.");
        }
        System.out.println("Value processed: " + value);
    }

    public static void main(String[] args) {
        LogicProcessor lp = new LogicProcessor();
        try {
            lp.processValue(-5);
        } catch (MyCheckedException e) {
            System.out.println("Caught: " + e.getMessage());
        }
    }
}
Q2711 hard code error
What is the output of this code?
java
import java.util.LinkedList;
import java.util.List;
import java.util.Arrays;

public class MyClass {
    public static void main(String[] args) {
        LinkedList<Integer> original = new LinkedList<>(Arrays.asList(10, 20, 30, 40, 50));
        List<Integer> subList = original.subList(1, 4); // [20, 30, 40]
        original.add(0, 5); // Structural modification to original list
        for (Integer i : subList) { // Iterating structurally modified subList
            System.out.print(i + " ");
        }
    }
}
Q2712 hard code output
What is the output of this code?
java
public class OperatorChallenge {
    public static void main(String[] args) {
        int i = 5;
        int j = i++ + ++i;
        System.out.println(j);
    }
}
Q2713 hard code output
What does this code print, demonstrating the behavior of a `final` array reference with mutable contents?
java
class DataHolder {
    private final int[] values;
    public DataHolder(int[] initialValues) {
        this.values = initialValues;
    }
    public int[] getValues() {
        return values;
    }
}

public class Main {
    public static void main(String[] args) {
        int[] originalArray = {10, 20, 30};
        DataHolder holder = new DataHolder(originalArray);
        System.out.println("Initial data: " + holder.getValues()[0]);
        int[] retrievedArray = holder.getValues();
        retrievedArray[0] = 100;
        System.out.println("Data after external modification: " + holder.getValues()[0]);
    }
}
Q2714 hard code error
Considering the use of reflection, what is the most likely error when executing the following Java code on a modern JVM (Java 12+)?
java
import java.lang.reflect.Field;

final class AppConfig {
    private final String version;
    public AppConfig(String version) { this.version = version; }
    public String getVersion() { return version; }
}

public class ReflectionFinalBreaker {
    public static void main(String[] args) throws Exception {
        AppConfig config = new AppConfig("1.0");
        Field versionField = AppConfig.class.getDeclaredField("version");
        versionField.setAccessible(true);
        versionField.set(config, "2.0"); // Attempt to set a final field via reflection
    }
}
Q2715 easy code error
What error will occur when attempting to serialize an instance of the `Book` class defined below?
java
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;

class Book {
    String title;
    int pages;

    public Book(String title, int pages) {
        this.title = title;
        this.pages = pages;
    }
}

public class Main {
    public static void main(String[] args) {
        Book book = new Book("Java Basics", 300);
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("book.ser"))) {
            oos.writeObject(book);
        } catch (IOException e) {
            System.err.println(e.getClass().getSimpleName() + ": " + e.getMessage());
        }
    }
}
Q2716 hard
Why is `HashSet` inherently not thread-safe, and what type of failure is most likely to occur if it's accessed concurrently by multiple threads without external synchronization?
Q2717 easy code output
What is the output of this Java code snippet?
java
public class Main {
    public static void main(String[] args) {
        boolean isSunny = true;
        boolean isWeekend = false;
        if (isSunny && isWeekend) {
            System.out.println("Go to the beach!");
        } else {
            System.out.println("Stay home and relax.");
        }
    }
}
Q2718 hard code error
What compile-time error will occur when compiling this Java code snippet?
java
public class Test {
    public static void main(String[] args) {
        int[][] arr;
        arr = new int[2,3];
        System.out.println(arr[0][0]);
    }
}
Q2719 easy code output
What is the output of this code?
java
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        try {
            System.out.println(fruits.get(2));
        } catch (IndexOutOfBoundsException e) {
            System.out.println("Error: " + e.getClass().getSimpleName());
        }
    }
}
Q2720 medium code error
What is the error in the following Java code?
java
class User {
    private int userId;
    private String username;

    public User(int userId, String username) {
        this.userId = userId;
        this.username = username;
    }
}

class UserDetailsUpdater {
    public void updateUsername(User user, String newUsername) {
        user.username = newUsername; // Direct access to private field
    }

    public static void main(String[] args) {
        User user = new User(1, "john_doe");
        new UserDetailsUpdater().updateUsername(user, "jane_doe");
    }
}
← Prev 134135136137138 Next → Page 136 of 200 · 3994 questions