☕ Java MCQ Questions – Page 152

Questions 3021–3040 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3021 easy code error
What is the compilation or runtime error in the following Java code?
java
public class Main {
    public static void main(String[] args) {
        String str = "Java";
        char c = str.charAt(str.length());
        System.out.println(c);
    }
}
Q3022 hard code error
What is the error in the logic of this Java code snippet that prevents it from terminating as expected?
java
public class ContinueInfiniteLoop {
    public static void main(String[] args) {
        int counter = 0;
        while (counter < 5) {
            System.out.println("Current counter: " + counter);
            if (counter % 2 == 0) {
                continue; // Skips counter++ if even
            }
            counter++; // This line is skipped for even numbers
        }
        System.out.println("Loop finished.");
    }
}
Q3023 medium code output
What is the output of this Java code?
java
class StatusThread extends Thread {
    public void run() {
        System.out.println(getName() + " running.");
        try { Thread.sleep(20); } catch (InterruptedException e) {}
        System.out.println(getName() + " finished.");
    }
}
public class Main {
    public static void main(String[] args) throws InterruptedException {
        StatusThread t = new StatusThread();
        System.out.println("Initial state: " + t.isAlive());
        t.start();
        // No sleep here, child might not have started printing yet.
        System.out.println("After start state: " + t.isAlive());
        t.join(); // Wait for child to complete
        System.out.println("Final state: " + t.isAlive());
    }
}
Q3024 hard
A developer needs to read a file that is known to contain only ASCII characters. They are concerned about optimal performance and minimal resource usage. Which statement about `FileReader` and its interaction with charsets for ASCII files is most accurate?
Q3025 easy code error
What kind of error will occur when compiling this Java code?
java
import java.io.BufferedWriter;
import java.io.FileWriter;

public class Main {
    public static void main(String[] args) {
        // The BufferedWriter constructor can throw IOException
        BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"));
        try {
            bw.write("Hello");
            bw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Q3026 medium code output
What is the output of this Java code?
java
@FunctionalInterface
interface StringTransformer {
    String transform(String s);
}

public class Test {
    public static void main(String[] args) {
        StringTransformer upperCase = String::toUpperCase;
        StringTransformer reverse = s -> new StringBuilder(s).reverse().toString();

        System.out.println(upperCase.transform("hello"));
        System.out.println(reverse.transform("world"));
    }
}
Q3027 medium
What role do abstract methods in an abstract class play in polymorphism?
Q3028 hard
What is the fundamental difference between using `yield` and `return` inside a `switch` *expression*?
Q3029 hard code error
What error occurs when running this Java code?
java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class IteratorCMEBetweenOps {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
        Iterator<Integer> it = numbers.iterator();
        
        it.next(); // 1
        it.remove(); // Removes '1', list is now [2, 3, 4, 5]
        
        numbers.add(0, 0); // External structural modification: list is now [0, 2, 3, 4, 5]
        
        it.next(); // Expect CME here
    }
}
Q3030 medium code output
What is the output of this code?
java
public class StringBuilderTest {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        sb.append("Hello");
        sb.append("World");
        System.out.println(sb);
    }
}
Q3031 easy code error
What type of error will occur when compiling or running this Java code snippet?
java
public class ArrayTest {
    public static void main(String[] args) {
        int[] numbers = new int[-5];
        System.out.println(numbers.length);
    }
}
Q3032 hard
If a `RuntimeException` occurs within a `finally` block, and there was already an exception pending from the `try` or `catch` block, which exception takes precedence and propagates?
Q3033 easy code output
What is the output of this Java code?
java
class MyThread extends Thread {
    public void run() {
        System.out.println("Thread started: " + Thread.currentThread().getName());
    }
}

public class TestThread {
    public static void main(String[] args) {
        MyThread t = new MyThread();
        t.start();
        System.out.println("Main thread: " + Thread.currentThread().getName());
    }
}
Q3034 medium
Why are getter and setter methods (accessor and mutator methods) crucial for maintaining proper encapsulation?
Q3035 medium
What is the primary purpose of a constructor in Java?
Q3036 easy code output
What does this code snippet print to the console?
java
class Product {
    String name;
    double price;

    public Product(String name) {
        this.name = name;
        this.price = 0.0;
    }
    public Product(String name, double price) {
        this.name = name;
        this.price = price;
    }
}
public class Main {
    public static void main(String[] args) {
        Product p = new Product("Keyboard");
        System.out.print("Name: " + p.name + ", Price: " + p.price);
    }
}
Q3037 medium
What is the primary purpose of the `intern()` method for a `String` object?
Q3038 hard code error
What is the compilation error, if any, for the provided Java code snippet?
java
class Overloader {
    public void process(long l, Integer... args) {}
    public void process(Long l, int... args) {}

    public static void main(String[] args) {
        Overloader o = new Overloader();
        o.process(10, 20); // Line X
    }
}
Q3039 medium
What is the primary purpose of creating custom exception classes in Java?
Q3040 easy code output
What is the output of this Java code?
java
public class MyTask implements Runnable {
    @Override
    public void run() {
        System.out.println("Task is running in a new thread.");
    }

    public static void main(String[] args) {
        MyTask task = new MyTask();
        Thread thread = new Thread(task);
        thread.start();
    }
}
← Prev 150151152153154 Next → Page 152 of 200 · 3994 questions