-
web.groovymark@gmail.com
- December 10, 2024
Question 01
What does the break statement do in a switch-case structure?
A. Terminates the switch statement
B. Exits the current loop
C. Breaks the program
D. Skips the next iteration
Answer: A
Explanation: The break statement terminates the switch-case structure, preventing fall-through to the next case.
Question 02
Which of the following is a valid way to compare two strings for equality?
A. str1.equals(str2)
B. str1 == str2
C. str1.compareTo(str2) == 0
D. Both A and C
Answer: D
Explanation: Both str1.equals(str2) and str1.compareTo(str2) == 0 are valid methods for comparing strings for equality.
Question 03
What will be the output of System.out.println(5 * 2 + 3);?
A. 8
B. 13
C. 10
D. 5
Answer: B
Explanation: The expression evaluates to 10 (5 * 2) + 3 = 13.
Question 04
How can you declare a method that takes no arguments?
A. public void methodName() { … }
B. public void methodName(int x) { … }
C. void methodName;
D. methodName() { … }
Answer: A
Explanation: The correct syntax to declare a method that takes no arguments is public void methodName() { ... }.
Question 05
What is the result of the following operation: int[] arr = {1, 2, 3}; arr[1] = 5; System.out.println(arr[1]);?
A. 1
B. 2
C. 3
D. 5
Answer: D
Explanation: The second element (index 1) of the array is updated to 5, so the output is 5
Question 06
Which of the following statements correctly creates a new object of a class named Car?
A. Car myCar = new Car();
B. Car myCar = Car();
C. new Car myCar();
D. Car myCar;
Answer: A
Explanation: The correct syntax to create a new object is Car myCar = new Car();.
Question 07
How do you declare a method that returns an integer value?
A. int methodName() { … }
B. void methodName() { … }
C. methodName() int { … }
D. public int methodName() { … }
Answer: D
Explanation: The correct syntax to declare a method that returns an integer is public int methodName() { ... }.
Question 08
What is the purpose of the import statement?
A. To create a new class
B. To include other classes and packages
C. To define a method
D. To declare a variable
Answer: B
Explanation: The import statement is used to include classes and packages from other libraries.
Question 09
How do you create a multi-dimensional array in Java?
A. int[][] arr = new int[3][4];
B. int arr[][] = new int[3][4];
C. Both A and B
D. int[3][4] arr;
Answer: C
Explanation: Both int[][] arr = new int[3][4]; and int arr[][] = new int[3][4]; are valid ways to declare a multi-dimensional array.
Question 10
What will happen if you try to access an index outside the bounds of an array?
A. It returns null
B. It crashes the program
C. It throws an ArrayIndexOutOfBoundsException
D. It returns 0
Answer: C
Explanation: Accessing an index that is out of bounds will throw an ArrayIndexOutOfBoundsException.
Question 11
What does the continue statement do in a loop?
A. Exits the loop
B. Skips the current iteration and proceeds to the next
C. Terminates the program
D. Causes an error
Answer: B
Explanation: The continue statement skips the current iteration of the loop and continues with the next iteration.
Question 12
What will be the output of System.out.println(“Hello”.length());?
A. 5
B. 6
C. Error
D. 0
Answer: A
Explanation: The length() method returns the number of characters in the string, which is 5 for "Hello".
Question 13
How do you convert a string to an integer in Java?
A. Integer.valueOf(str);
B. Integer.parseInt(str);
C. Both A and B
D. str.toInt();
Answer: C
Explanation: Both Integer.valueOf(str); and Integer.parseInt(str); can convert a string to an integer.
Question 14
Which of the following is the correct way to define an interface in Java?
A. public class MyInterface { … }
B. public interface MyInterface { … }
C. interface MyInterface { … }
D. Both B and C
Answer: D
Explanation: An interface can be defined using either public interface MyInterface { ... } or interface MyInterface { ... }.
Question 15
What is the main purpose of the final keyword?
A. To define a variable that can change
B. To define a method that can be overridden
C. To define a constant value
D. To create an abstract class
Answer: C
Explanation: The final keyword is used to declare a constant value that cannot be modified.
Question 16
How do you check if a string is empty in Java?
A. str.length() == 0
B. str.equals(“”)
C. str.isEmpty()
D. All of the above
Answer: D
Explanation: All options are valid ways to check if a string is empty in Java.
Question 17
Which of the following statements is used to handle exceptions in Java?
A. try-catch
B. throw-catch
C. exception
D. finally
Answer: A
Explanation: The try-catch statement is used to handle exceptions in Java.
Question 18
What is the output of the following code snippet? System.out.println(7 % 4);
A. 3
B. 1
C. 0
D. 7
Answer: A
Explanation: The % operator returns the remainder of the division, which is 3.
Question 19
How do you define a constructor in Java?
A. It must have the same name as the class and no return type.
B. It must have a return type of void.
C. It can have any name.
D. It must be public.
Answer: A
Explanation: A constructor must have the same name as the class and does not have a return type.
Question 20
Which of the following keywords is used to create a subclass in Java?
A. extends
B. implements
C. inherits
D. uses
Answer: A
Explanation: The extends keyword is used to create a subclass from a superclass.