Updated MCQ Practice
Updated MCQ Practice
java
Copy code
int[] numbers = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < numbers.length; i++) {
if (i % 2 == 0) {
sum += numbers[i];
}
}
System.out.println(sum);
• A) 9
• B) 12
• C) 8
• D) 15
java
Copy code
int[] arr = new int[5];
for (int i = 0; i <= arr.length; i++) {
arr[i] = i;
}
2. String Manipulation
java
Copy code
String s = "hello";
s = s.toUpperCase();
System.out.println(s);
• A) HELLO
• B) hello
• C) Compilation error
• D) NullPointerException
java
Copy code
String s1 = "Hello";
String s2 = "hello";
if (s1.equals(s2)) {
System.out.println("Same");
} else {
System.out.println("Different");
}
• A) Same
• B) Different
• C) Compilation error
• D) NullPointerException
java
Copy code
public class Main {
public static int multiply(int a, int b) {
return a * b;
}
public static void main(String[] args) {
System.out.println(multiply(2, 3));
}
}
• A) 5
• B) 6
• C) Compilation error
• D) 23
4. 2D Arrays
java
Copy code
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
System.out.println(matrix[1][2]);
• A) 4
• B) 5
• C) 6
• D) 9
java
Copy code
int[][] arr = new int[3][3];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = i + j;
}
}
System.out.println(arr[2][1]);
• A) 3
• B) 2
• C) 1
• D) 4
java
Copy code
class Car {
String model;
public Car(String model) {
this.model = model;
}
}
public class Main {
public static void main(String[] args) {
Car c = new Car("Toyota");
System.out.println(c.model);
}
}
• A) Compilation error
• B) Toyota
• C) Null
• D) Undefined behavior
java
Copy code
class Student {
String name;
int age;
6. Exception Handling
java
Copy code
try {
int a = 10 / 0;
System.out.println("Result: " + a);
} catch (ArithmeticException e) {
System.out.println("Arithmetic Exception occurred");
}
• A) Result: 0
• B) Arithmetic Exception occurred
• C) Compilation error
• D) Division by zero exception
java
Copy code
try {
int[] arr = {1, 2, 3};
System.out.println(arr[5]);
} catch (Exception e) {
System.out.println("Exception caught");
} finally {
System.out.println("Finally block executed");
}
This set of questions should help cover the essential concepts outlined in your request. Let me
know if you want further refinement or more questions!
1. Arrays and Loops
java
Copy code
int[] arr = {2, 4, 6, 8, 10};
int product = 1;
for (int num : arr) {
product *= num;
}
System.out.println(product);
• A) 240
• B) 3840
• C) 960
• D) 120
java
Copy code
public class LoopTest {
public static void main(String[] args) {
for (int i = 0; i < 5; i--) {
System.out.println(i);
}
}
}
java
Copy code
String str = "OpenAI";
String sub = str.substring(1, 4);
System.out.println(sub);
• A) pen
• B) Open
• C) ene
• D) peN
java
Copy code
String s = "Java Programming";
System.out.println(s.replace("a", "o"));
• A) Jova Programming
• B) Jovo Progrormming
• C) Java Progrormming
• D) Jova Progrormming
java
Copy code
public class Calculator {
public static double divide(int a, int b) {
return a / b;
}
• A) 2
• B) 2.5
• C) 2.0
• D) Compilation error
java
Copy code
public class Example {
public static void greet() {
System.out.println("Hello!");
}
4. 2D Arrays
java
Copy code
int[][] grid = {
{1, 2},
{3, 4},
{5, 6}
};
int sum = 0;
for (int[] row : grid) {
for (int num : row) {
sum += num;
}
}
System.out.println(sum);
• A) 12
• B) 15
• C) 21
• D) 18
java
Copy code
public class Array2DTest {
public static void main(String[] args) {
int[][] matrix = new int[2][];
matrix[0][0] = 1;
matrix[1][1] = 2;
System.out.println(matrix[0][0] + matrix[1][1]);
}
}
• A) No error; it prints 3.
• B) Compilation error due to incomplete 2D array initialization.
• C) Runtime error (NullPointerException).
• D) Index out of bounds error.
java
Copy code
class Person {
String name;
int age;
void display() {
System.out.println(name + " is " + age + " years old.");
}
}
java
Copy code
class Rectangle {
int length;
int width;
Rectangle(int l, int w) {
length = l;
width = w;
}
int area() {
return length * width;
}
}
6. Exception Handling
java
Copy code
try {
int[] numbers = {1, 2, 3};
System.out.println(numbers[3]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Index out of bounds!");
} finally {
System.out.println("End of try-catch.");
}
• A) 3
• B) Index out of bounds!
• C) Compilation error
• D) Index out of bounds!
End of try-catch.
java
Copy code
public class ExceptionTest {
public static void main(String[] args) {
try {
String s = null;
System.out.println(s.length());
} catch (ArithmeticException e) {
System.out.println("Arithmetic Exception caught");
}
}
}
Q1 Answer: A) 9
Explanation:
The loop iterates through the array indices 0 to 4. It adds the elements at even indices:
• numbers[0] = 1
• numbers[2] = 3
• numbers[4] = 5
Sum = 1 + 3 + 5 = 9.
Explanation:
The loop condition uses i <= arr.length, which means i will take values 0 to 5 inclusive.
However, array indices in Java are 0 to 4 for an array of length 5. When i = 5, arr[5] is
accessed, leading to an IndexOutOfBoundsException.
2. String Manipulation
Q3 Answer: A) HELLO
Explanation:
The toUpperCase() method converts all characters in the string to uppercase. Therefore,
"hello" becomes "HELLO".
Q4 Answer: B) Different
Explanation:
The equals() method is case-sensitive. "Hello" is not equal to "hello" due to the difference in
the first character's case, resulting in "Different" being printed.
Explanation:
The multiply method returns the product of 2 and 3, which is 6. This value is then printed.
Explanation:
printMessage() is an instance method (non-static), but it's being called from the static main
method without creating an instance of the Test class. This leads to a compilation error.
4. 2D Arrays
Q7 Answer: C) 6
Explanation:
matrix[1][2] accesses the third element (index 2) of the second row (index 1). The value at
this position is 6.
Q8 Answer: A) 3
Explanation:
The nested loops assign arr[i][j] = i + j. Therefore:
• arr[2][1] = 2 + 1 = 3
Q9 Answer: B) Toyota
Explanation:
A Car object is created with the model "Toyota". Printing c.model outputs "Toyota".
6. Exception Handling
Explanation:
Dividing by zero (10 / 0) throws an ArithmeticException, which is caught by the catch
block, resulting in "Arithmetic Exception occurred" being printed.
Explanation:
Accessing arr[5] in an array of size 3 throws an ArrayIndexOutOfBoundsException. This
exception is caught by the generic catch (Exception e) block, printing "Exception
caught". Additionally, the finally block executes, printing "Finally block executed".
Explanation:
The product of all elements in the array {2, 4, 6, 8, 10} is calculated as:
• 2×4=8
• 8 × 6 = 48
• 48 × 8 = 384
• 384 × 10 = 3840
Explanation:
The loop initializes i to 0 and decrements i (i--) on each iteration. The condition i < 5 will
always be true as i becomes increasingly negative, resulting in an infinite loop.
2. String Manipulation (Additional Questions)
Explanation:
The substring(1, 4) method extracts characters from index 1 to 3 (the end index is exclusive):
• "OpenAI"
• Characters at indices 1, 2, and 3 are 'p', 'e', 'n', forming "pen".
Explanation:
The replace("a", "o") method replaces all occurrences of 'a' with 'o' in the string "Java
Programming":
Explanation:
The divide method performs integer division (5 / 2), which results in 2. This integer 2 is then
converted to a double, resulting in 2.0.
Explanation:
The greet() method is static and can be called from the non-static callGreet() method
without any issues. The program runs correctly and prints "Hello!".
Q19 Answer: C) 21
Explanation:
The sum of all elements in the grid is calculated as:
• 1 + 2 + 3 + 4 + 5 + 6 = 21
Explanation:
The matrix is initialized with new int[2][], meaning each sub-array (matrix[0] and
matrix[1]) is null. Attempting to access matrix[0][0] or matrix[1][1] results in a
NullPointerException at runtime.
Explanation:
A Person object is created with name = "Alice" and age = 30. The display() method prints
"Alice is 30 years old.".
Explanation:
The Rectangle class constructor requires two parameters (int l, int w), but in
TestRectangle, a Rectangle object is instantiated with only one argument (new
Rectangle(5)). This mismatch leads to a compilation error.
Explanation:
Output:
csharp
Copy code
Index out of bounds!
End of try-catch.
Explanation: