☕ Java MCQ Questions – Page 57

Questions 1121–1140 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1121 hard
In Java, a "jagged array" is a multidimensional array where inner arrays can have different lengths. Which statement accurately describes the memory representation of an `int[][]` jagged array?
Q1122 hard
How does the behavior of `java.io.File.listRoots()` typically differ between a Windows operating system and a Unix-like operating system (e.g., Linux or macOS)?
Q1123 medium code error
What is the compilation error for the provided Java code?
java
public class InstanceofError {
    public static void main(String[] args) {
        int x = 10;
        if (x instanceof Integer) {
            System.out.println("x is an Integer object.");
        }
    }
}
Q1124 easy
Which method returns the character at a specified index within the string?
Q1125 medium
What is a key difference in execution guarantee between a `do-while` loop and a `while` loop in Java?
Q1126 easy code error
What kind of error will occur when compiling or running the following Java code snippet?
java
import java.io.File;
import java.io.IOException;

public class FileError5 {
    public static void main(String[] args) throws IOException {
        File existingFile = new File("temp.txt");
        existingFile.createNewFile(); // Assuming this succeeds
        File newFile = new File(existingFile, "child.txt");
        newFile.createNewFile();
        System.out.println("Child file path: " + newFile.getAbsolutePath());
    }
}
Q1127 hard code output
What is the output of this code snippet?
java
import java.util.concurrent.atomic.AtomicInteger;

public class SharedCounterTest {
    private static final AtomicInteger counter = new AtomicInteger(0);

    public static void main(String[] args) throws InterruptedException {
        Runnable incrementTask = () -> {
            for (int i = 0; i < 50000; i++) {
                counter.incrementAndGet();
            }
        };

        Thread t1 = new Thread(incrementTask);
        Thread t2 = new Thread(incrementTask);

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println("Final counter: " + counter.get());
    }
}
Q1128 hard code output
What is the output of this code?
java
public class Test {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Start");
        String nullString = null;
        sb.append(" ").append(nullString).append(" End");
        System.out.println(sb);
    }
}
Q1129 medium code output
What is the final output of 'Final Count'?
java
import java.util.concurrent.Semaphore;

public class Main {
    private static Semaphore semaphore = new Semaphore(1);
    private static int count = 0;

    public static void performTask() {
        try {
            semaphore.acquire();
            for (int i = 0; i < 500; i++) {
                count++;
            }
            System.out.println(Thread.currentThread().getName() + " finished. Count: " + count);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } finally {
            semaphore.release();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(Main::performTask, "Thread-1");
        Thread t2 = new Thread(Main::performTask, "Thread-2");

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println("Final Count: " + count);
    }
}
Q1130 easy code error
What is the compile-time error in the following Java code?
java
class Calculator {
    void add(int a, int b) {
        // Does something
    }

    int add(int a, int b) {
        return a + b;
    }
}
Q1131 medium
What is the primary effect of calling `Thread.yield()`?
Q1132 easy code error
What kind of error will occur when compiling this Java code?
java
public class Main {
    public static void main(String[] args) {
        int x = 10;
        if (x > 5) {
            System.out.println("X is greater than 5");
        } else System.out.println("X is 5 or less");
        else {
            System.out.println("This is an extra else");
        }
    }
}
Q1133 hard code output
What is the output of this code?
java
class A {
    private void display() {
        System.out.println("Class A display");
    }
    public void show() {
        display();
    }
}

class B extends A {
    public void display() {
        System.out.println("Class B display");
    }
}

public class Main {
    public static void main(String[] args) {
        A obj = new B();
        obj.show();
    }
}
Q1134 easy
How can you open a `FileWriter` to append data to an existing file instead of overwriting it?
Q1135 medium code error
What will be the compilation error in this code?
java
class Base {
    public int calculate() {
        return 10;
    }
}

class Derived extends Base {
    @Override
    public double calculate() {
        return 20.0;
    }
}
Q1136 hard code error
What is the compile-time error in the `Circle` class?
java
class Shape {
    public void draw(int size) {
        System.out.println("Drawing Shape of size " + size);
    }
}
class Circle extends Shape {
    @Override
    public void draw(double radius) { // Line 8
        System.out.println("Drawing Circle with radius " + radius);
    }
}
public class Canvas {
    public static void main(String[] args) {
        Shape s = new Circle();
        s.draw(10);
    }
}
Q1137 hard
Consider the `read(char[] cbuf, int off, int len)` method of `FileReader`. If `len` is specified as 0, what is the guaranteed return value, assuming the stream is open and valid?
Q1138 hard code output
What is the output of this code?
java
class InitialException extends Exception { public InitialException(String msg) { super(msg); } }
class WrappedException extends Exception { public WrappedException(String msg, Throwable c) { super(msg, c); } }
class FinallyException extends RuntimeException { public FinallyException(String msg) { super(msg); } }
public class Main {
    public static void foo() throws InitialException { throw new InitialException("Original problem"); }
    public static void bar() throws WrappedException {
        try { foo(); }
        catch (InitialException e) { System.out.println("Caught in bar: " + e.getMessage()); throw new WrappedException("Wrapped original", e); }
        finally { System.out.println("Finally in bar"); throw new FinallyException("Finally block issue"); }
    }
    public static void main(String[] args) {
        try { bar(); }
        catch (Throwable t) { System.out.println("Caught in main: " + t.getClass().getSimpleName() + ": " + t.getMessage()); }
    }
}
Q1139 medium
Which of the following is a key functional difference between `java.util.HashMap` and `java.util.Hashtable`?
Q1140 medium code output
What is the output of this Java program?
java
class FinallyThrow {
    public static void main(String[] args) {
        try {
            System.out.println("Entering try");
            throw new RuntimeException("Exception from try");
        } catch (Exception e) {
            System.out.println("Catching: " + e.getMessage());
            throw new IllegalArgumentException("Rethrown from catch");
        } finally {
            System.out.println("Entering finally");
            throw new IllegalStateException("Exception from finally");
        }
    }
}
← Prev 5556575859 Next → Page 57 of 200 · 3994 questions