C Questions and Answers Part-2

1. Which of the following cannot be a variable name in C?
a) true
b) volatile
c) friend
d) export

Answer: b
Explanation: volatile is C keyword.

2. 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 experience infinite looping
b) It will cause a run-time error
c) It will run without any error and prints 3
d) It will cause a compile-time error

Answer: c
Explanation: A C program can have same function name and same variable name.
So, Output is 3

3. 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 14
b) The program will print 12
c) The program will have a runtime error
d) The program will cause a compile-time error due to redeclaration

Answer: a
Explanation: Variable names ThisIsVariablename and ThisIsVariableName are both distinct as C is case sensitive.
So, Output is 14

4. Which of the following is a User-defined data type?
a) typedef int Boolean;
b) struct {char name[10], int age};
c) typedef enum {Mon, Tue, Wed, Thu, Fri} Workdays;
d) all of the above

Answer: d
Explanation: typedef and struct are used to define user-defined data types.

5. 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! followed by a junk value
d) Hello World! 10000

Answer: a
Explanation: Since y is already defined, redefining it results in an error.

6. C99 standard guarantees uniqueness of __________ characters for internal names.
a) 14
b) 31
c) 63
d) 12

Answer: c
Explanation: ISO C99 compiler may consider only first 63 characters for internal names.

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

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.

8. The format identifier ‘%i’ is also used for _____ data type.
a) float
b) int
c) char
d) double

Answer: b
Explanation: Both %d and %i can be used as a format identifier for int data type.

9. What is the size of an int data type?
a) 8 Bytes
b) 4 Bytes
c) Depends on the system/compiler
d) Cannot be determined

Answer: c
Explanation: The size of the data types depend on the system.

10. 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) Compile time error
c) Hello World!
d) Hello World! followed by a junk value

Answer: b
Explanation: It results in an error since x is used without declaring the variable x.