☕ Java MCQ Questions – Page 67

Questions 1321–1340 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1321 medium code error
What is the compilation error in the provided Java code?
java
abstract class Processor {
    public final abstract void processData();
}

class DataProcessor extends Processor {
    public void processData() {
        System.out.println("Processing data...");
    }
}
Q1322 easy code error
What compilation error will occur in the following Java code?
java
abstract class Logger {
    private abstract void logMessage(String message);
}
Q1323 hard code output
What does this Java code print?
java
public class ThreadStateTransition {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // ignore
            }
            System.out.println("Worker thread finished.");
        });

        System.out.println("State 1: " + t.getState());
        t.start();
        System.out.println("State 2: " + t.getState());
        Thread.sleep(50);
        System.out.println("State 3: " + t.getState());
        t.join();
        System.out.println("State 4: " + t.getState());
    }
}
Q1324 hard code output
What is the output of this code?
java
public class DataTypeChallenge {
    public static void main(String[] args) {
        long l1 = 2_147_483_647 + 1;
        long l2 = 2_147_483_647L + 1;
        System.out.println(l1);
        System.out.println(l2);
    }
}
Q1325 hard code output
What is the output of this code? (Assuming Java 17+ for `switch` expression syntax, but `case null` is explicitly NOT handled)
java
public class SwitchTest {
    public static void main(String[] args) {
        Integer value = null;
        String result = switch (value) {
            case 1 -> "One";
            case 2 -> "Two";
            default -> "Other";
        };
        System.out.println(result);
    }
}
Q1326 hard code error
What is the most likely error when executing this Java code snippet?
java
import java.io.BufferedWriter;
import java.io.StringWriter;
import java.io.IOException;

public class Test {
    public static void main(String[] args) {
        StringWriter sw = new StringWriter();
        BufferedWriter bw = new BufferedWriter(sw);
        try (bw) {
            bw.write("Line one");
        } catch (IOException e) {
            System.err.println(e.getMessage());
        }
        try {
            bw.newLine(); // Attempt to use after close
        } catch (IOException e) {
            System.err.println("Post-close error: " + e.getMessage());
        }
        System.out.println("Final content: " + sw.toString());
    }
}
Q1327 easy code error
What kind of error will occur when this Java code is executed?
java
public class Test {
    public static void main(String[] args) {
        StringBuilder sb = "Hello";
        System.out.println(sb);
    }
}
Q1328 easy
What is the default value for elements of an `int` array in Java when it's initialized with a size but no explicit values?
Q1329 easy
What is the average time complexity for accessing an element by its index (e.g., `get(index)`) in a `java.util.LinkedList`?
Q1330 easy code error
What will happen when this Java code is executed?
java
public class LoopError6 {
    public static void main(String[] args) {
        boolean keepGoing = true;
        int count = 0;
        while (keepGoing) {
            System.out.println("Iteration: " + count);
            count++;
            if (count > 5) {
                // keepGoing = false; // Missing this line
            }
        }
    }
}
Q1331 medium
What is the primary benefit of using a `BufferedWriter` over directly using a `FileWriter` for writing large amounts of text data to a file in Java?
Q1332 medium code error
What error will occur when running this Java code snippet?
java
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

class CustomKey {
    String name;
    Integer id;

    public CustomKey(String name, Integer id) {
        this.name = name;
        this.id = id;
    }

    @Override
    public int hashCode() {
        return name.hashCode() + id.hashCode(); // No null check for id
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        CustomKey other = (CustomKey) obj;
        return Objects.equals(name, other.name) && Objects.equals(id, other.id);
    }
}

public class Test {
    public static void main(String[] args) {
        Map<CustomKey, String> map = new HashMap<>();
        CustomKey key1 = new CustomKey("Test", 1);
        CustomKey key2 = new CustomKey("Test", null); // id is null
        map.put(key1, "Value1");
        map.put(key2, "Value2"); // This put operation triggers hashCode()
        System.out.println(map.get(key2));
    }
}
Q1333 easy code error
What is the compilation error in the following Java code snippet?
java
public class LoopError {
    public static void main(String[] args) {
        int y;
        do {
            System.out.println("Looping...");
            y = 10;
        } while (y > 5);
    }
}
Q1334 easy code error
What error will occur when compiling the following Java code?
java
public class Main {
    public static void main(String[] args) {
        byte b = 10;
        b = b + 5;
        System.out.println(b);
    }
}
Q1335 medium
Which of the following exceptions might be thrown directly by the `FileReader(String fileName)` constructor if the specified file does not exist?
Q1336 easy code output
What is the output of the following Java program?
java
class Book {
    String title;
    int pages;
    boolean isAvailable;
}
public class Main {
    public static void main(String[] args) {
        Book myBook = new Book();
        System.out.print("Title: " + myBook.title + ", Pages: " + myBook.pages + ", Available: " + myBook.isAvailable);
    }
}
Q1337 hard code output
What is the output of this code?
java
public class StaticInitOrder {
    static int i = 10; // Initializer 1
    static {
        i = 20; // Static block 1
    }
    public StaticInitOrder() {
        i = 30; // Constructor
    }
    public static void main(String[] args) {
        System.out.println(i); // Main execution
        new StaticInitOrder(); // Create instance
        System.out.println(i);
    }
}
Q1338 hard code output
What is the output of the following Java program?
java
class Base {
    private void show() { System.out.println("Base::show() - private"); }
    public void print() { show(); }
}
class Derived extends Base {
    public void show() { System.out.println("Derived::show() - public"); }
}
public class Main {
    public static void main(String[] args) {
        Base b = new Derived();
        b.print();
    }
}
Q1339 hard code output
What is the output of this code?
java
public class NestedSharedWhile {
    public static void main(String[] args) {
        int i = 0;
        String s = "";
        while (i < 3) {
            s += "O";
            int j = 0; 
            while (j < 2) {
                s += "I";
                if (i == 1 && j == 0) {
                    break;
                }
                j++;
            }
            i++;
        }
        System.out.println(s);
    }
}
Q1340 easy code error
What is the error in this Java code?
java
import java.util.HashMap;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        Map<String, String> myMap = new HashMap<>();
        myMap.put("name", "Alice");
        String city = myMap.get("city");
        System.out.println(city.length());
    }
}
← Prev 6566676869 Next → Page 67 of 200 · 3994 questions