☕ Java MCQ Questions – Page 56
Questions 1101–1120 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat error will this Java code produce when executed?
java
public class ArrayError {
public static void main(String[] args) {
int[][] matrix = {{1, 2}, {3, 4}};
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j <= matrix[i].length; j++) { // Loop condition error
System.out.print(matrix[i][j]);
}
}
}
}
What error will this code produce at runtime?
java
import java.util.ArrayList;
import java.util.List;
public class ConcurrentModLoop {
public static void main(String[] args) {
List<String> items = new ArrayList<>();
items.add("Apple");
items.add("Banana");
items.add("Cherry");
for (String item : items) {
if (item.equals("Banana")) {
items.remove(item);
}
}
System.out.println(items);
}
}
Which of the following is NOT a direct benefit of applying encapsulation in Java programming?
Consider the declaration: `int[][] matrix = new int[3][];` Which statement about the state of `matrix` after this declaration is true?
What is the output of this Java program?
java
import java.io.*;
class Person implements Serializable {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return "Name: " + name + ", Age: " + age;
}
}
public class Main {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Person p = new Person("Alice", 30);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(p);
oos.close();
p = new Person("Bob", 25); // Original object reference changed
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
Person deserializedP = (Person) ois.readObject();
ois.close();
System.out.println(deserializedP);
}
}
What is the compile-time or runtime error in the following Java code snippet when `main` method is executed?
java
import java.io.*;
class TaskContainer implements Serializable {
// Runnable is not Serializable by default, and lambda expressions are tricky to serialize.
private Runnable task = () -> System.out.println("Executing task");
private String description = "Simple Task";
}
public class SerializationQuestion5 {
public static void main(String[] args) throws IOException {
TaskContainer container = new TaskContainer();
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("task.ser"))) {
oos.writeObject(container);
}
}
}
What compile-time error will this Java code produce?
java
public class DataTypeCharStringError {
public static void main(String[] args) {
char initial = "J";
System.out.println(initial);
}
}
What is the compile-time error in the provided Java code snippet?
java
public class InvalidSwitchSelectorType {
public static void main(String[] args) {
long id = 12345L;
String category = "Unknown";
switch (id) { // Error expected here
case 100L: category = "A"; break;
case 200L: category = "B"; break;
default: category = "Other";
}
System.out.println(category);
}
}
What error will occur after deserializing the `User` object, given the `password` field is `transient`?
java
import java.io.*;
class User implements Serializable {
String username;
transient String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getPassword() { return password; }
}
public class Main {
public static void main(String[] args) {
User user = new User("admin", "secret123");
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("user.ser"))) {
oos.writeObject(user);
} catch (IOException e) { /* handle exception */ }
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("user.ser"))) {
User deserializedUser = (User) ois.readObject();
System.out.println(deserializedUser.getPassword().length()); // Access transient field
} catch (IOException | ClassNotFoundException e) {
System.err.println(e.getClass().getSimpleName() + ": " + e.getMessage());
}
}
}
How does Java 8+ allow for a form of 'multiple inheritance of implementation' and what mechanism resolves potential conflicts?
A `TreeSet<Integer>` named `mainSet` contains `{1, 2, 3, 4, 5}`. A `headSet` view is created as `viewSet = mainSet.headSet(3, true)`. If `viewSet.remove(2)` is called, what is the state of `mainSet` afterwards?
What error will occur when compiling or running this Java code?
java
public class ArrayError {
public static void main(String[] args) {
Object[] myObjects = new String[3];
myObjects[0] = new Integer(123);
}
}
What will happen when you try to compile this Java code?
java
class MyValue {
private final int val;
public MyValue(int v) {
this.val = v;
}
public void attemptChange(int newVal) {
this.val = newVal;
}
public int getVal() { return val; }
}
public class ImmutableInternalReassignment {
public static void main(String[] args) {
MyValue mv = new MyValue(100);
mv.attemptChange(200);
System.out.println(mv.getVal());
}
}
What is the compile-time error in this Java code?
java
public class ParameterizedRun implements Runnable {
@Override
public void run(String message) { // Line 3
System.out.println(message);
}
public static void main(String[] args) {
// new Thread(new ParameterizedRun()).start();
}
}
What is the default value for an uninitialized instance variable of a reference type (e.g., String, Object) in Java?
What is the output of this code?
java
public class Test {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("JavaIsFun");
sb.setLength(15);
sb.setCharAt(10, 'A');
sb.setLength(7);
System.out.println(sb.toString());
}
}
You have a class `class MyKey { int id; MyKey(int id) { this.id = id; } }` which does *not* implement `Comparable`. If you create `TreeMap<MyKey, String> map = new TreeMap<>();` and then call `map.put(new MyKey(1), "value");`, what will occur?
What does a Java HashMap primarily store?
What is the output of this code?
java
abstract class Animal {
abstract String makeSound();
}
class Dog extends Animal {
@Override
String makeSound() {
return "Woof";
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
System.out.println(myDog.makeSound());
}
}
What are the default values for elements in a newly initialized `int[][]` array in Java?