☕ Java MCQ Questions – Page 60

Questions 1181–1200 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1181 medium code output
What does this code print?
java
import java.util.Date;

public class CustomImmutabilityIssue {
    private final Date creationDate;

    public CustomImmutabilityIssue(Date date) {
        this.creationDate = date;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public static void main(String[] args) {
        Date originalDate = new Date(100, 0, 1);
        CustomImmutabilityIssue obj = new CustomImmutabilityIssue(originalDate);
        originalDate.setYear(101);
        System.out.println(obj.getCreationDate().getYear());
    }
}
Q1182 hard code output
What is the output of this code?
java
import java.io.*;

class Node implements Serializable {
    private static final long serialVersionUID = 1L;
    String name;
    Node next;
    Node prev;

    public Node(String name) {
        this.name = name;
    }

    public void setNext(Node next) { this.next = next; }
    public void setPrev(Node prev) { this.prev = prev; }

    public String getConnections() {
        String nextName = (next != null) ? next.name : "null";
        String prevName = (prev != null) ? prev.name : "null";
        return name + " -> " + nextName + " <- " + prevName;
    }
}

public class SerializationTest {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Node nodeA = new Node("A");
        Node nodeB = new Node("B");
        
        nodeA.setNext(nodeB);
        nodeB.setPrev(nodeA);
        
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(nodeA);
        oos.close();

        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        Node deserializedA = (Node) ois.readObject();
        ois.close();

        Node deserializedB = deserializedA.next;

        System.out.println("Node A connections: " + deserializedA.getConnections());
        System.out.println("Node B connections: " + deserializedB.getConnections());
        System.out.println("Are A.next and B the same object? " + (deserializedA.next == deserializedB));
        System.out.println("Are B.prev and A the same object? " + (deserializedB.prev == deserializedA));
    }
}
Q1183 easy code error
What error occurs when calling `start()` on a `null` `Thread` reference?
java
public class Main {
    public static void main(String[] args) {
        Thread t = null;
        t.start(); // Calling start() on a null reference
    }
}
Q1184 medium code error
What kind of error will occur when executing this Java code?
java
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        TreeSet set = new TreeSet(); // Raw type
        set.add(Integer.valueOf(5));
        set.add("Hello");
        System.out.println(set.size());
    }
}
Q1185 easy code error
What will happen when this Java code is compiled?
java
public class MyClass {
    public static void main(String[] args) {
        price = 99.99;
        System.out.println(price);
    }
}
Q1186 easy code output
What is the output of this code?
java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<Character> chars = new ArrayList<>();
        chars.add('X');
        chars.add('Y');
        chars.add('Z');
        Iterator<Character> it = chars.iterator();
        int count = 0;
        while (it.hasNext()) {
            it.next();
            count++;
        }
        System.out.println(count);
    }
}
Q1187 hard code error
What runtime error will this Java code snippet throw?
java
public class Test {
    public static void main(String[] args) {
        Object[][] data = new Object[2][];
        data[0] = new String[]{"A", "B"};
        System.out.println(data[1][0]);
    }
}
Q1188 easy
By default, what data type does a decimal literal (e.g., `3.14`) represent in Java?
Q1189 easy code error
What kind of error will occur when compiling this Java code?
java
import java.io.BufferedWriter;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        try {
            // BufferedWriter requires a Writer object, not a String directly
            BufferedWriter bw = new BufferedWriter("output.txt");
            bw.write("Data");
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Q1190 hard code error
What is the error encountered when running this Java code?
java
public class SystemArrayCopyNullDest {
    public static void main(String[] args) {
        int[] source = {1, 2, 3};
        int[] destination = null;
        System.arraycopy(source, 0, destination, 0, source.length);
        System.out.println(source[0]);
    }
}
Q1191 hard code output
What does this code print?
java
public class StringStripTest {
    public static void main(String[] args) {
        String s = "\u2005 Hello \n "; // U+2005 is a Four-Per-Em Space
        System.out.println(s.trim().length() + ", " + s.strip().length());
    }
}
Q1192 medium
Which of the following statements about the `run()` method of the `Runnable` interface is true?
Q1193 hard
What determines the lifetime of a local variable declared within a method in Java?
Q1194 hard code error
Which exception is thrown when the `main` method of this Java code is executed?
java
public class StringInternNull {
    public static void main(String[] args) {
        String str = null;
        try {
            String interned = str.intern();
            System.out.println(interned);
        } catch (Exception e) {
            System.out.println(e.getClass().getSimpleName());
        }
    }
}
Q1195 medium code output
What does this code print?
java
import java.util.TreeMap;

public class TreeMapReplaceValue {
    public static void main(String[] args) {
        TreeMap<String, Integer> map = new TreeMap<>();
        map.put("A", 1);
        map.put("B", 2);
        map.put("A", 3);
        System.out.println(map.get("A") + "," + map.size());
    }
}
Q1196 easy code output
What is the output of this code?
java
public class Main {
    public static void main(String[] args) {
        int[][] matrix = {{1, 2, 3}, {4, 5, 6}};
        System.out.println(matrix[0][1]);
    }
}
Q1197 medium code output
What is the output of the following Java program with nested try-finally?
java
public class NestedTryFinally {
    public static void main(String[] args) {
        try {
            System.out.println("Outer try");
            try {
                System.out.println("Inner try");
                throw new Exception("Inner Exception");
            } finally {
                System.out.println("Inner finally");
            }
        } catch (Exception e) {
            System.out.println("Outer catch: " + e.getMessage());
        } finally {
            System.out.println("Outer finally");
        }
    }
}
Q1198 easy
Which method converts all characters in a string to lowercase?
Q1199 easy
Which keyword can be used to immediately exit a `while` loop before its condition becomes `false`?
Q1200 medium code error
What error occurs when running this Java code?
java
public class ThrowNull {
    public static void causeError() {
        Throwable t = null;
        throw t;
    }

    public static void main(String[] args) {
        causeError();
    }
}
← Prev 5859606162 Next → Page 60 of 200 · 3994 questions