☕ Java MCQ Questions – Page 121
Questions 2401–2420 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhich of these primitive types can be implicitly converted to a `double`?
What is the output of this code?
java
class Item {
String description;
int quantity;
boolean inStock;
}
public class Main {
public static void main(String[] args) {
Item item = new Item();
System.out.println("Description: " + item.description);
System.out.println("Quantity: " + item.quantity);
System.out.println("In Stock: " + item.inStock);
}
}
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("JavaProgramming"); // Length 15
String replacement = "Awesome";
// Attempt to replace from index 10 to 5 (start > end)
sb.replace(10, 5, replacement);
System.out.println(sb);
}
}
What is the error in the following Java code?
java
class Product {
private final String sku;
private String name;
public Product(String sku, String name) {
this.sku = sku;
this.name = name;
}
public void renameProduct(String newName) {
this.sku = newName; // Attempt to modify final field
}
public String getName() { return name; }
public String getSku() { return sku; }
}
public class Inventory {
public static void main(String[] args) {
Product p = new Product("P123", "Laptop");
p.renameProduct("Desktop");
}
}
What is the output of this Java code snippet?
java
public class OverloadDemo6 {
void execute(long l) {
System.out.println("Executing long: " + l);
}
void execute(float f) {
System.out.println("Executing float: " + f);
}
public static void main(String[] args) {
OverloadDemo6 obj = new OverloadDemo6();
obj.execute(30);
}
}
What is the output of this code?
java
public class Main {
public static void main(String[] args) {
int value = 0;
try {
value = 10;
} finally {
value = 20;
}
System.out.println(value);
}
}
What kind of error will occur when executing the following Java code?
java
public class MyNotifier {
private final Object lock = new Object();
public void signalWaitingThread() {
// Calling notify() without holding the monitor for 'lock'
lock.notify();
}
public static void main(String[] args) {
MyNotifier notifier = new MyNotifier();
new Thread(notifier::signalWaitingThread).start();
}
}
What is the valid range for a thread's priority in Java, and what is its default value?
Given the following Java code snippet:
java
String s1 = "hello";
String s2 = "hello";
String s3 = new String("hello");
System.out.println(s1 == s2);
System.out.println(s1 == s3);
What will be the output?
What is the content of 'auto_closed.txt' after this code executes?
java
import java.io.FileWriter;
import java.io.IOException;
public class TryWithResourcesTest {
public static void main(String[] args) {
try (FileWriter writer = new FileWriter("auto_closed.txt")) {
writer.write("This is automatically closed.");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
// No explicit close() is called here.
}
}
What is the output of this code?
java
import java.util.LinkedList;
import java.util.Arrays;
public class MyClass {
public static void main(String[] args) {
LinkedList<Double> values = new LinkedList<>(Arrays.asList(1.1, 2.2, 3.3));
values.remove(values.size()); // Invalid index for remove
System.out.println(values);
}
}
When creating a thread by extending the `Thread` class, which of the following is a mandatory step?
What is the error encountered when running this Java code?
java
public class PrimitiveArrayCastError {
public static void main(String[] args) {
Object obj = new Integer[]{1, 2, 3};
int[] intArray = (int[]) obj;
System.out.println(intArray[0]);
}
}
What does this Java code print to the console?
java
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class Test {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
Consumer<String> addToList = names::add;
Consumer<String> printName = System.out::println;
addToList.accept("Alice");
printName.accept("Bob");
System.out.println(names.size());
}
}
What is the output of this Java code?
java
class DaemonTask implements Runnable {
public void run() {
try {
System.out.println("Daemon thread started.");
Thread.sleep(1000); // Simulate long running task
System.out.println("Daemon thread finished.");
} catch (InterruptedException e) {
System.out.println("Daemon interrupted.");
}
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
Thread daemonThread = new Thread(new DaemonTask());
daemonThread.setDaemon(true);
daemonThread.start();
Thread.sleep(50); // Give daemon a chance to start
System.out.println("Main thread exiting.");
}
}
What will this Java code print?
java
public class StringBufferTest {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Test");
String s = "Java";
sb.append(s).insert(sb.length(), "!");
System.out.print(sb.length() + " " + sb.capacity());
}
}
What error will occur when compiling the following Java code?
java
public class Main {
public static void main(String[] args) {
int x = 7;
if (5 < x < 10) {
System.out.println("X is between 5 and 10.");
} else {
System.out.println("X is not between 5 and 10.");
}
}
}
How do you create a new single-dimensional integer array of size 5 named `scores`?
What does a `Thread` object represent in Java?
Considering memory footprint and garbage collection overhead, how does `java.util.LinkedList` compare to `java.util.ArrayList` for storing a large number of objects?