☕ Java MCQ Questions – Page 163

Questions 3241–3260 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3241 hard code error
What will be the result of compiling and running this Java code?
java
public class A {
    public static final int VALUE = B.OTHER_VALUE + 1;
}
public class B {
    public static final int OTHER_VALUE = A.VALUE + 1;
    public static void main(String[] args) {
        System.out.println(A.VALUE);
    }
}
Q3242 hard code error
Which compile-time error will occur when compiling this Java snippet?
java
public class ByteArithmetic {
    public static void main(String[] args) {
        byte a = 50;
        byte b = 80;
        byte c = a + b; // Arithmetic operations on byte/short/char promote to int
        System.out.println(c);
    }
}
Q3243 easy code error
What is the error in this Java code?
java
import java.util.HashMap;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        Map<String, String> myMap = HashMap<>();
        myMap.put("city", "London");
        System.out.println(myMap.get("city"));
    }
}
Q3244 medium
If a constructor in a subclass does not explicitly call `super()` or `this()`, what action does the Java compiler take?
Q3245 hard
Why is `FileWriter` almost universally recommended to be wrapped in a `BufferedWriter` for writing significant amounts of text data, especially when performing numerous small `write()` operations?
Q3246 medium code output
What does this code print?
java
class A {
    A() {
        System.out.print("A");
    }
}

class B extends A {
    B() {
        System.out.print("B");
    }
}

public class Main {
    public static void main(String[] args) {
        new B();
    }
}
Q3247 hard code error
What runtime error will this Java code snippet throw?
java
public class Test {
    public static void main(String[] args) {
        int[][] matrix = new int[2][];
        matrix[0] = new int[3];
        matrix[1] = new int[2];

        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j <= matrix[1].length; j++) {
                matrix[i][j] = i * 10 + j;
            }
        }
        System.out.println(matrix[0][0]);
    }
}
Q3248 hard code error
What is the error in the execution of this Java code snippet?
java
public class BuilderError {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Example");
        String sub = sb.substring(-1, 3);
        System.out.println(sub);
    }
}
Q3249 hard code error
Which line causes a compilation error?
java
class Base {
    public final void print() {
        System.out.println("Base final");
    }
}
class Derived extends Base {
    public void print() { // Line 7
        System.out.println("Derived print");
    }
}
public class Test {
    public static void main(String[] args) {
        Derived d = new Derived();
        d.print();
    }
}
Q3250 easy code output
What output does the following Java code produce?
java
public class Main {
    public static void main(String[] args) {
        String result = "";
        for (int i = 1; i <= 5; i++) {
            if (i % 2 == 0) {
                continue;
            }
            result += i;
        }
        System.out.print(result);
    }
}
Q3251 hard code error
What is the most likely error when compiling this Java code snippet?
java
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test {
    public static void main(String[] args) {
        try {
            BufferedWriter bw = new BufferedWriter(new FileOutputStream("output.txt"));
            bw.write("Data");
            bw.close();
        } catch (IOException e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
}
Q3252 hard
How does `java.util.LinkedList` handle `null` elements?
Q3253 hard code output
What does this code print?
java
String s = null;
Object o = null;

System.out.println(String.valueOf(s));
System.out.println(String.valueOf(o));
try {
    System.out.println(s.toString());
} catch (Exception e) {
    System.out.println(e.getClass().getSimpleName());
}
Q3254 easy
How do you initialize an integer variable named `counter` to the value 10?
Q3255 easy
Which of the following is NOT a valid Java variable name?
Q3256 hard
What is the primary implication of declaring a reference variable as `final` in Java?
Q3257 hard
In the context of `BlockingQueue` operations, what is the fundamental difference between calling `queue.take()` and `queue.poll()` without a timeout?
Q3258 hard code error
What error will be thrown when `Worker` thread attempts to call `condition.await()`?
java
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

class SharedData {
    private ReentrantLock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public void waitForSignal() {
        // This thread does not hold 'lock'
        try {
            condition.await(); 
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}

public class Main {
    public static void main(String[] args) {
        SharedData data = new SharedData();
        Thread worker = new Thread(data::waitForSignal);
        worker.start();
    }
}
Q3259 easy code output
What does this code print?
java
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        TreeSet<String> ts = new TreeSet<>();
        ts.add("apple");
        ts.add("banana");
        ts.add("apple");
        System.out.println(ts.size());
    }
}
Q3260 easy code output
Analyze the Java code and determine its output.
java
public class Main {
    public static void main(String[] args) {
        for (int i = 0; i < 4; i++) {
            if (i == 1) {
                System.out.print("X");
                continue;
            }
            if (i == 2) {
                System.out.print("Y");
                break;
            }
            System.out.print(i);
        }
    }
}
← Prev 161162163164165 Next → Page 163 of 200 · 3994 questions