C Programming Questions and Answers – Constants – 2

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

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

1. enum types are processed by _________
a) Compiler
b) Preprocessor
c) Linker
d) Assembler
View Answer

Answer: a
Explanation: None.

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

advertisement
advertisement
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         printf("sanfoundry\rclass\n");
  5.         return 0;
  6.     }

a) sanfoundryclass
b)

   sanfoundry
   class

c) classundry
d) sanfoundry
View Answer

Answer: c
Explanation: r is carriage return and moves the cursor back. sanfo is replaced by class.
Output:
$ cc pgm8.c
$ a.out
classundry

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

advertisement
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         printf("sanfoundry\r\nclass\n");
  5.         return 0;
  6.     }

a) sanfoundryclass
b)

   sanfoundry
   class
advertisement

c) classundry
d) sanfoundry
View Answer

Answer: b
Explanation: rn combination makes the cursor move to the next line.
Output:
$ cc pgm9.c
$ a.out
sanfoundry
class

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

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         const int p;
  5.         p = 4;
  6.         printf("p is %d", p);
  7.         return 0;
  8.     }

a) p is 4
b) Compile time error
c) Run time error
d) p is followed by a garbage value
View Answer

Answer: b
Explanation: Since the constant variable has to be declared and defined at the same time, not doing it results in an error.
Output:
$ cc pgm10.c
pgm10.c: In function ‘main’:
pgm10.c:5: error: assignment of read-only variable ‘p’

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

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int k = 4;
  5.         int *const p = &k;
  6.         int r = 3;
  7.         p = &r;
  8.         printf("%d", p);
  9.     }

a) Address of k
b) Address of r
c) Compile time error
d) Address of k + address of r
View Answer

Answer: c
Explanation: Since the pointer p is declared to be constant, trying to assign it with a new value results in an error.
Output:
$ cc pgm11.c
pgm11.c: In function ‘main’:
pgm11.c:7: error: assignment of read-only variable ‘p’
pgm11.c:8: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int * const’

6. Which of the following statement is false?
a) Constant variables need not be defined as they are declared and can be defined later
b) Global constant variables are initialized to zero
c) const keyword is used to define constant values
d) You cannot reassign a value to a constant variable
View Answer

Answer: a
Explanation: Since the constant variable has to be declared and defined at the same time, not doing it results in an error.

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

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int const k = 5;
  5.         k++;
  6.         printf("k is %d", k);
  7.     }

a) k is 6
b) Error due to const succeeding int
c) Error, because a constant variable can be changed only twice
d) Error, because a constant variable cannot be changed
View Answer

Answer: d
Explanation: Constant variable has to be declared and defined at the same time. Trying to change it results in an error.
Output:
$ cc pgm12.c
pgm12.c: In function ‘main’:
pgm12.c:5: error: increment of read-only variable ‘k’

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

  1.     #include <stdio.h>
  2.     int const print()
  3.     {
  4.         printf("Sanfoundry.com");
  5.         return 0;
  6.     }
  7.     void main()
  8.     {
  9.         print();
  10.     }

a) Error because function name cannot be preceded by const
b) Sanfoundry.com
c) Sanfoundry.com is printed infinite times
d) Blank screen, no output
View Answer

Answer: b
Explanation: None.
Output:
$ cc pgm13.c
$ a.out
Sanfoundry.com

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.