☕ Java MCQ Questions – Page 52

Questions 1021–1040 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1021 easy code output
What does this Java code print?
java
public class Main {
    public static void main(String[] args) {
        int temp = 25;
        if (temp > 30) {
            System.out.println("It's hot!");
        } else {
            System.out.println("It's pleasant.");
        }
    }
}
Q1022 medium
Which statement correctly describes a Java abstract class?
Q1023 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 Logger implements Serializable {
    private String logName;
    // FileInputStream is not serializable
    private FileInputStream fis; 

    public Logger(String name, String filePath) throws FileNotFoundException {
        this.logName = name;
        this.fis = new FileInputStream(filePath); 
    }
}

public class SerializationQuestion10 {
    public static void main(String[] args) throws IOException {
        // Create a dummy file for FileInputStream to exist
        try (FileOutputStream tempFos = new FileOutputStream("dummy.txt")) {
            tempFos.write("temp data".getBytes());
        }

        Logger logger = new Logger("AppLog", "dummy.txt");
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("logger.ser"))) {
            oos.writeObject(logger);
        }
    }
}
Q1024 medium
Given the String `input = "one,,two,three"`, what would be the length of the array returned by `input.split(",")`?
Q1025 medium
Which of the following Java Collection API methods commonly accepts a lambda expression, typically an instance of `Comparator`, for custom sorting behavior?
Q1026 hard code output
What does this code print?
java
interface Calculator {
    static int add(int a, int b) {
        return a + b;
    }
    default int subtract(int a, int b) {
        return a - b;
    }
}

class BasicCalculator implements Calculator {}

public class StaticInterfaceTest {
    public static void main(String[] args) {
        System.out.println(Calculator.add(10, 5));
    }
}
Q1027 easy code output
What does this code print?
java
interface Drivable {
    void drive();
}

class Truck implements Drivable {
    @Override
    public void drive() {
        System.out.println("Driving a truck.");
    }
}

public class Main {
    public static void main(String[] args) {
        Drivable d = new Truck();
        d.drive();
    }
}
Q1028 hard
Given an `ArrayList<String> list = new ArrayList<>(Arrays.asList("A", "B", "C"));` and a `ListIterator<String> lit = list.listIterator();`. After calling `lit.next();` and then `lit.set("Z");`, what is the effect on the list's `modCount` and how does this affect subsequent `next()` or `previous()` calls by `lit`?
Q1029 medium
If you declare `String[][] names = new String[2][];` but do not initialize the inner arrays (e.g., `names[0] = new String[3];`), what will be the value of `names[0]`?
Q1030 hard code output
What does this code print?
java
import java.util.Arrays;

public class ArrayFillObjectReference {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello");
        StringBuilder[] sbs = new StringBuilder[3];
        Arrays.fill(sbs, sb);
        sbs[0].append(" World");
        
        for (StringBuilder s : sbs) {
            System.out.println(s);
        }
    }
}
Q1031 easy code output
What does this code print?
java
public class Main {
    public static void main(String[] args) {
        int value = 0;
        try {
            value = 10;
            int x = 1 / 0; // Throws ArithmeticException
        } catch (ArithmeticException e) {
            value = 30;
        } finally {
            value = 40;
        }
        System.out.println(value);
    }
}
Q1032 easy code error
What kind of error will this Java code produce?
java
public class LoopError3 {
    public static void main(String[] args) {
        int value; // Not initialized
        while (value < 10) { // Error here
            System.out.println("Value is: " + value);
            value++;
        }
    }
}
Q1033 medium code error
What error occurs when running this Java code?
java
public class MyClass {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Java");
        sb.deleteCharAt(4);
        System.out.println(sb);
    }
}
Q1034 hard code output
What is the output of this Java code?
java
String base = "base";
String ext = "ext";
String fullLiteral = "baseext";
String concatenated1 = base + ext;
String concatenated2 = "base" + "ext";
String interned = (base + ext).intern();

System.out.println(fullLiteral == concatenated1);
System.out.println(fullLiteral == concatenated2);
System.out.println(fullLiteral == interned);
Q1035 easy code error
What compile-time error will occur in the `Child` class?
java
class Parent {
    public final void greeting() {
        System.out.println("Hello from Parent");
    }
}

class Child extends Parent {
    @Override
    public void greeting() {
        System.out.println("Hello from Child");
    }
}
Q1036 medium code error
Identify the runtime error in the following Java code snippet.
java
import java.util.TreeMap;

class CustomKey {
    int id;
    public CustomKey(int id) { this.id = id; }
}

public class Main {
    public static void main(String[] args) {
        TreeMap<CustomKey, String> map = new TreeMap<>();
        map.put(new CustomKey(1), "One");
        map.put(new CustomKey(2), "Two");
    }
}
Q1037 medium code error
What kind of error will occur when compiling the following Java code, assuming 'existing.txt' exists?
java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class TestClass {
    public static void main(String[] args) {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader("existing.txt"));
            String line = br.readLine();
            System.out.println(line);
        } catch (IOException e) {
            System.err.println("Error reading file: " + e.getMessage());
        } finally {
            if (br != null) {
                br.close();
            }
        }
    }
}
Q1038 hard
Consider a class `Wallet` with a `private List<String> notes;` field. To properly encapsulate `notes` when returned via a public getter, which approach is crucial?
Q1039 hard
A thread `T` is in the `TIMED_WAITING` state after calling `Thread.sleep(1000)`. If another thread calls `T.interrupt()` immediately, what state will `T` enter after the interruption?
Q1040 medium code error
What error will this Java code produce?
java
public class ArrayError {
    public static void main(String[] args) {
        int[][] matrix = new { {1,2}, {3,4} };
        System.out.println(matrix[0][0]);
    }
}
← Prev 5051525354 Next → Page 52 of 200 · 3994 questions