Adobe Interview Question


Country: United States
Interview Type: Phone Interview




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

Lets take the Machine as Big Endian (Read from left to right)
and size of int as 4.

size of inner most Union D=max{size of char,size of int[5]} = 20 bytes ======5*sizeof(int)

size of Union C=max{size of int , size of Union D} = 20 bytes

size of Union B= max { size of double , size of Union C} =20 bytes

size of the whole Union=max {size of long int[5],size of Union B}= 5*sizeof(long int) = 20

if we store p->b.a.k= 15 in union
because it is an int so it will take first 4bytes and the whole 20 bytes will look as
000F 0000 0000 0000 0000 in Hexadecimal

So when reading p->b.a.s.x[0] as first int it will print 15
and reading p->y[0] as long int it will again print 15 because both are 4 byte.

- dilip kasana September 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

big endian
0F 00 00 00 00 00 00 00 (reading an int will give 15)
little endian
00 00 00 00 00 00 00 0F (reading an int will give 0)

- AB September 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

its doesn't matter how many fields in union is declare but they all share same memory i.e. just long enough to save biggest field in this case 40 bytes will be allocated as its size of union A
now value of 'y' which is 15 will be stored in same shared block now u access any data type it will print value 15(if it is int,floa,double or short ) otherwise it will print some garbage value

- krishnam6767 September 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

But when i replaced long int y[5] to char i[5]; and int x[5] to char x[5].
Then also the result is 15 and 15. So could you please tell me why?
To my knowledge it should print element stord in 1st bit //x[0], y[0].
Don't you think so?

- rasmiranjanbabu September 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@rasmirnjanbabu did u changed the format specifier in printf statement use %c then se what u get

- krishnam6767 September 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

because printf is designed for taking variable no of arguments so it NEVER CHECK WHAT FORMAT SPECIFIER YOU GAVE
if its %d it will print first 4 bytes iirespective of what variable you gave

- krishnam6767 September 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry i have wrriten 40 bytes in hurry it will be 20 bytes

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

Answer will depend on endian ness of machine. if it had been a little endian machine it would come 15,0

- AB September 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It depends on sizeof(int) as well.
you are using sizeof(int)=2

right me if i am wrong

- dilip kasana September 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include<stdio.h> #include<stdlib.h>
int main(){
union A {
long int y[5];
union B{
double g;
union C{
int k;
union D{
char ch;
int x[5];
} s;
}a;
}b;
}*p;
p=(union A * ) malloc ( sizeof (union A));
p->b.a.k=15;
printf("%d %d \n",p->b.a.s.x[0],p->y[0]);
}

- Anonymous November 28, 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