0% found this document useful (0 votes)
26 views17 pages

Updated MCQ Practice

Uploaded by

Awande Madiba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views17 pages

Updated MCQ Practice

Uploaded by

Awande Madiba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

UPDATED MCQ PRACTICE

1. Arrays and Loops

Q1: What is the output of the following code?

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

Q2: What is wrong with the following code?

java
Copy code
int[] arr = new int[5];
for (int i = 0; i <= arr.length; i++) {
arr[i] = i;
}

• A) Nothing is wrong; it runs fine.


• B) Index out of bounds error at runtime.
• C) Compilation error due to incorrect array declaration.
• D) Infinite loop.

2. String Manipulation

Q3: What is the output of the following code?

java
Copy code
String s = "hello";
s = s.toUpperCase();
System.out.println(s);
• A) HELLO
• B) hello
• C) Compilation error
• D) NullPointerException

Q4: What will be printed?

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

3. Method Creation and Usage

Q5: What is the output of this code?

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

Q6: What is wrong with the following code?


java
Copy code
public class Test {
public void printMessage() {
System.out.println("Hello, World");
}

public static void main(String[] args) {


printMessage();
}
}

• A) Nothing is wrong; it runs fine.


• B) Compilation error because printMessage() is non-static.
• C) Runtime exception.
• D) The method signature is incorrect.

4. 2D Arrays

Q7: What is the output of the following code?

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

Q8: What will this code print?

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

5. Object-Oriented Programming (Classes, Objects, Methods)

Q9: What will the following code output?

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

Q10: Identify the error in the code below:

java
Copy code
class Student {
String name;
int age;

public void display() {


System.out.println(name + " is " + age + " years old.");
}
}

public class Test {


public static void main(String[] args) {
Student s = new Student();
s.display();
}
}
• A) No error, it will print default values.
• B) NullPointerException
• C) Compilation error because age and name are not initialized.
• D) Runtime error because the constructor is missing.

6. Exception Handling

Q11: What is the output of the following code?

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

Q12: What is wrong with the following code?

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");
}

• A) ArrayIndexOutOfBoundsException will occur and will be caught.


• B) Compilation error because catch block is missing.
• C) The code will run successfully and print an array element.
• D) NullPointerException.

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

Q13: What is the output of the following code?

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

Q14: Identify the error in the following code:

java
Copy code
public class LoopTest {
public static void main(String[] args) {
for (int i = 0; i < 5; i--) {
System.out.println(i);
}
}
}

• A) No error; it prints numbers from 0 to 4.


• B) Infinite loop due to incorrect loop condition.
• C) Compilation error because of invalid decrement operator.
• D) Runtime error due to stack overflow.
2. String Manipulation

Q15: What will the following code print?

java
Copy code
String str = "OpenAI";
String sub = str.substring(1, 4);
System.out.println(sub);

• A) pen
• B) Open
• C) ene
• D) peN

Q16: What is the output of this code?

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

3. Method Creation and Usage

Q17: What is the output of the following code?

java
Copy code
public class Calculator {
public static double divide(int a, int b) {
return a / b;
}

public static void main(String[] args) {


System.out.println(divide(5, 2));
}
}

• A) 2
• B) 2.5
• C) 2.0
• D) Compilation error

Q18: Identify the error in the code below:

java
Copy code
public class Example {
public static void greet() {
System.out.println("Hello!");
}

public void callGreet() {


greet();
}

public static void main(String[] args) {


Example ex = new Example();
ex.callGreet();
}
}

• A) No error; it runs correctly.


• B) Compilation error because greet() is static and cannot be called from a non-static
method.
• C) Runtime error due to null reference.
• D) Method greet() should return a value.

4. 2D Arrays

Q19: What is the output of the following code?

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

Q20: Identify the error in the following code:

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.

5. Object-Oriented Programming (Classes, Objects, Methods)

Q21: What will the following code output?

java
Copy code
class Person {
String name;
int age;

Person(String name, int age) {


this.name = name;
this.age = age;
}

void display() {
System.out.println(name + " is " + age + " years old.");
}
}

public class Main {


public static void main(String[] args) {
Person p = new Person("Alice", 30);
p.display();
}
}
• A) Compilation error
• B) Alice is 30 years old.
• C) null is 0 years old.
• D) Alice is thirty years old.

Q22: Identify the error in the following code:

java
Copy code
class Rectangle {
int length;
int width;

Rectangle(int l, int w) {
length = l;
width = w;
}

int area() {
return length * width;
}
}

public class TestRectangle {


public static void main(String[] args) {
Rectangle rect = new Rectangle(5);
System.out.println(rect.area());
}
}

• A) No error; it prints the area.


• B) Compilation error due to missing constructor parameter.
• C) Runtime error due to null values.
• D) The method area() is incorrectly defined.

6. Exception Handling

Q23: What is the output of the following code?

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.

Q24: Identify the error in the following code:

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");
}
}
}

• A) No error; it runs correctly.


• B) Compilation error due to missing catch block for NullPointerException.
• C) Runtime error (NullPointerException not caught).
• D) It catches the exception and prints a message.
Answers:

1. Arrays and Loops

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.

Q2 Answer: B) Index out of bounds error at runtime.

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.

3. Method Creation and Usage


Q5 Answer: B) 6

Explanation:
The multiply method returns the product of 2 and 3, which is 6. This value is then printed.

Q6 Answer: B) Compilation error because printMessage() is non-static.

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

5. Object-Oriented Programming (Classes, Objects, Methods)

Q9 Answer: B) Toyota

Explanation:
A Car object is created with the model "Toyota". Printing c.model outputs "Toyota".

Q10 Answer: A) No error, it will print default values.


Explanation:
When a Student object is created without initializing name and age, they default to null and 0,
respectively. The display() method prints "null is 0 years old." without any runtime
errors.

6. Exception Handling

Q11 Answer: B) Arithmetic Exception occurred

Explanation:
Dividing by zero (10 / 0) throws an ArithmeticException, which is caught by the catch
block, resulting in "Arithmetic Exception occurred" being printed.

Q12 Answer: A) ArrayIndexOutOfBoundsException will occur and will be caught.

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".

1. Arrays and Loops (Additional Questions)

Q13 Answer: B) 3840

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

Q14 Answer: B) Infinite loop due to incorrect loop condition.

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)

Q15 Answer: A) pen

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".

Q16 Answer: B) Jovo Progrormming

Explanation:
The replace("a", "o") method replaces all occurrences of 'a' with 'o' in the string "Java
Programming":

• "Java Programming" → "Jovo Progrormming"

3. Method Creation and Usage (Additional Questions)

Q17 Answer: C) 2.0

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.

Q18 Answer: A) No error; it runs correctly.

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!".

4. 2D Arrays (Additional Questions)

Q19 Answer: C) 21
Explanation:
The sum of all elements in the grid is calculated as:

• 1 + 2 + 3 + 4 + 5 + 6 = 21

Q20 Answer: C) Runtime error (NullPointerException).

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.

5. Object-Oriented Programming (Classes, Objects, Methods) (Additional


Questions)

Q21 Answer: B) Alice is 30 years old.

Explanation:
A Person object is created with name = "Alice" and age = 30. The display() method prints
"Alice is 30 years old.".

Q22 Answer: B) Compilation error due to missing constructor parameter.

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.

6. Exception Handling (Additional Questions)

Q23 Answer: D) Index out of bounds!


End of try-catch.

Explanation:

• Attempting to access numbers[3] in an array of size 3 (indices 0-2) throws an


ArrayIndexOutOfBoundsException.
• The catch block catches this exception and prints "Index out of bounds!".
• The finally block always executes, printing "End of try-catch.".

Output:

csharp
Copy code
Index out of bounds!
End of try-catch.

Q24 Answer: C) Runtime error (NullPointerException not caught).

Explanation:

• The string s is null. Calling s.length() throws a NullPointerException.


• The catch block is specifically for ArithmeticException, so it does not catch the
NullPointerException.
• As a result, the program terminates with a NullPointerException at runtime.

You might also like