C Programming Questions and Answers – Variable Names – 2

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

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.

1. Which is valid C expression?
a) int my_num = 100,000;
b) int my_num = 100000;
c) int my num = 1000;
d) int $my_num = 10000;
View Answer

Answer: b
Explanation: Space, comma and $ cannot be used in a variable name.

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

advertisement
advertisement
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         printf("Hello World! %d \n", x);
  5.         return 0;
  6.     }

a) Hello World! x;
b) Hello World! followed by a junk value
c) Compile time error
d) Hello World!
View Answer

Answer: c
Explanation: It results in an error since x is used without declaring the variable x.
Output:
$ cc pgm1.c
pgm1.c: In function ‘main’:
pgm1.c:4: error: ‘x’ undeclared (first use in this function)
pgm1.c:4: error: (Each undeclared identifier is reported only once
pgm1.c:4: error: for each function it appears in.)
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int y = 10000;
  5.         int y = 34;
  6.         printf("Hello World! %d\n", y);
  7.         return 0;
  8.     }

a) Compile time error
b) Hello World! 34
c) Hello World! 1000
d) Hello World! followed by a junk value
View Answer

Answer: a
Explanation: Since y is already defined, redefining it results in an error.
Output:
$ cc pgm2.c
pgm2.c: In function ‘main’:
pgm2.c:5: error: redefinition of ‘y’
pgm2.c:4: note: previous definition of ‘y’ was here
advertisement

4. Which of the following is not a valid variable name declaration?
a) float PI = 3.14;
b) double PI = 3.14;
c) int PI = 3.14;
d) #define PI 3.14
View Answer

Answer: d
Explanation: #define PI 3.14 is a macro preprocessor, it is a textual substitution.
advertisement

5. What will happen if the following C code is executed?

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

a) It will cause a compile-time error
b) It will cause a run-time error
c) It will run without any error and prints 3
d) It will experience infinite looping
View Answer

Answer: c
Explanation: A C program can have same function name and same variable name.
$ cc pgm3.c
$ a.out
3

6. What is the problem in the following variable declaration?

float 3Bedroom-Hall-Kitchen?;

a) The variable name begins with an integer
b) The special character ‘-‘
c) The special character ‘?’
d) All of the mentioned
View Answer

Answer: d
Explanation: A variable name cannot start with an integer, along with that the C compiler interprets the ‘-‘ and ‘?’ as a minus operator and a question mark operator respectively.

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

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

a) The program will print 12
b) The program will print 14
c) The program will have a runtime error
d) The program will cause a compile-time error due to redeclaration
View Answer

Answer: b
Explanation: Variable names ThisIsVariablename and ThisIsVariableName are both distinct as C is case sensitive.
Output:
$ cc pgm4.c
$ a.out
14

8. Which of the following cannot be a variable name in C?
a) volatile
b) true
c) friend
d) export
View Answer

Answer: a
Explanation: volatile is C keyword.

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.