Amazon Interview Question for Software Engineer / Developers






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

Pointers and Reference looks similar but there are some difference between both of them.

POINTER

1) Its not necessary to initialize the pointer at the time of declaration. Like

int a = 10;

int *P = &a; //It is not necessary

Another way is :

int a = 10;

int *P;
P = &a;

2) You can create the array of Pointer.

3) You can assign NULL to the pointer like

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

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

int &a = NULL; //Error

4) You can not use reference to reference.

- Messi April 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

And most important, pointer arithmatics, for which pointers are considered so powerful, are not available with references.

- Anonymous June 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In addition to what Bala said,
You can never unseat a reference, so
int x,y;
int &b = x;
b = y; //Compiler error.

On a more philosophical level, think of pointers as things pointing to chunks
of memory, while references are really handles to objects (note that I am using
object in the general sense of the word, not c++ objects).

- Jasmeet bagga April 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Jasmeet bagga
you are right that "we can't unseat a reference" but
in your example you won't get compiler error...it will work fine and will assign the value of y to x via reference b....

- Anonymous April 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are right, I used a wrong example. Thanks

- Jasmeet Bagga April 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

does amazon ask such trivial questions ??

- Anonymous April 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. A reference must be initialized when it is created but pointers can be initialized at any time.
2. Once a reference is initialized to an object, it cannot be changed to refer to another object but pointers can be pointed to another object at any time.
3. references cannot be NULL but pointers can.

- APURBA October 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Fundamentally reference can be taken as const pointer. And it is auto de-referenced.

- peter tang October 09, 2012 | 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