Hewlett Packard Interview Question for Software Engineer / Developers






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

@bhaskar: you are not right the output will be same in any standard compiler of c
and that will be 10 0 0
further ur exaplanation is right

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

tux@tux-laptop:/tmp$ cat a.c
#include<stdio.h>

#define swap(a,b) temp=a;a=b;b=temp;

main()
{
int i,j,temp;
i=5;
j=10;
temp=0;
if (i>j)
swap(i,j);
printf("%d %d %d",i,j,temp);
}


tux@tux-laptop:/tmp$ ./a.out
10 0 0


gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)

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

But I really wonder how this code it is returning TRUE for condition (i > j) ?
and this returns TRUE only if I have swap(i,j) call.

Can some one pls explain detailly ?

- Vinod November 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, its returning false... see my comment below.
micro swap(a,b) is replaced by those three statements .
only 1st statement is not executed rest two are executed as condition is false.
Now u ll get the correct output.

- Narendra yadav November 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

if (i>j)
temp = i; i = j; j = temp;
if is only for temp = i ; so it will skip that part and run I=J and j=temp which is zero

- nikamipara29 September 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Its correct ;
Replace that micro . After replacement code will be like this
if(i>j)
temp=a;
a=b;
b=temp;
and if condition is false, therefore only last two statements will be executed.
a(i) will get b(10), b(j) will get temp(0)
finally i=10,j=0;temp=0;

- Narendra yadav November 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

And if u want desired result put swap(i,j) inside {}
if(i>j)
{
swap(a,b)
}
then output will be 5 10 0 .

- Narendra yadav November 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define swap(a,b)
temp=a;
a=b;
b=temp;
main( )
{ int i, j, temp;
i=10;
j=5;
temp=0;
if( i > j)
swap( i, j );
printf( %d %d %d, i++, ++j, temp);
}

- Anonymous August 13, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Simple macro substitution
if (i>j)
temp = i; i = j; j = temp;

condition false, so temp will be 0.
That is all.

- Sergey August 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Actually its "correct" output should be "10 10 0" as gcc shows.

>gcc --save-temps prog.c
Now check the macro expansion in prog.i file.

if(i>j)
temp=i;i=j;j=i;;



Hence,
i=j; /* i becomes 10 */
j=i; /* j becomes 10 */
temp remains as 0

- bhaskar August 19, 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