Qualcomm Interview Question for Software Engineer / Developers






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

Depends on the type of z.
If z is a signed integer, then answer is -14.
If z is an unsigned integer, then x is first "converted" into an unsigned integer and then added i.e. z will become a huge number approaching the limits of the int size i.e. near 4G.

- LSK August 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
2
of 2 vote

main()
{
int x = -20,z1;
unsigned int y = 6,z;
z = x + y;
z1=x + y;
printf("%d %u\n",z,z);
printf("%d %u\n",z1,z1);
return 0;
}
Output:
-14 4294967282
-14 4294967282

so the output depends on the format while printing,however,z,z1 store same value

- Satish January 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

This is because of promotion rules. Unsigned int + signed int will always be unsigned int. Meaning signed type is promoted to unsigned. It will be a very large <unsigned> integer which is the answer you have given. :)

- Salil September 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Let me add, while printing, type of z matters. If you do a comparison, it takes unsigned value. See following ex.

int main()
{
int x = -20, z;
unsigned int y = 6;
z = x + y;
if((x+y)>6)
{
printf("HAAAA");
}
return 0;
}

- Salil September 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

if u will fallow STANDARD C AIRTHMATIC CONVERSION RULE LOWER TYPE WILL BE UPGRADED TO HIGHER TYPE .

if one is signed int and one is unsigned both will be upgraded to unsigned so result will b 4294967282 correctly.

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

Answer is -14 while z is signed and unsigned integer.

- Bond7Ghost August 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

verified ..it's correct

- ano July 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

LSK is right.
x will be promoted to unsigned before its addition to y.
Z will have a huge value if z is unsigned otherwise in case of signed it will get saturated.

- Peter Pan August 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Lsk and peter both are wrong
Answer is -14 while z is signed and unsigned integer.

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

confusion...tell us whats the right answer

- fresher August 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you talking to me?

- ap January 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The value (in term of binary) of z is the same regardless signed or unsigned. If print z as signed int, you get -14, if you print z as unsigned int, you get the huge number, i.e. near 4G.

- waterfall.zhang August 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Z = 4294967282 if unsined int
z = -14 if int

- Hitesh August 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Answer is -14 does't matter z is signed or unsigned

- dhaval0129 August 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

yeah!irrespective of being signed or unsigned ..the answer is -14,tried it out in C-Free!

- dinnu.k August 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Are simply Run the program. :P

- Hahahah August 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

depends how you print it. if you printf("%d", z) then it is -14. if you printf("%ud", z) then it is large number around 4GB

- Ganga August 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@gaga second you. no matter where you are delcaring z, only matters is how you r printing it.

- codex August 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

LSK is correct.. The value of Z depends on its declared type. the output of z will depend on the format specifier %d or %u..

- deepan August 31, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Actually all the people who are saying that answer in both the cases are -14 are using the statement: printf("%d",z) and that's wrong because an unsigned int is tyepcasted as signed.
The should print the result as printf("%u",z), this will show the exact result

- Pradeep Chhetri September 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hitesh is corerct,
....Z = 4294967282 if unsined int
....z = -14 if int
In both cases it happens like this: z = x + (int)y; //implicit promotion of unsigned variable to signed type under "sign preserving" schema...then temporary result gets promoted to unsigned int LHS.

- Dilip September 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

its called 2's complement arithmetic
negative numbers are represented in their 2'complement form i.e. flip all bits and add 1
2's complement of -20 in hex (for 32 bit machine) is oxffffffea
6 in hex 0x00000006

now add 0xffffffea and 0x00000006
you get 0xfffffff2 which is -14 in 2's complement

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

both case o/p is -14

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

It depends on how you interpret the result:

-14 is the same as the binary representation of 4294967282.

- maverick June 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

true maverick

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

main()
{
int x = -20,z1;
unsigned int y = 6,z;
z = x + y;
z1=x + y;
printf("%d %u\n",z,z);
printf("%d %u\n",z1,z1);
return 0;
}
Output:
-14 4294967282
-14 4294967282

so the output depends on the format while printing,however,z,z1 store same value

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

I checked the result in C++ using cout,
cout<<z<<endl;
it seems that z is automatically treated as unsigned int.
Just print out 4294967282

- TJ Liu February 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

tested by running the code:
o/p in both cases is: -14

- Anonymous September 25, 2011 | 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