Interview Question


Country: United States




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

your array is of size 3 but you are storing 4 items in it. so the compiler seems to throwing an error - stack around the variable is corrupted.
as such the first code prints
1
1
1
1

and the second one prints
0
1
2
3

- Anonymous July 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i've executed both the programs on gcc .
second prog is giving o/p 0 1 2 3
but first one is neither showing any o/p neither any an error . Cursor remain static .

- Shobhit July 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i guess it depends on the compiler too. I got the output in microsofts c++ compiler in visual studio 2010 along with the error.

however in gcc in unix i seem to having the exact same output as yours.

- Anonymous July 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

But what could be the possible reason ??
Changing i to 1 changes the pattern of whole output .

- Shobhit July 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Stackoverflow has a nice thread on the dangers of errors arising due to exceeding the bounds of arrays.

stackoverflow.com/questions/1239938/c-accesses-an-array-out-of-bounds-gives-no-error-why

- Anonymous July 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

out of index.
stack will overflow, so last a[3] will overwrite to buffer of variable 'i' on some O/S.
Anyway, the result is;
(1)
1
1
1
1

(2)
0
1
2
3

- und3r July 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

plz . run the first prog on gcc .

- Shobhit July 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you run it in visual studio 2010 it's giving answer 1 1 1 1 and 0 1 2 3

- kk July 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

out of bound error
int a[3] means three elements a[0],a[1], a[2]
for(i=0;i<=3;i++) will throw error for trying a[3]

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

the question wont run properly....
it will return a segmentation fault..
instead change the array declaration to int a[4]
and then use
for(i=0;i<=3;i++)
a[i]=i;
it will emit the output
1
1
1
1

- Anonymous July 08, 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