☕ Java MCQ Questions – Page 58
Questions 1141–1160 of 3994 total — Java interview practice
▶ Practice All Java QuestionsIn a complex `if` condition like `if (a > 10 && b < 5 || c == 0)`, which logical operator has the lowest precedence and therefore is evaluated last?
What kind of error will occur when executing the following Java code?
java
import java.util.concurrent.ArrayBlockingQueue;
import java.util.Queue;
public class QError3 {
public static void main(String[] args) {
Queue<Integer> abq = new ArrayBlockingQueue<>(1);
abq.add(10);
abq.add(20);
System.out.println(abq.element());
}
}
Given `byte b1 = 100; byte b2 = 50;`, which of the following expressions will compile without error and produce an `int` result equal to `150`?
In a class hierarchy, what is the correct order of constructor execution when an object of a subclass is created?
What is the compilation error in the following code?
java
class Parent {
private void confidentialMethod() {
System.out.println("Parent's confidential method");
}
}
class Child extends Parent {
@Override
public void confidentialMethod() {
System.out.println("Child's confidential method");
}
}
What is the output of this code?
java
import java.util.HashSet;
import java.util.Objects;
class ZeroHashItem {
int value;
public ZeroHashItem(int value) { this.value = value; }
@Override public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ZeroHashItem that = (ZeroHashItem) o;
return value == that.value;
}
@Override public int hashCode() { return 0; } // Constant hashCode
}
public class Main {
public static void main(String[] args) {
HashSet<ZeroHashItem> set = new HashSet<>();
set.add(new ZeroHashItem(10));
set.add(new ZeroHashItem(20));
set.add(new ZeroHashItem(10));
System.out.println(set.size());
}
}
If an exception occurs in the `try` block and there is no matching `catch` block to handle it, what happens regarding the `finally` block?
What does this code print?
java
class A {
public final void f1() { System.out.println("A.f1()"); }
public void f2() { System.out.println("A.f2()"); }
}
class B extends A {
@Override
public void f2() { System.out.println("B.f2()"); }
}
public class Test {
public static void main(String[] args) {
A obj = new B();
obj.f1();
obj.f2();
}
}
What is the output of this Java program?
java
import java.io.*;
class Counter implements Serializable {
private int instanceCount;
public static int staticCount = 0;
public Counter(int i) {
this.instanceCount = i;
staticCount++;
}
public String toString() {
return "Instance: " + instanceCount + ", Static: " + staticCount;
}
}
public class Main {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Counter obj1 = new Counter(1);
Counter.staticCount = 100; // Change static field before serialization
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj1);
oos.close();
Counter.staticCount = 200; // Change static field before deserialization
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
Counter deserializedObj = (Counter) ois.readObject();
ois.close();
System.out.println(deserializedObj);
}
}
What does this Java code print?
java
public class ArrayTest {
public static void main(String[] args) {
char[] letters = {'x', 'y', 'z'};
System.out.println(letters[1]);
}
}
What is the output of this Java code?
java
import java.util.TreeMap;
import java.util.Iterator;
import java.util.Set;
public class Test {
public static void main(String[] args) {
TreeMap<Integer, String> map = new TreeMap<>();
map.put(1, "One");
map.put(2, "Two");
map.put(3, "Three");
Set<Integer> keys = map.keySet();
Iterator<Integer> it = keys.iterator();
while (it.hasNext()) {
Integer key = it.next();
if (key == 2) {
map.put(4, "Four"); // Modifying map directly
}
}
System.out.println(map.size());
}
}
What happens if an exception is thrown from within a `finally` block, and there was already an exception pending from the `try` block?
What is the primary issue with this Java code snippet?
java
public class LoopError5 {
public static void main(String[] args) {
int x = 0;
while (x < 2); // Semicolon here
System.out.println("X: " + x);
x++; // This statement is after the infinite empty loop
}
}
What happens if a non-serializable object is referenced by a field in a serializable class, and that field is not marked `transient`?
What is the output of this code?
java
import java.util.HashSet;
public class Test {
public static void main(String[] args) {
HashSet<String> data = new HashSet<>();
data.add("Hello");
data.add(null);
data.add("World");
data.add(null); // Duplicate null
System.out.println(data.size());
}
}
Consider a Java class with an instance variable `int count` and a method that also declares a local variable `int count`. If `count` is referenced within that method without `this.`, which variable is accessed?
What compilation error will occur when compiling this Java code?
java
import java.io.BufferedReader;
import java.io.FileReader;
public class MyClass {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new FileReader("nonexistent.txt"));
}
}
What is the compilation error in the following Java code snippet?
java
public class Main {
public static void main(String[] args) {
var greeting = () -> System.out.println("Hello from lambda");
greeting.run();
}
}
What is the output of this code?
java
public class DataTypeTest {
public static void main(String[] args) {
char grade = 'A';
System.out.println(grade);
}
}
Which statement regarding method overriding and exception declarations in Java is true?