C Programming Questions and Answers – Constants – 1

This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Constants – 1”.

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

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

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         enum {ORANGE = 5, MANGO, BANANA = 4, PEACH};
  5.         printf("PEACH = %d\n", PEACH);
  6.     }

a) PEACH = 3
b) PEACH = 4
c) PEACH = 5
d) PEACH = 6
View Answer

Answer: c
Explanation: In enum, the value of constant is defined to the recent assignment from left.
Output:
$ cc pgm1.c
$ a.out
PEACH = 5
advertisement
advertisement

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

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         printf("C programming %s", "Class by\n%s Sanfoundry", "WOW");
  5.     }

a)

   C programming Class by
   WOW Sanfoundry
advertisement

b) C programming Class by\n%s Sanfoundry
c)

   C programming Class by
   %s Sanfoundry

d) Compilation error
View Answer

Answer: c
Explanation: This program has only one %s within first double quotes, so it does not read the string “WOW”.
The %s along with the Sanfoundry is not read as a format modifier while new line character prints the new line.
Output:
$ cc pgm2.c
$ a.out
C programming Class by
%s Sanfoundry
advertisement

3. In the following code snippet, character pointer str holds a reference to the string ___________

char *str =  "Sanfoundry.com\0" "training classes";

a) Sanfoundry.com
b) Sanfoundry.com\0training classes
c) Sanfoundry.comtraining classes
d) Invalid declaration
View Answer

Answer: b
Explanation: ‘\0’ is accepted as a char in the string. Even though strlen will give length of string “Sanfoundry.com”, in memory str is pointing to entire string including training classes.

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

  1.     #include <stdio.h>
  2.     #define a 10
  3.     int main()
  4.     {
  5.         const int a = 5;
  6.         printf("a = %d\n", a);
  7.     }

a) a = 5
b) a = 10
c) Compilation error
d) Runtime error
View Answer

Answer: c
Explanation: The #define substitutes a with 10 without leaving any identifier, which results in Compilation error.
Output:
$ cc pgm3.c
pgm3.c: In function ‘main’:
pgm3.c:5: error: expected identifier or ‘(’ before numeric constant

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

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int var = 010;
  5.         printf("%d", var);
  6.     }

a) 2
b) 8
c) 9
d) 10
View Answer

Answer: b
Explanation: 010 is octal representation of 8.
Output:
$ cc pgm4.c
$ a.out
8

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

  1.     #include <stdio.h>
  2.     enum birds {SPARROW, PEACOCK, PARROT};
  3.     enum animals {TIGER = 8, LION, RABBIT, ZEBRA};
  4.     int main()
  5.     {
  6.         enum birds m = TIGER;
  7.         int k;
  8.         k = m;
  9.         printf("%d\n", k);
  10.         return 0;
  11.     }

a) 0
b) Compile time error
c) 1
d) 8
View Answer

Answer: d
Explanation: m is an integer constant, hence it is compatible.
Output:
$ cc pgm5.c
$ a.out
8

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

  1.     #include <stdio.h>
  2.     #define MAX 2
  3.     enum bird {SPARROW = MAX + 1, PARROT = SPARROW + MAX};
  4.     int main()
  5.     {
  6.         enum bird b = PARROT;
  7.         printf("%d\n", b);
  8.         return 0;
  9.     }

a) Compilation error
b) 5
c) Undefined value
d) 2
View Answer

Answer: b
Explanation: MAX value is 2 and hence PARROT will have value 3 + 2.
Output:
$ cc pgm6.c
$ a.out
5

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

  1.     #include <stdio.h>
  2.     #include <string.h>
  3.     int main()
  4.     {
  5.         char *str = "x";
  6.         char c = 'x';
  7.         char ary[1];
  8.         ary[0] = c;
  9.         printf("%d %d", strlen(str), strlen(ary));
  10.         return 0;
  11.     }

a) 1 1
b) 2 1
c) 2 2
d) 1 (undefined value)
View Answer

Answer: d
Explanation: str is null terminated, but ary is not null terminated.
Output:
$ cc pgm7.c
$ a.out
1 5

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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
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.