Java Programming MCQs
Q.1 Spot the mistake:
int counter = 0;
while(counter < 5){
counter++;
}
System.out.println(“Count: ” + counter);
A. Loop never terminates
B. Counter not incremented
C. No error
D. Print statement incorrect
Q.2 Identify the error in this code:
for(int i=0;
i<=5;
i++) {
System.out.println(i);
}
System.out.println(i);
A. Syntax error outside loop
B. No error
C. Variable i not accessible outside loop
D. Infinite loop
Q.3 Analyze this pseudocode:
SET num = 0 WHILE num <= 5 IF num MOD 2 = 0 THEN PRINT num END IF INCREMENT num
A. 0 2 4
B. 0 1 2 3 4 5
C. 2 4
D. 0 2 4 6
Q.4 What will this pseudocode output?
SET count = 5 DO PRINT count COUNTDOWN count
A. 5 4 3 2 1
B. 5
C. 1 2 3 4 5
D. None
Q.5 What is the output of this code?
int x = 1;
while(x < 4){
System.out.println(x); x++;
}
A. 1 2 3
B. 2 3 4
C. 1 2 3 4
D. 1
Q.6 What will the following loop print?
for(int i = 0;
i < 3;
i++){
System.out.println(i);
}
A. 0 1 2
B. 1 2 3
C. 0 1 2 3
D. None
Q.7 Which keyword is used to exit a loop prematurely in Java?
A. break
B. continue
C. exit
D. stop
Q.8 What is the difference between ‘while’ and ‘do-while’ loops in Java?
A. Syntax only
B. The ‘do-while’ loop executes at least once
C. Execution speed
D. No difference
Q.9 In a ‘switch-case’ statement, what is the role of the ‘break’ keyword?
A. To pause the execution
B. To terminate the case block
C. To skip to the next case
D. To repeat the case block
Q.10 What will be the output of the following code snippet:
if(false){
System.out.println(“True”);
}
else{
System.out.println(“False”);
}
A. True
B. False
C. Error
D. No Output
Q.11 Which control structure is used to execute a block of code multiple times?
A. if
B. switch-case
C. for
D. try-catch
Q.12 Spot the mistake in this code snippet:
int i = 0;
while(i < 10) {
i++;
}
System.out.println(i);
A. Infinite loop
B. Syntax error
C. No error
D. Prints 0
Q.13 Identify the error in this code:
int[] nums = new int[2]; nums[0] = 1; nums[1] = 2; nums[2] = 3;
A. Array index out of bounds
B. Incorrect array declaration
C. No error
D. Syntax error
Q.14 Determine the output:
SET num = 8
IF num MOD 2 = 0
THEN PRINT “Even”
ELSE
PRINT “Odd”
A. Even
B. Odd
C. Error
D. No output
Q.15 Evaluate this pseudocode:
SET a = 3 SET b = 4 PRINT a * b
A. 7
B. 12
C. Error
D. None of the above
Q.16 What is the output of this pseudocode?
SET x = 10
IF x > 5
THEN PRINT “Greater”
ELSE PRINT “Lesser”
A. Greater
B. Lesser
C. Error
D. No output
Q.17 What does the following Java code print?
int x = 5;
int y = x++;
System.out.println(y);
A. 5
B. 6
C. Syntax Error
D. None of the above
Q.18 Identify the output of this code:
boolean isJavaFun = true;
System.out.println(!isJavaFun);
A. true
B. false
C. Error
D. null
Q.19 What will be the output of the following code snippet:
int a = 10;
int b = 20;
System.out.println(a + b);
A. 10
B. 20
C. 30
D. Error
Q.20 What is the range of the short data type in Java?
A. -32768 to 32767
B. -128 to 127
C. -2147483648 to 2147483647
D. 0 to 65535
Q.21 Which keyword is used to define a constant variable in Java?
A. final
B. static
C. const
D. immutable
Q.22 What is the result of this operation in Java:
(int)(7.9)?
A. 7
B. 7.9
C. 8
D. Syntax Error
Q.23 What is the default value of a boolean variable in Java?
A. true
B. false
C. 0
D. null
Q.24 Which data type would be best for storing a person’s age in Java?
A. int
B. double
C. long
D. byte
Q.25 vWhich of these is a single-line comment in Java?
A. /* comment */
B. // comment
C. <!−− comment −−>
D. % comment
Q.26 In Java, how should class names be written?
A. camelCase
B. snake_case
C. PascalCase
D. kebab-case
Q.27 Which feature of Java makes it possible to run a Java program on different platforms?
A. Object-Oriented
B. Platform-Independent
C. Syntax
D. Memory Management
Q.28 What is the purpose of the PATH environment variable in Java?
A. To locate Java libraries
B. To store Java bytecode
C. To locate the Java compiler
D. To optimize Java code
Q.29 Which component of Java is responsible for running the compiled Java bytecode?
A. JDK
B. JVM
C. JRE
D. JIT
Q.30 Which of these is not a feature of Java?
A. Object-oriented
B. Platform-independent
C. Compiled
D. Interpreted language
Q.31 Which access modifier in Java makes a member accessible only within its own class?
A. private
B. public
C. protected
D. default
Q.32 What distinguishes an abstract class from a regular class in Java?
A. An abstract class cannot create objects
B. An abstract class only contains abstract methods
C. An abstract class cannot contain constructors
D. An abstract class cannot have instance variables
Q.33 What is polymorphism in Java?
A. The ability of a variable to hold different data types
B. The ability of a class to implement multiple methods with the same name
C. The ability of a method to perform different tasks based on the context
D. The ability of a class to extend multiple classes
Q.34 In Java, how is a ‘static’ variable different from an ‘instance’ variable?
A. Static variables are shared across all instances of a class
B. Static variables are reinitialized with each object instance
C. Instance variables are shared across all instances of a class
D. Instance variables are constants
Q.35 What is the purpose of a constructor in Java?
A. To create an instance of a class
B. To initialize variables
C. To define methods
D. To declare classes
Q.36 Which keyword is used for inheritance in Java?
A. extends
B. implements
C. inherits
D. super
Q.37 What is an instance variable in Java?
A. A variable defined inside a method
B. A variable defined outside of any method but inside a class
C. A static variable
D. A final variable
Q.38 What does ‘inheritance’ in Java imply?
A. A class can call methods of another class
B. A class shares structure and behaviors from another class
C. A class must override all methods of another class
D. A class is a blueprint for objects
Q.39 Which of these is not a principle of Object-Oriented Programming?
A. Inheritance
B. Polymorphism
C. Compilation
D. Abstraction
Q.40 What is the primary feature of Object-Oriented Programming in Java?
A. Inheritance
B. Abstraction
C. Encapsulation
D. Polymorphism
Q.41 Identify the flaw in this Java code:
char[] chars = new char[-1];
A. Negative array size
B. Syntax error
C. No error
D. Logic error
Q.42 Find the error in this Java code:
int[][] matrix = new int[2][2];
matrix[0][0] = 1;
matrix[0][1] = 2;
matrix[1][0] = 3;
System.out.println(matrix[1][1]);
A. Prints incorrect value
B. No error
C. Syntax error
D. Missing initialization for matrix[1][1]
Q.43 Spot the mistake in this code:
String[] names = {“Java”, “Python”, “C”};
for(int i = 0;
i < names.length;
i++) {
System.out.println(names[i].length());
}
A. Prints incorrect lengths
B. No error
C. Syntax error
D. Logic error
Q.44 Identify the issue in this code snippet:
int[] numbers = new int[5];
for(int i = 0;
i <= numbers.length;
i++) {
System.out.println(numbers[i]);
}
A. Out of bounds array access
B. No issue
C. Syntax error
D. Logic error
Q.45 What will be the result of this pseudocode?
SET matrix = [[1, 2], [3, 4]]
PRINT matrix[0][1]
A. 1
B. 2
C. 3
D. 4
Q.46 Determine the output of this pseudocode:
SET arr = [10, 20, 30]
SET sum = 0
FOR i = 0 TO LENGTH(arr) – 1
INCREMENT sum BY arr[i]
PRINT sum
A. 60
B. 10 20 30
C. Error
D. None
Q.47 What will this pseudocode output?
SET arr = [1, 2, 3] FOR EACH num IN arr PRINT num
A. 1 2 3
B. 123
C. Error
D. None
Q.48 Analyze this code:
int[] nums = new int[3];
nums[1] = 10;
int x = nums[1] + nums[0];
System.out.println(x);
A. 0
B. 10
C. Error
D. None
Q.49 What will be the result of executing this code snippet?
String[] names = {“Java”, “Python”, “C++”};
System.out.println(names[1].length());
A. 4
B. 5
C. 6
D. Error
Q.50 What does this Java code do?
int[][] arr = {{1, 2}, {3, 4}};
for(int i = 0;
i < arr.length;
i++) {
for(int j = 0;
j < arr[i].length;
j++) {
System.out.print(arr[i][j] + ” “); } System.out.println();
}
A. Prints a 2D array in matrix form
B. Prints only the first row of the array
C. Prints the sum of the array elements
D. Gives an error
Q.51 What will the following code output:
int[] arr = {1, 2, 3};
for(int num : arr) {
System.out.println(num);
}
A. 1 2 3
B. 123
C. Error
D. None
Q.52 In a multi-dimensional array, how do you access the element in the second row and third column of an array named ‘matrix’?
A. matrix[1][2]
B. matrix[2][3]
C. matrix[2][2]
D. matrix[1][3]
Q.53 Which of these is not a valid way to instantiate an array in Java?
A. int[] arr = new int[5];
B. int arr[] = new int[5];
C. int arr[] = {1, 2, 3, 4, 5};
D. int arr[] = int[5];
Q.54 What is the purpose of the ‘length’ attribute in a Java array?
A. To determine the size of the array
B. To modify the size of the array
C. To sort the array
D. To initialize the array
Q.55 What will happen if you try to access an index outside the bounds of an array in Java?
A. Compile-time error
B. Runtime error: ArrayIndexOutOfBoundsException
C. No error
D. Logical error
Q.56 How do you access the third element in an array named ‘arr’?
A. arr[2]
B. arr[3]
C. arr(2)
D. arr(3)
Q.57 What does ‘BufferedReader’ in Java provide that ‘Scanner’ does not?
A. Faster input reading
B. Input from file only
C. Different data types
D. Graphical interface
Q.58 In Java, what is the default value of an array of integers?
A. 0
B. null
C. 1
D.-1
Q.59 Which class is commonly used for simple keyboard input in Java?
A. Scanner
B. InputStream
C. BufferedReader
D. FileReader
Q.60 Find the error in this code:
int num = 1;
do{
System.out.println(num);
num++;
}
while(num <= 3);
System.out.println(num);
A. Infinite loop
B. Syntax error
C. Prints wrong values
D. No error
Q.61 What will this pseudocode output?
SET a = 10
SET b = 0
TRY
SET c = a / b
CATCH EXCEPTION PRINT “Error” END TRY
A. Error
B. 10
C. 0
D. None
Q.62 Analyze the output of this code snippet:
try {
int[] arr = new int[5];
arr[10] = 100;
System.out.println(“Value set”);
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println(“Index Error”);
}
A. Value set
B. Index Error
C. Error
D. None
Q.63 What will the following code output:
try {
int a = 5 / 0;
System.out.println(a);
}
catch (ArithmeticException e) {
System.out.println(“Arithmetic Error”);
}
A. Arithmetic Error
B. 5
C. Error
D. None
Q.64 What is the difference between checked and unchecked exceptions in Java?
A. Checked exceptions are detected at compile-time, unchecked at runtime
B. Checked exceptions are fatal, unchecked are not
C. Checked exceptions are errors in the code, unchecked are system errors
D. There is no difference
Q.65 Which keyword is used to manually throw an exception in Java?
A. throw
B. throws
C. try
D. catch
Q.66 What happens if an exception is thrown in a try block and is not caught in the corresponding catch block?
A. The exception is ignored
B. The program terminates
C. The exception is handled by the default handler
D. The program continues execution
Q.67 What is the purpose of a try-catch block in Java?
A. To handle exceptions
B. To optimize code
C. To declare variables
D. To control program flow
Q.68 What is an exception in Java?
A. An error during program execution
B. A type of Java class
C. A method declaration
D. A user input error
Q.69 What is the significance of the CLASSPATH environment variable in Java?
A. It specifies the installation directory of Java
B. It sets the maximum memory allocation for the Java Virtual Machine
C. It tells the JVM where to look for user-defined classes and packages
D. It configures the security settings of the JVM
Q.70 How do you access a class from a package in Java?
A. By using the ‘package’ keyword before the class name
B. By importing the class
C. By inheriting the class
D. By implementing an interface
Q.71 What does the ‘import’ statement do in a Java program?
A. It includes native libraries
B. It enhances performance
C. It allows access to classes in packages
D. It compiles the Java code
Q.72 What is the primary purpose of a package in Java?
A. To provide network support
B. To optimize code performance
C. To group related classes and interfaces
D. To manage software versions
Q.73 Analyze the output of this code snippet:
interface A {
int val = 5;
}
interface B extends A {
int val = 10;
}
class Test implements B {
void display() {
System.out.println(val);
}
} public class Main {
public static void main(String[] args) {
Test t = new Test();
t.display();
}
}
A. 5
B. 10
C. Error
D. None
Q.74 What will this Java code output?
interface Printable {
void print();
}
class Test implements Printable { public void print() {
System.out.println(“Hello”);
} }
public class Main {
public static void main(String[] args) {
Test t = new Test();
t.print(); }
}
A. Hello
B. Error
C. Nothing
D. Printable
Q.75 Can an interface in Java extend multiple interfaces?
A. Yes
B. No
C. Only if they are marker interfaces
D. Only abstract classes can extend multiple interfaces
Q.76 What happens when a class implements an interface in Java?
A. It must provide implementation for all methods in the interface
B. It becomes an abstract class
C. It can no longer extend other classes
D. It automatically inherits from java.lang.Object
Q.77 Can an interface in Java contain default methods?
A. Yes, from Java 8 onwards
B. No, interfaces cannot have default methods
C. Only in abstract classes
D. Only static methods are allowed
Q.78 In Java, what is an interface?
A. A fully abstract class
B. A regular class
C. A concrete class
D. A template class
Q.79 How does StringBuilder differ from StringBuffer in Java?
A. StringBuilder is synchronized; StringBuffer is not
B. StringBuilder is faster as it is not synchronized
C. StringBuilder and StringBuffer have different methods
D. StringBuilder allows storing strings of variable length
Q.80 What is an abstract class in Java?
A. A class that cannot be instantiated and has at least one abstract method
B. A class with only static methods
C. A final class
D. A class that cannot be extended
Q.81 Find the error in this Java code:
class Animal {
Animal() {
System.out.println(“An animal is created”);
}
} class Dog extends Animal {
Dog() { super();
System.out.println(“A dog is created”);
}
} public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
}
}
A. Incorrect use of super()
B. No error
C. Constructor not called
D. Syntax error
Q.82 Spot the mistake:
class Calculator {
static void sum(int a, int b) {
System.out.println(a + b);
}
public static void main(String[] args) {
sum(10, 20); }
}
A. No static context for sum
B. No error
C. sum method should return a value
D. Incorrect parameters
Q.83 Identify the issue in this code snippet:
class Book {
private String title; void setTitle(String title) {
title = title; }
}
A. The method does not set the instance variable
B. No issue
C. Syntax error
D. Logic error
Q.84 Analyze this pseudocode:
CLASS Shape METHOD area RETURN 0 END CLASS Circle EXTENDS Shape VARIABLE radius = 5 METHOD area RETURN 3.14 * radius * radius END CLASS Main CREATE Circle c PRINT c.area
A. 0
B. 78.5
C. Error
D. None
Q.85 What will be the result of this pseudocode?
CLASS Book VARIABLE title = “Java Programming” METHOD getTitle PRINT title END CLASS Main CREATE Book b CALL b.getTitle
A. Java Programming
B. null
C. Error
D. Title
Q.86 Determine the output of this pseudocode:
CLASS Animal METHOD sound PRINT “Generic Sound” END CLASS Dog EXTENDS Animal METHOD sound PRINT “Bark” END CLASS Main CREATE Dog d CALL d.sound
A. Generic Sound
B. Bark
C. Error
D. None
Q.87 What will this pseudocode output?
CLASS Vehicle SET wheels = 4 METHOD showWheels PRINT wheels END CLASS Main CREATE Vehicle v CALL v.showWheels
A. 4
B. 0
C. Error
D. None
Q.88 What does this Java code do?
class Animal {
void sound() {
System.out.println(“Generic Sound”);
}
} class Dog extends Animal { void sound() { System.out.println(“Bark”); } } public class Main { public static void main(String[] args) { Animal myAnimal = new Dog(); myAnimal.sound(); } }
A. Prints “Generic Sound”
B. Prints “Bark”
C. Error
D. Does nothing
Q.89 Analyze the output of this code snippet:
class Car {
String model; Car(String model) { this.model = model;
}
} public class Main {
public static void main(String[] args) { Car myCar = new Car(“Tesla”); System.out.println(myCar.model); } }
A. Tesla
B. null
C. Error
D. Model
Q.90 What will the following code output:
class Test {
static int x = 10;
} public class Main {
public static void main(String[] args) {
Test t1 = new Test(); System.out.println(t1.x); } }
A. 10
B. 0
C. Error
D. Null
Q.91 Why is it recommended to use StringBuilder or StringBuffer for string manipulation in loops?
A. To reduce memory usage
B. To increase execution speed
C. To avoid creating many intermediate String objects
D. Both B and C
Q.92 What is the result of concatenating strings using the ‘+’ operator in Java?
A. A new String object is created every time
B. The original string is modified
C. No new objects are created
D. A StringBuilder object is used internally
Q.93 How does StringBuilder differ from StringBuffer in Java?
A. StringBuilder is immutable, StringBuffer is not
B. StringBuilder is faster as it is not synchronized
C. StringBuilder and StringBuffer have different methods
D. There is no difference
Q.94 What is the String Constant Pool in Java?
A. A collection of all String literals
B. A memory area for constant String storage
C. A pool for reusable String objects
D. All of the above
Q.95 Identify the flaw in this Java code:
List list = new LinkedList<>(); list.add(“Java”); list.add(1, “Python”); list.remove(2);
A. The remove index is out of bounds
B. LinkedList does not allow insertion at an index
C. No error
D. Syntax error
Q.96 Find the error in this Java code:
Map<String, Integer> map = new HashMap<>();
map.put(null, 1);
map.put(null, 2);
System.out.println(map.get(null));
A. HashMap does not allow null keys
B. Prints 1
C. No error
D. Syntax error
Q.97 Spot the mistake:
Set set = new TreeSet<>(); set.add(null);
A. TreeSet does not allow null
B. No error
C. Set should be HashSet
D. Syntax error
Q.98 Identify the issue in this code snippet:
List list = new ArrayList<>(); list.get(0);
A. The list is empty
B. The get method is used incorrectly
C. The list should be a LinkedList
D. No error
Q.99 What will this pseudocode output?
CREATE map SET map[1] = “Java” SET map[2] = “Python” SET map[1] = “C++” PRINT map[1]
A. Java
B. Python
C. C++
D. Error
Q.100 Analyze this pseudocode:
CREATE set ADD “Java” TO set ADD “Python” TO set ADD “Java” TO set IF “Java” IN set PRINT “Yes” ELSE PRINT “No”
A. Yes
B. No
C. Error
D. None
Q.101 What will be the result of this pseudocode?
CREATE map SET map[“Java”] = 1 SET map[“Python”] = 2 SET map[“Java”] = 3 PRINT map[“Java”]
A. 1
B. 2
C. 3
D. Error
Q.102 Determine the output of this pseudocode:
CREATE list = [“Java”, “Python”, “Java”] FOR EACH element IN list PRINT element
A. Java Python Java
B. Java Python
C. Java
D. Python Java
Q.103 What is the result of executing this code?
Map<String, Integer> map = new TreeMap<>();
map.put(“Python”, 3);
map.put(“Java”, 1);
System.out.println(map);
A. {Java=1, Python=3}
B. {Python=3, Java=1}
C. {1=Java, 3=Python}
D. Error
Q.104 What will this Java code print?
Set set = new HashSet<>();
set.add(1); set.add(1);
System.out.println(set.size());
A. 1
B. 2
C. 0
D. Error
Q.105 Analyze the output of this code snippet:
List list = new ArrayList<>();
list.add(“Java”);
list.add(“Java”);
System.out.println(list.get(1));
A. Java
B. Error
C. null
D. None
Q.106 What does this Java code output?
Map<Integer, String> map = new HashMap<>();
map.put(1, “Java”);
map.put(2, “Python”);
System.out.println(map.size());
A. 1
B. 2
C. 3
D. None
Q.107 Which of the following is true about a TreeMap in Java?
A. It does not maintain an order
B. It is not based on the Map interface
C. It orders elements based on natural ordering or a comparator
D. It allows null keys
Q.108 What is the difference between the add() and put() methods in Java’s Collection Framework?
A. add() is for lists, put() is for maps
B. add() inserts at a specific index, put() does not
C. add() is for sets, put() is for lists
D. There is no difference
Q.109 What happens when a duplicate key is put into a HashMap?
A. It replaces the existing key’s value
B. It is ignored
C. It throws an exception
D. A new entry is created
Q.110 In Java Collections Framework, which interface represents a mapping from unique keys to values?
A. List
B. Set
C. Map
D. Queue
Q,111 What is the difference between a HashMap and a Hashtable in Java?
A. HashMap is synchronized, Hashtable is not
B. Hashtable is synchronized, HashMap is not
C. HashMap allows one null key, Hashtable does not
D. All of the above
Q.112 Which collection type does not allow duplicate elements?
A. ArrayList
B. Vector
C. Set
D. List
Q.113 What is the primary advantage of using generics in Java?
A. Faster execution
B. Reduced memory usage
C. Stronger type checking at compile-time
D. Easier code maintenance
Q.114 What are generics in Java?
A. Templates for creating collections
B. Special methods in Java
C. Memory allocation techniques
D. Data types in Java
Q.115 Identify the flaw in this Java code:
class CustomException extends Exception { }
try {
throw new CustomException();
}
catch (CustomException e) {
System.out.println(“Custom exception caught”);
}
A. CustomException should not be caught
B. CustomException should extend RuntimeException
C. Missing try block
D. No error
Q.116 Find the error in this Java code:
try {
File file = new File(“test.txt”);
FileReader fr = new FileReader(file);
}
catch (FileNotFoundException e) {
System.out.println(“File not found”);
}
A. File not found
B. No error
C. Missing import statements for File and FileReader
D. Syntax error
Q.117 Spot the mistake:
try {
String s = null;
System.out.println(s.length());
}
catch (NullPointerException e) {
System.out.println(“Null Error”);
}
A. No error
B. Null Error should not be caught
C. s should be initialized
D. The length() method cannot be used on strings
Q.118 Identify the issue in this code snippet:
try {
int x = 5 / 0;
}
catch (Exception e) {
e.printStackTrace();
}
finally {
System.out.println(“Done”);
}
A. Division by zero
B. No error
C. Missing throw statement
D. Syntax error
Q.119 What will be the result of this pseudocode?
SET arr = [1, 2, 3]
TRY
PRINT arr[3]
CATCH EXCEPTION
PRINT “Array Error” END TRY
A. 1 2 3
B. Array Error
C. Error
D. None
Q.120 Determine the output of this pseudocode:
TRY PRINT “Start” THROW NEW EXCEPTION “Failed” CATCH EXCEPTION PRINT “Caught” END TRY PRINT “End”
A. Start Caught End
B. Start Failed
C. Caught
D. Start Caught
Q.121 What is a key consideration when designing an application to support multiple languages?
A. Ensuring all text is in English and translated later
B. Using non-localized date and time formats
C. Using external files for text to facilitate easy translation
D. Hardcoding all strings in the application
Q.122 What is the primary purpose of the Locale class in Java?
A. To handle currency conversions
B. To manage different time zones
C. To represent a specific geographical, political, or cultural region
D. To change the layout of graphical components
Q.123 What will this pseudocode output?
CREATE list = [1, 2, 3, 4] CREATE stream = list.stream() FILTER stream WITH (n -> n > 2) PRINT stream
A. [1, 2, 3, 4]
B. [3, 4]
C. [1, 2]
D. Empty list
Q.124 Analyze the output of this code snippet:
List numbers = Arrays.
asList(1, 2, 3);
numbers.stream().
map(n -> n * n).
forEach(System.out::println);
A. Prints the squares of the numbers
B. Prints the numbers multiplied by 2
C. Prints the sum of the numbers
D. Prints the numbers as they are
Q.125 In Java, what are the benefits of using Callable and Future interfaces?
A. To handle synchronization between threads
B. To perform operations in the background and retrieve their results
C. To handle exceptions in multithreaded code
D. To manage thread lifecycle
Q.126 What is the purpose of the CompletableFuture class in Java?
A. To simplify multithreading programming
B. To replace the use of raw threads
C. To provide a more complex future interface
D. To handle synchronous programming
Q.127 How does the Date and Time API in Java 8 improve over the older java.util.Date class?
A. It provides more methods for date manipulation
B. It is thread-safe and immutable
C. It supports internationalization better
D. All of the above
Q.128 What is the advantage of using method references in Java 8?
A. They make the code run faster
B. They make the code more readable when referring to methods or constructors
C. They replace all lambda expressions
D. They are necessary for functional programming
Q.129 How does the Optional class help in Java?
A. It replaces all null values in a program
B. It is used to avoid NullPointerExceptions
C. It provides a better way of handling multithreading
D. It optimizes memory usage
Q.130 What are functional interfaces in Java?
A. Interfaces with only static methods
B. Interfaces with multiple abstract methods
C. Interfaces with a single abstract method
D. Interfaces that support lambda expressions only
Q.131 What is the purpose of the Streams API in Java 8?
A. To handle I/O streams
B. To create a new type of collection
C. To enable functional-style operations on collections
D. To manage threads
Q.132 What is a lambda expression in Java?
A. A type of exception
B. A new collection type
C. A concise way to represent an anonymous function
D. A data storage format
Q.133 Spot the mistake:
synchronized void myMethod() { notify(); wait(); }
A. wait() should be called before notify()
B. notify() and wait() cannot be in the same method
C. No error
D. The method is not properly synchronized
Q.134 Identify the issue in this code snippet:
class MyThread extends Thread {
public void run() {
wait(); }
}
A. Incorrect use of wait()
B. Syntax error
C. No issue
D. Incorrect thread initialization
Q.135 Analyze this pseudocode:
CREATE thread1, thread2 START thread1 CALL thread1.wait() START thread2 CALL thread2.notify()
A. thread2 starts after thread1 waits
B. thread1 and thread2 run concurrently
C. thread1 waits indefinitely
D. An error occurs
Q.136 What will this pseudocode output?
CREATE thread START thread PRINT “Thread started” END
A. Thread started
B. An error
C. Nothing
D. Thread started and then an error
Q.137 Analyze the output of this code snippet:
class MyThread extends Thread { public void run() {
for(int i = 0;
i < 5;
i++) {
System.out.println(i); }
}
} public class Main {
public static void main(String[] args) {
MyThread t = new MyThread();
t.start();
for(int i = 5;
i < 10;
i++) {
System.out.println(i); }
}
}
A. The numbers 0 to 9 in sequential order
B. The numbers 5 to 9 followed by 0 to 4
C. A mix of numbers 0 to 9 in no particular order
D. A runtime error
Q.138 What is the primary purpose of the wait() and notify() methods in Java?
A. To start and stop threads
B. To manage thread priorities
C. To handle synchronization between threads
D. To handle exceptions in threads
Q.139 In Java, what is the difference between a user thread and a daemon thread?
A. User threads are low priority, daemon threads are high priority
B. User threads run in the background, daemon threads do not
C. User threads prevent the JVM from exiting, daemon threads do not
D. Daemon threads are used for garbage collection
Q.140 What is the main difference between a single-threaded and a multi-threaded application?
A. Single-threaded applications can perform multiple tasks at once
B. Multi-threaded applications are always faster
C. Single-threaded applications use less memory
D. Multi-threaded applications can perform multiple tasks at the same time
Q.141 Find the error in this Java code:
String s1 = new String(“Java”);
String s2 = new String(“Java”);
System.out.println(s1 == s2);
A. s1 and s2 refer to the same object
B. No error
C. s1 and s2 refer to different objects
D. Syntax error
Q.142 Spot the mistake:
StringBuffer buffer = new StringBuffer(“Java”);
buffer.setLength(2);
System.out.println(buffer);
A. It will print “Ja”
B. The setLength method is used incorrectly
C. No error
D. Syntax error
Q.143 Identify the issue in this code snippet:
StringBuilder sb = “Java”; sb.append(“Script”);
A. Incorrect initialization of StringBuilder
B. No issue
C. append method used incorrectly
D. Syntax error
Q.144 What will be the result of this pseudocode?
SET str1 = “Java” SET str2 = “Java” IF str1 EQUALS str2 PRINT “Equal” ELSE PRINT “Not Equal”
A. Equal
B. Not Equal
C. Error
D. None
Analyze this pseudocode:
Q.145 CREATE buffer = StringBuffer(“Java”) CALL buffer.reverse() PRINT buffer
A. avaJ
B. Java
C. A runtime error occurs
D. None of the above
Q.146 What will this pseudocode output?
SET str = “Java” SET builder = StringBuilder(str) builder.append(“Script”) PRINT builder
A. JavaScript
B. Java
C. JavaScriptJava
D. Error
Q.147 Analyze the output of this code snippet:
StringBuffer buffer = new StringBuffer(“Java”);
buffer.reverse();
System.out.println(buffer);
A. avaJ
B. Java
C. A runtime error occurs
D. None of the above
Q.148 What does this Java code do?
StringBuilder sb = new StringBuilder(“Java”);
sb.append(” Programming”);
System.out.println(sb);
A. Appends ” Programming” to sb and prints “Java Programming”
B. Creates a new StringBuilder object
C. Throws an error
D. Prints “Java”
Q.149 What will this Java code output?
String s1 = “Java”;
String s2 = “Java”;
System.out.println(s1 == s2);
A. true
B. false
C. An error occurs
D. None of the above
Q.150 In Java, what happens when two string literals with the same content are created?
A. They refer to different objects in memory
B. They refer to the same object in the String Constant Pool
C. A runtime error occurs
D. A new object is created each time