☕ Java MCQ Questions – Page 68

Questions 1341–1360 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1341 medium code output
What is the output of this code?
java
public class ThreadStateDemo4 {
    public static void main(String[] args) throws InterruptedException {
        Thread childThread = new Thread(() -> {
            try { Thread.sleep(200); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
        });
        childThread.start();
        Thread mainThread = Thread.currentThread();
        Thread observer = new Thread(() -> {
            try {
                Thread.sleep(50); // Give main thread time to call join
                System.out.println("Observer checking main state: " + mainThread.getState());
            } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
        });
        observer.start();
        childThread.join(); // Main thread waits for childThread
        System.out.println("Main thread finished join.");
    }
}
Q1342 easy
Which method in `StringBuffer` is used to insert a string at a specific index?
Q1343 hard
Consider an `abstract` method in a superclass that returns type `T`. When a concrete subclass overrides this method, which statement correctly describes the use of covariant return types?
Q1344 medium
A thread executes `Thread.sleep(1000)` for one second. Which state does this thread enter during the sleep duration?
Q1345 easy code error
What is the outcome of attempting to compile the given Java code?
java
import java.util.Queue;
import java.util.LinkedList;

public class QueueError {
    public static void main(String[] args) {
        Queue<int> invalidQueue = new LinkedList<int>();
        invalidQueue.offer(10);
        System.out.println(invalidQueue.poll());
    }
}
Q1346 hard code error
What compile-time error occurs when attempting to compile this Java code?
java
abstract class Shape {
    public abstract void draw();
    public void getDescription() {
        System.out.println("This is a shape.");
    }
}

public class Main {
    public static void main(String[] args) {
        Shape s = new Shape();
    }
}
Q1347 medium
When does the stream pipeline processing truly begin?
Q1348 hard code error
What kind of error will occur when compiling or running the following Java code?
java
public class SBError {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Result: ");
        String suffix = null;
        sb.append("Prefix").reverse().append(suffix.toString());
        System.out.println(sb);
    }
}
Q1349 medium code error
What error will be reported when attempting to compile the `MyClass` class?
java
class MyClass {
    int instanceVar = 10;

    static void printValue() {
        System.out.println(instanceVar); 
    }
}

public class Main {
    public static void main(String[] args) {
        MyClass.printValue();
    }
}
Q1350 hard code output
What is the output of this code, assuming System.getProperty("line.separator") returns "\n"?
java
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;

public class Test {
    public static void main(String[] args) throws IOException {
        StringWriter sw = new StringWriter();
        BufferedWriter bw = new BufferedWriter(sw);
        bw.write(65);
        bw.write('B');
        bw.write(0x0043);
        bw.newLine();
        bw.write("Hello");
        bw.close();
        System.out.println(sw.toString().replace("\n", "\\n").replace("\r", "\\r"));
    }
}
Q1351 easy code output
What is the output of this code?
java
public class Main {
    public static void main(String[] args) {
        int a = 10;
        int b = 3;
        System.out.println(a / b + a % b);
    }
}
Q1352 medium code error
What is the compilation error in the provided Java code?
java
public class MyClass {
    public abstract void performAction();
    public void someMethod() {
        System.out.println("Hello");
    }
}
Q1353 easy
Which of the following is a valid argument type for a `FileReader` constructor to specify the source file?
Q1354 medium
Which statement best describes a `static` variable in Java?
Q1355 easy code error
What is the expected error when compiling and running the following Java code?
java
import java.util.LinkedList;

public class MyClass {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.set(0, "First"); // Tries to replace an element at index 0
    }
}
Q1356 hard code output
What is the output of this code?
java
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;

public class Test {
    public static void main(String[] args) {
        StringWriter sw = new StringWriter();
        BufferedWriter bw = new BufferedWriter(sw);
        try {
            bw.write("Data");
            bw.close();
            System.out.println("First close complete.");
            bw.close(); // Second close call
            System.out.println("Second close complete.");
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        } finally {
            System.out.println("Content: " + sw.toString());
        }
    }
}
Q1357 easy code error
What is the compilation error in the `Main` class?
java
class Singleton {
    private Singleton() {
        System.out.println("Singleton instance created.");
    }
}

public class Main {
    public static void main(String[] args) {
        Singleton instance = new Singleton();
    }
}
Q1358 easy code output
What does this Java code print?
java
import java.io.File;

public class FileTest {
    public static void main(String[] args) {
        File file = new File("documents/reports/report.pdf");
        System.out.println(file.getParent());
    }
}
Q1359 medium code error
What error occurs when compiling this Java code?
java
public class LoopTest {
    public static void main(String[] args) {
        do {
            int x = 10;
            System.out.println(x);
        } while (x < 13);
    }
}
Q1360 easy
What does it mean if a class is declared with the `final` keyword?
← Prev 6667686970 Next → Page 68 of 200 · 3994 questions