Nexabion Interview Question for Developer Program Engineers






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

why this printf not working...

have answer?
it goes into infinite loop, look at the for loop condition. i<=5 ,
which actually(array[5]) outer to the array. And in storage in stack
this will overwrite the i=0 ,( array[5] is the same as i's mem location.)
and it goes to infinite loop ,
cheers..

- Anonymous March 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

pretty good q to understand the stack...if following printf() prints same addr(for a[5] & i) then u will go in infinite loop..

int
 main()
{
int i=0;
int array[5];
printf("addr a[5]:%p i:%p\n", &array[5], &i);
 
for(i=0;i<=5;i++)
   array[i]=0;

printf("why this printf not working?");
 return 0;
}

- raja roy June 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Its working.... I tried in Dev-C++

we have array of size 5
'0' will be assigned to
array[0]....1....2...3.....array[4]
in case of array[5] it will accept assignment but will not throw any error...
array[5]=0 is like nothing.... depends on C++ compiler...!

- PKT March 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I still didn't understand why the address of i is the same as the address of array[5]? Even if you switch the order of the variable declarations from

int i=0;
int array[5];

to

int array[5];
int i=0;

, it seems to have the same behavior.
This is strictly compiler dependent I feel. This is the behavior under gcc.
Anybody?

- souravghosh.btbg March 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you are right.
both cases results same output ...
when i look into the assembly both code looks the same.ie, i=0 before array and after. i dont know why but still i m thinking may be part of compiler optimization stuff.

- vimal March 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Technically this code should work.
It worked at my end.

i am surprised which version of gcc you are using.

- Uttam March 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It may or may not work. We are accessing 6th element of array , where as it is declared to be array of 5 element.

- Anonymous March 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

One thing is sure it will corrupt the stack

- Anonymous March 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

working in my borland c compiler

- sandy880 March 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

its working on dev c++ compiler.!!

- Anonymous March 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

go to in infinite loop

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

go to in infinite loop

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

gcc -o2 <file.c> -o <exeName>

- mlakshmanarao July 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Friends try this solution:
............................
void main()
{
int i=0;
int j =0;
int array[5];
for(i=0;i<=5;i++)
array[i]=0;
printf("how this printf is working fine?");
}
.........................................
Above program'll clarify your doubts.

- intruder August 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i tried in visual studio.. addresses are different and it is printed as usual..

- rockstar June 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Dear rockstar...
Visual studio is giving wrong results...How did you checked my dear friend ?

- topcoder June 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

hey BRO.... @topcoder.. i tried it in my visual studio 2010,it is givng exactly right answer.. the addresses are also diffrnt.. did u got any othr answer???

- rockstar June 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

array[5] allocates space for 5 intergers.
when for loop executes it exceeds array boundaries.

- Anonymous August 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Address of variable i - -4201868
Address of array element 0 - -4201896
Address of variable i - -4201868
Address of array element 1 - -4201892
Address of variable i - -4201868
Address of array element 2 - -4201888
Address of variable i - -4201868
Address of array element 3 - -4201884
Address of variable i - -4201868
Address of array element 4 - -4201880
Address of variable i - -4201868
Address of array element 5 - -4201876

As you can see compiler kept some spacing between array allocation and int allocation, if you run the loop 2 more times, it will overwrite address of i and then it becomes infinite loop.

- Anonymous December 18, 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