☕ Java MCQ Questions – Page 186

Questions 3701–3720 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3701 hard code error
What is the compile-time error in this Java code?
java
public class CallStartOnRunnable {
    public static void main(String[] args) {
        Runnable myJob = () -> System.out.println("Job running.");
        myJob.start(); // Line 4
    }
}
Q3702 easy code output
What does this Java code print?
java
public class LoopTest {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            if (i == 3) {
                break;
            }
            System.out.print(i);
        }
    }
}
Q3703 hard
Which of the following best describes the relationship between `FileReader` and `InputStreamReader` in the Java I/O hierarchy?
Q3704 medium
What is the primary distinction between a thread in the `WAITING` state and one in the `TIMED_WAITING` state?
Q3705 easy code error
What kind of error will occur when compiling this Java code?
java
public class Main {
    public static void main(String[] args) {
        int value;
        if (true) {
            value = 10;
        } else {
            // value is not initialized here
        }
        System.out.println(value);
    }
}
Q3706 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 writer = new FileWriter("output.txt", true)) {
            writer.write("Line 1\n");
        } catch (IOException e) {}
        try (FileWriter writer = new FileWriter("output.txt", true)) {
            writer.write("Line 2");
        } catch (IOException e) {}
    }
}
Q3707 medium
Which `java.io` interface does `BufferedWriter` implement, indicating its primary role in handling character output?
Q3708 easy code error
What kind of error will occur when executing the following Java code?
java
public class MyThread implements Runnable {
    @Override
    public void run() {
        System.out.println("Thread is running and completing.");
    }

    public static void main(String[] args) throws InterruptedException {
        MyThread myRunnable = new MyThread();
        Thread t = new Thread(myRunnable);
        t.start();
        t.join(); // Wait for the thread to complete
        t.start(); // Attempt to start the same thread after it finished
    }
}
Q3709 easy code output
What is the output of this code?
java
import java.util.HashSet;

public class Test {
    public static void main(String[] args) {
        HashSet<String> colors = new HashSet<>();
        colors.add("Red");
        colors.add("Green");
        colors.add("Blue");
        System.out.println(colors.size());
    }
}
Q3710 easy code error
What error occurs during compilation for the following code snippet?
java
public class Main {
    public static void main(String[] args) {
        System.out.println("Before sleep");
        Thread.sleep(1000); // Attempting to sleep without handling InterruptedException
        System.out.println("After sleep");
    }
}
Q3711 easy
Is the order of elements guaranteed in a Java HashMap?
Q3712 easy
Can an `else` statement exist in Java without being immediately preceded by an `if` statement?
Q3713 medium
When is the `this()` keyword used within a constructor in Java?
Q3714 hard code output
What is the output of this code?
java
public class Test {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("ABCDEF");
        CharSequence cs = "1234567890";
        sb.append(cs, 2, 5);
        System.out.println(sb);
    }
}
Q3715 medium
If you instantiate a `FileWriter` with `new FileWriter("existingFile.txt");` and "existingFile.txt" already exists, what happens by default?
Q3716 medium
Which static method of the String class is used to construct a new String by concatenating elements from an `Iterable` or array, separated by a specified delimiter?
Q3717 hard code error
What is the specific compilation error in this Java code snippet?
java
public class VarAsField {
    // 'var' keyword is only for local variables
    private var data = "hello"; 

    public static void main(String[] args) {
        System.out.println("Attempting to compile...");
    }
}
Q3718 hard
Consider a `Serializable` class `A` that contains an instance of `Serializable` class `B`. If the `serialVersionUID` of class `B` is changed, and then an object of class `A` (which contains an instance of the *old* `B`) is deserialized with the *new* `B` class definition, what is the most likely outcome?
Q3719 medium code output
What does this code print?
java
public class StringTest {
    public static void main(String[] args) {
        String text = "Java programming is fun.";
        String sub = text.substring(5, 16);
        System.out.println(sub);
    }
}
Q3720 hard
When considering performance, what is the primary trade-off associated with using immutable objects extensively, especially for operations that involve frequent modifications (e.g., string concatenation in a loop)?
← Prev 184185186187188 Next → Page 186 of 200 · 3994 questions