C Programming Language
Q1. Which of the following is a valid C variable name? a) 1variable
b) variable_name
c) variable-name
d) variable name
Answer: b) variable_name
Q2. What is the size of an int data type in C (assuming a 32-bit system)? a) 2 bytes
b) 4 bytes
c) 8 bytes
d) Depends on the compiler
Answer: b) 4 bytes
Q3. Which of the following is used to print formatted output in C? a) scanf()
b) printf()
c) gets()
d) puts()
Answer: b) printf()
Q4. What will be the output of the following code?
c
Copy code
#include <stdio.h>
int main() {
int a = 10;
printf("%d\n", a++);
return 0;
}
a) 10
b) 11
c) Compilation error
d) Undefined behavior
Answer: a) 10
Q5. Which of the following is the correct syntax to declare a pointer in C? a) int ptr;
b) int ptr;
c) int &ptr;
d) int ptr;
Answer: b) int *ptr;
Q6. How do you allocate memory dynamically in C? a) malloc() and free()
b) new and delete
c) allocate() and deallocate()
d) None of the above
Answer: a) malloc() and free()
Q7. What is the output of the following code?
c
Copy code
#include <stdio.h>
int main() {
char str[] = "Hello, World!";
printf("%s\n", str);
return 0;
}
a) Hello, World!
b) Hello
c) World
d) Compilation error
Answer: a) Hello, World!
Q8. Which of the following statements is true for an array in C? a) The size of the array must
be specified at compile time.
b) Arrays can have elements of different data types.
c) Array elements are stored in contiguous memory locations.
d) Array index can start from any integer value.
Answer: c) Array elements are stored in contiguous memory locations.
Q9. Which of the following function is used to compare two strings in C? a) strcmp()
b) strcpy()
c) strcat()
d) strlen()
Answer: a) strcmp()
Q10. What is the correct way to declare a function in C? a) return_type
function_name(parameters);
b) function_name(parameters): return_type;
c) function_name return_type(parameters);
d) return_type: function_name(parameters);
Answer: a) return_type function_name(parameters);
Q11. Which keyword is used to prevent a variable from being modified? a) const
b) volatile
c) static
d) register
Answer: a) const
Q12. What will be the output of the following code?
c
Copy code
#include <stdio.h>
int main() {
int a = 5, b = 10;
int *p = &a, *q = &b;
printf("%d\n", *p + *q);
return 0;
}
a) 15
b) 10
c) 5
d) Compilation error
Answer: a) 15
Q13. Which of the following is a loop construct in C? a) for
b) while
c) do-while
d) All of the above
Answer: d) All of the above
Q14. Which operator is used to access a structure member in C? a) . (dot)
b) -> (arrow)
c) * (asterisk)
d) & (ampersand)
Answer: a) . (dot)
Q15. What is the output of the following code?
c
Copy code
#include <stdio.h>
int main() {
int x = 5;
if (x == 5)
printf("x is 5\n");
else
printf("x is not 5\n");
return 0;
}
a) x is 5
b) x is not 5
c) Compilation error
d) Undefined behavior
Answer: a) x is 5
Q16. Which of the following correctly declares an array of 10 integers in C? a) int arr[10];
b) int arr;
c) int arr[10][10];
d) int arr[10] = {0};
Answer: a) int arr[10];
Q17. What does the following code print?
c
Copy code
#include <stdio.h>
int main() {
int a = 10;
int b = a++;
printf("%d %d\n", a, b);
return 0;
}
a) 10 10
b) 11 10
c) 10 11
d) 11 11
Answer: b) 11 10
Q18. What is the result of the following expression: 10 % 3? a) 0
b) 1
c) 2
d) 3
Answer: b) 1
Q19. Which of the following is not a valid storage class in C? a) auto
b) register
c) mutable
d) static
Answer: c) mutable
Q20. What will be the output of the following code?
c
Copy code
#include <stdio.h>
#define SQUARE(x) (x * x)
int main() {
int a = 4;
printf("%d\n", SQUARE(a + 1));
return 0;
}
a) 25
b) 20
c) 17
d) Compilation error
Answer: b) 20
Q21. Which header file is needed to use the function sqrt() in C? a) math.h
b) stdlib.h
c) stdio.h
d) string.h
Answer: a) math.h
Q22. Which of the following cannot be a structure member? a) int
b) float
c) void
d) char
Answer: c) void
Q23. What is the output of the following code?
c
Copy code
#include <stdio.h>
void func(int *x) {
*x = 20;
}
int main() {
int a = 10;
func(&a);
printf("%d\n", a);
return 0;
}
a) 10
b) 20
c) Compilation error
d) Undefined behavior
Answer: b) 20
Q24. Which of the following functions is used to read a single character from the console in
C? a) printf()
b) scanf()
c) getchar()
d) putchar()
Answer: c) getchar()
Q25. How do you declare a function that does not return a value? a) int functionName();
b) void functionName();
c) float functionName();
d) None of the above
Answer: b) void functionName();
Q26. Which keyword is used to come out of a loop in C? a) exit
b) stop
c) break
d) terminate
Answer: c) break
Q27. What is the correct syntax for a multi-line comment in C? a) // comment //
b) /* comment */
c) # comment #
d) <comment>
Answer: b) /* comment */
Q28. Which function is used to allocate memory in C? a) malloc()
b) allocate()
c) alloc()
d) memalloc()
Answer: a) malloc()
Q29. Which of the following is used to read a formatted input from the console? a) printf()
b) puts()
c) scanf()
d) gets()
Answer: c) scanf()
Q30. What will be the output of the following code?
c
Copy code
#include <stdio.h>
int main() {
int a = 5, b = 2;
printf("%d\n", a / b);
return 0;
}
a) 2
b) 2.5
c) 3
d) 3.5
Answer: a) 2