☕ Java MCQ Questions – Page 127
Questions 2521–2540 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat is the output of this code?
java
public class DataTypeTest {
public static void main(String[] args) {
long bigNumber = 9876543210L;
System.out.println(bigNumber);
}
}
What is the compile-time error in this Java code if any?
java
import java.io.IOException;
interface Service {
void operation();
}
class ConcreteService implements Service {
@Override
public void operation() throws IOException {
System.out.println("Performing operation");
throw new IOException("Service error");
}
}
public class Main {
public static void main(String[] args) {}
}
What is the error encountered when compiling this Java code?
java
public class VarargsError {
static void processStrings(String... values) {
for (String s : values) {
System.out.print(s + " ");
}
}
public static void main(String[] args) {
Object[] data = {"hello", 123, true};
processStrings(data);
}
}
What is the error encountered when running this Java code?
java
import java.io.FileReader;
import java.io.IOException;
import java.io.File;
public class FileReaderError7 {
public static void main(String[] args) {
File file = new File("test_fr7.txt");
try {
file.createNewFile();
try (FileReader fr = new FileReader(file)) {
char[] buffer = new char[10];
fr.read(buffer, -1, 5); // Negative offset
System.out.println("Read successfully");
}
} catch (IOException e) {
System.err.println(e.getClass().getSimpleName() + ": " + e.getMessage());
} catch (IndexOutOfBoundsException e) {
System.err.println(e.getClass().getSimpleName());
} finally {
file.delete();
}
}
}
What error will prevent the compilation of the following Java enum definition?
java
enum UserRole {
ADMIN("Administrator"),
EDITOR("Content Editor");
private final String description; // Field is final
UserRole(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
public void setDescription(String newDescription) {
this.description = newDescription; // Attempt to reassign a final field
}
}
public class EnumImmutability {
public static void main(String[] args) {
UserRole.ADMIN.setDescription("Super Admin");
}
}
What does this Java code print?
java
import java.util.TreeSet;
class MyObject {
int id;
MyObject(int id) { this.id = id; }
public String toString() { return "MyObject-" + id; }
}
public class Test {
public static void main(String[] args) {
TreeSet<MyObject> myObjects = new TreeSet<>();
try {
myObjects.add(new MyObject(1));
myObjects.add(new MyObject(2));
System.out.println(myObjects);
} catch (ClassCastException e) {
System.out.println("ClassCastException caught!");
}
}
}
Given the following Java code:
java
int[][] arr = new int[2][2];
arr[0][0] = 1;
arr[0][1] = 2;
arr[1][0] = 3;
arr[1][1] = 4;
int[] newRow = {5, 6};
arr[0] = newRow;
System.out.println(arr[0][0] + " " + arr[1][0]);
What is the output of this code?
If a `while(true)` loop is used within a method, what is the most idiomatic and graceful way to terminate both the loop and the method's execution upon a specific condition being met, without throwing an exception or stopping the entire JVM?
Given an array `String[] names = {"Alice", "Bob", "Charlie"};`, how would you access the element "Bob"?
Which of the following compile-time errors will occur in this Java snippet?
java
public class DataTypeShortError {
public static void main(String[] args) {
short value = 33000;
System.out.println(value);
}
}
What does this code print to the console?
java
import java.io.*;
class SimpleData implements Serializable {
private int id;
public SimpleData(int id) {
this.id = id;
}
public int getId() {
return id;
}
}
public class Main {
public static void main(String[] args) throws IOException, ClassNotFoundException {
SimpleData data1 = new SimpleData(10);
SimpleData data2 = new SimpleData(20);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(data1);
oos.writeObject(data2);
oos.close();
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
SimpleData readData1 = (SimpleData) ois.readObject();
SimpleData readData2 = (SimpleData) ois.readObject();
ois.close();
System.out.println(readData1.getId() + ", " + readData2.getId());
}
}
What error will occur when running this code, assuming `data.ser` was created with a serialized object of a class named `MyOldClass` that is no longer on the classpath?
java
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("data.ser"))) {
Object obj = ois.readObject(); // This will try to load MyOldClass
System.out.println("Object deserialized.");
} catch (IOException | ClassNotFoundException e) {
System.err.println(e.getClass().getSimpleName() + ": " + e.getMessage());
}
}
}
What is the result of running this Java code?
java
import java.util.TreeMap;
public class TreeMapError6 {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
TreeMap map = new TreeMap();
map.put(1, "Integer Key");
map.put("Two", "String Key");
System.out.println(map.size());
}
}
What is the output of this Java program?
java
class Counter {
static {
System.out.println("Static block executed.");
}
Counter() {
System.out.println("Counter object created.");
}
}
public class Main {
public static void main(String[] args) {
System.out.println("Main method start.");
Counter c1 = new Counter();
Counter c2 = new Counter();
}
}
What does this code print?
java
public class Main {
public static void main(String[] args) {
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[]{1, 2};
jaggedArray[1] = new int[]{3};
jaggedArray[2] = new int[]{4, 5, 6};
System.out.println(jaggedArray[2][1]);
}
}
For frequent string concatenations within a loop, using `StringBuilder` is generally preferred over using the `+` operator with `String` objects because:
What will be the content of the file 'output.txt' after this code executes?
java
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterTest {
public static void main(String[] args) {
try (FileWriter writer1 = new FileWriter("output.txt")) {
writer1.write("First Line");
} catch (IOException e) {
e.printStackTrace();
}
try (FileWriter writer2 = new FileWriter("output.txt")) {
writer2.write("Second Line");
} catch (IOException e) {
e.printStackTrace();
}
}
}
What error will this code produce at compile time?
java
public class VarForLoopUpdateError {
public static void main(String[] args) {
for (var i = 0, j = 0; i < 5; var k = i++, j++) {
System.out.println(i + j);
}
}
}
Which of the following is a correct way to declare a two-dimensional integer array in Java?
What error will this Java code produce when executed?
java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
Iterator<Integer> it = numbers.iterator();
if (it.hasNext()) {
it.next(); // Retrieves 10
it.remove(); // Removes 10
it.remove(); // Attempts to remove again for the same next() call
}
}
}