C Programming Questions and Answers – Data Types and Sizes – 2

«
»

This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Data Types and Sizes – 2”.

Pre-requisite for C Data Types MCQ set: Video Tutorial on C Data Types.

advertisement

1. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         float f1 = 0.1;
  5.         if (f1 == 0.1)
  6.             printf("equal\n");
  7.         else
  8.             printf("not equal\n");
  9.     }

a) equal
b) not equal
c) output depends on the compiler
d) error
View Answer

Answer: b
Explanation: 0.1 by default is of type double which has different representation than float resulting in inequality even after conversion.
Output:
$ cc pgm4.c
$ a.out
not equal
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

2. What will be the output of the following C code?

advertisement
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         float f1 = 0.1;
  5.         if (f1 == 0.1f)
  6.             printf("equal\n");
  7.         else
  8.             printf("not equal\n");
  9.     }

a) equal
b) not equal
c) output depends on compiler
d) error
View Answer

Answer: a
Explanation: 0.1f results in 0.1 to be stored in floating point representations.
Output:
$ cc pgm5.c
$ a.out
equal
advertisement

3. What will be the output of the following C code on a 32-bit machine?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int x = 10000;
  5.         double y = 56;
  6.         int *p = &x;
  7.         double *q = &y;
  8.         printf("p and q are %d and %d", sizeof(p), sizeof(q));
  9.         return 0;
  10.     }

a) p and q are 4 and 4
b) p and q are 4 and 8
c) compiler error
d) p and q are 2 and 8
View Answer

Answer: a
Explanation: Size of any type of pointer is 4 on a 32-bit machine.
Output:
$ cc pgm6.c
$ a.out
p and q are 4 and 4
advertisement

4. Which is correct with respect to the size of the data types?
a) char > int > float
b) int > char > float
c) char < int < double
d) double > char > int
View Answer

Answer: c
Explanation: char has less bytes than int and int has less bytes than double in any system

5. What will be the output of the following C code on a 64 bit machine?

  1.     #include <stdio.h>
  2.     union Sti
  3.     {
  4.         int nu;
  5.         char m;
  6.     };
  7.     int main()
  8.     {
  9.         union Sti s;
  10.         printf("%d", sizeof(s));
  11.         return 0;
  12.     }

a) 8
b) 5
c) 9
d) 4
View Answer

Answer: d
Explanation: Since the size of a union is the size of its maximum data type, here int is the largest data type. Hence the size of the union is 4.
Output:
$ cc pgm7.c
$ a.out
4

6. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         float x = 'a';
  5.         printf("%f", x);
  6.         return 0;
  7.     }

a) a
b) run time error
c) a.0000000
d) 97.000000
View Answer

Answer: d
Explanation: Since the ASCII value of a is 97, the same is assigned to the float variable and printed.
Output:
$ cc pgm8.c
$ a.out
97.000000

7. Which of the data types has the size that is variable?
a) int
b) struct
c) float
d) double
View Answer

Answer: b
Explanation: Since the size of the structure depends on its fields, it has a variable size.

Sanfoundry Global Education & Learning Series – C Programming Language.

To practice all areas of C language, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement

Leave a Comment

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.