Multiple Choice Questions and Answers for Variable Names in C Language
https://www.computersprofessor.com/2017/08/multiple-choice-questions-and-answers.html
List of C interview questions on “Variable Names” along with answers, explanations and/or solutions:
1. C99 standard guarantees uniqueness of ____ characters for internal names.
a) 31
b) 63
c) 12
d) 14
a) 31
b) 63
c) 12
d) 14
Answer:b
Explanation:ISO C99 compiler may consider only first 63 characters for internal.
Explanation:ISO C99 compiler may consider only first 63 characters for internal.
2. C99 standard guarantess uniqueness of _____ characters for external names.
a) 31
b) 6
c) 12
d) 14
a) 31
b) 6
c) 12
d) 14
Answer:a
Explanation:ISO C99 compiler may consider only first 31 characters for external
variables having 31 characters due to which it may not be unique.
Explanation:ISO C99 compiler may consider only first 31 characters for external
variables having 31 characters due to which it may not be unique.
3. Which of the following is not a valid variable name declaration?
a) int __a3;
b) int __3a;
c) int __A3;
d) None of the mentioned
a) int __a3;
b) int __3a;
c) int __A3;
d) None of the mentioned
Answer:d
Explanation:None.
Explanation:None.
4. Which of the following is not a valid variable name declaration?
a) int _a3;
b) int a_3;
c) int 3_a;
d) int _3a
a) int _a3;
b) int a_3;
c) int 3_a;
d) int _3a
Answer:c
Explanation:Variable name cannot start with a digit.
Explanation:Variable name cannot start with a digit.
5. Variable names beginning with underscore is not encouraged. Why?
a) It is not standardized
b) To avoid conflicts since assemblers and loaders use such names
c) To avoid conflicts since library routines use such names
d) To avoid conflicts with environment variables of an operating system
a) It is not standardized
b) To avoid conflicts since assemblers and loaders use such names
c) To avoid conflicts since library routines use such names
d) To avoid conflicts with environment variables of an operating system
Answer:c
6. All keywords in C are in
a) LowerCase letters
b) UpperCase letters
c) CamelCase letters
d) None
a) LowerCase letters
b) UpperCase letters
c) CamelCase letters
d) None
Answer:a
7. Variable name resolving (number of significant characters for uniqueness of variable) depends on
a) Compiler and linker implementations
b) Assemblers and loaders implementations
c) C language
d) None
a) Compiler and linker implementations
b) Assemblers and loaders implementations
c) C language
d) None
Answer:a
Explanation:It depends on the standard to which compiler and linkers are adhering to.
Explanation:It depends on the standard to which compiler and linkers are adhering to.
8. Which of the following is not a valid C variable name?
a) int number;
b) float rate;
c) int variable_count;
d) int $main;
a) int number;
b) float rate;
c) int variable_count;
d) int $main;
Answer:d
Explanation:Since only underscore and no other special character is allowed in a variable name, it results in an error.
Explanation:Since only underscore and no other special character is allowed in a variable name, it results in an error.
9. Which of the following is true for variable names in C?
a) They can contain alphanumeric characters as well as special characters
b) It is not an error to declare a variable to be one of the keywords(like goto, static)
c) Variable names cannot start with a digit
d) Variable can be of any length
a) They can contain alphanumeric characters as well as special characters
b) It is not an error to declare a variable to be one of the keywords(like goto, static)
c) Variable names cannot start with a digit
d) Variable can be of any length
Answer:c
Explanation:According to the syntax for C variable name, it cannot start with a digit.
Explanation:According to the syntax for C variable name, it cannot start with a digit.
10. 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;
a) int my_num = 100,000;
b) int my_num = 100000;
c) int my num = 1000;
d) int $my_num = 10000;
Answer:b
Explanation:space, comma and $ cannot be used in a variable name.
Explanation:space, comma and $ cannot be used in a variable name.
11. What is the output of this C code?
#include
int main()
{
printf("Hello World! %d \n", x);
return 0;
}
a) Hello World! x;
b) Hello World! followed by a junk value
c) Compile time error
d) Hello World!
b) Hello World! followed by a junk value
c) Compile time error
d) Hello World!
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.)
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.)
12. What is the output of this C code?
#include
int main()
{
int y = 10000;
int y = 34;
printf("Hello World! %d\n", y);
return 0;
}
a) Compile time error
b) Hello World! 34
c) Hello World! 1000
d) Hello World! followed by a junk value
b) Hello World! 34
c) Hello World! 1000
d) Hello World! followed by a junk value
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
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
13. 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
a) float PI = 3.14;
b) double PI = 3.14;
c) int PI = 3.14;
d) #define PI 3.14
Answer:d
Explanation:#define PI 3.14 is a macro preprocessor, it is a textual substitution.
Explanation:#define PI 3.14 is a macro preprocessor, it is a textual substitution.
14. What will happen if the below program is executed?
#include
int main()
{
int main = 3;
printf("%d", main);
return 0;
}
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
b) It will cause a run-time error
c) It will run without any error and prints 3
d) It will experience infinite looping
Answer:c
Explanation:A C program can have same function name and same variable name.
$ cc pgm3.c
$ a.out
3
Explanation:A C program can have same function name and same variable name.
$ cc pgm3.c
$ a.out
3
15. What is the problem in 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
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
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.
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.
16. Comment on the output of this C code?
#include
int main()
{
int ThisIsVariableName = 12;
int ThisIsVariablename = 14;
printf("%d", ThisIsVariablename);
return 0;
}
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
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
Answer:b
Explanation:Variable names ThisIsVariablename and ThisIsVariableName are both distinct as C is case sensitive.
Output:
$ cc pgm4.c
$ a.out
14
Explanation:Variable names ThisIsVariablename and ThisIsVariableName are both distinct as C is case sensitive.
Output:
$ cc pgm4.c
$ a.out
14
17. Which of the following cannot be a variable name in C?
a) volatile
b) true
c) friend
d) export
a) volatile
b) true
c) friend
d) export
Answer: a
Explanation:volatile is C keyword.
Explanation:volatile is C keyword.