Showing 1–10 of 60 questions
in C Programming
Which header file must be included to use the printf() function in C?
A
#include
B
#include
C
#include
D
#include
Correct Answer:
B. #include
EXPLANATION
The printf() function is defined in the Standard Input/Output library. Therefore, '#include <stdio.h>' must be included at the beginning of the program to use printf() and other I/O functions like scanf(), getchar(), putchar(), etc.
What will be the value of 'x' after execution of the following code? int x = 10; x += 5; x *= 2;
EXPLANATION
Step 1: x = 10 initially. Step 2: x += 5 means x = x + 5 = 10 + 5 = 15. Step 3: x *= 2 means x = x * 2 = 15 * 2 = 30. Therefore, x = 30.
What is the purpose of the strlen() function in C?
A
To compare two strings
B
To find the length of a string
C
To copy one string to another
D
To concatenate two strings
Correct Answer:
B. To find the length of a string
EXPLANATION
The strlen() function returns the number of characters in a string, excluding the null terminator ('\0').
What is the output of the following C code?
int main() {
int a = 10;
printf("%d", a += 5);
return 0;
}
A
10
B
15
C
5
D
Compilation Error
EXPLANATION
The += operator adds 5 to a (a = a + 5 = 10 + 5 = 15), and printf prints the updated value 15.
In the expression: int arr[3][3]; arr[1][2] = 5; What is being accessed?
A
Element in row 1, column 2
B
Element in row 2, column 3
C
Element in row 2, column 1
D
Invalid access - syntax error
Correct Answer:
A. Element in row 1, column 2
EXPLANATION
In C, array indices are 0-based. arr[1][2] refers to the element in the second row (index 1) and third column (index 2) of the 2D array.
Which function is used to read a single character from standard input?
A
scanf()
B
getchar()
C
gets()
D
fgetc()
Correct Answer:
B. getchar()
EXPLANATION
getchar() reads a single character from standard input (stdin). While fgetc() can also read a character, getchar() is the standard dedicated function for this purpose.
What is the correct way to declare a pointer to an integer?
A
int p*;
B
int *p;
C
*int p;
D
pointer int p;
Correct Answer:
B. int *p;
EXPLANATION
The correct syntax is 'int *p;' where int is the data type, * indicates pointer, and p is the pointer variable name.
What keyword is used to create a pointer variable in C?
EXPLANATION
The asterisk (*) symbol is used to declare pointer variables. For example: int *ptr; declares a pointer to an integer.
What will be the output of: printf("%d", 5 + 3 * 2);?
EXPLANATION
Operator precedence: multiplication (*) is performed before addition (+). So: 3 * 2 = 6, then 5 + 6 = 11.
Which of the following is NOT a valid variable name in C?
A
_myVar
B
myVar123
C
123myVar
D
my_var
Correct Answer:
C. 123myVar
EXPLANATION
Variable names in C cannot start with a digit. They must begin with a letter (a-z, A-Z) or underscore (_). '123myVar' is invalid.