Please Turn On Java Script To Access This Site.

C Institute | Questions


Previous Topic : Variable name and scope 2Next Topic : Arithmetic operator 1

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: Which is invalid variable declaration?
a)  int a=10,a=20;
b)  int A=10,a=20;
c)  int a=10,A=20;
d)  int a=10; float A=20;
Correct Answer : a
Explanation : Two variables can not have the same name.


Q2: Can we use a global variable inside a function, if the function have a local variable with same name, in c?
a)  No
b)  Yes
c)  Depends on the compiler
d)  None of these
Correct Answer : a
Explanation : In C, we cannot access a global variable if we have a local variable with same name, but it is possible in C++ using scope resolution operator (::).


Q3: What will be the output of the following code?
#define getVariableName(var) #var
int main()
{
        int x=10;
     printf("%s", getVariableName(x));
     return 0;
}
a)  x
b)  10
c)  Either x or 10
d)  None of these
Correct Answer : a
Explanation : In C, there’s a # directive, also called ‘Stringizing Operator’, which does this magic. Basically # directive converts its argument in a string.


Q4: Why variable name does not start with numbers in C ?
a)  To avoid Backtracking to lexical analysis phase
b)  To avoid backtracking to syntax analysis phase
c)  Both of these
d)  None of these
Correct Answer : a
Explanation : Backtracking is avoided in lexical analysis phase while compiling the piece of code. The variable like number; , the compiler will know its a identifier right away when it meets letter n character in the lexical Analysis phase. However, a variable like ; 789number, compiler won’t be able to decide if its a number or identifier until it hits n and it needs backtracking to go in the lexical analysis phase to identify that it is a variable. But it is not supported in compiler. When compiler is parsing the token compiler only have to look at the first character to determine if it’s an identifier or literal and then send it to the correct function for processing. So that’s a performance optimization.


Q5: Consider the following C code, which variable will have the longest scope?
int x;
int main()
{
     int y;
    
     // ...
     // ...
     return 0;
}
int z;
a)  x
b)  y
c)  z
d)  All have same scope length
Correct Answer : a
Explanation : x is accessible everywhere. y is limited to main() z is accessible only after its declaration. so x have longest scope.


Q6: What will be the output?
int spicy = 40;
int main()
{
    int spicy = spicy+spicy;
    printf("%d ", spicy);
    return 0;
}
a)  40
b)  Compilation Error
c)  Garbaze Value
d)  80
Correct Answer : c
Explanation : First spicy is declared, then value is assigned to it. As soon as spicy is declared as a local variable, it hides the global variable spicy;


Q7: What will be the output?
int main()
{
    int a = 024;
    printf("%d", a);
    return 0;
}
a)  24
b)  024
c)  20
d)  None of these
Correct Answer : c
Explanation : When a constant value starts with 0, it is considered as octal number. Therefore the value of x is 2*8 + 4 = 20


Q8: What will be the output?
int spicy = 40;
int main()
{
    spicy=spicy+spicy;
    printf("%d ", spicy);
    return 0;
}
a)  40
b)  80
c)  Garbaze
d)  Compilation Error
Correct Answer : b
Explanation : First spicy is declared, then value is assigned to it. in main the value of spicy is doubled by the expression spicy=spicy+spicy;


Q9: What will be the output?
int spicy = 40;
int main()
{
    spicy+spicy;
    printf("%d ", spicy);
    return 0;
}
a)  40
b)  80
c)  Garbaze
d)  Compilation Error
Correct Answer : a
Explanation : First spicy is declared, then value is assigned to it. in main the value of spicy is not changed by the expression spicy+spicy;


Q10: What will be the output?
int spicy = 40;
int main()
{
    spicy+=spicy;
    printf("%d ", spicy);
    return 0;
}
a)  40
b)  80
c)  Garbaze
d)  Syntax Erro
Correct Answer : b
Explanation : First spicy is declared, then value is assigned to it. in main the value of spicy is doubled by the expression spicy+=spicy;


Previous Topic : Variable name and scope 2Next Topic : Arithmetic operator 1