☕ Java MCQ Questions – Page 56

Questions 1101–1120 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1101 easy code error
What error will this Java code produce when executed?
java
public class ArrayError {
    public static void main(String[] args) {
        int[][] matrix = {{1, 2}, {3, 4}};
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j <= matrix[i].length; j++) { // Loop condition error
                System.out.print(matrix[i][j]);
            }
        }
    }
}
Q1102 hard code error
What error will this code produce at runtime?
java
import java.util.ArrayList;
import java.util.List;

public class ConcurrentModLoop {
    public static void main(String[] args) {
        List<String> items = new ArrayList<>();
        items.add("Apple");
        items.add("Banana");
        items.add("Cherry");

        for (String item : items) {
            if (item.equals("Banana")) {
                items.remove(item);
            }
        }
        System.out.println(items);
    }
}
Q1103 medium
Which of the following is NOT a direct benefit of applying encapsulation in Java programming?
Q1104 hard
Consider the declaration: `int[][] matrix = new int[3][];` Which statement about the state of `matrix` after this declaration is true?
Q1105 easy code output
What is the output of this Java program?
java
import java.io.*;

class Person implements Serializable {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String toString() {
        return "Name: " + name + ", Age: " + age;
    }
}

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

        p = new Person("Bob", 25); // Original object reference changed

        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        Person deserializedP = (Person) ois.readObject();
        ois.close();

        System.out.println(deserializedP);
    }
}
Q1106 medium code error
What is the compile-time or runtime error in the following Java code snippet when `main` method is executed?
java
import java.io.*;

class TaskContainer implements Serializable {
    // Runnable is not Serializable by default, and lambda expressions are tricky to serialize.
    private Runnable task = () -> System.out.println("Executing task");
    private String description = "Simple Task";
}

public class SerializationQuestion5 {
    public static void main(String[] args) throws IOException {
        TaskContainer container = new TaskContainer();
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("task.ser"))) {
            oos.writeObject(container);
        }
    }
}
Q1107 easy code error
What compile-time error will this Java code produce?
java
public class DataTypeCharStringError {
    public static void main(String[] args) {
        char initial = "J";
        System.out.println(initial);
    }
}
Q1108 hard code error
What is the compile-time error in the provided Java code snippet?
java
public class InvalidSwitchSelectorType {
    public static void main(String[] args) {
        long id = 12345L;
        String category = "Unknown";
        switch (id) { // Error expected here
            case 100L: category = "A"; break;
            case 200L: category = "B"; break;
            default: category = "Other";
        }
        System.out.println(category);
    }
}
Q1109 easy code error
What error will occur after deserializing the `User` object, given the `password` field is `transient`?
java
import java.io.*;

class User implements Serializable {
    String username;
    transient String password; 

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }
    public String getPassword() { return password; }
}

public class Main {
    public static void main(String[] args) {
        User user = new User("admin", "secret123");
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("user.ser"))) {
            oos.writeObject(user);
        } catch (IOException e) { /* handle exception */ }

        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("user.ser"))) {
            User deserializedUser = (User) ois.readObject();
            System.out.println(deserializedUser.getPassword().length()); // Access transient field
        } catch (IOException | ClassNotFoundException e) {
            System.err.println(e.getClass().getSimpleName() + ": " + e.getMessage());
        }
    }
}
Q1110 hard
How does Java 8+ allow for a form of 'multiple inheritance of implementation' and what mechanism resolves potential conflicts?
Q1111 hard
A `TreeSet<Integer>` named `mainSet` contains `{1, 2, 3, 4, 5}`. A `headSet` view is created as `viewSet = mainSet.headSet(3, true)`. If `viewSet.remove(2)` is called, what is the state of `mainSet` afterwards?
Q1112 medium code error
What error will occur when compiling or running this Java code?
java
public class ArrayError {
    public static void main(String[] args) {
        Object[] myObjects = new String[3];
        myObjects[0] = new Integer(123);
    }
}
Q1113 easy code error
What will happen when you try to compile this Java code?
java
class MyValue {
    private final int val;
    public MyValue(int v) {
        this.val = v;
    }
    public void attemptChange(int newVal) {
        this.val = newVal;
    }
    public int getVal() { return val; }
}
public class ImmutableInternalReassignment {
    public static void main(String[] args) {
        MyValue mv = new MyValue(100);
        mv.attemptChange(200);
        System.out.println(mv.getVal());
    }
}
Q1114 hard code error
What is the compile-time error in this Java code?
java
public class ParameterizedRun implements Runnable {
    @Override
    public void run(String message) { // Line 3
        System.out.println(message);
    }

    public static void main(String[] args) {
        // new Thread(new ParameterizedRun()).start();
    }
}
Q1115 easy
What is the default value for an uninitialized instance variable of a reference type (e.g., String, Object) in Java?
Q1116 hard code output
What is the output of this code?
java
public class Test {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("JavaIsFun");
        sb.setLength(15);
        sb.setCharAt(10, 'A');
        sb.setLength(7);
        System.out.println(sb.toString());
    }
}
Q1117 hard
You have a class `class MyKey { int id; MyKey(int id) { this.id = id; } }` which does *not* implement `Comparable`. If you create `TreeMap<MyKey, String> map = new TreeMap<>();` and then call `map.put(new MyKey(1), "value");`, what will occur?
Q1118 easy
What does a Java HashMap primarily store?
Q1119 easy code output
What is the output of this code?
java
abstract class Animal {
    abstract String makeSound();
}

class Dog extends Animal {
    @Override
    String makeSound() {
        return "Woof";
    }
}

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        System.out.println(myDog.makeSound());
    }
}
Q1120 easy
What are the default values for elements in a newly initialized `int[][]` array in Java?
← Prev 5455565758 Next → Page 56 of 200 · 3994 questions