100% found this document useful (1 vote)
537 views

C Programming Questions and Answers

This document contains a summary of 9 interview questions about variable names in the C programming language, along with their answers. It discusses valid and invalid variable name declarations in C, rules for variable names, and common mistakes made with variable names that could cause errors. The questions cover topics like valid characters in names, name length limits, name collisions, and name redefinitions.

Uploaded by

ashwani nagar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
537 views

C Programming Questions and Answers

This document contains a summary of 9 interview questions about variable names in the C programming language, along with their answers. It discusses valid and invalid variable name declarations in C, rules for variable names, and common mistakes made with variable names that could cause errors. The questions cover topics like valid characters in names, name length limits, name collisions, and name redefinitions.

Uploaded by

ashwani nagar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

C Programming Questions and Answers – Variable Names – 1

This section on C interview questions and answers focuses on “Variable Names”. One shall practice
these interview questions to improve their C programming skills needed for various interviews
(campus interviews, walkin interviews, company interviews), placements, entrance exams and other
competitive exams. These questions can be attempted by anyone focusing on learning C
Programming language. They can be a beginner, fresher, engineering graduate or an experienced
IT professional. Our C Interview questions come with detailed explanation of the answers which
helps in better understanding of C concepts.
Here is a listing of C interview questions on “Variable Names” along with answers,
explanations and/or solutions:

Who is father of C Language?


A. Bjarne Stroustrup

B. James A. Gosling

C. Dennis Ritchie

D Dr. E.F. Codd

Answer –c

C Language developed at _________?


A. AT & T's Bell Laboratories of USA in 1972

B. AT & T's Bell Laboratories of USA in 1970

C. Sun Microsystems in 1973

D. Cambridge University in 1972

Anser A
C programs are converted into machine language with the help of
A. An Editor

B. A compiler

C. An operating system

D. None of these.

Answer B

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
View Answer
Answer:d
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
View Answer
Answer:c
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
View Answer
Answer:c
Explanation:None.
6. All keywords in C are in
a)LowerCase letters
b)UpperCase letters
c)CamelCase letters
d) None
View Answer
Answer:a
Explanation:None.

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
View Answer-a
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;
View Answer-d
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
View Answer
Answer:c

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;
View Answer
Answer:b
Explanation:space, comma and $ cannot be used in a variable name.
2. What is the output of this C code?
1. #include <stdio.h>
2. int main()
3. {
4. printf("Hello World! %d \n", x);
5. return 0;
6. }

a) Hello World! x;
b) Hello World! followed by a junk value
c) Compile time error
d) Hello World!
View Answer
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.)
3. What is the output of this C code?

1. #include <stdio.h>
2. int main()
3. {
4. int y = 10000;
5. int y = 34;
6. printf("Hello World! %d\n", y);
7. return 0;
8. }

a) Compile time error


b) Hello World! 34
c) Hello World! 1000
d) Hello World! followed by a junk value
View Answer
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
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
View Answer
Answer:d
Explanation:#define PI 3.14 is a macro preprocessor, it is a textual substitution.
5. What will happen if the below program is executed?

1. #include <stdio.h>
2. int main()
3. {
4. int main = 3;
5. printf("%d", main);
6. return 0;
7. }

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
View Answer
Answer:c
Explanation:A C program can have same function name and same variable name.
$ cc pgm3.c
$ a.out
3
6. 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
View Answer
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.
7. Comment on the output of this C code?

1. #include <stdio.h>
2. int main()
3. {
4. int ThisIsVariableName = 12;
5. int ThisIsVariablename = 14;
6. printf("%d", ThisIsVariablename);
7. return 0;
8. }

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
View Answer
Answer:b
Explanation:Variable names ThisIsVariablename and ThisIsVariableName are both distinct as C is
case sensitive.
Output:
$ cc pgm4.c
$ a.out
14
8. Which of the following cannot be a variable name in C?
a) volatile
b) true
c) friend
d) export
View Answer
Answer: a
Explanation:volatile is C keyword
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;
View Answer
Answer:b
Explanation:space, comma and $ cannot be used in a variable name.
2. What is the output of this C code?

1. #include <stdio.h>
2. int main()
3. {
4. printf("Hello World! %d \n", x);
5. return 0;
6. }

a) Hello World! x;
b) Hello World! followed by a junk value
c) Compile time error
d) Hello World!
View Answer
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.)
3. What is the output of this C code?

1. #include <stdio.h>
2. int main()
3. {
4. int y = 10000;
5. int y = 34;
6. printf("Hello World! %d\n", y);
7. return 0;
8. }

a) Compile time error


b) Hello World! 34
c) Hello World! 1000
d) Hello World! followed by a junk value
View Answer
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
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
View Answer
Answer:d
Explanation:#define PI 3.14 is a macro preprocessor, it is a textual substitution.
5. What will happen if the below program is executed?

1. #include <stdio.h>
2. int main()
3. {
4. int main = 3;
5. printf("%d", main);
6. return 0;
7. }

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
View Answer
Answer:c
Explanation:A C program can have same function name and same variable name.
$ cc pgm3.c
$ a.out
3
6. 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
View Answer
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.
7. Comment on the output of this C code?

1. #include <stdio.h>
2. int main()
3. {
4. int ThisIsVariableName = 12;
5. int ThisIsVariablename = 14;
6. printf("%d", ThisIsVariablename);
7. return 0;
8. }

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
View Answer
Answer:b
Explanation:Variable names ThisIsVariablename and ThisIsVariableName are both distinct as C is
case sensitive.
Output:
$ cc pgm4.c
$ a.out
14
8. Which of the following cannot be a variable name in C?
a) volatile
b) true
c) friend
d) export
View Answer
Answer: a
Explanation:volatile is C keyword
1. Comment on the output of this C code?

1. #include <stdio.h>
2. int main()
3. {
4. float f1 = 0.1;
5. if (f1 == 0.1)
6. printf("equal\n");
7. else
8. printf("not equal\n");
9. }

a) equal
b) not equal
c) Output depends on compiler
d) None of the mentioned
View Answer
Answer:b
Explanation:0.1 by default is of type double which has different representation than float resulting in
inequality even after conversion.
Output:
$ cc pgm4.c
$ a.out
not equal
2. Comment on the output of this C code?

1. #include <stdio.h>
2. int main()
3. {
4. float f1 = 0.1;
5. if (f1 == 0.1f)
6. printf("equal\n");
7. else
8. printf("not equal\n");
9. }

a) equal
b) not equal
c) Output depends on compiler
d) None of the mentioned
View Answer
Answer:a
Explanation:0.1f results in 0.1 to be stored in floating point representations.
Output:
$ cc pgm5.c
$ a.out
equal
3. What is the output of this C code (on a 32-bit machine)?

1. #include <stdio.h>
2. int main()
3. {
4. int x = 10000;
5. double y = 56;
6. int *p = &x;
7. double *q = &y;
8. printf("p and q are %d and %d", sizeof(p), sizeof(q));
9. return 0;
10. }

a) p and q are 4 and 4


b) p and q are 4 and 8
c) Compiler error
d) p and q are 2 and 8
View Answer
Answer:a
Explanation:Size of any type of pointer is 4 on a 32-bit machine.
Output:
$ cc pgm6.c
$ a.out
p and q are 4 and 4
4. Which is correct with respect to size of the datatypes?
a) char > int > float
b) int > char > float
c) char < int < double
d) double > char > int
View Answer
Answer:c
Explanation:char has lesser bytes than int and int has lesser bytes than double in any system
5. What is the output of the following C code(on a 64 bit machine)?

1. #include <stdio.h>
2. union Sti
3. {
4. int nu;
5. char m;
6. };
7. int main()
8. {
9. union Sti s;
10. printf("%d", sizeof(s));
11. return 0;
12. }

a) 8
b) 5
c) 9
d) 4
View Answer
Answer:d
Explanation:Since the size of a union is the size of its maximum datatype, here int is the largest
hence 4.
Output:
$ cc pgm7.c
$ a.out
4
6. What is the output of this C code?

1. #include <stdio.h>
2. int main()
3. {
4. float x = 'a';
5. printf("%f", x);
6. return 0;
7. }

a) a
b) run time error
c) a.0000000
d) 97.000000
View Answer
Answer:d
Explanation:Since the ASCII value of a is 97, the same is assigned to the float variable and printed.
Output:
$ cc pgm8.c
$ a.out
97.000000
7. Which of the datatypes have size that is variable?
a) int
b) struct
c) float
d) double
View Answer
Answer:b

2. What is the output of this C code?

1. #include <stdio.h>
2. int main()
3. {
4. printf("C programming %s", "Class by\n%s Sanfoundry", "WOW");
5. }

a) C programming Class by
WOW Sanfoundry
b) C programming Class by\n%s Sanfoundry
c) C programming Class by
%s Sanfoundry
d) Compilation error
View Answer
Answer:c
Explanation:This program has only one %s within first double quotes, so it does not read the string
“WOW”.
The %s along with the Sanfoundry is not read as a format modifier while new line character prints
the new line.
Output:
$ cc pgm2.c
$ a.out
C programming Class by
%s Sanfoundry
3. For the following code snippet:
char *str = “Sanfoundry.com\0” “training classes”;
The character pointer str holds reference to string:
a) Sanfoundry.com
b) Sanfoundry.com\0training classes
c) Sanfoundry.comtraining classes
d) Invalid declaration
View Answer
Answer:b
Explanation:’\0′ is accepted as a char in the string. Even though strlen will give length of string
“Sanfoundry.com”, in memory str is pointing to entire string including training classes”
4. What is the output of this C code?

1. #include <stdio.h>
2. #define a 10
3. int main()
4. {
5. const int a = 5;
6. printf("a = %d\n", a);
7. }

a) a = 5
b) a = 10
c) Compilation error
d) Runtime error
View Answer
Answer:c
Explanation:The #define substitutes a with 10 leaving no identifier and hence compilation error.
Output:
$ cc pgm3.c
pgm3.c: In function ‘main’:
pgm3.c:5: error: expected identifier or ‘(’ before numeric constant
5. What is the output of this C code?

1. #include <stdio.h>
2. int main()
3. {
4. int var = 010;
5. printf("%d", var);
6. }

a) 2
b) 8
c) 9
d) 10
View Answer
Answer:b
Explanation:010 is octal representation of 8.
Output:
$ cc pgm4.c
$ a.out
8
6. What is the output of this C code?

1. #include <stdio.h>
2. enum birds {SPARROW, PEACOCK, PARROT};
3. enum animals {TIGER = 8, LION, RABBIT, ZEBRA};
4. int main()
5. {
6. enum birds m = TIGER;
7. int k;
8. k = m;
9. printf("%d\n", k);
10. return 0;
11. }

a) 0
b) Compile time error
c) 1
d) 8
View Answer
Answer:d
Explanation:m is an integer constant, hence compatible.
Output:
$ cc pgm5.c
$ a.out
8
7. What is the output of this C code?

1. #include <stdio.h>
2. #define MAX 2
3. enum bird {SPARROW = MAX + 1, PARROT = SPARROW + MAX};
4. int main()
5. {
6. enum bird b = PARROT;
7. printf("%d\n", b);
8. return 0;
9. }

a) Compilation error
b) 5
c) Undefined value
d) 2
View Answer
Answer:b
Explanation:MAX value is 2 and hence PARROT will have value 3 + 2.
Output:
$ cc pgm6.c
$ a.out
5
8. What is the output of this C code?

1. #include <stdio.h>
2. #include <string.h>
3. int main()
4. {
5. char *str = "x";
6. char c = 'x';
7. char ary[1];
8. ary[0] = c;
9. printf("%d %d", strlen(str), strlen(ary));
10. return 0;
11. }
a) 1 1
b) 2 1
c) 2 2
d) 1 (undefined value)
View Answer
Answer:d
Explanation:str is null terminated but ary is not.
Output:
$ cc pgm7.c
$ a.out
15

You might also like