☕ Java MCQ Questions – Page 172
Questions 3421–3440 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhich exception is thrown when the `main` method of this Java code is executed?
java
import java.nio.charset.Charset;
public class CharsetNullConstructor {
public static void main(String[] args) {
byte[] bytes = {72, 101, 108, 108, 111}; // ASCII for "Hello"
String charsetName = null;
try {
String s = new String(bytes, charsetName);
System.out.println(s);
} catch (Exception e) {
System.out.println(e.getClass().getSimpleName());
}
}
}
What does this code print?
java
class LimitExceededException extends RuntimeException {
public LimitExceededException() {
// Default constructor, relies on parent's default
}
}
public class Main {
public static void checkLimit(int value) {
if (value > 100) {
throw new LimitExceededException();
}
}
public static void main(String[] args) {
try {
checkLimit(150);
} catch (LimitExceededException e) {
System.out.println("Limit exceeded!");
System.out.println(e.getClass().getSimpleName());
}
}
}
In which major version of Java were Lambda Expressions officially introduced?
What error will be thrown by the `MyTask` thread when the `main` thread interrupts it?
java
class MyTask implements Runnable {
@Override
public void run() {
try {
System.out.println("Task sleeping...");
Thread.sleep(5000);
System.out.println("Task woke up normally.");
} catch (InterruptedException e) {
// Exception caught but not handled correctly, it's just printed
System.err.println("Task interrupted: " + e.getMessage());
}
}
}
public class InterruptExample {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(new MyTask());
t.start();
Thread.sleep(100);
t.interrupt(); // Interrupt the sleeping thread
}
}
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("Data"); // Length 4
sb.setCharAt(sb.length(), '!'); // Attempt to set char at index 4
System.out.println(sb);
}
}
What error occurs when compiling this Java code?
java
import java.net.URISyntaxException;
public class MyResource {
public MyResource() {
// Simulate an operation that might throw URISyntaxException
throw new URISyntaxException("invalid uri", "description");
}
public static void main(String[] args) {
// Nothing happens here
}
}
What will be the result of compiling this Java code?
java
class Base {
private Base() {
System.out.println("Base constructor");
}
}
class Derived extends Base {
public Derived() {
// super(); // Implicit or explicit call to Base() is needed
System.out.println("Derived constructor");
}
public static void main(String[] args) {
Derived d = new Derived();
}
}
What does this code print?
java
public class DirectRunCall {
public void run() {
System.out.println("Direct run call.");
}
public static void main(String[] args) {
DirectRunCall obj = new DirectRunCall();
obj.run();
}
}
Can constructors in Java be overloaded?
When a thread attempts to enter a `synchronized` block or method but the monitor is currently held by another thread, what state does the attempting thread enter?
Which of the following is a common characteristic of a class designed to be immutable?
What error will occur when compiling this Java code?
java
public class ConditionCheck {
public static void main(String[] args) {
int counter = 0;
if (counter == 0) {
System.out.println("Starting count.");
break;
}
System.out.println("Counted.");
}
}
What is the output of this code?
java
class VolatileExample {
private volatile boolean flag = false;
private int value = 0;
public void writer() {
value = 123;
flag = true;
}
public void reader() {
while (!flag) {
Thread.yield();
}
System.out.println("Value is: " + value);
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
VolatileExample example = new VolatileExample();
Thread writer = new Thread(example::writer);
Thread reader = new Thread(example::reader);
reader.start();
Thread.sleep(100);
writer.start();
writer.join();
reader.join();
}
}
When an object is serialized, what happens to its `static` fields?
What does this code print?
java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
Iterator<Integer> it = numbers.iterator();
while (it.hasNext()) {
System.out.print(it.next());
}
}
}
What is the output of this Java code snippet?
java
public class Test {
public static void main(String[] args) {
int[] data = {100};
System.out.println(data.length - 1);
}
}
What error does this Java code produce when executed?
java
public class Main {
public static void main(String[] args) {
String text = null;
System.out.println(text.length());
}
}
What is the output of this Java code?
java
public class MyClass {
public static void riskyMethod() {
System.out.println("Attempting operation...");
throw new IllegalArgumentException("Invalid input");
}
public static void main(String[] args) {
riskyMethod();
System.out.println("Operation completed.");
}
}
What does this Java code print?
java
public class Main {
public static void main(String[] args) {
System.out.println("Before sleep.");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println("Interrupted during sleep.");
}
System.out.println("After sleep.");
}
}
Consider a `BufferedReader` reading from a large file. If `mark()` is called, what is a potential issue if the `readAheadLimit` specified is too small?