- What is the output of the following code?
int x = 10; System.out.println(x++);
A) 10
B) 11
C) 9
D) Compiler error
Answer: A) 10
- Which of the following access specifiers provides the widest accessibility in Java?
A) public
B) private
C) protected
D) default
Answer: A) public
- What is the correct way to convert a string to an integer in Java?
A) int num = Integer.parseInt("10");
B) int num = String.toInt("10");
C) int num = "10".toInteger();
D) int num = (int)"10";
Answer: A) int num = Integer.parseInt("10");
- In Java, what does the term "polymorphism" refer to?
A) A class having multiple constructors
B) A method having multiple return types
C) A variable having multiple data types
D) The ability of an object to take many forms
Answer: D) The ability of an object to take many forms
- Which of the following statements about interfaces in Java is true?
A) An interface can contain implementation of methods.
B) A class can implement multiple interfaces using the 'implements' keyword.
C) An interface cannot extend another interface.
D) Interfaces can have constructors in Java.
Answer: B) A class can implement multiple interfaces using the 'implements' keyword.
- What is the purpose of the 'this' keyword in Java?
A) It refers to the superclass of the current class.
B) It refers to the subclass of the current class.
C) It refers to the current instance of the class.
D) It refers to the parent class of the current class.
Answer: C) It refers to the current instance of the class.
- Which of the following statements is true about method overloading in Java?
A) Method overloading occurs when two methods have the same name and parameters but different return types.
B) Method overloading occurs when two methods have the same name and return type but different parameters.
C) Method overloading occurs when two methods have the same name, parameters, and return type.
D) Method overloading is not allowed in Java.
Answer: B) Method overloading occurs when two methods have the same name and return type but different parameters.
- What is the output of the following code?
String str1 = "hello"; String str2 = "world"; System.out.println(str1 + str2);
A) helloworld
B) hello world
C) hello+world
D) Compiler error
Answer: A) helloworld
- What does the 'new' keyword do in Java?
A) It creates a new instance of a class.
B) It allocates memory for an object.
C) It initializes variables.
D) All of the above
Answer: D) All of the above
- Which of the following is a valid declaration of a multidimensional array in Java?
A) int[][] arr = new int[3,3];
B) int[][] arr = new int[3][3];
C) int arr[][] = new int[3][3];
D) All of the above
Answer: B) int[][] arr = new int[3][3];
Comments