Goldman Sachs Interview Question for Java Developers


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
3
of 5 vote

Java is only call by value irrespective of the arguement type passed. These examples are quoted from Core Java Volume 1.
//code

double percent = 10;
tripleValue(percent);
public static void tripleValue(double x) // doesn't work
{
   x = 3 * x;
}

this doesn't change the primitive value of percent.Local variable x is destroyed as soon as the function ends.

Employee a = new Employee("Alice", . . .);

Employee b = new Employee("Bob", . . .);

swap(a, b);

// does a now refer to Bob, b to Alice?
//guess what? It doesn't.

That means you can only change the state(attributes) of an object but you cannot change the references stored in the variables a and b.
Same is the case for ArrayList.
So JAVA ONLY USES CALL BY VALUE.

- mohdsalmanshaikh63 January 10, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

pass by value. Even the references are copied to and then send. The copied value of reference refers to the same object, making confusion for the pass by reference. In the case of String, its an immutable object and there is a String pool for the string objects to any string argument would be taken as a new values and would not refer to the same string argument which is being passed.

- nj February 05, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

All parameters in java are passed by value.

Primitives types values are copied: so any change to that parameter is not visible outside of the function.

For object is the reference that is copied when passed as a parameter: any change to the internal state of the object is visible outside, but a change to the reference itself is not visible outside. For example setting to null a parameter will not affect the external references.

- davide.marino February 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java is pass by value and not pass by reference

- pratikpalashikar January 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Is not correct. Object are not passed by reference. In java parameters are always passed by value. For object is the reference that is passed by value.

- davide.marino February 09, 2016 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

its both.for primitives it is pass by value,for objects its pass by reference

- Anonymous November 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I dont think you can say that. What about, if i create object of Integer class.

Integer i = new Integer(4);

and pass this to a function??

- Joey December 10, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Integer is not the same as int Joey. It it is a wrapper class that holds an int as a value. If you can pass it into a function you can edit the content, just as you can with any other object. However, it is not the same as the primitive int, which is always passed by value.

- Anonymous December 16, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Is not correct. Object are not passed by reference. In java parameters are always passed by value. For object is the reference that is passed by value.

- davide.marino February 09, 2016 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Java is pass by value for primitives and pass by reference for object except wrapper and immutable objects. Array is an object so even you pass primitive array to function, it will change the value.

- YogarajKV January 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Is not correct. Object are not passed by reference. In java parameters are always passed by value. For object is the reference that is passed by value.

- davide.marino February 09, 2016 | Flag


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