-
web.groovymark@gmail.com
- December 10, 2024
Question 21
What is the purpose of the void keyword in a method declaration?
A. Indicates a return value
B. Indicates no return value
C. Indicates a static method
D. Indicates an abstract method
Answer: B
Explanation: The void keyword indicates that the method does not return a value.
Question 22
What does the import statement do in Java?
A. Creates a new class
B. Includes a package or class for use
C. Declares variables
D. Defines methods
Answer: B
Explanation: The import statement allows the use of classes and packages in the current file.
Question 23
What is a package in Java?
A. A group of related classes
B. A type of data structure
C. A method of storing data
D. A collection of methods
Answer: A
Explanation: A package is a namespace that organizes a set of related classes and interfaces.
Question 24
Which of the following keywords is used to inherit a class in Java?
A. implements
B. extends
C. inherits
D. uses
Answer: B
Explanation: The extends keyword is used to indicate inheritance in Java.
Question 25
What is the output of the following code snippet: System.out.println(10 / 3);?
A. 3
B. 3.3333
C. 3.0
D. 10/3
Answer: A
Explanation: Integer division results in the quotient without the remainder, so the output is 3.
Question 26
How do you define a constant variable in Java?
A. const int MAX_VALUE = 100;
B. static int MAX_VALUE = 100;
C. final int MAX_VALUE = 100;
D. int MAX_VALUE = 100;
Answer: C
Explanation: The final keyword is used to declare a constant variable in Java.
Question 27
Which method is used to compare two strings in a case-insensitive manner?
A. str1.equals(str2);
B. str1.compareTo(str2);
C. str1.equalsIgnoreCase(str2);
D. str1.compareToIgnoreCase(str2);
Answer: C
Explanation: The equalsIgnoreCase method is used to compare two strings while ignoring case.
Question 28
What is the purpose of the this keyword in Java?
A. Refers to the current object
B. Declares a static variable
C. Calls a method
D. Creates a new instance
Answer: A
Explanation: The this keyword refers to the current object instance in a class.
Question 29
How can you declare a method that does not return any value?
A. public void methodName() { … }
B. public methodName() { … }
C. public void methodName;
D. methodName() { … }
Answer: A
Explanation: The correct syntax to declare a method that does not return a value is public void methodName() { ... }.
Question 30
What exception is thrown when trying to divide by zero in Java?
A. NullPointerException
B. ArithmeticException
C. ArrayIndexOutOfBoundsException
D. ClassCastException
Answer: B
Explanation: An ArithmeticException is thrown when attempting to divide by zero.
Question 31
Which of the following statements is used to create a new instance of a class?
A. ClassName instanceName;
B. ClassName instanceName = new ClassName();
C. ClassName instanceName = ClassName();
D. new ClassName instanceName;
Answer: B
Explanation: The correct way to create a new instance of a class is by using ClassName instanceName = new ClassName();.
Question 32
What does the length property of an array return?
A. The total memory allocated for the array
B. The number of elements in the array
C. The maximum value of the array
D. The minimum value of the array
Answer: B
Explanation: The length property returns the number of elements in the array.
Question 33
How do you access the first element of an array named myArray?
A. myArray[1];
B. myArray[0];
C. myArray.first();
D. myArray.get(0);
Answer: B
Explanation: Array indices in Java are zero-based, so the first element is accessed with myArray[0];
Question 34
Which of the following statements is used to read input from the keyboard in Java?
A. Scanner scnr = new Scanner(System.in);
B. Input scnr = new Input();
C. Read scnr = new Read();
D. Console scnr = new Console();
Answer: A
Explanation: The Scanner class is used to read input from the keyboard, instantiated with new Scanner(System.in);.
Question 35
What is the result of the expression 5 == 5 in Java?
A. True
B. False
C. Error
D. Undefined
Answer: A
Explanation: The expression evaluates to true because both sides are equal.
Question 36
Which of the following is NOT a valid way to create an array in Java?
A. int[] arr = new int[10];
B. int[] arr = {1, 2, 3};
C. int arr[] = new int[5];
D. int arr = new int[5];
Answer: D
Explanation: int arr = new int[5]; is invalid because it lacks array brackets.
Question 37
How can you declare a two-dimensional array in Java?
A. int[][] arr = new int[5][5];
B. int arr[][] = new int[5][5];
C. int arr[5][5];
D. Both A and B
Answer: D
Explanation: Both int[][] arr = new int[5][5]; and int arr[][] = new int[5][5]; are valid declarations for a two-dimensional array.
Question 38
What is the output of System.out.println(“Java”.charAt(0));?
A. J
B. a
C. Java
D. Error
Answer: A
Explanation: The charAt(0) method returns the character at index 0, which is 'J'.
Question 39
Which of the following methods can be used to compare two strings lexicographically?
A. str1.equals(str2);
B. str1.compareTo(str2);
C. str1.equalsIgnoreCase(str2);
D. str1.contains(str2);
Answer: B
Explanation: The compareTo method compares two strings lexicographically.
Question 40
What does the toUpperCase() method do in Java?
A. Converts a string to lowercase
B. Converts a string to uppercase
C. Trims whitespace from a string
D. Splits a string into an array
Answer: B
Explanation: The toUpperCase() method converts all characters in the string to uppercase.