☕ Java MCQ Questions – Page 99
Questions 1961–1980 of 3994 total — Java interview practice
▶ Practice All Java QuestionsAssume a file named 'data.txt' exists and contains the bytes `0xc3 0xa9` (which is 'é' in UTF-8). The Java application is run on a platform where the default character encoding is ISO-8859-1. What is the output of this code?
java
import java.io.FileReader;
import java.io.IOException;
import java.io.File;
public class FileReaderEncoding {
public static void main(String[] args) throws IOException {
// Assume 'data.txt' has content: 'é' (UTF-8 encoded: 0xC3 0xA9)
// Assume platform default encoding is ISO-8859-1
StringBuilder sb = new StringBuilder();
try (FileReader reader = new FileReader(new File("data.txt"))) {
int c;
while ((c = reader.read()) != -1) {
sb.append((char) c);
}
}
System.out.println(sb.toString());
}
}
What is the runtime error produced by the following Java code?
java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class TestClass {
public static void main(String[] args) {
BufferedReader br = null;
try {
new FileReader("valid.txt"); // File exists, but not assigned to 'br'
String line = br.readLine();
System.out.println(line);
} catch (IOException e) {
System.err.println("IO Error");
} catch (NullPointerException e) {
System.err.println("NPE Caught: " + e.getMessage());
}
}
}
A thread is in which state immediately after `start()` is called on it, but before its `run()` method has begun executing?
What is the output of this Java code?
java
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.nio.file.Files;
import java.nio.file.Path;
public class FileWriterAppendFileObject {
public static void main(String[] args) {
String filename = "append_file.txt";
File file = new File(filename);
try {
try (FileWriter fw = new FileWriter(file)) { fw.write("First line.\n"); }
try (FileWriter fw = new FileWriter(file, true)) { fw.write("Second line.\n"); }
try (FileWriter fw = new FileWriter(file, false)) { fw.write("Third line."); }
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line; while ((line = br.readLine()) != null) { System.out.println(line); }
}
} catch (IOException e) { System.out.println("Error: " + e.getMessage());
} finally { try { Files.deleteIfExists(Path.of(filename)); } catch (IOException ignored) {} }
}
}
What is the output of this Java code?
java
import java.util.TreeSet;
import java.util.Set;
public class Test {
public static void main(String[] args) {
Set<Integer> numbers = new TreeSet<>();
numbers.add(5);
numbers.add(2);
numbers.add(8);
numbers.add(1);
System.out.println(numbers);
}
}
When overriding a method that declares a `IOException` (a checked exception) in its superclass, which of the following is a valid declaration for the overriding method?
What is the output of this code?
java
import java.util.ArrayList;
import java.util.List;
public class PassMutable {
public static void modifyList(List<String> list) {
list.add("C++");
System.out.println("Inside method: " + list);
}
public static void main(String[] args) {
List<String> languages = new ArrayList<>();
languages.add("Java");
languages.add("Python");
modifyList(languages);
System.out.println("Outside method: " + languages);
}
}
What does the following Java code print to the console?
java
public class OverloadDemo8 {
void showChar(int val) {
System.out.println("Int value: " + val);
}
void showChar(char val) {
System.out.println("Char value: " + val);
}
public static void main(String[] args) {
OverloadDemo8 obj = new OverloadDemo8();
obj.showChar('C');
}
}
What is the output of this code?
java
public class VarargsArrayConversion {
public static void printArgs(String... args) {
System.out.println("Number of args: " + args.length);
for (String s : args) {
System.out.print(s + " ");
}
System.out.println();
}
public static void main(String[] args) {
String[] arr = {"hello", "world"};
printArgs(arr);
printArgs("java", "is", "fun");
printArgs();
}
}
What is the compile-time error in this Java switch expression using an enum?
java
enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }
public class EnumSwitchExhaustiveError {
public static void main(String[] args) {
Day today = Day.MONDAY;
String typeOfDay = switch (today) { // Error expected here
case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> "Weekday";
case SATURDAY -> "Weekend";
// Missing SUNDAY
};
System.out.println(typeOfDay);
}
}
What happens when an unchecked exception (like `ArithmeticException`) occurs within a thread's `run()` method?
java
class MyThread extends Thread {
public void run() {
System.out.println("Thread started.");
int x = 10 / 0; // This will cause an ArithmeticException
System.out.println("Thread finished.");
}
}
public class Main {
public static void main(String[] args) {
MyThread t = new MyThread();
t.start();
}
}
What is the compilation error in the following code?
java
class Base {
public Base() {
System.out.println("Base constructor");
}
}
class Derived extends Base {
public Derived() {
System.out.println("Derived constructor");
super(); // Call to super() is not the first statement
}
}
What is the primary purpose of using `initCause()` or the `Throwable` constructor that accepts a `cause` argument when creating and throwing an exception?
What kind of error will occur when executing the following Java code?
java
import java.util.PriorityQueue;
import java.util.Queue;
class CustomObject {}
public class QError2 {
public static void main(String[] args) {
Queue<CustomObject> pq = new PriorityQueue<>();
pq.add(new CustomObject());
pq.add(new CustomObject());
System.out.println(pq.poll());
}
}
What does `Thread.currentThread()` return?
What is the output of this code?
java
interface A { default void show() { System.out.println("A"); } }
interface B { default void show() { System.out.println("B"); } }
interface C { void show(); }
class MyClass implements A, B, C {
@Override
public void show() {
A.super.show();
System.out.println("MyClass");
}
}
public class Test {
public static void main(String[] args) {
new MyClass().show();
}
}
Which exception will be thrown when executing this Java code?
java
public class StringError2 {
public static void main(String[] args) {
String text = "hello"; // Length is 5, valid indices are 0-4
char c = text.charAt(5);
System.out.println(c);
}
}
How do you correctly create an empty `StringBuilder` object with a default initial capacity?
What does this code print?
java
import java.io.*;
public class BufferReadArrayError {
public static void main(String[] args) {
try (StringReader sr = new StringReader("Hello World");
BufferedReader br = new BufferedReader(sr)) {
char[] buffer = new char[5];
// Invalid offset and length combination: off=3, len=3, buffer.length=5. off+len (6) > buffer.length
br.read(buffer, 3, 3);
System.out.println(new String(buffer));
} catch (IndexOutOfBoundsException e) {
System.out.println("Error: " + e.getMessage());
} catch (IOException e) {
System.out.println("IOException: " + e.getMessage());
}
}
}
What error occurs when running this Java code?
java
public class MyClass {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Test");
sb.setCharAt(4, '!');
System.out.println(sb);
}
}