Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

It is 66, 0

Static variables will be declared once only and though g is declared globally, it is also declared inside main again, so while printing g, the one which declared locally will be preferred and not the one which is declared globally.

- Prathamesh March 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Static variables will be initialized once only, no matter how many times the function is terminated. Coz there scope will be the whole program.
I mistakenly written "declared"... :)...

- Prathamesh March 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Error..!!! come
undefined reference to 'add'.
it's a tricky question

- Rishi Kumar Meena March 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yeah, my first answer is compiler error will prevent program execution. Second answer is 66,0

- Coder May 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yeah, my first answer is compiler error will prevent program execution. Second answer is 66,0

- Coder May 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yeah, my first answer is compiler error will prevent program execution. Second answer is 66,0

- Coder May 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

66 0
coz while printing 'g' it would take the local variable..and static and global variables retain values.

do you remember ..one question was from recursion..and we have to tell the output..i gues f(1)..?

- Dheeraj Sharma March 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Did you sit for full time employment? Written test?

- aravind March 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yup i did sit

- Dheeraj Sharma March 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@aravind and dheeraj sharma : can u share more questions which were asked to you!!!!

- student June 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

garbage value as j is not initialised

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

66,0

- best_coder March 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

correct

- test March 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Any 1 got any email confirmation abt FTE written?

- Coder March 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nope..not till now

- Dheeraj Sharma March 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The output would be add function was not declared.
C is case sensitive ;)

- Starzinger March 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is 66, 0

Static variables will be declared once only and though g is declared globally, it is also declared inside main again, so while printing g, the one which declared locally will be preferred and not the one which is declared globally.

- Prathamesh March 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It's 66, 0, static varaible s will be declared and initialized once.

- wally.azab March 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The output will be 66,11 cause every time you pass i and that value is get added with g in function add

So, in last iteration of for loop i will be 11 and so after completion of add the value of g will be 11

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

priority of local variable is greater than global variable so value of g inside main function is what is initialize but s which is static variable it will initialize one times and s will 66

- neelabh@iiita March 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

priority of local variable is greater than global variable so value of g inside main function is what is initialize but s which is static variable it will initialize one times and s will 66

- neelabh@iiita March 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

answer should be 66, 0

value of g should be 0 because is scope is only inside main
value of j is 66(1+2+3+...+11) as its value depends upon static int variable which keeps on adding contiguous integers from 1 to 11 .and in the last iteration returned value will become value of j

- harsh March 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Error..!!! come
undefined reference to 'add'.
it's a tricky question
otherwise answer will be 66,0

- Rishi Kumar Meena March 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yeah you can't place static variable in function
so it's makes error

- Raja March 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Got below error because we cannot declare static in side method.

"The modifier 'static' is not valid for this item"

- Sidhartha April 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

{66 0 is the real answer}

- Md istiyak April 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

error will come becoz function is called by add();
which is not used anywhere in the given source program
i,e main calls add() but program have Add()

- Md istiyak April 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
int g = 0;
int Add(int i)
{
static int s=0;
s =s+i;
g=g+i;
return s;
}
int main()
{
int ,j;
for(int i=1;i<=11;i++)
{ int g=0;s=0;
j = add(i);
}
printf("%d,%d",j,g);
return 0;
}


in this g declared in for loop then scop of g inside for loop. and printf outside for loop so it will print globa g i.e 66 coz global ge updated in add function.

ans ids 66,66

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

initially there was compile error as add has undefined reference.Add was used in function definition and add was used in calling function.Finally it is 66 as s is declared to be static and g=0 as g is initialised to 0 globally;

- apoorva September 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I remember that in the original question, the variable g was declared inside the for loop not in the main. So the output will be 66, 66

- anonymous March 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Youre right

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

Youre right

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

i hpe u r right

- aka March 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If g is declared inside for loop then I think there will compilation error.

- dumbo March 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes , you are right as in loop s is every time initialize with 0 so answer will be 11,0

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

yes in the original question g was declared inside the loop .

- SachinGuptaMNNIT March 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

i hope it is 11 ,0

- reddy March 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes , you are right as in loop s is every time initialize with 0 so answer will be 11,0

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

s is static in add and the scope of s in the main doesnt affect s in the function. So the static variable only is taken in to account

- Aravind March 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

i hope u right ,its 11,0

- aka March 06, 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