-
web.groovymark@gmail.com
- December 26, 2024
Question 41
Which of the following methods can be used to convert a string to an integer?
- A. String.toInt()
- B. Integer.parseInt(String)
- C. String.parseInt()
- D. Integer.convert(String)
Answer: B
Explanation: The correct method to convert a string to an integer is Integer.parseInt(String).
Question 42
Which method is called when an object is created from a class?
- A. finalize()
- B. constructor
- C. create()
- D. initialize()
Answer: B
Explanation: The constructor method is called when an object is created from a class.
Question 43
How can you check if a string is empty in Java?
- A. str.isEmpty();
- B. str.length() == 0;
- C. Both A and B
- D. str.size() == 0;
Answer: C
Explanation: You can check if a string is empty using either str.isEmpty(); or str.length() == 0;.
Question 44
What is the purpose of the super keyword?
- A. To call the parent class constructor
- B. To access parent class methods
- C. To create an object of the parent class
- D. Both A and B
Answer: D
Explanation: The super keyword is used to call the parent class constructor and access parent class methods.
Question 45
Which method can be used to remove an element from an ArrayList?
- A. removeElement()
- B. delete()
- C. remove()
- D. discard()
Answer: C
Explanation: The remove() method can be used to remove an element from an ArrayList.
Question 46
What will the output be for System.out.println(1 + 2 + “3”);?
- A. 6
- B. 123
- C. 3
- D. 33
Answer: D
Explanation: The expression evaluates to "33" due to string concatenation.
Question 47
How do you access the first element of an array in Java?
- A. array[1]
- B. array[0]
- C. array.first()
- D. array.get(0)
Answer: B
Explanation: The first element of an array is accessed using array[0].
Question 48
What type of loop will always execute at least once?
- A. for loop
- B. while loop
- C. do-while loop
- D. enhanced for loop
Answer: C
Explanation: A do-while loop always executes at least once because the condition is checked after the loop body.
Question 49
Which of the following will create an instance of a class named Car?
- A. Car myCar;
- B. Car myCar = new Car();
- C. new Car myCar();
- D. Car myCar = Car();
Answer: B
Explanation: Car myCar = new Car(); creates a new instance of the Car class.
Question 50
What will the following code output? System.out.println(0.1 + 0.2 == 0.3);?
- A. true
- B. false
- C. 0.3
- D. Error
Answer: B
Explanation: Due to floating-point precision issues, 0.1 + 0.2 does not exactly equal 0.3, so it evaluates to false.