☕ Java MCQ Questions – Page 154
Questions 3061–3080 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat 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);
}
}
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());
}
}
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());
}
}
}
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();
}
}
When would `StringBuffer` be a more appropriate choice than `StringBuilder`?
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();
}
}
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 {} }
What happens if you don't define any constructor in a Java class?
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);
}
}
}
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.");
}
}
Which keyword would you use if you want to stop processing items in a loop entirely once a certain condition is met?
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());
}
}
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);
}
}
Which of the following describes 'Widening Type Casting' in Java?
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();
}
}
When overriding a method in Java, how must the access modifier of the overriding method relate to the overridden method?
If a `return` statement is executed within the `do` block of a `do-while` loop, what happens regarding the evaluation of the `while` condition?
Which of the following interfaces does `TreeSet` primarily implement, providing features like `headSet`, `tailSet`, and `floor`?
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());
}
}
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();
}
}