☕ Java MCQ Questions – Page 8

Questions 141–160 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q141 easy code error
What happens when you attempt to compile this Java program?
java
public class Main {
    public static void main(String[] args) {
        try {
            break;
        } catch (Exception e) {
            System.out.println("Caught exception");
        }
    }
}
Q142 easy code output
What is the output of this code?
java
class SimpleCounter {
    private int count = 0;
    public synchronized void increment() {
        count++;
        System.out.print(count);
    }
}

public class MyProgram {
    public static void main(String[] args) throws InterruptedException {
        SimpleCounter sc = new SimpleCounter();
        Thread t1 = new Thread(() -> sc.increment());
        Thread t2 = new Thread(() -> sc.increment());
        t1.start();
        t2.start();
        t1.join();
        t2.join();
    }
}
Q143 hard code error
What compilation error will occur when compiling this Java code?
java
public class LabeledNonLoopContinue {
    public static void main(String[] args) {
        int count = 0;
        outerBlock: { 
            for (int i = 0; i < 3; i++) {
                if (i == 1) {
                    continue outerBlock;
                }
                count++;
            }
        }
        System.out.println("Count: " + count);
    }
}
Q144 medium
What is the default initial capacity of a `StringBuffer` object when constructed using its no-argument constructor?
Q145 easy code error
What compilation error will occur when compiling this Java code?
java
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;

public class MyClass {
    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(new StringReader("hello"));
        try {
            String line = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        br.close();
    }
}
Q146 easy code error
Which error will occur when running this Java code?
java
import java.io.FileReader;
import java.io.IOException;
import java.io.File;
import java.io.FileWriter;

public class FileReaderIssue {
    public static void main(String[] args) throws IOException {
        File tempFile = File.createTempFile("test", ".txt");
        FileWriter writer = new FileWriter(tempFile);
        writer.write("Hello");
        writer.close();
        
        FileReader reader = new FileReader(tempFile);
        char[] buffer = null; // Null buffer
        reader.read(buffer); // Attempt to read into null buffer
        reader.close();
        tempFile.deleteOnExit();
    }
}
Q147 easy code output
What is the output of this code?
java
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        ArrayList<Double> prices = new ArrayList<>();
        prices.add(19.99);
        prices.add(25.50);
        prices.clear();
        System.out.println(prices.isEmpty());
    }
}
Q148 medium
When is an explicit call to a superclass constructor (`super()`) required in a subclass constructor?
Q149 medium
In the context of method overriding, what does 'covariant return type' refer to?
Q150 hard code error
Consider the following Java code attempting to write to a read-only file. What error will it produce?
java
import java.io.*;

public class FileWriterError7 {
    public static void main(String[] args) {
        File file = new File("readonly.txt");
        try {
            // Create and close the file first to ensure it exists
            new FileWriter(file).close(); 
            file.setReadOnly(); // Set the file to read-only
            
            // Attempt to write to the read-only file in overwrite mode
            FileWriter fw = new FileWriter(file, false); 
            fw.write("New data");
            fw.close();
        } catch (IOException e) {
            System.out.println("Error: " + e.getClass().getSimpleName() + " - " + e.getMessage());
        } finally {
            file.setWritable(true); // Make writable to ensure deletion
            file.delete(); // Clean up
        }
    }
}
Q151 easy code error
What compile-time error will occur in the `Child` class?
java
class Parent {
    public void display() {
        System.out.println("Parent's display");
    }
}

class Child extends Parent {
    @Override
    protected void display() {
        System.out.println("Child's display");
    }
}
Q152 easy code output
What does this Java code snippet print to the console?
java
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        TreeMap<Character, Double> treeMap = new TreeMap<>();
        treeMap.put('X', 10.0);
        treeMap.put('Y', 20.0);
        treeMap.put('X', 30.0); // Overwrites the previous value
        System.out.println(treeMap.get('X'));
    }
}
Q153 easy code error
What compile-time error will this code produce?
java
class Printer {
    public void printDocument() {
        System.out.println("Printing document...");
    }
}

public class Main {
    public static void main(String[] args) {
        Printer printer = new Printer();
        Runnable runnablePrinter = printer; // Attempt to assign non-Runnable to Runnable
        new Thread(runnablePrinter).start();
    }
}
Q154 easy code error
What compile-time error will occur in the `MyClass()` constructor?
java
class MyClass {
    MyClass() {
        this(); // Recursive constructor invocation
    }
}
public class Main {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
    }
}
Q155 easy code error
What compilation error will arise from the following Java code?
java
interface ServiceA {
    void performA();
}

interface ServiceB {
    void performB();
}

@FunctionalInterface
interface CombinedService extends ServiceA, ServiceB {
    // This interface itself has no abstract methods, but inherits two from parents.
}

public class Main {
    public static void main(String[] args) {
        // Attempt to use CombinedService
    }
}
Q156 easy
Given an array `int[] data = {10, 20, 30};`, how would you access the second element (which has the value 20)?
Q157 easy code output
What is the output of this Java code?
java
public class CharArrayMutability {
    public static void main(String[] args) {
        char[] charArray = {'a', 'b', 'c'};
        charArray[1] = 'x';
        System.out.println(charArray);
    }
}
Q158 medium code error
What is the compile-time error in this Java code?
java
interface Processor {
    void process();
}

public class LambdaError {
    static int data = 100;

    public static void executeProcessor(Processor p) {
        p.process();
    }

    public static void main(String[] args) {
        int localData = 50;
        executeProcessor(() -> {
            data = data + localData; // Modifying static field using effectively final local variable
        });
        System.out.println(data);
    }
}
Q159 easy code output
What does this code print?
java
public class LambdaThread {
    public static void main(String[] args) {
        Thread myThread = new Thread(() -> {
            System.out.println("Lambda thread says hi from " + Thread.currentThread().getName());
        });
        myThread.start();
    }
}
Q160 easy code output
What is the output of the following Java code?
java
public class Test {
    public static void main(String[] args) {
        int[] values = new int[4];
        values[0] = 5;
        values[2] = 15;
        System.out.println(values[0] + values[1]);
    }
}
← Prev 678910 Next → Page 8 of 200 · 3994 questions