Please Turn On Java Script To Access This Site.

C Institute | Questions


Previous Topic : #defineNext Topic : Variable name and scope 2

This section on C interview questions, tricky questions, and puzzles focuses on “Variable naming convention”. One shall practice these questions and puzzles to improve their C programming skills needed for various interviews (campus interviews, walk-in interviews, viva exams and company interviews), placements, entrance exams and other competitive exams. These programming questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate, engineering student, bca student, mca student, mcs students or an experienced IT professional. Here is a listing of C questions and puzzles on “variable names” along with answers and/or explanations.

Q1: What will be the output of the following C code?

int main()
{ printf("Hi! %d ", t);
return 0;
}
a)  Hi! followed by garbaze value
b)  Hi! 0
c)  Runtime error
d)  Compilation error
Correct Answer : d
Explanation : Compiler will generate syntax error, because the variable t used in printf is not declared.


Q2: What will be the output of following code?
int main()
{
        int y=10;
        int Y=20;
        printf("Hello World%d",Y);
        return 0;
}
a)  Hello World 10
b)  Hello World 20
c)  Compilation error due to redeclaration of y
d)  Runtime error
Correct Answer : b
Explanation : The program will compile without error and prints:
Hello World 20
The y and Y two different variables because c is case sensitive language.


Q3: Which is not a valid variable name
a)  int __a__;
b)  int Float;
c)  int double_b;
d)  None of the mentioned
Correct Answer : d
Explanation : All the option satisfied all variable naming rules.


Q4: Which is the invalid variable name?
a)  int integer;
b)  int _2month_salary;
c)  int 2month_expenses;
d)  None of above
Correct Answer : c
Explanation : Variable name cannot be started by a digit.


Q5: Which is the valid variable name?
a)  int If;
b)  int volatile;
c)  float a=2,345;
d)  double your salary;
Correct Answer : a
Explanation : only option a satisfies all rules of variable naming.


Q6: Which is not an invalid variable name?
a)  int rollNo. ;
b)  double roll-number;
c)  float roll_Number;
d)  All are invalid
Correct Answer : c
Explanation : In the third choice all of the rules for variable naming are followed properly.


Q7: What can not be used in variable names?
a)  Hyphen (-)
b)  Period (.)
c)  Dollar ($)
d)  All of the above can not be used
Correct Answer : d
Explanation : Any special symbol rather than underscore ( _ ) can not be used in identifiers (variable names).


Q8: Which can be a part of variable name?
a)  Uppercase Letters
b)  Hyphen Sign
c)  Comma
d)  None of the above
Correct Answer : a
Explanation : Both lowercase and uppercase letters are allowed in the identifiers.


Q9: Why do the use of such variable names which are started with the underscore are not encouraged?
a)  It is not standardized
b)  To avoid conflicts since assemblers and loaders uses such names
c)  To avoid conflicts since library routines use such names
d)  To avoid conflicts with environment variables of an operating system
Correct Answer : c


Q10: Which is not a valid variable name in c?
a)  roll_number
b)  $roll_number
c)  Double
d)  floaT
Correct Answer : b
Explanation : $ symbol is not allowed in identifiers in c.


Previous Topic : #defineNext Topic : Variable name and scope 2