-
web.groovymark@gmail.com
- December 26, 2024
Question 21
What will be the output of System.out.println(“5” + 5);?
- A. 10
- B. 55
- C. 5
- D. Error
Answer: B
Explanation: The expression evaluates to "55" due to string concatenation.
Question 22
Which collection allows duplicate elements in Java?
- A. Set
- B. List
- C. Map
- D. Queue
Answer: B
Explanation: The List interface allows duplicate elements.
Question 23
What is the result of Integer.valueOf(“100”)?
- A. 100
- B. “100”
- C. Error
- D. null
Answer: A
Explanation: The Integer.valueOf("100") method returns an Integer object representing the value 100.
Question 24
How do you find the length of a string in Java?
- A. str.length();
- B. str.size();
- C. str.getLength();
- D. str.length;
Answer: A
Explanation: The length() method is used to find the length of a string.
Question 25
What does the synchronized keyword do?
- A. Makes a variable static
- B. Prevents thread interference
- C. Defines an abstract method
- D. Creates a volatile variable
Correct Answer: b) It ensures data is unreadable without proper authorization
Explanation: Encryption secures data by converting it into a form that cannot be read
without the correct decryption key.
Question 26
Which of the following is used to handle a potential exception in Java?
- A. try
- B. catch
- C. throw
- D. all of the above
Answer: D
Explanation: All the keywords try, catch, and throw are used in exception handling in Java.
Question 27
How do you declare a two-dimensional array in Java?
- A. int[][] arr;
- B. int arr[][];
- C. Both A and B
- D. int arr[2][2];
Answer: C
Explanation: Both int[][] arr; and int arr[][]; are valid ways to declare a two-dimensional array.
Question 28
What will be the output of the following code? System.out.println(3 * 5 + ” apples”);
- A. 15 apples
- B. 35 apples
- C. apples 15
- D. Error
Answer: A
Explanation: The expression evaluates to "15 apples" due to string concatenation.
Question 29
Which of the following keywords is used to create an instance of a class?
- A. new
- B. instance
- C. create
- D. object
Answer: A
Explanation: The new keyword is used to create an instance of a class.
Question 30
How do you define an interface in Java?
- A. public interface MyInterface { }
- B. public class MyInterface { }
- C. interface MyInterface { }
- D. Both A and C
nswer: D
Explanation: You can define an interface using either public interface MyInterface { } or interface MyInterface { }.
Question 31
What is the main purpose of the finally block in exception handling?
- A. To catch exceptions
- B. To execute code regardless of an exception
- C. To throw exceptions
- D. To log exceptions
Answer: B
Explanation: The finally block is used to execute code regardless of whether an exception was thrown or not.
Question 32
Which collection type allows elements to be stored in key-value pairs?
- A. Set
- B. List
- C. Map
- D. Queue
Answer: C
Explanation: The Map interface allows elements to be stored in key-value pairs.
Question 33
What is the default value of a boolean variable in Java?
- A. true
- B. false
- C. 0
- D. null
Answer: B
Explanation: The default value of a boolean variable in Java is false.
Question 34
Which of the following statements correctly creates an ArrayList?
- A. ArrayList<int> list = new ArrayList<int>();
- B. ArrayList<Object> list = new ArrayList<>();
- C. ArrayList<String> list = new ArrayList<String>();
- D. Both B and C
Answer: D
Explanation: Both ArrayList<Object> list = new ArrayList<>(); and ArrayList<String> list = new ArrayList<String>(); correctly create ArrayLists.
Question 35
What does the throw statement do in Java?
- A. Catches an exception
- B. Creates a new exception
- C. Passes an exception to the caller
- D. Defines a custom exception
Answer: C
Explanation: The throw statement is used to pass an exception to the caller.
Question 36
Which of the following is a valid way to comment in Java?
- A. // This is a comment
- B. /* This is a comment */
- C. /** This is a comment */
- D. All of the above
Answer: D
Explanation: All the options represent valid ways to comment in Java.
Question 37
What is the result of 5 > 3 && 3 < 2?
- A. true
- B. false
- C. 0
- D. 1
Answer: B
Explanation: The expression evaluates to false since one of the conditions is not true.
Question 38
What keyword is used to prevent a class from being subclassed?
- A. final
- B. static
- C. private
- D. protected
Answer: A
Explanation: The final keyword is used to prevent a class from being subclassed.
Question 39
What does Collections.sort(list); do?
- A. Sorts the list in ascending order
- B. Sorts the list in descending order
- C. Shuffles the list
- D. Merges two lists
Answer: A
Explanation: The Collections.sort(list); method sorts the list in ascending order.
Question 40
What will happen if you attempt to divide by zero in Java?
- A. It returns zero
- B. It throws an exception
- C. It returns infinity
- D. It returns NaN
Answer: B
Explanation: Attempting to divide by zero will throw an ArithmeticException.