C Quizzes

 Here is a 15 minutes long C language Quiz, try to use a timer and answer the questions, when you finish you will find the answers at the bottom of the page.

  1. What is the correct syntax for declaring a variable in C?

            a. variable_name;

            b. int variable_name;

            c. variable_name = int;

            d. declare variable_name;

        

        2. How do you initialize an array in C with the values {1, 2, 3}?

            a. int arr[] = {1, 2, 3};

            b. array arr = {1, 2, 3};

            c. int arr(1, 2, 3);

            d. arr[] = {1, 2, 3};


        3. What is the purpose of the sizeof operator in C?

            a. Returns the size of a data type or object

            b. Finds the square root of a number

            c. Calculates the remainder of a division

            d. Concatenates two strings


        4. How do you allocate dynamic memory in C?

            a. alloc()

            b. malloc()

            c. new()

            d. allocate()


        5. What is the purpose of the break statement in a switch statement?

            a. Exits the switch statement

            b. Skips the current iteration of a loop

            c. Jumps to the next case in the switch statement

            d. Terminates the program


        6. In C, what is the purpose of the typedef keyword?

            a. Declares a new variable

            b. Defines a new data type

            c. Allocates memory for a variable

            d. Includes a header file


        7. What is the output of the following code snippet?


    #include <stdio.h>
    int main() {
        int x = 5;
        printf("%d\n", x++);
        printf("%d\n", ++x);
        return 0;
    }

            a. 5, 6

            b. 6, 7

            c. 5, 7

            d. 6, 6


        8. How do you open a file named "example.txt" in C for writing?

            a. FILE *fp = open("example.txt", "w");

            b. FILE *fp = fopen("example.txt", "write");

            c. FILE *fp = fopen("example.txt", "w");

            d. open("example.txt", "w");


        9. What is the purpose of the #define directive in C?

            a. Declares a constant

            b. Declares a function

            c. Includes a header file

            d. Allocates memory for a variable


        10. What is the difference between ++i and i++ in C?

            a. Both increment the variable, but ++i returns the original value.

            b. Both increment the variable, but i++ returns the updated value.

            c. ++i increments the variable after its current value is used.

            d. i++ increments the variable before its current value is used.


        11. In C, what is the purpose of the const keyword?

            a. Declares a constant variable

            b. Declares a variable with a constant value

            c. Declares a variable that cannot be modified

            d. All of the above


        12. What is the output of the following code snippet?


    #include <stdio.h>
    int main() {
        char str[] = "Hello";
        printf("%c\n", *str);
        return 0;
    }

            a. H

            b. e

            c. H e l l o

            d. 72


          13. How do you close a file in C after opening it with fopen?

            a. close(fp);

            b. fclose(fp);

            c. file_close(fp);

            d. close_file(fp);


        14. What is the purpose of the strcmp function in C?

            a. Compares two strings

            b. Concatenates two strings

            c. Copies one string to another

            d. Finds the length of a string


        15. What is the difference between printf and scanf in C?

            a. printf is used for output, and scanf is used for input.

            b. printf is used for input, and scanf is used for output.

            c. Both are used for input.

            d. Both are used for output.


Answers:

1. b,

2. a,

3. a,

4. b,

5. a,

6. b,

7. b,

8. c,

9. a,

10. c,

11. d,

12. a,

13. b,

14. a,

15. a

Post a Comment

You're welcome to share your ideas with us in comments.