☕ Java MCQ Questions – Page 170

Questions 3381–3400 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3381 hard code error
What will be the result of compiling and running this Java code?
java
public class MyClass {
    private static final int VALUE;
    static {
        System.out.println("Static init block");
        VALUE = computeValue();
    }
    private static int computeValue() {
        return VALUE + 1; 
    }
    public static void main(String[] args) {
        System.out.println(MyClass.VALUE);
    }
}
Q3382 easy code output
What does this Java code print to the console?
java
public class Main {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            if (i == 2) {
                continue;
            }
            System.out.print(i + " ");
        }
    }
}
Q3383 medium
For a `TreeMap` to work correctly using the natural ordering of its keys, what essential characteristic must the key objects possess?
Q3384 easy code output
What is the output of this code?
java
class Item {
    private String name;
    public Item(String name) { this.name = name; }
    public synchronized void process() {
        System.out.print(name + " ");
    }
}

public class MyProgram {
    public static void main(String[] args) throws InterruptedException {
        Item itemA = new Item("A");
        Item itemB = new Item("B");
        Thread t1 = new Thread(() -> itemA.process());
        Thread t2 = new Thread(() -> itemB.process());
        t1.start();
        t2.start();
        t1.join();
        t2.join();
    }
}
Q3385 medium code output
What does this Java code print?
java
import java.util.function.IntFunction;
import java.util.function.Function;

public class Test {
    public static void main(String[] args) {
        IntFunction<String> intToString = String::valueOf;
        Function<Integer, String> integerToString = String::valueOf;

        String result1 = intToString.apply(100);
        String result2 = integerToString.apply(200);

        System.out.println(result1.length() + result2.length());
    }
}
Q3386 medium code output
What is the content of 'new_file.txt' after this code executes, assuming 'new_file.txt' does not exist initially?
java
import java.io.FileWriter;
import java.io.IOException;

public class FileCreationTest {
    public static void main(String[] args) {
        try (FileWriter writer = new FileWriter("new_file.txt")) {
            writer.write("Content for a new file.");
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}
Q3387 medium
What is the approximate default buffer size (in characters/bytes) used by `BufferedReader` if not specified in its constructor?
Q3388 hard code output
What is the output of this code?
java
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(10); 
        numbers.add(20); 
        numbers.add(30); 
        numbers.add(40); 

        numbers.remove(new Integer(20)); 
        numbers.remove(1); 

        System.out.println(numbers);
    }
}
Q3389 easy code error
What will happen when this Java code is compiled and executed?
java
class MyClass {
    public void process() {
        try {
            throw new java.io.IOException("Issue");
        } catch (java.io.IOException e) {
            throw e; 
        }
    }
}
Q3390 hard code error
What is the error in this Java code snippet?
java
public class BuilderError {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Mutable");
        String s = sb;
        System.out.println(s);
    }
}
Q3391 easy code output
What does this code print to the console?
java
public class Main {
    public static void main(String[] args) {
        int p = 7;
        p += 3;
        System.out.println(p);
    }
}
Q3392 easy code error
What is the expected error when compiling and running the following Java code?
java
import java.util.LinkedList;

public class MyClass {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("A");
        list.add("B");
        for (String s : list) {
            if (s.equals("A")) {
                list.add("C"); // Modifying list while iterating with a for-each loop
            }
        }
    }
}
Q3393 hard code error
What is the runtime error encountered when executing this Java code snippet?
java
import java.util.HashSet;
import java.util.Iterator;

public class Main {
    public static void main(String[] args) {
        HashSet<String> set = new HashSet<>();
        set.add("A");
        set.add("B");
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()) {
            String element = iterator.next();
            if (element.equals("A")) {
                set.remove(element);
            }
        }
    }
}
Q3394 hard
What is the primary architectural purpose of the `ThreadGroup` class in Java?
Q3395 easy code error
What is the output or error of the following Java code?
java
public class ExceptionTest {
    public static void main(String[] args) {
        try {
            System.out.println("Inside try block");
        }
    }
}
Q3396 hard
You have a `TreeMap<Integer, String> map` containing `{1="A", 5="E", 10="J"}`. You create a `NavigableMap<Integer, String> sub = map.subMap(1, true, 5, true);`. If you then call `sub.remove(1);`, what will be the state of the original `map`?
Q3397 hard
Given the following Java code structure (without curly braces): `if (condition1) if (condition2) statementA; else statementB;`. To which `if` statement does `else statementB;` logically belong according to Java's rules?
Q3398 hard code error
What compile-time error will this Java code produce?
java
public class OperatorError3 {
    public static void main(String[] args) {
        int x = 5;
        double y = 10.0;
        int result = (x > y) ? x : "Hello"; // Error line
        System.out.println(result);
    }
}
Q3399 easy code error
What compilation error will occur when compiling this Java code?
java
import java.io.BufferedReader;
import java.io.IOException;

public class MyClass {
    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(System.in);
        try {
            String line = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Q3400 medium code output
What is the outcome of running this code?
java
public class Main {
    private static Object lock1 = new Object();
    private static Object lock2 = new Object();

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            synchronized (lock1) {
                System.out.println("Thread 1: Holding lock 1...");
                try { Thread.sleep(100); } catch (InterruptedException e) {}
                synchronized (lock2) {
                    System.out.println("Thread 1: Holding lock 1 & 2");
                }
            }
        });

        Thread t2 = new Thread(() -> {
            synchronized (lock2) {
                System.out.println("Thread 2: Holding lock 2...");
                try { Thread.sleep(100); } catch (InterruptedException e) {}
                synchronized (lock1) {
                    System.out.println("Thread 2: Holding lock 1 & 2");
                }
            }
        });

        t1.start();
        t2.start();
    }
}
← Prev 168169170171172 Next → Page 170 of 200 · 3994 questions