SCJP Sun Certified Java Programmer MCQ 2 — Questions and Answers
Question 1: What is the output of: System.out.println(1 + 2 + "3" + 4 + 5);
- 1235
- 12345
- 3345 (Correct answer)
- 33345
Correct answer: 3345
Left-to-right evaluation: 1+2=3, then 3+"3"="33", then "33"+4="334", then "334"+5="3345".
Question 2: Which access modifier allows a member to be accessed only within its own package and by subclasses?
- private
- public
- protected (Correct answer)
- package-private
Correct answer: protected
The 'protected' modifier grants access within the same package and to subclasses in other packages.
Question 3: What happens when you try to call a method on a null reference in Java?
- NullPointerException is thrown (Correct answer)
- A compile-time error occurs
- The method is skipped silently
- A default value is returned
Correct answer: NullPointerException is thrown
Calling any method on a null reference at runtime throws a NullPointerException.
Question 4: Which of the following correctly declares a two-dimensional array in Java?
- int[2][3] arr;
- int arr[2][3];
- int[][] arr = new int[2][3]; (Correct answer)
- int arr = new int[2,3];
Correct answer: int[][] arr = new int[2][3];
The correct syntax is 'int[][] arr = new int[2][3];' which declares and allocates a 2x3 array.
Question 5: What is the default value of an instance variable of type boolean in Java?
- true
- false (Correct answer)
- null
- 0
Correct answer: false
Instance variables of type boolean default to false if not explicitly initialized.
Question 6: Which keyword is used to prevent method overriding in Java?
- static
- abstract
- final (Correct answer)
- synchronized
Correct answer: final
Declaring a method as 'final' prevents it from being overridden in any subclass.
Question 7: What will be the result of 7 % 3 in Java?
- 2
- 1 (Correct answer)
- 2.33
- 0
Correct answer: 1
The modulo operator returns the remainder: 7 divided by 3 is 2 with remainder 1.
What is the output of: System.out.println(1 + 2 + "3" + 4 + 5);