Goldman Sachs Interview Question for Developer Program Engineers






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

POINTER
1. Its not necessary to initialize the pointer at the time of declaration. Like
Code:

int a = 10;
int *P = &a; //It is not necessary
Another way is :
Code:
int a = 10;
int *P;
P = &a;
2. You can create the array of Pointer.
3. You can assign NULL to the pointer like
Code:
int *P = NULL; //Valid
4. You can use pointer to pointer.
REFERENCE
1. Its necessary to initialize the Reference at the time of declaration. Like
Code:
int &a = 10;
int &a; //Error here but not in case of Pointer.
2. You can not create the Array of reference.
3. You can not assign NULL to the reference like
Code:
int &a = NULL; //Error
4. You can not use reference to reference.

- siva July 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Good answers.
However, some points are missing. I am re-writing including the missing ones.

1. Memory is allocated when a pointer is defined. A reference however, is a name alias & hence no memory is allocated for it.

2. Reference is bound to be initialized at the time of definition because, a reference is implemented with a constant pointer & hence cannot be made to point to the other object.
A pointer however, is not necessary to be initialized at the time of definition & hence can also be changed to point to some other object.

3. A reference automatically gets de-referenced. When you write cout<<p;
It is automatically de-referenced by the compiler & treated as cout<<*p; by the compiler.

Here, p is the reference.

4. A reference to a reference is not possible.Whenever, you declare a reference to a reference, its actually the reference to the same variable.
e.g.

int i;
       int &r1=i;
       int &r2=r1; <-------------------2

Compiler interprets the statement 2 as:
int &r2=(*r1)
and (*r1) is nothing but the variable i itself.

A pointer to a pointer is however possible.

5. Array of pointer is possible while array of references is not possible(Why?).

6. Address of pointer is possible. Address of reference is not possible. It gives of the address of the variable.

7. There are situations where you are bound to use references.You cannot use pointers there.
Consider the below example:

A a=b+c;

Where a,b,c are objects of class A.
The operator '+' is overloaded as follows:

const A& operator+(const A& o)
        {
                return A(i+o.i);
        }

See sample code here: ideone.com/Q0pE1
Here the reference in the argument list is used to save the memory footprints.
You cannot use pointer in the argument list as you are bound to pass the address of object in the operator function.
A a=&b + &c;
However, if pointer is used in the parameter list, then we will end up adding the addresses rather than object itself.

Any other difference??

- Aashish July 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Pointer is a variable that holds address of another variable
Reference is an alias for a variable

- Ashish Kaila June 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can change pointer, but you can not change a reference. Reference could not be rvalue, except c++11 semantic.

- Mike October 25, 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