Midierm Examination COMP 2401 Questions 3 and 4 are based on the following decla
ID: 3708251 • Letter: M
Question
Midierm Examination COMP 2401 Questions 3 and 4 are based on the following declarations are defined and are used on a computer where the alignment . Assume that the following data typedet struct cart LA long numCars short fuelTankSize unsigned long serialNumber : unsigned char numDoors CarType struct truck unsigned long serialNumberi long numTrucks; short loadCapacity char numSeats: union u CarType car: struct truck trucki 3. What will be printed when main is executed int main (int argc, char argv) union u x printf("sizeo a siseof (ostret cruck) - d n sizeof (CarType), sizeof (struct truck))i Answer: a) sizeof (CarType) 16 sizeof (struct truck) - 12 b) sizeof (CarType) -11 sizeof (struct truck) 11 c) sizeof (CarType) 12 sizeof (struct truck) 16 d) sizeof (CarType) 12 sizeof (struct truck) - 12 e) None of the aboveExplanation / Answer
Solution:
The answer should be b.
Because for the CarType the memory allocated to the variables inside the struct are
4+2+4+1= 11
and similarly, for struct truck
the memory allocated is
4(long)+4(long)+2(short)+1(char) = 11 => the memory to be allocated is given in the question itself, so we don't have to consider the general C approach where long is allocated 8 bytes of memory.
The answer is 11, 11
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)