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

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

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

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

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int a[5] = {1, 2, 3, 4, 5};
  5.         int i;
  6.         for (i = 0; i < 5; i++)
  7.             if ((char)a[i] == '5')
  8.                 printf("%d\n", a[i]);
  9.             else
  10.                 printf("FAIL\n");
  11.     }

a) The compiler will flag an error
b) The program will compile and print the output 5
c) The program will compile and print the ASCII value of 5
d) The program will compile and print FAIL for 5 times
View Answer

Answer: d
Explanation: The ASCII value of 5 is 53, the char type-casted integral value 5 is 5 only.
Output:
$ cc pgm1.c
$ a.out
FAIL
FAIL
FAIL
FAIL
FAIL
advertisement
advertisement

2. The format identifier ‘%i’ is also used for _____ data type.
a) char
b) int
c) float
d) double
View Answer

Answer: b
Explanation: Both %d and %i can be used as a format identifier for int data type.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

3. Which data type is most suitable for storing a number 65000 in a 32-bit system?
a) signed short
b) unsigned short
c) long
d) int
View Answer

Answer: b
Explanation: 65000 comes in the range of short (16-bit) which occupies the least memory. Signed short ranges from -32768 to 32767 and hence we should use unsigned short.

4. Which of the following is a User-defined data type?
a) typedef int Boolean;
b) typedef enum {Mon, Tue, Wed, Thu, Fri} Workdays;
c) struct {char name[10], int age};
d) all of the mentioned
View Answer

Answer: d
Explanation: typedef and struct are used to define user-defined data types.
advertisement

5. What is the size of an int data type?
a) 4 Bytes
b) 8 Bytes
c) Depends on the system/compiler
d) Cannot be determined
View Answer

Answer: c
Explanation: The size of the data types depend on the system.

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

advertisement
  1.     #include  <stdio.h>
  2.     int main()
  3.     {
  4.        signed char chr;
  5.        chr = 128;
  6.        printf("%d\n", chr);
  7.        return 0;
  8.     }

a) 128
b) -128
c) Depends on the compiler
d) None of the mentioned
View Answer

Answer: b
Explanation: The range of signed character is from -128 to +127. Since we are assigning a value of 128 to the variable ‘chr’, the result will be negative. 128 in binary is represented as “1000 0000” for character datatype. As you can see that the sign bit is set to 1, followed by 7 zeros (0), its final decimal value will be -128 (negative 128).
Output:
$ cc pgm2.c
$ a.out
-128

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

  1.     #include  <stdio.h>
  2.     int main()
  3.     {
  4.         char c;
  5.         int i = 0;
  6.         FILE *file;
  7.         file = fopen("test.txt", "w+");
  8.         fprintf(file, "%c", 'a');
  9.         fprintf(file, "%c", -1);
  10.         fprintf(file, "%c", 'b');
  11.         fclose(file);
  12.         file = fopen("test.txt", "r");
  13.         while ((c = fgetc(file)) !=  -1)
  14.             printf("%c", c);
  15.         return 0;
  16.     }

a) a
b) Infinite loop
c) Depends on what fgetc returns
d) Depends on the compiler
View Answer

Answer: a
Explanation: None.
Output:
$ cc pgm3.c
$ a.out
a

8. What is short int in C programming?
a) The basic data type of C
b) Qualifier
c) Short is the qualifier and int is the basic data type
d) All of the mentioned
View Answer

Answer: c
Explanation: None.

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 & discussions at Telegram SanfoundryClasses.