Create New Post

C MCQs - 7

  1. What is the output of the following code?

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

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

    Answer: c) 3

  2. Which function is used to find the largest integer not greater than x in C?

    a) ceil()
    b) floor()
    c) round()
    d) trunc()

    Answer: b) floor()

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

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

    a) 5, 6, 10
    b) 5, 6, 11
    c) 5, 5, 10
    d) 5, 5, 11

    Answer: a) 5, 6, 10

  4. Which of the following is used to access the last element of an array in C?

    a) arr[length - 1]
    b) arr[length]
    c) arr[length + 1]
    d) arr[-1]

    Answer: a) arr[length - 1]

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

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

    a) H
    b) e
    c) l
    d) \0

    Answer: d) \0

  6. Which of the following is the correct syntax for declaring a structure in C?

    a) struct { int x; };
    b) structure { int x; };
    c) struct x { int x; };
    d) struct { int x; } x;

    Answer: c) struct x { int x; };

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

    #include <stdio.h>
    int main() {
        int x = 5, y = 10;
        if (x > y)
            printf("x is greater than y\n");
        else if (x < y)
            printf("x is less than y\n");
        else
            printf("x is equal to y\n");
        return 0;
    }
    

    a) x is greater than y
    b) x is less than y
    c) x is equal to y
    d) No output

    Answer: b) x is less than y

  8. Which function is used to convert a string to an integer in C?

    a) atoi()
    b) itoa()
    c) atoi()
    d) strtoi()

    Answer: a) atoi()

  9. What does the strchr() function in C do?

    a) Finds the first occurrence of a character in a string
    b) Compares two strings
    c) Copies one string to another
    d) Concatenates two strings

    Answer: a) Finds the first occurrence of a character in a string

  10. What is the output of the following code?

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

    a) 5
    b) 10
    c) 20
    d) 40

    Answer: c) 20

Comments

Leave a Reply

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

40815