Create New Post

C MCQs - 2

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

    int i = 5;
    printf("%d\n", ++i);
    
    a) 5
    b) 6
    c) Undefined behavior
    d) Compiler error

    Answer: b) 6

  2. Which symbol is used for a single-line comment in C?

    a) //
    b) /* */
    c) #
    d) <!-- -->

    Answer: a) //

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

    int a = 5, b = 10;
    printf("%d\n", a > b ? a : b);
    

    a) 5
    b) 10
    c) Error
    d) Undefined

    Answer: b) 10

  4. In C, what is the function used to read a character from the standard input?

    a) read()
    b) getchar()
    c) scanf()
    d) gets()

    Answer: b) getchar()

  5. What will be the output of the following code?

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

    a) 0 1 2 3 4
    b) 1 2 3 4 5
    c) 0 1 2 3
    d) Infinite loop

    Answer: a) 0 1 2 3 4

  6. Which of the following is NOT a valid data type in C?

    a) double
    b) char*
    c) float
    d) string

    Answer: d) string

  7. What does the scanf() function return?

    a) The value entered by the user
    b) The number of characters read
    c) The number of format specifiers successfully matched and assigned
    d) A pointer to the input buffer

    Answer: c) The number of format specifiers successfully matched and assigned

  8. Which operator is used to access the value stored at a particular address in C?

    a) &
    b) *
    c) ->
    d) ::

    Answer: b) * (asterisk)

  9. In C, which of the following is NOT a valid way to initialize an array?

    a) int arr[] = {1, 2, 3};
    b) int arr[3] = {1, 2};
    c) int arr[3]; arr = {1, 2, 3};
    d) int arr[3]; arr[0] = 1; arr[1] = 2; arr[2] = 3;

    Answer: c) int arr[3]; arr = {1, 2, 3};

  10. What will be the output of the following code?

    #include <stdio.h>
    int main() {
        int arr[] = {1, 2, 3, 4, 5};
        printf("%d\n", *arr + 2);
        return 0;
    }
     

    a) 1
    b) 3
    c) 2
    d) Compiler error

    Answer: b) 3

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

83125