250+ TOP MCQs on Constants and Answers

online C test on “Constants”. One shall practice these test questions to improve their C programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our online C test questions come with the detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of online C test questions on “Constants” along with answers, explanations and/or solutions:

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

  1.     #include 
  2.     int main()
  3.     {
  4.         enum {ORANGE = 5, MANGO, BANANA = 4, PEACH};
  5.         printf("PEACH = %dn", PEACH);
  6.     }

a) PEACH = 3
b) PEACH = 4
c) PEACH = 5
d) PEACH = 6
Answer: c
Clarification: In enum, the value of constant is defined to the recent assignment from left.
Output:
$ cc pgm1.c
$ a.out
PEACH = 5

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

  1.     #include 
  2.     int main()
  3.     {
  4.         printf("C programming %s", "Class byn%s ", "WOW");
  5.     }

a)

   C programming Class by
   WOW 

b) C programming Class byn%s
c)

   C programming Class by
   %s 

d) Compilation error
Answer: c
Clarification: This program has only one %s within first double quotes, so it does not read the string “WOW”.
The %s along with the 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

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

char *str =  ".com " "training classes";

a) .com
b) .com training classes
c) .comtraining classes
d) Invalid declaration
Answer: b
Clarification: ‘ ’ is accepted as a char in the string. Even though strlen will give length of string “.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 
  2.     #define a 10
  3.     int main()
  4.     {
  5.         const int a = 5;
  6.         printf("a = %dn", a);
  7.     }

a) a = 5
b) a = 10
c) Compilation error
d) Runtime error
Answer: c
Clarification: 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 
  2.     int main()
  3.     {
  4.         int var = 010;
  5.         printf("%d", var);
  6.     }

a) 2
b) 8
c) 9
d) 10
Answer: b
Clarification: 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 
  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("%dn", k);
  10.         return 0;
  11.     }

a) 0
b) Compile time error
c) 1
d) 8
Answer: d
Clarification: 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 
  2.     #define MAX 2
  3.     enum bird {SPARROW = MAX + 1, PARROT = SPARROW + MAX};
  4.     int main()
  5.     {
  6.         enum bird b = PARROT;
  7.         printf("%dn", b);
  8.         return 0;
  9.     }

a) Compilation error
b) 5
c) Undefined value
d) 2
Answer: b
Clarification: 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 
  2.     #include 
  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)
Answer: d
Clarification: str is null terminated, but ary is not null terminated.
Output:
$ cc pgm7.c
$ a.out
1 5

  250+ TOP MCQs on Increment and Decrement Operators and Answers

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top