Java OCA OCP Practice Question 3052

Question

Given:

2. public class Main {  
3.   public static void main(String[] args) {      
4.     String s = "12";  
5.     s.concat("ab");  
6.     s = go(s);  /*from   w  w w . j  a v a2  s.  com*/
7.     System.out.println(s);  
8.   }  
9.   static String go(String s) {  
10.     s.concat("56");  
11.     return s;  
12. } } 

What is the result?

  • A. ab
  • B. 12
  • C. ab56
  • D. 12ab
  • E. 1256
  • F. 12ab56
  • G. Compilation fails.


B is correct.

Note

Strings are immutable.

If we captured the result of line 5, we would have "12ab", but we didn't capture that, so that String is lost.

Similarly, the result of line 10 is lost.




PreviousNext

Related