C Programming MCQ | Variable Names - 02

C Programming MCQ | Variable Names - 02

Q1. Which of the following cannot be a variable name in C?

a) volatile
b) true
c) friend
d) export

Answer: a
Explanation: volatile is C keyword.

 

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

#include <stdio.h>
    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

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

 

Q3. 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

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.

 

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

#include <stdio.h>
    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

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

 

Q5. 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

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

 

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

 #include <stdio.h>
    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

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

 

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

#include <stdio.h>
    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!

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.)

 

Q8. 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;

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

 

 

 

 

Previous Post Next Post