Goldman Sachs Interview Question for Java Developers


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
7
of 7 vote

Since java passes object references rather than objects, and it passes these references by value rather than by reference, changing the copied object reference in the method will have no effect on the original reference, hence the value will be unchanged.
The same would happen if you had a method assigning any other new object to an object reference inside a method.

- Anonymous July 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is correct. It is a matter of java parameter passing.

- SoMiE July 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

ya it is called as shadowing of variables....

- karthik-skcet July 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

I kind of disagree on the local copy and "no effect on the original reference" and stuff. Lets try to understand few basic things . 1) In java References are created on stack and Objects are created in System ( heap ) . 2) As there are no explicit pointers in java as in c or C++ , we don't differentiate value( primitive) or address in ( reference) . Yes a reference is an address which points to an object in the heap . But in java everything is pass by the value - -if we have to define it clearly "In java the reference is holding address and jut the value in that reference is passed " so if I declare String str = new String ("hello world"); then here str is a reference which is created on stack ( to be much clear "method local stack") and the object is created in heap. If we come to this program first thing when we see a declaration String iAmOfAnArgumentativeNature = "I am born new" then here "hello" string goes to STL ( string literal pool ) and Stings are immutable in java . when the method m1( iAmOfAnArgumentativeNature) is called the value in iAmOfAnArgumentativeNature reference ( **that is the address ) is passed to the method m1(String arg1) and it can be denoted as arg1 = iAmOfAnArgumentativeNature so now arg1 and iAmOfAnArgumentativeNature are pointing to the same string "I am born new" in the SLP . At this point there are two references to the string object in the SLP --"I am born new" .And in that method they do arg1 = "Am I going to disappear?" so here the ar1 which was initially pointing to string "I am born new" is now replace with the address of the new string in SLP "Am I going to disappear?" . So we see the out put as I am born new . If that String is changed with StingBuilder or StringBuffer then you see totally a different out put as StringBuilder/StringBuffer are mutalble .

- Mahanth Hiremath July 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Mahanth Even if you change the code as below it would not result in any change

class Test
{
    public void  m1(StringBuffer arg1){
              arg1 = "Am I going to disappear?";
    }

public static void main (String[] args)
{
    Test test = new Test();
     StringBuffer iAmOfAnArgumentativeNature = new StringBuffer("I am born new");
            m1(iAmOfAnArgumentativeNature);
     System.out.print(iAmOfAnArgumentativeNature);
}
 }// end class

only if you would have called append method on StringBuffer in m1() its value would have been changed in the main method.

- Algorithmist July 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
5
of 5 vote

Compile time Error..calling non static method inside a static method.

- Anonymous August 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Oops..Change main() as follows..

public static void main (String[] args)
{
    Test test = new Test();
     String iAmOfAnArgumentativeNature = "I am born new";
            test.m1(iAmOfAnArgumentativeNature);
     System.out.print(iAmOfAnArgumentativeNature);
}

- SoMiE July 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

import java.io.*;
public class Test
{
public void m1(String arg1){
arg1 = "Am I going to disappear?";
}

public static void main (String[] args)
{
Test test = new Test();
String iAmOfAnArgumentativeNature = "I am born new";
m1(iAmOfAnArgumentativeNature);
System.out.print(iAmOfAnArgumentativeNature);
}
}// end class


goto browxy website and paste this ..surely u ll get output...correct me if i am wrong.....

- Riya August 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Above Code would result in Compile time error. In Java you cannot call a non-static method from a static method.

- Algorithmist July 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Point taken but don't divert the discussion. Assuming the code compiles, what will happen? Look at the update . The point of the question is not about compilation. And - I already know the answer - Not asking for the answer. Just posted for community benefit.

- SoMiE July 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry I didn't saw the update :).it would print I am born new ,mainly because it is arg1 which has started to reference other string ,original reference variable "iAmOfAnArgumentativeNature " still points to the string "I am born new".

And 10x for sharing the question :).

- Algorithmist July 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Since the String Objects are immutable. The output will be

I am born new

- Anonymous July 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The answer is correct, but the reasoning is wrong.

- SoMiE July 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Output will be
I am born new
Reason: you are just printing the string which is "I am born new"

- Manish Gautam July 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

compile time error. Because static method is called in static void main . Unless it throws compile time error. if you have changed it to test.m1 . then it prints " i am born new "

- spbalamurugan July 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Compile Time error.. since m1 is instance method and here we are calling without any reference to object so call it by t.m1 then it will print "i am new born". as argument pass is pass by value

- Deepak Bisht July 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Output: I am born new
Because java is pass by value.

Please make comment if solution is not OK.

- Sharma July 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I kind of disagree on the local copy and "no effect on the original reference" and stuff. Lets try to understand few basic things . 1) In java References are created on stack and Objects are created in System ( heap ) . 2) As there are no explicit pointers in java as in c or C++ , we don't differentiate value( primitive) or address in ( reference) . Yes a reference is an address which points to an object in the heap . But in java everything is pass by the value - -if we have to define it clearly "In java the reference is holding address and jut the value in that reference is passed " so if I declare String str = new String ("hello world"); then here str is a reference which is created on stack ( to be much clear "method local stack") and the object is created in heap. If we come to this program first thing when we see a declaration String iAmOfAnArgumentativeNature = "I am born new" then here "hello" string goes to STL ( string literal pool ) and Stings are immutable in java . when the method m1( iAmOfAnArgumentativeNature) is called the value in iAmOfAnArgumentativeNature reference ( **that is the address ) is passed to the method m1(String arg1) and it can be denoted as arg1 = iAmOfAnArgumentativeNature so now arg1 and iAmOfAnArgumentativeNature are pointing to the same string "I am born new" in the SLP . At this point there are two references to the string object in the SLP --"I am born new" .And in that method they do arg1 = "Am I going to disappear?" so here the ar1 which was initially pointing to string "I am born new" is now replace with the address of the new string in SLP "Am I going to disappear?" . So we see the out put as I am born new . If that String is changed with StingBuilder or StringBuffer then you see totally a different out put as StringBuilder/StringBuffer are mutalble .

- Mahanth Hiremath July 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Class credits {
public static void main(string[] arguments {
// set up film information
String title = "The Piano ";
int year= 1993;
String director = " Jane Campion";
String role1 = "Ada";
String actor1 = "Holly hunter";
String role2 = "Baines";
String actor2 = "Harvey Keital";
String role2 = "Stewart";

- Anonymous July 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String actor3 ="Sam Neil";
String role4 = "Flora";
String actor4 = "Anna Paquin";
// display information

System.out.print (title + " ( ", + year + " ) \n"+
"A" +director + " film.\n\n"+
role1+ "\t" + actor1 + "\n"+
role2+ "\ t" + actor2+ "\n"+
role3+ "\t" + actor3+ "\n"+
role4+ "\t" + actor4+ "\n"+
}

}


what is the output?

- Anonymous July 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class credits { 
 public static void main(String[] arguments){ 
 // set up film information 
 String title = "The Piano "; 
 int year= 1993; 
 String director = " Jane Campion"; 
 String role1 = "Ada"; 
 String actor1 = "Holly hunter"; 
 String role2 = "Baines"; 
 String actor2 = "Harvey Keital"; 
 String role3 = "Stewart";
 String actor3 ="Sam Neil"; 
String role4 = "Flora"; 
String actor4 = "Anna Paquin"; 
// display information 

System.out.print(title + " ( " + year + " ) \n"+ 
"A" +director + " film.\n\n"+ 
role1+ "\t" + actor1 + "\n"+ 
role2+ "\t" + actor2+ "\n"+ 
role3+ "\t" + actor3+ "\n"+ 
role4+ "\t" + actor4+ "\n"); 
} 

}

- Shepherd Siduli August 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The output will be : “I am born new“

- Anonymous August 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The output will be : “I am born new“

- huang3981658 August 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it will give error, because you are calling a non static method inside main method (static).

so you have to make the method call as
object.methodname();

- prashantdubey194 August 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String is mutable and blab la concepts wont be applied here
"I am born new"; is one string object and "Am I going to disappear?"; is another.
iAmOfAnArgumentativeNature is the refrence and arg1 another refrence.
iAmOfAnArgumentativeNature is pointed to "I am born new" string object
and arg1 is also pointed to "I am born new" string object
but than arg1 is pointed to "Am I going to disappear?";..
so even if you use string buffer, linked list or whatever it wont change.
It will print "I am born new";

- neha December 06, 2013 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More