Adobe Interview Question for Computer Scientists


Team: Big Data
Country: United States
Interview Type: In-Person




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

In call by value a copy of variable is made and all the computation is done on the copied variable not affecting the original value .like if we have int x=9 , int y=8; and we want to pass this to add function
int add(int u,int v){} .... then the copies of x and y are made into u and v respectively , without affecting the values of x and y .

In contrast in call by reference the actual object is being copied and the computation being done in the method affects the actual object.
like we have a order object .
Order or = new Order();
or.setName("Adobe");


now we have method pass this order object to a method
public int orderSubmission(Order or){} and set the name of order different as Google , Now the original value that was Amazon gets changed .

- akshaymattoo November 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In your second paragraph, you meant to say "the actual object is NOT being copied".

- Adam Smith November 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I have written in context to java , where everything is call by value , but when we pass the actual object the modifications done in the calle function makes it affect to the actual object

- akshaymattoo November 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Even in Java, the actual object on the heap is not copied. It behaves as you say, but only because the reference to the object is being copied and passed by value. In your example, there are two variables named "or", both of which reference the same Order object. Inside your orderSubmission() function, you can use the local "or" to access the object, but you could reassign the local copy of "or" to point to a new Order object, without affecting what the calling code's "or" reference points to.

- Adam Smith November 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am pasting a code where i did the same thing but it is changing the calling code reference.

{
package com.test;

public class CallByReference {

public static void main(String[] args) {
Number a = new Number();
a.x = 3;
System.out.println("Value of a.x before calling increment() is " + a.x);
increment(a);
System.out.println("Value of a.x after calling increment() is " + a.x);
}

public static void increment(Number n) {
Number temp = n;


System.out.println("Value of temp before incrementing x is " + temp.x);


temp.x = temp.x + 1;

System.out.println("Value of temp.x after incrementing x is " + temp.x);
}
}

class Number {
int x;
}


}

the output is
Value of a.x before calling increment() is 3
Value of temp before incrementing x is 3
Value of temp.x after incrementing x is 4
Value of a.x after calling increment() is 4

- akshaymattoo November 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, this is all as expected. As you can see, the Number object is not copied. There are 3 reference variables in play: main::a, increment::n and increment::temp and you are making them all point to the same Number object. It's pass by value because increment::n is a copy of main::a, that is to say, main::a is passed by value. Run the following modification to your program and you will see what I mean:

package com.test;

public class CallByReference
{

	public static void main(String[] args)
	{
		Number a = new Number();
		a.x = 3;
		System.out.println("Value of a.x before calling increment() is " + a.x);
		increment(a);
		System.out.println("Value of a.x after calling increment() is " + a.x);
	}

	public static void increment(Number n)
	{
		System.out.println("Value of n.x before re-assign is " + n.x);

		n = new Number();
		n.x = 7;

		System.out.println("Value of n.x after re-assign is " + n.x);
	}
}

class Number {
	int x;
}

Value of a.x before calling increment() is 3
Value of n.x before re-assign is 3
Value of n.x after re-assign is 7
Value of a.x after calling increment() is 3

The program changes what 'n' points to, without affecting 'a', which is possible because 'n' is a copy of 'a', not a reference to 'a'. It doesn't matter so much in Java, because there is no other way to pass the object to the function, but in other languages like C++ you have multiple options that behave differently. It's possible in C++ to actually pass 'a' by reference and change what it points to.

- Adam Smith November 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Perfect that what is expected ...i got what you mean ....

- akshaymattoo November 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

OK great!

For those who are familiar with Java and are scratching their heads wondering why this example is a pass-by-value situation and not pass-by-reference, it comes down to what exactly is being put on the call stack. In the above example, think of "a" and "n" as being integers. "n" is a new variable that gets a copy of the value of "a", hence pass by value. For it to be a pass-by-reference call, you would have to pass the memory address of "a" itself to the function, so that even what "a" points to could be altered. This one level of indirection is the distinction. You cannot do this in Java, but that is what is meant by reference in other languages.

- Adam Smith November 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

thanks to explain

- Suman Kumar Jha November 06, 2014 | 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