Create New Post

C MCQs - 5

  1. Which statement is used to dynamically allocate memory for an array in C?

    a) new
    b) malloc
    c) allocate
    d) array_alloc

    Answer: b) malloc

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

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

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

    Answer: b) 2

  3. In C, what does the static keyword mean when used with a global variable?

    a) The variable cannot be accessed from outside the file it is defined in
    b) The variable is only accessible within the function it is defined in
    c) The variable retains its value between function calls
    d) The variable cannot be modified after initialization

    Answer: c) The variable retains its value between function calls

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

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

    a) 0 1 2 3 4
    b) 1 2 3 4 5
    c) 1 2 3 4
    d) Compiler error

    Answer: a) 0 1 2 3 4

  5. What does the strcat() function in C do?

    a) Compares two strings
    b) Copies one string to another
    c) Concatenates two strings
    d) Searches for a substring in a string

    Answer: c) Concatenates two strings

  6. Which header file is required to use the strlen() function in C?

    a) <stdio.h>
    b) <stdlib.h>
    c) <string.h>
    d) <ctype.h>

    Answer: c) <string.h>

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

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

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

    Answer: b) 10

  8. What does the putc() function do in C?

    a) Reads a character from a file
    b) Writes a character to a file
    c) Reads a character from the console
    d) Writes a character to the console

    Answer: b) Writes a character to a file

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

    #include <stdio.h>
    int main() {
        printf("Hello, world!\n");
        return 0;
    }
    

    a) Hello, world!
    b) Compiler error
    c) Undefined behavior
    d) No output

    Answer: a) Hello, world!

  10. What is 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);
        return 0;
    }
     

    a) 1, 6
    b) 1, 5
    c) 0, 6
    d) 0, 5

    Answer: b) 1, 5

Comments

Leave a Reply

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

59663