Here is a listing of C interview questions on “Variable Names” along with answers, explanations and/or solutions:
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;
Explanation:space, comma and $ cannot be used in a variable name.
2. What is the output of this C code?
printf("Hello World! %d \n", x);
a) Hello World! x;
b) Hello World! followed by a junk value
c) Compile time error
d) Hello World!
Explanation:It results in an error since x is used without declaring the variable x.
$ 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.)
3. What is the output of this C code?
int y = 10000;
int y = 34;
printf("Hello World! %d\n", y);
a) Compile time error
b) Hello World! 34
c) Hello World! 1000
d) Hello World! followed by a junk value
Explanation:Since y is already defined, redefining it results in an error.
$ 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
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
Explanation:#define PI 3.14 is a macro preprocessor, it is a textual substitution.
5. What will happen if the below program is executed?
int main = 3;
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
Explanation:A C program can have same function name and same variable name.
$ cc pgm3.c
6. What is the problem in following variable declaration?
a) The variable name begins with an integer
b) The special character ‘-‘
c) The special character ‘?’
d) All of the mentioned
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. Comment on the output of this C code?
int ThisIsVariableName = 12;
int ThisIsVariablename = 14;
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
Explanation:Variable names ThisIsVariablename and ThisIsVariableName are both distinct as C is case sensitive.
$ cc pgm4.c
8. Which of the following cannot be a variable name in C?
Explanation:volatile is C keyword.
Sanfoundry Global Education & Learning Series – C Programming Language.
To practice all features of C programming language, here is complete set of 1000+ Multiple Choice Questions and Answers on C.