☕ Java MCQ Questions – Page 127

Questions 2521–2540 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2521 easy code output
What is the output of this code?
java
public class DataTypeTest {
    public static void main(String[] args) {
        long bigNumber = 9876543210L;
        System.out.println(bigNumber);
    }
}
Q2522 hard code error
What is the compile-time error in this Java code if any?
java
import java.io.IOException;
interface Service {
    void operation();
}
class ConcreteService implements Service {
    @Override
    public void operation() throws IOException {
        System.out.println("Performing operation");
        throw new IOException("Service error");
    }
}
public class Main {
    public static void main(String[] args) {}
}
Q2523 hard code error
What is the error encountered when compiling this Java code?
java
public class VarargsError {
    static void processStrings(String... values) {
        for (String s : values) {
            System.out.print(s + " ");
        }
    }

    public static void main(String[] args) {
        Object[] data = {"hello", 123, true};
        processStrings(data);
    }
}
Q2524 hard code error
What is the error encountered when running this Java code?
java
import java.io.FileReader;
import java.io.IOException;
import java.io.File;

public class FileReaderError7 {
    public static void main(String[] args) {
        File file = new File("test_fr7.txt");
        try {
            file.createNewFile();
            try (FileReader fr = new FileReader(file)) {
                char[] buffer = new char[10];
                fr.read(buffer, -1, 5); // Negative offset
                System.out.println("Read successfully");
            }
        } catch (IOException e) {
            System.err.println(e.getClass().getSimpleName() + ": " + e.getMessage());
        } catch (IndexOutOfBoundsException e) {
            System.err.println(e.getClass().getSimpleName());
        } finally {
            file.delete();
        }
    }
}
Q2525 hard code error
What error will prevent the compilation of the following Java enum definition?
java
enum UserRole {
    ADMIN("Administrator"),
    EDITOR("Content Editor");

    private final String description; // Field is final

    UserRole(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String newDescription) {
        this.description = newDescription; // Attempt to reassign a final field
    }
}

public class EnumImmutability {
    public static void main(String[] args) {
        UserRole.ADMIN.setDescription("Super Admin");
    }
}
Q2526 medium code output
What does this Java code print?
java
import java.util.TreeSet;

class MyObject {
    int id;
    MyObject(int id) { this.id = id; }
    public String toString() { return "MyObject-" + id; }
}

public class Test {
    public static void main(String[] args) {
        TreeSet<MyObject> myObjects = new TreeSet<>();
        try {
            myObjects.add(new MyObject(1));
            myObjects.add(new MyObject(2));
            System.out.println(myObjects);
        } catch (ClassCastException e) {
            System.out.println("ClassCastException caught!");
        }
    }
}
Q2527 hard
Given the following Java code: java int[][] arr = new int[2][2]; arr[0][0] = 1; arr[0][1] = 2; arr[1][0] = 3; arr[1][1] = 4; int[] newRow = {5, 6}; arr[0] = newRow; System.out.println(arr[0][0] + " " + arr[1][0]); What is the output of this code?
Q2528 hard
If a `while(true)` loop is used within a method, what is the most idiomatic and graceful way to terminate both the loop and the method's execution upon a specific condition being met, without throwing an exception or stopping the entire JVM?
Q2529 easy
Given an array `String[] names = {"Alice", "Bob", "Charlie"};`, how would you access the element "Bob"?
Q2530 easy code error
Which of the following compile-time errors will occur in this Java snippet?
java
public class DataTypeShortError {
    public static void main(String[] args) {
        short value = 33000;
        System.out.println(value);
    }
}
Q2531 easy code output
What does this code print to the console?
java
import java.io.*;

class SimpleData implements Serializable {
    private int id;
    public SimpleData(int id) {
        this.id = id;
    }
    public int getId() {
        return id;
    }
}

public class Main {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        SimpleData data1 = new SimpleData(10);
        SimpleData data2 = new SimpleData(20);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(data1);
        oos.writeObject(data2); 
        oos.close();

        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        SimpleData readData1 = (SimpleData) ois.readObject();
        SimpleData readData2 = (SimpleData) ois.readObject(); 
        ois.close();

        System.out.println(readData1.getId() + ", " + readData2.getId());
    }
}
Q2532 easy code error
What error will occur when running this code, assuming `data.ser` was created with a serialized object of a class named `MyOldClass` that is no longer on the classpath?
java
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("data.ser"))) {
            Object obj = ois.readObject(); // This will try to load MyOldClass
            System.out.println("Object deserialized.");
        } catch (IOException | ClassNotFoundException e) {
            System.err.println(e.getClass().getSimpleName() + ": " + e.getMessage());
        }
    }
}
Q2533 hard code error
What is the result of running this Java code?
java
import java.util.TreeMap;

public class TreeMapError6 {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        TreeMap map = new TreeMap(); 
        map.put(1, "Integer Key");
        map.put("Two", "String Key"); 
        System.out.println(map.size());
    }
}
Q2534 easy code output
What is the output of this Java program?
java
class Counter {
    static {
        System.out.println("Static block executed.");
    }
    Counter() {
        System.out.println("Counter object created.");
    }
}
public class Main {
    public static void main(String[] args) {
        System.out.println("Main method start.");
        Counter c1 = new Counter();
        Counter c2 = new Counter();
    }
}
Q2535 easy code output
What does this code print?
java
public class Main {
    public static void main(String[] args) {
        int[][] jaggedArray = new int[3][];
        jaggedArray[0] = new int[]{1, 2};
        jaggedArray[1] = new int[]{3};
        jaggedArray[2] = new int[]{4, 5, 6};
        System.out.println(jaggedArray[2][1]);
    }
}
Q2536 easy
For frequent string concatenations within a loop, using `StringBuilder` is generally preferred over using the `+` operator with `String` objects because:
Q2537 easy code output
What will be the content of the file 'output.txt' after this code executes?
java
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterTest {
    public static void main(String[] args) {
        try (FileWriter writer1 = new FileWriter("output.txt")) {
            writer1.write("First Line");
        } catch (IOException e) {
            e.printStackTrace();
        }
        try (FileWriter writer2 = new FileWriter("output.txt")) {
            writer2.write("Second Line");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Q2538 hard code error
What error will this code produce at compile time?
java
public class VarForLoopUpdateError {
    public static void main(String[] args) {
        for (var i = 0, j = 0; i < 5; var k = i++, j++) {
            System.out.println(i + j);
        }
    }
}
Q2539 easy
Which of the following is a correct way to declare a two-dimensional integer array in Java?
Q2540 medium code error
What error will this Java code produce when executed?
java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();
        numbers.add(10);
        numbers.add(20);
        Iterator<Integer> it = numbers.iterator();
        if (it.hasNext()) {
            it.next(); // Retrieves 10
            it.remove(); // Removes 10
            it.remove(); // Attempts to remove again for the same next() call
        }
    }
}
← Prev 125126127128129 Next → Page 127 of 200 · 3994 questions