☕ Java MCQ Questions – Page 29

Questions 561–580 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q561 medium code output
What does this code print?
java
import java.io.*;

class MyDataV1 implements Serializable {
    private static final long serialVersionUID = 1L;
    private String value;
    public MyDataV1(String value) { this.value = value; }
    public String getValue() { return value; }
}

class MyDataV2 implements Serializable {
    private static final long serialVersionUID = 2L; // Changed serialVersionUID
    private String value;
    private int count; 
    public MyDataV2(String value, int count) { this.value = value; this.count = count; }
    public String getValue() { return value + ":" + count; }
}

public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        try {
            MyDataV1 dataV1 = new MyDataV1("Original");
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(dataV1);
            oos.close();

            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);
            MyDataV2 deserializedData = (MyDataV2) ois.readObject();
            ois.close();
            System.out.println(deserializedData.getValue()); 
        } catch (Exception e) {
            System.out.println(e.getClass().getSimpleName());
        }
    }
}
Q562 hard code error
What is the expected outcome when executing this Java code snippet?
java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class UnmodifiableListAttempt {
    public static void main(String[] args) {
        List<String> mutableList = new ArrayList<>();
        mutableList.add("Item1");
        
        List<String> unmodifiableList = Collections.unmodifiableList(mutableList);
        unmodifiableList.add("Item2"); // Attempt to modify unmodifiable list
    }
}
Q563 easy code output
What is the output of this Java code?
java
class ResourceNotFoundException extends Exception {
    public ResourceNotFoundException(String resourceName) {
        super("Resource '" + resourceName + "' not found.");
    }
}

public class Main {
    public static void findResource(String name) throws ResourceNotFoundException {
        throw new ResourceNotFoundException(name);
    }

    public static void main(String[] args) {
        try {
            findResource("config.xml");
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}
Q564 medium code output
What is the output of this Java code?
java
import java.util.function.Function;

class StringHelper {
    public static String reverse(String s) {
        return new StringBuilder(s).reverse().toString();
    }
}

public class MethodRefTest {
    public static void main(String[] args) {
        Function<String, String> reverser = StringHelper::reverse;
        String original = "hello";
        String reversed = reverser.apply(original);
        System.out.println(original + " -> " + reversed);
    }
}
Q565 medium code error
What type of error will occur when compiling the following Java code?
java
public class Main {
    public static void main(String[] args) {
        long longValue = 100L;
        int intValue = longValue;
        System.out.println(intValue);
    }
}
Q566 easy
In an encapsulated class, what types of methods are typically provided to allow controlled access to private instance variables?
Q567 easy
What is the primary purpose of a variable in Java programming?
Q568 medium
Which of the following is an example of compile-time polymorphism in Java?
Q569 hard code output
Assume a file named 'chars.txt' exists and contains the text "ABC". What is the output of this code?
java
import java.io.FileReader;
import java.io.IOException;
import java.io.File;

public class FileReaderCloseInLoop {
    public static void main(String[] args) throws IOException {
        // Assume 'chars.txt' has content: "ABC"
        StringBuilder sb = new StringBuilder();
        File file = new File("chars.txt");
        try (FileReader reader = new FileReader(file)) {
            int c;
            while ((c = reader.read()) != -1) {
                sb.append((char) c);
                reader.close(); // Closing inside the loop
            }
        } catch (IOException e) {
            sb.append("\nError: " + e.getMessage());
        }
        System.out.println(sb.toString());
    }
}
Q570 medium
A variable declared inside the `do { ... }` block of a `do-while` loop in Java:
Q571 hard code error
What is the result of running this Java code?
java
import java.util.TreeMap;
import java.util.Map;
import java.util.Iterator;

public class TreeMapError4 {
    public static void main(String[] args) {
        TreeMap<Integer, String> map = new TreeMap<>();
        map.put(1, "A"); map.put(2, "B"); map.put(3, "C"); map.put(4, "D");

        Map<Integer, String> subMap = map.subMap(2, true, 4, false);
        Iterator<Map.Entry<Integer, String>> iterator = subMap.entrySet().iterator();

        while (iterator.hasNext()) {
            Map.Entry<Integer, String> entry = iterator.next();
            System.out.println("Processing " + entry.getKey());
            if (entry.getKey() == 2) {
                map.put(5, "E"); 
            }
        }
    }
}
Q572 medium
Which interface must a class implement to allow its instances to be targets of the enhanced for-loop (for-each loop) in Java?
Q573 hard
Consider an `ArrayList<String> list = new ArrayList<>(Arrays.asList("A", "B", "C"));` and an `Iterator<String> it = list.iterator();`. If `it.next();` is called once, then `it.remove();` is called, what is the exact state and behavior if `it.remove();` is called *again* immediately after?
Q574 medium code error
What is the output of this Java code?
java
public class ExceptionFlow7 {
    public static void main(String[] args) {
        testOuter();
    }

    public static void testOuter() {
        try {
            System.out.println("Outer Try");
            try {
                System.out.println("Inner Try");
                throw new RuntimeException("Inner Exception");
            } catch (RuntimeException e) {
                System.out.println("Inner Catch: " + e.getMessage());
            } finally {
                System.out.println("Inner Finally");
            }
            System.out.println("After Inner Block");
        } finally {
            System.out.println("Outer Finally");
        }
    }
}
Q575 medium
How is the principle of encapsulation primarily achieved in Java?
Q576 hard code error
What is the compile-time error in this Java code snippet involving switch pattern matching?
java
public class UnreachableCasePatternError {
    public static void main(String[] args) {
        Object obj = 10;
        String type = switch (obj) {
            case Integer i -> "Integer: " + i;
            case Number n -> "Number: " + n; // Error expected here
            case String s -> "String: " + s;
            default -> "Unknown";
        };
        System.out.println(type);
    }
}
Q577 medium code output
What is the output of this code?
java
class MyCustomException extends Exception {
    public MyCustomException(String message) {
        super(message);
    }
}

public class Main {
    public static void main(String[] args) {
        try {
            throw new MyCustomException("Something went wrong!");
        } catch (MyCustomException e) {
            System.out.println("Caught: " + e.getMessage());
        }
    }
}
Q578 hard
Which statement best describes the effect of calling `Thread.yield()`?
Q579 easy
Which method is used to insert characters at a specified index within a `StringBuilder` object?
Q580 hard code error
What error will this Java code produce when compiled and run?
java
public class ArrayCopyError {
    public static void main(String[] args) {
        int[] source = {1, 2, 3, 4, 5};
        int[] dest = new int[3];
        // Attempt to copy 5 elements into a destination array of size 3
        System.arraycopy(source, 0, dest, 0, 5);
        for (int i : dest) {
            System.out.print(i + " ");
        }
    }
}
← Prev 2728293031 Next → Page 29 of 200 · 3994 questions