☕ Java MCQ Questions – Page 68
Questions 1341–1360 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat 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.");
}
}
Which method in `StringBuffer` is used to insert a string at a specific index?
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?
A thread executes `Thread.sleep(1000)` for one second. Which state does this thread enter during the sleep duration?
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());
}
}
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();
}
}
When does the stream pipeline processing truly begin?
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);
}
}
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();
}
}
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"));
}
}
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);
}
}
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");
}
}
Which of the following is a valid argument type for a `FileReader` constructor to specify the source file?
Which statement best describes a `static` variable in Java?
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
}
}
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());
}
}
}
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();
}
}
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());
}
}
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);
}
}
What does it mean if a class is declared with the `final` keyword?