Create New Post

C MCQs - 9

  1. What will be the output of the following code snippet?

    #include <stdio.h>
    int main() {
        int x = 0;
        while (x < 5) {
            printf("%d ", x++);
        }
        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

  2. In C, which function is used to read a line from a file?

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

    Answer: a) fgets()

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

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

    a) 10.000000
    b) 10
    c) Compiler error
    d) Undefined behavior

    Answer: a) 10.000000

  4. Which of the following functions is used to find the square root of a number in C?

    a) sqrt()
    b) pow()
    c) sqr()
    d) root()

    Answer: a) sqrt()

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

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

    a) Hello
    b) lo
    c) ello
    d) Compiler error

    Answer: b) lo

  6. Which operator is used to access the member of a structure in C?

    a) . (dot)
    b) -> (arrow)
    c) : (colon)
    d) :: (double colon)

    Answer: a) . (dot)

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

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

    a) 5
    b) 10
    c) 15
    d) Compiler error

    Answer: b) 10

  8. Which of the following is NOT a valid way to declare a structure variable in C?

    a) struct { int x; } s;
    b) struct s { int x; };
    c) struct s { int x; } s;
    d) struct s { int x; } s = {5};

    Answer: a) struct { int x; } s;

  9. What will be the output of the following code snippet?

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

    a) 1 3 5
    b) 1 2 3 4
    c) 2 4 6
    d) 1 2 3

    Answer: b) 1 2 3 4

  10. Which function is used to convert a floating-point number to a string in C?

    a) atof()
    b) ftoa()
    c) dtoa()
    d) float_to_string()

    Answer: c) dtoa()

Comments

Leave a Reply

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

75718