☕ Java MCQ Questions – Page 154

Questions 3061–3080 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3061 hard code error
What compile-time error will this Java code snippet produce?
java
public class BitwiseOpTest {
    public static void main(String[] args) {
        byte b = 10; // Binary 00001010
        byte result = ~b; // Bitwise NOT operator promotes operand to int
        System.out.println(result);
    }
}
Q3062 medium code output
What does this code print?
java
public class Main {
    private int count = 0;

    public void incrementIncorrectly() {
        synchronized (new Object()) { 
            count++;
        }
    }

    public int getCount() {
        return count;
    }

    public static void main(String[] args) throws InterruptedException {
        Main obj = new Main(); 
        Runnable task = () -> {
            for (int i = 0; i < 5000; i++) {
                obj.incrementIncorrectly(); 
            }
        };

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

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

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

        System.out.println(obj.getCount());
    }
}
Q3063 medium code error
What error will this Java code produce when executed?
java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> data = null;
        Iterator<String> it = data.iterator(); // Calling iterator() on a null reference
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }
}
Q3064 medium code output
What is the result of running this Java code?
java
class BaseClass {
    private void displayMessage() {
        System.out.println("BaseClass message");
    }

    public void callDisplay() {
        displayMessage();
    }
}

class DerivedClass extends BaseClass {
    public void displayMessage() { // New method, not an override
        System.out.println("DerivedClass message");
    }
}

public class Main {
    public static void main(String[] args) {
        DerivedClass obj = new DerivedClass();
        obj.callDisplay();
        obj.displayMessage();
    }
}
Q3065 easy
When would `StringBuffer` be a more appropriate choice than `StringBuilder`?
Q3066 hard code error
What is the compile-time error in this Java code?
java
abstract class Shape {
    public abstract double area();
    public void display() {
        System.out.println("Displaying shape.");
    }
}

public class Main {
    public static void main(String[] args) {
        Shape s = new Shape();
    }
}
Q3067 easy code error
What will happen when this Java code is compiled and executed?
java
class Base { public void methodB() throws java.io.FileNotFoundException {} }
class Derived extends Base { public void methodB() throws java.io.IOException {} }
Q3068 easy
What happens if you don't define any constructor in a Java class?
Q3069 easy code error
What error will this code produce?
java
public class LoopError {
    public static void main(String[] args) {
        int singleValue = 5;
        for (int num : singleValue) {
            System.out.println(num);
        }
    }
}
Q3070 easy code output
What does this code print?
java
public class Main {
    public static void main(String[] args) {
        try {
            String s = null;
            s.length(); // Throws NullPointerException
            System.out.print("Try block executed. ");
        } catch (ArithmeticException e) {
            System.out.print("Catch block executed. ");
        } finally {
            System.out.print("Finally block executed. ");
        }
        System.out.print("Program ends.");
    }
}
Q3071 easy
Which keyword would you use if you want to stop processing items in a loop entirely once a certain condition is met?
Q3072 medium code output
What does this code print?
java
import java.io.*;

class Parent {
    protected int parentValue;
    public Parent() {
        this.parentValue = 10; // Default value
    }
    public int getParentValue() { return parentValue; }
}

class Child extends Parent implements Serializable {
    private int childValue;
    public Child(int childValue) {
        super();
        this.childValue = childValue;
    }
    public int getChildValue() { return childValue; }
}

public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Child originalChild = new Child(20); 
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(originalChild);
        oos.close();

        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        Child deserializedChild = (Child) ois.readObject();
        ois.close();
        System.out.println(deserializedChild.getParentValue() + "," + deserializedChild.getChildValue());
    }
}
Q3073 hard code output
What is the output of this code?
java
public class Test {
    public static void main(String[] args) {
        StringBuilder sb1 = new StringBuilder("HelloWorld");
        CharSequence cs = sb1.subSequence(5, 10);
        StringBuilder sb2 = new StringBuilder("Java");
        sb2.append(cs);
        System.out.println(sb2);
    }
}
Q3074 easy
Which of the following describes 'Widening Type Casting' in Java?
Q3075 easy code error
What compile-time error will this code produce?
java
public class Main {
    public static void main(String[] args) {
        Runnable myRunnable = new Runnable() {
            // Missing implementation of run() method
        };
        new Thread(myRunnable).start();
    }
}
Q3076 medium
When overriding a method in Java, how must the access modifier of the overriding method relate to the overridden method?
Q3077 hard
If a `return` statement is executed within the `do` block of a `do-while` loop, what happens regarding the evaluation of the `while` condition?
Q3078 medium
Which of the following interfaces does `TreeSet` primarily implement, providing features like `headSet`, `tailSet`, and `floor`?
Q3079 medium code output
What does this code print?
java
import java.io.*;

class User implements Serializable {
    private String username;
    private transient String password; // Will not be serialized

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }
    public String getDetails() {
        return "Username: " + username + ", Password: " + password;
    }
}

public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        User user = new User("admin", "secret");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(user);
        oos.close();

        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        User deserializedUser = (User) ois.readObject();
        ois.close();
        System.out.println(deserializedUser.getDetails());
    }
}
Q3080 hard code error
What is the compile-time error in this Java code?
java
public class LambdaModifyOuterVariable {
    public static void main(String[] args) {
        int count = 0;
        Runnable task = () -> {
            // count++; // Line 5
            System.out.println("Current count: " + count);
        };
        new Thread(task).start();
    }
}
← Prev 152153154155156 Next → Page 154 of 200 · 3994 questions