wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Mastering C Programming Concepts

Total questions: 44

Worksheet time: 22mins

Name
Class
Date
1.

1. Which of the following is a valid declaration of an integer variable in C? A) int 1x; B) int x1; C) int x 1; D) int x-1;

a)

int x1_

b)

int x.1

c)

int 1_x

d)

B) int x1;

2.

2. What is the output of the following code? int a = 5, b = 10; printf("%d", a + b); A) 15 B) 510 C) 5 D) 10;

a)

A) 15

b)

B) 510

c)

C) 5

d)

D) 10

3.

3. Which operator is used to access the value at a pointer's address? A) & B) * C) -> D) ;

a)

A) &

b)

C) ->

c)

B) *

d)

D) ;

4.

4. What will be the output of the following code? int x = 10; if (x > 5) { printf("Greater"); } else { printf("Smaller"); } A) Greater B) Smaller C) Error D) None;

a)

Undefined

b)

A) Greater

c)

Lesser

d)

Equal

5.

5. Which of the following loops will always execute at least once? A) for B) while C) do-while D) None of the above;

a)

C) do-while

b)

D) until

c)

B) repeat-until

d)

A) foreach

6.

6. How do you declare a two-dimensional array in C? A) int arr[5][5]; B) int arr(5,5); C) int arr{5,5}; D) int arr<5,5>;

a)

int arr<5][5>;

b)

int arr[5,5];

c)

A) int arr[5][5];

d)

int arr{5;5};

7.

7. What is the correct way to define a function in C? A) void functionName(); B) functionName void(); C) void functionName[]; D) functionName[] void;

a)

A) void functionName();

b)

void functionName()[]

c)

functionName() void;

d)

void[] functionName();

8.

8. Which of the following is a valid pointer declaration? A) int *ptr; B) int ptr*; C) *int ptr; D) ptr int*;

a)

*ptr int;

b)

int ptr*;

c)

ptr *int;

d)

A) int *ptr;

9.

9. What does the malloc function do in C? A) Allocates memory on the stack B) Allocates memory on the heap C) Frees allocated memory D) None of the above;

a)

Deallocates memory from the heap

b)

Allocates memory in the cache

c)

B) Allocates memory on the heap

d)

Reserves memory for global variables

10.

10. Which of the following is NOT a valid storage class in C? A) auto B) register C) static D) dynamic;

a)

G) global

b)

D) dynamic

c)

F) external

d)

E) virtual

11.

11. What is the purpose of the #define preprocessor directive? A) To define a constant B) To include a file C) To create a function D) To declare a variable;

a)

G) To handle exceptions

b)

A) To define a constant

c)

E) To execute a loop

d)

F) To manage memory allocation

12.

12. How can you pass an array to a function in C? A) By value B) By reference C) Both A and B D) None of the above;

a)

By index

b)

By pointer

c)

By size

d)

C) Both A and B

13.

13. What will be the output of the following code? char str[] = "Hello"; printf("%c", str[1]); A) H B) e C) l D) o;

a)

H

b)

l

c)

o

d)

B

14.

14. Which of the following statements is true about unions in C? A) They can hold multiple values at once. B) They can hold only one value at a time. C) They are faster than structures. D) None of the above;

a)

They require more memory than structures.

b)

They can be initialized with multiple values.

c)

B) They can hold only one value at a time.

d)

They can store different data types.

15.

15. What is the correct syntax for opening a file in C? A) fopen(file, mode); B) open(file, mode); C) file_open(file, mode); D) None of the above;

a)

get_file(file, mode);

b)

A) fopen(file, mode);

c)

file_access(file, mode);

d)

read_file(file, mode);

16.

16. Which of the following is a valid command-line argument in C? A) int main(int argc, char *argv[]) B) int main(char *argv[], int argc) C) int main() D) None of the above;

a)

int main(char argc)

b)

int main(int argv[], char argc)

c)

int main(int argc)

d)

A) int main(int argc, char *argv[])

17.

17. What is the output of the following code? int x = 5; printf("%d", ++x); A) 5 B) 6 C) 7 D) Error;

a)

A) 4

b)

D) Compilation Error

c)

B) 6

d)

C) 8

18.

18. Which of the following is a feature of recursion? A) It must have a base case. B) It cannot call itself. C) It is always faster than iteration. D) None of the above;

a)

It can only call itself once.

b)

It requires multiple base cases.

c)

A) It must have a base case.

d)

It is less efficient than loops.

19.

19. What is the purpose of the free function in C? A) To allocate memory B) To deallocate memory C) To initialize memory D) To check memory usage;

a)

C) To clear memory

b)

A) To reserve memory

c)

B) To deallocate memory

d)

D) To monitor memory usage

20.

20. Which of the following is true about strings in C? A) They are arrays of characters. B) They are null-terminated. C) Both A and B D) None of the above;

a)

C

b)

They are immutable.

c)

They are fixed-length arrays.

d)

They can contain integers.

21.

21. What is the correct way to declare a constant in C? A) const int x = 10; B) int const x = 10; C) #define x 10; D) All of the above;

a)

C) #define x 10;

b)

A) const int x = 10;

c)

D) All of the above;

d)

B) int const x = 10;

22.

22. Which of the following is a valid way to define a structure in C? A) struct { int x; }; B) struct point { int x; int y; }; C) struct point { int x, y; }; D) Both B and C;

a)

A) struct { int x; };

b)

B) struct point { int x; int y; };

c)

D) Both B and C;

d)

C) struct point { int x, y; };

23.

23. What will be the output of the following code? int arr[] = {1, 2, 3}; printf("%d", arr[2]); A) 1 B) 2 C) 3 D) Error;

a)

A) 1

b)

B) 2

c)

C) 3

d)

D) Error

24.

24. What is the output of the following code? int a = 10; int b = 20; printf("%d", a * b); A) 200 B) 30 C) 10 D) 20;

a)

A) 200

b)

B) 30

c)

C) 10

d)

D) 20

25.

25. Which of the following is a valid way to declare a pointer to a function in C? A) int (*ptr)(); B) int ptr(); C) int *ptr(); D) None of the above;

a)

A) int (*ptr)();

b)

B) int ptr();

c)

D) None of the above;

d)

C) int *ptr();

26.

26. What is the correct way to initialize a pointer in C? A) int *ptr = NULL; B) int ptr = *NULL; C) int *ptr = 0; D) Both A and C;

a)

A) int *ptr = NULL;

b)

B) int ptr = *NULL;

c)

D) Both A and C;

d)

C) int *ptr = 0;

27.

27. Which of the following is the correct syntax for a for loop in C? A) for (int i = 0; i < 10; i++) B) for int i = 0; i < 10; i++ C) for (i = 0; i < 10; i++) D) for (int i; i < 10; i++);

a)

A) for (int i = 0; i < 10; i++)

b)

B) for int i = 0; i < 10; i++

c)

C) for (i = 0; i < 10; i++)

d)

D) for (int i; i < 10; i++);

28.

28. What is the output of the following code? int x = 3; int y = 4; printf("%d", x + y * 2); A) 11 B) 14 C) 10 D) 7;

a)

C) 10

b)

A) 11

c)

B) 14

d)

D) 7

29.

29. Which of the following statements is true about the sizeof operator in C? A) It returns the size of a variable in bytes. B) It can be used with any data type. C) Both A and B D) None of the above;

a)

D) None of the above

b)

B) It can be used with any data type.

c)

C) Both A and B

d)

A) It returns the size of a variable in bytes.

30.

30. Which of the following is a correct way to declare a constant in C? A) const int x = 10; B) int const x = 10; C) Both A and B D) None of the above;

a)

A) const int x = 10;

b)

B) int const x = 10;

c)

C) Both A and B

d)

D) None of the above

31.

31. What is the purpose of the 'return' statement in a function? A) To exit the function B) To return a value to the caller C) Both A and B D) None of the above;

a)

B) To return a value to the caller

b)

A) To exit the function

c)

D) None of the above

d)

C) Both A and B

32.

32. Which of the following is a valid way to define a macro in C? A) #define PI 3.14 B) #define PI(x) (3.14 * (x)) C) Both A and B D) None of the above;

a)

C) Both A and B

b)

B) #define PI(x) (3.14 * (x))

c)

A) #define PI 3.14

d)

D) None of the above

33.

25. Which of the following is the correct syntax for declaring a structure in C? A) struct { int x; }; B) struct myStruct { int x; }; C) Both A and B D) None of the above;

a)

D) None of the above

b)

C) Both A and B

c)

B) struct myStruct { int x; };

d)

A) struct { int x; };

34.

15. What will be the output of the following code? int a = 2; int b = 3; printf("%d", a + b * 3); A) 11 B) 9 C) 5 D) 6;

a)

D) 6

b)

C) 5

c)

B) 9

d)

A) 11

35.

22. Which of the following is a valid way to declare a function pointer in C? A) int (*ptr)(); B) int ptr*(); C) *int ptr(); D) ptr int*();

a)

D) ptr int*();

b)

A) int (*ptr)();

c)

B) int ptr*();

d)

C) *int ptr();

36.

30. Which of the following is the correct way to declare a function that takes two integers and returns their sum? A) int sum(int a, int b); B) void sum(int a, int b); C) int sum(a, b); D) int sum(int, int);

a)

A) int sum(int a, int b);

b)

D) int sum(int, int);

c)

B) void sum(int a, int b);

d)

C) int sum(a, b);

37.

24. What is the output of the following code? int a = 2, b = 3; printf("%d", a * b + b); A) 9 B) 8 C) 6 D) 5;

a)

A) 9

b)

B) 8

c)

C) 6

d)

D) 5

38.

25. Which of the following statements is true about arrays in C? A) They can hold different data types. B) They are fixed in size once declared. C) They can be resized dynamically. D) None of the above;

a)

A) They can hold different data types.

b)

C) They can be resized dynamically.

c)

B) They are fixed in size once declared.

d)

D) None of the above

39.

11. Which of the following is a valid way to declare a structure in C? A) struct { int x; }; B) struct myStruct { int x; }; C) Both A and B D) None of the above;

a)

B) struct myStruct { int x; };

b)

C) Both A and B

c)

D) None of the above

d)

A) struct { int x; };

40.

20. What is the purpose of the 'sizeof' operator in C? A) To determine the size of a variable B) To allocate memory C) To deallocate memory D) To initialize a variable;

a)

A) To determine the size of a variable

b)

B) To allocate memory

c)

C) To deallocate memory

d)

D) To initialize a variable

41.

27. Which of the following statements is true about pointers in C? A) Pointers can be incremented. B) Pointers can only point to integers. C) Pointers cannot be null. D) None of the above;

a)

B) Pointers can only point to integers.

b)

A) Pointers can be incremented.

c)

D) None of the above

d)

C) Pointers cannot be null.

42.

30. What will be the output of the following code? int x = 4; int y = 5; printf("%d", x * y + y); A) 25 B) 20 C) 24 D) 30;

a)

A) 25

b)

B) 20

c)

C) 24

d)

D) 30

43.

31. Which of the following is a valid way to define a function in C? A) void myFunction() {} B) function myFunction() {} C) myFunction() void {} D) None of the above;

a)

D) None of the above

b)

C) myFunction() void {}

c)

B) function myFunction() {}

d)

A) void myFunction() {}

44.

32. What is the output of the following code? int a = 1, b = 2; printf("%d", a == b); A) 1 B) 0 C) Error D) None of the above;

a)

B) 0

b)

A) 1

c)

D) None of the above

d)

C) Error