☕ Java MCQ Questions – Page 59

Questions 1161–1180 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1161 hard code error
What error will this code produce at runtime?
java
public class ArrayOutOfBoundsLoop {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30};
        for (int i = 0; i <= numbers.length; i++) {
            System.out.println(numbers[i]);
        }
    }
}
Q1162 easy code error
What is the compilation error in the following Java code snippet?
java
public class LoopError {
    public static void main(String[] args) {
        int counter = 5;
        do; {
            System.out.println(counter);
            counter--;
        } while (counter > 0);
    }
}
Q1163 hard code error
What type of error will this Java code generate during compilation?
java
class Test {
    public static void main(String[] args) {
        int count = 0;
        do {
            count++;
        } while (count); // 'count' is an int
        System.out.println("Done.");
    }
}
Q1164 medium
If you attempt to add an element that already exists (based on its comparison) in a `TreeSet`, what will be the outcome?
Q1165 hard code output
What is the output of this code?
java
public class VarShadowing {
    public static void main(String[] args) {
        String value = "Outer";
        {
            // This line attempts to redeclare 'value' in an inner scope.
            // Local variable type inference (var) does not change scoping rules.
            var value = "Inner";
            System.out.println(value);
        }
        System.out.println(value);
    }
}
Q1166 hard code output
What does this code print?
java
abstract class Elem {
    String tag = "generic";
    void render() { System.out.println("Render: " + tag); paint(); }
    abstract void paint();
}
class Button extends Elem {
    Button(String t) { this.tag = t; }
    @Override void paint() { System.out.println("Paint Button: " + tag); }
}
public class Main {
    public static void main(String[] args) {
        Elem e = new Button("Submit");
        e.render();
    }
}
Q1167 easy code error
What is the error in this code?
java
import java.util.TreeMap;

public class Test {
    public static void main(String[] args) {
        TreeMap<int, String> map = new TreeMap<>();
        map.put(10, "Ten");
        System.out.println(map.get(10));
    }
}
Q1168 easy code error
What type of error will this Java code produce when executed?
java
import java.util.Queue;
import java.util.LinkedList;

public class QueueError {
    public static void main(String[] args) {
        Queue rawQueue = new LinkedList();
        rawQueue.offer("Hello");
        rawQueue.offer(123);
        Integer value = (Integer) rawQueue.peek();
        System.out.println(value);
    }
}
Q1169 hard
Consider the following code: java import java.util.function.Function; Function<String, Integer> f1 = Integer::parseInt; Function<Integer, String> f2 = i -> "Value: " + i; Function<String, String> f3 = f1.andThen(f2); Function<Integer, Integer> f4 = i -> i * 2; Which of the following chained functional interface operations will compile successfully?
Q1170 medium code error
What error occurs when compiling this Java code?
java
public class MyClass {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Java");
        String s = sb;
        System.out.println(s);
    }
}
Q1171 medium code output
What is printed when this Java code is executed?
java
public class FinallyUncaughtException {
    public static void main(String[] args) {
        try {
            System.out.println("In try block");
            Integer.parseInt("abc"); // Throws NumberFormatException
        } finally {
            System.out.println("In finally block");
        }
        System.out.println("Program finished");
    }
}
Q1172 hard
Consider the method `file.setWritable(true, false)`. What is the precise effect of the second argument (`false`) on a Unix-like operating system?
Q1173 easy code error
What compile-time error will this code produce?
java
class MyJob {
    public void execute() {
        System.out.println("Executing job...");
    }
}

public class Main {
    public static void main(String[] args) {
        MyJob job = new MyJob();
        Thread thread = new Thread(job);
        thread.start();
    }
}
Q1174 hard code output
What is the output of this Java code snippet, assuming the process has initial write permissions to the created file?
java
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class SetWritableBehavior {
    public static void main(String[] args) throws IOException {
        Path tempFile = Files.createTempFile("set_writable", ".txt");
        File fileObj = tempFile.toFile();

        System.out.println("Initial canWrite: " + fileObj.canWrite());

        boolean setWritableResult1 = fileObj.setWritable(false); // Equivalent to setWritable(false, false)
        System.out.println("setWritable(false) result: " + setWritableResult1);
        System.out.println("After setWritable(false) canWrite: " + fileObj.canWrite());

        boolean setWritableResult2 = fileObj.setWritable(true, true); // ownerOnly = true
        System.out.println("setWritable(true, true) result: " + setWritableResult2);
        System.out.println("After setWritable(true, true) canWrite: " + fileObj.canWrite());

        Files.delete(tempFile);
    }
}
Q1175 hard
When converting an `ArrayList<String>` to a `String[]` array, what is the crucial difference and potential advantage of using `toArray(T[] a)` over `toArray()`?
Q1176 medium code output
What is the output of this code?
java
import java.util.LinkedList;

public class Test {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("A");
        list.add("B");
        list.add("C");
        list.remove(1); 
        list.add(1, "D"); 
        System.out.println(list.get(2));
    }
}
Q1177 medium code error
What is the error in this Java code snippet?
java
public class MyClass {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Java");
        sb.deleteCharAt(4);
        System.out.println(sb);
    }
}
Q1178 hard code output
What is the output of this code?
java
public class Test {
    public static void main(String[] args) {
        String result = "";
        outer: for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (i == 1 && j == 0) continue outer;
                if (i == 2 && j == 1) break;
                result += i + "" + j;
            }
        }
        System.out.print(result);
    }
}
Q1179 hard code error
Which exception is thrown when the `main` method of this Java code is executed?
java
public class GetBytesUnsupportedEncoding {
    public static void main(String[] args) {
        String s = "Hello World";
        try {
            byte[] bytes = s.getBytes("UTF-160000"); // An intentionally invalid encoding name
            System.out.println(new String(bytes));
        } catch (Exception e) {
            System.out.println(e.getClass().getSimpleName());
        }
    }
}
Q1180 easy
Which method of `BufferedReader` is commonly used to read an entire line of text?
← Prev 5758596061 Next → Page 59 of 200 · 3994 questions