Oracle Certified Associate Java Se 8 Programmer · Free Practice Question Hard
Question 49
What will be the result of compiling and executing Test class?
- package com.udayan.oca;
- public class Test {
- public static void main(String[] args) {
- StringBuilder sb = new StringBuilder("Good"); //Line 3
- change(sb); //Line 4
- System.out.println(sb); //Line 5
- }
- private static void change(StringBuilder s) {
- s.append("_Morning"); //Line 9
- }
- }
-
A
Good
-
B
Good_Morning
-
C
None of the other options
-
D
_Morning
Reveal correct answer
Correct answer: B
Explanation
When change method is called, both variable s and sb refers to same StringBuilder object.
Line 9 modifies the passed object and appends "_Morning" to it. As a result s now refers to "Good_Morning" and sb also refers to "Good_Morning" so when control goes back to calling method main(String[]) Line 5 prints "Good_Morning" on to the console.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
You must be logged in to post a comment.
