Interview Question for Software Engineer / Developers






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

it should be segfault since when we do int *p=1 it means we are assigning non lvalue to a pointer variable

- cunomad September 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Ans 5

p is pointing to address 1
p++ is p +=4
now p is pointing to address 5

- Gaurav September 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ Gaurav: how do you know that 1 is an address. read the question properly. its a value and cunomad is right, as we're trying to put some value to some undefined memory location hence it will lead to a "Seg Fault".

- googler September 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here the definition and assignment of 'p' is being done in single statement.
int *p=1 -> int *p, p=1; and not int *p, *p=1.

Also in printf we are printing 'p' and not '*p'.
If we print '*p' we get a seg fault.

- Gaurav September 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Gaurav gives a good answer, even we know it is an illegal address, it'll be no hurt to print out the address as an integer, though accessing the value of such address will generate a segment fault.

- NIR September 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

printing p will not lead to seg-fault but *p will, so gaurav's answer is right @googler

- klpd September 04, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Compilation error at line 1

error C2440: 'initializing' : cannot convert from 'int' to 'int *'

- a September 04, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

depends on compiler.....c should make it pass though i guess

- @a September 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The answer indeed is 5, with a DevC++ compiler for a C file.
But if you are right, p is pointing to address 1. If I change the next line as p--, it gives me -3 as the answer. How can it point to address -3?

- Anonymous September 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

hey... print it by %u as the address always is unsigned..... its converting an unsigned number in signed one... thats all.

regards,
lukka

- lukka September 16, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

guys its pretty clear that p is an integer pointer and incrementing that makes it point to the next integer and its perfectly legal to print the address of the pointer [printing the value pointed to may give segmentation fault errors]. Here is the output of compiling/running using gcc4.3 on linux:

kk@kk-laptop:~$ gcc temp.c 
temp.c: In function ‘main’:
temp.c:5: warning: initialization makes pointer from integer without a cast
temp.c:7: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
temp.c:4: warning: return type of ‘main’ is not ‘int’
kk@kk-laptop:~$ ./a.out 
5

- kk September 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If we use the C compiler, it will just give warning of invalid cast.
But C++ is more strict compiler in terms of casting and it will give error on int *p = 1 saying u can convert int to int*

-Peace :-)

- Hiten Parmar October 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I mean u cannot convert .. a little typo :-)

- Hiten Parmar October 08, 2009 | 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