☕ Java MCQ Questions – Page 142

Questions 2821–2840 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2821 easy code error
What is the compilation error in the following Java code snippet?
java
import java.util.ArrayList;

public class Question7 {
    public static void main(String[] args) {
        ArrayList<String> words = new ArrayList<>();
        words.add("hello");
        words.add("world");
        for (Integer w : words) {
            System.out.println(w);
        }
    }
}
Q2822 easy
What does FIFO stand for in the context of data structures like Queue?
Q2823 hard code error
Analyze the following Java code. Which error will it produce?
java
import java.io.*;

public class FileWriterError9 {
    public static void main(String[] args) {
        File nonExistentParentDir = new File("nonexistent_parent_dir/child_dir/file.txt"); 
        try {
            FileWriter fw = new FileWriter(nonExistentParentDir);
            fw.write("Test data.");
            fw.close();
        } catch (IOException e) {
            System.out.println("Error: " + e.getClass().getSimpleName() + " - " + e.getMessage());
        }
    }
}
Q2824 medium
What is the main advantage gained by utilizing method overriding in object-oriented programming with Java?
Q2825 medium
Which of the following `Queue` implementations is generally preferred over `LinkedList` when used purely as a FIFO queue, especially for single-threaded environments, due to better performance characteristics?
Q2826 easy
If an instance variable in a Java class is declared `private`, how can other classes access or modify its value in an encapsulated design?
Q2827 easy
Which of the following is NOT a primitive data type in Java?
Q2828 medium code output
What is the output of this Java code?
java
class MyJob implements Runnable {
    public void run() {
        System.out.println(Thread.currentThread().getName() + " is executing.");
    }
}
public class Main {
    public static void main(String[] args) {
        MyJob job = new MyJob();
        Thread t1 = new Thread(job, "Thread-1");
        t1.run(); // Call run() directly
        System.out.println(Thread.currentThread().getName() + " is done.");
    }
}
Q2829 medium
Consider an `Iterator` for a mutable collection. If `iterator.remove()` is called immediately after `iterator.next()`, what is the expected outcome?
Q2830 hard code error
What compile-time error will this Java code produce?
java
public class OperatorError6 {
    public static void main(String[] args) {
        byte b1 = 100;
        byte b2 = 50;
        byte result = b1 + b2; // Error line
        System.out.println(result);
    }
}
Q2831 medium code error
Which line will cause a compilation error in the following code for an immutable class?
java
public class ProductIdentifier {
    private final String id;

    private ProductIdentifier(String id) {
        this.id = id;
    }

    public static ProductIdentifier create(String id) {
        return new ProductIdentifier(id);
    }

    public static void main(String[] args) {
        ProductIdentifier p = new ProductIdentifier("P123"); // This line causes the error
    }
}
Q2832 easy code output
What does the following code print?
java
public class Test {
    public static void main(String[] args) {
        double[] prices = {10.5, 20.0, 30.0};
        prices[0] = 12.5;
        System.out.println(prices[0] + prices[1]);
    }
}
Q2833 medium
How does `Thread.interrupt()` typically affect a thread that is currently blocked in methods like `sleep()`, `join()`, or `wait()`?
Q2834 medium
What is the primary requirement for a Java interface to be implemented by a lambda expression?
Q2835 hard code error
What will be the result of compiling this Java code?
java
public class Outer {
    private int outerValue = 10;
    class Inner {
        public static int innerStaticValue = 5; // Problematic line
        public void display() {
            System.out.println(outerValue);
        }
    }
    public static void main(String[] args) {
        Outer outer = new Outer();
        Outer.Inner inner = outer.new Inner();
        inner.display();
    }
}
Q2836 easy
What is the primary characteristic of an `ArrayList` in Java?
Q2837 hard
What is the primary effect of marking an instance variable with the `transient` keyword in Java?
Q2838 easy code error
What type of error will occur when compiling the following Java code?
java
class Printer {
    public void printMessage(String s) {
        System.out.println(s);
    }

    private void printMessage(String s) {
        System.out.println("Private: " + s);
    }
}
Q2839 medium code output
What is the output of this code?
java
import java.util.ArrayList;
import java.util.List;

public class FinalMutable {
    public static void main(String[] args) {
        final List<String> list = new ArrayList<>();
        list.add("Java");
        list.add("Python");
        System.out.println(list.size());
    }
}
Q2840 medium
If `List<String> mylist = new ArrayList<>();` and `Object obj = mylist;`, what is the correct way to cast `obj` back to `List<String>`?
← Prev 140141142143144 Next → Page 142 of 200 · 3994 questions