☕ Java MCQ Questions – Page 162

Questions 3221–3240 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3221 easy code output
What is the output of this Java code?
java
public class MyRunner implements Runnable {
    @Override
    public void run() {
        System.out.println("Runnable running.");
    }

    public static void main(String[] args) {
        System.out.println("Main starting...");
        Thread t = new Thread(new MyRunner());
        t.start();
        System.out.println("Main ending.");
    }
}
Q3222 easy code output
What is the output of this Java code snippet?
java
public class TypeCast {
    public static void main(String[] args) {
        long bigNum = 50000L;
        int smallNum = (int) bigNum;
        System.out.println(smallNum);
    }
}
Q3223 easy code output
What does this Java code print to the console?
java
public class TypeCast {
    public static void main(String[] args) {
        double myDouble = 9.78;
        int myInt = (int) myDouble;
        System.out.println(myInt);
    }
}
Q3224 hard
If `byte b = -5;`, what is the value of `~b` (bitwise NOT) when treated as an `int` after promotion?
Q3225 medium code error
What is the result of attempting to compile and run the following Java code?
java
class JoinerThread extends Thread {
    private Thread otherThread;
    public JoinerThread(Thread other) {
        this.otherThread = other;
    }
    public void run() {
        System.out.println("Waiting for other thread...");
        otherThread.join(); // This line
        System.out.println("Other thread finished!");
    }
}
public class Main {
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            try { Thread.sleep(100); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
        });
        t1.start();
        new JoinerThread(t1).start();
    }
}
Q3226 easy code output
What is the output of this code?
java
import java.util.HashSet;

public class Test {
    public static void main(String[] args) {
        HashSet<Integer> numbers = new HashSet<>();
        numbers.add(10);
        numbers.add(20);
        numbers.add(10); // Duplicate
        numbers.add(30);
        System.out.println(numbers.size());
    }
}
Q3227 easy code output
What is the output of this code?
java
public class StringTest {
    public static void main(String[] args) {
        String s = "computer";
        System.out.println(s.contains("put"));
    }
}
Q3228 easy code error
Will this Java code compile and run successfully, and if so, what kind of behavior is it demonstrating regarding the `while` loop?
java
public class LoopError7 {
    public static void main(String[] args) {
        final int LIMIT = 0;
        int count = 5;
        while (count < LIMIT) { // Condition is always false
            System.out.println("Count is: " + count);
            count++;
        }
        System.out.println("Program finished.");
    }
}
Q3229 easy
Which keyword is used to explicitly raise an exception instance?
Q3230 medium
Consider `class Animal {} class Dog extends Animal {}`. If you have `Animal a = new Dog();`, and you want to call a `bark()` method specific to the `Dog` class, what type of casting is required?
Q3231 easy
Which operator is used to invert the value of a boolean expression in Java?
Q3232 easy
What exception is thrown if you try to access an element at an invalid index (e.g., a negative index or an index greater than or equal to the array's length) in a Java array?
Q3233 medium
What is the primary characteristic of a Java array concerning its size after initialization?
Q3234 hard
Consider a method `void processStrings(String... values)`. If this method is invoked as `processStrings(null);`, what is the state of the `values` parameter inside the method body?
Q3235 easy code error
What error will occur when compiling the following Java code?
java
public class Main {
    public static void main(String[] args) {
        int num = 10;
        if (num instanceof Integer) {
            System.out.println("num is an Integer.");
        } else {
            System.out.println("num is not an Integer.");
        }
    }
}
Q3236 hard
When constructing a `FileReader` with a file path that points to a non-existent file, which specific type of `IOException` is most likely to be thrown?
Q3237 easy
Is the `Runnable` interface considered a functional interface in Java 8 and later?
Q3238 hard code error
What is the compilation error in the following Java code snippet?
java
class NonFunctionalTarget {
    void doIt() { System.out.println("Doing it."); }
}

public class Main {
    public static void main(String[] args) {
        NonFunctionalTarget target = () -> System.out.println("Lambda action");
        System.out.println("Attempting to assign lambda...");
    }
}
Q3239 easy code error
What kind of error will occur when running this Java code?
java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        BufferedWriter bw = null;
        try {
            // This condition ensures bw remains null for typical execution
            if (false) {
                bw = new BufferedWriter(new FileWriter("output.txt"));
            }
            bw.write("Attempt to write to a null buffer.");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            System.err.println("Caught NPE: " + e.getMessage());
        }
    }
}
Q3240 easy
What is the primary risk associated with narrowing type casting in Java?
← Prev 160161162163164 Next → Page 162 of 200 · 3994 questions