A variable in C programming is a storage location that is assigned a name. It can store data values.
2. Which of the following is a valid variable name in C?
a) 1number
b) _number
c) number1
d) number-1
Answer:
c) number1
Explanation:
Variable names in C can start with an underscore or a letter and can include numbers but cannot start with a number or include special characters like '-'.
3. How do you declare an integer variable named 'age' in C?
a) int age;
b) integer age;
c) age int;
d) declare age as integer;
Answer:
a) int age;
Explanation:
In C, an integer variable is declared with the keyword 'int' followed by the variable name, such as 'int age;'.
4. What will be the initial value of an uninitialized integer variable in C?
a) 0
b) A random number
c) 1
d) Null
Answer:
b) A random number
Explanation:
Uninitialized variables in C have an undefined value, often a random number, based on whatever is in the memory location.
5. How do you initialize a float variable 'temp' with the value 23.5 in C?
a) float temp = 23.5;
b) float 23.5 = temp;
c) float temp = 23.5f;
d) initialize temp with 23.5;
Answer:
c) float temp = 23.5f;
Explanation:
In C, float literals should be suffixed with 'f' or 'F' to distinguish them from double. So, 'float temp = 23.5f;' is correct.
6. Which of the following is the correct way to declare a character variable with the value 'A'?
a) char letter = 'A';
b) char letter = "A";
c) character letter = 'A';
d) letter = 'A';
Answer:
a) char letter = 'A';
Explanation:
A character variable is declared with the 'char' keyword and initialized with a single character enclosed in single quotes.
7. What is the correct syntax to declare a constant integer variable 'MAX' with value 100 in C?
a) constant int MAX = 100;
b) const int MAX = 100;
c) int constant MAX = 100;
d) #define MAX 100
Answer:
b) const int MAX = 100;
Explanation:
The 'const' keyword is used to declare a constant variable in C. Alternatively, '#define' could also be used as a preprocessor directive.
8. Which of the following is used to define a symbolic constant in C?
a) const
b) define
c) #define
d) #const
Answer:
c) #define
Explanation:
'#define' is a preprocessor directive used to define symbolic constants in C.
9. What is the default value of a global integer variable in C?
a) 0
b) 1
c) A random number
d) Null
Answer:
a) 0
Explanation:
Global and static variables in C are automatically initialized to zero.
10. What data type would you use to store a person's first name in C?
a) int
b) float
c) char array
d) bool
Answer:
c) char array
Explanation:
A string, like a person's first name, is stored in a char array in C.
11. How do you declare an array of 10 integers in C?
a) int array[10];
b) array int[10];
c) int[10] array;
d) array[10] int;
Answer:
a) int array[10];
Explanation:
Arrays in C are declared with the data type, followed by the array name and size in square brackets.
12. Which of the following operators is used to access the value at a specific address in a pointer?
a) &
b) *
c) ->
d) ::
Answer:
b) *
Explanation:
The '*' operator is used to dereference a pointer, i.e., to access the value at the memory address stored in the pointer.
13. What is the output of the following C code: int x = 10; printf("%d", x);
a) 10
b) x
c) %d
d) Error
Answer:
a) 10
Explanation:
The printf function prints the value of the 'x' variable, which is 10.
14. What is the purpose of the 'sizeof' operator in C?
a) To check the size of a variable
b) To allocate memory
c) To compare two values
d) To find the length of a string
Answer:
a) To check the size of a variable
Explanation:
The 'sizeof' operator is used to determine the size, in bytes, of a variable or data type.
15. What is the correct way to assign a new value to an existing variable in C?
a) int x;
b) int x;
c) x = 5;
d) assign x = 5;
Answer:
a) int x; x = 5;
Explanation:
In C, a variable is assigned a value using the '=' operator after declaring it.
16. What is the scope of a local variable in C?
a) Throughout the program
b) Only within the function it is declared
c) Throughout the file
d) Global
Answer:
b) Only within the function it is declared
Explanation:
A local variable in C is only accessible within the function in which it is declared.
17. Which of the following is not a valid data type in C?
a) int
b) string
c) float
d) double
Answer:
b) string
Explanation:
C does not have a native 'string' data type. Strings are represented as arrays of characters.
18. What does the following declaration mean? int *ptr;
a) ptr is an integer
b) ptr is a pointer to an integer
c) ptr is a function returning an integer
d) ptr is an array of integers
Answer:
b) ptr is a pointer to an integer
Explanation:
In C, the '*' symbol is used in the declaration to indicate that the variable is a pointer to the specified data type.
19. What is the result of the following expression in C: 5 + 3 * 2?
a) 16
b) 11
c) 13
d) 8
Answer:
b) 11
Explanation:
According to the order of operations in C, multiplication is performed before addition, so the expression evaluates to 5 + (3 * 2) = 11.
20. What is the purpose of the 'return' statement in a C function?
a) To exit the program
b) To return control to the calling function
c) To declare a variable
d) To print a value
Answer:
b) To return control to the calling function
Explanation:
The 'return' statement in a C function is used to return control to the calling function, optionally returning a value.
Comments
Post a Comment