Find Output of C Programs Questions with Answers

This collection of 35 C programming MCQs is designed to test and enhance your understanding of the C programming language through a variety of code snippets. Each question presents a brief C program followed by four possible outputs. Your task is to determine the correct output of the program. These questions cover a wide range of topics, including pointers, arrays, recursion, dynamic memory management, bitwise operations, string manipulation, control structures, and the use of static and dynamic variables.

The questions range from basic to advanced levels, challenging both beginners and experienced programmers alike. Some programs incorporate common programming constructs, while others include tricky scenarios that require a deeper understanding of how C handles memory, data types, and operations. This set is an excellent tool for preparing for programming interviews and exams and enhancing overall comprehension of C programming.

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

#include <stdio.h>
int main() {
    printf("%d", printf("Hello World!"));
    return 0;
}
a) Hello World!11
b) 11Hello World!
c) Hello World!
d) 11

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

#include <stdio.h>
int main() {
    int x = 10, y = 20;
    x = x + y - (y = x);
    printf("%d %d", x, y);
    return 0;
}
a) 20 10
b) 10 20
c) 20 20
d) 10 10

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

#include <stdio.h>
int main() {
    int a = 5;
    a = a - 2;
    printf("%d", a);
    return 0;
}
a) 3
b) 5
c) 2
d) 0

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

#include <stdio.h>
int main() {
    int num = 1;
    if (num = 0)
    printf("The number is 0");
    else
    printf("The number is not 0");
    return 0;
}
a) The number is 0
b) The number is not 0
c) Compilation error
d) No output

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

#include <stdio.h>
int main() {
    int i = 0;
    for (i = 0; i < 5; i++);
    printf("%d ", i);
    return 0;
}
a) 0
b) 1
c) 5
d) 4

6. What will be the output of the following C code?

#include <stdio.h>
int main() {
    char *ptr = "Hello World";
    printf("%c", *&*ptr);
    return 0;
}
a) H
b) e
c) Hello World
d) Error

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

#include <stdio.h>
int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int *ptr = arr;
    printf("%d", *++ptr);
    return 0;
}
a) 10
b) 20
c) 30
d) 40

8. What will be the output of the following C code?

#include <stdio.h>
int main() {
    printf("%d", sizeof("Hello"));
    return 0;
}
a) 5
b) 6
c) 4
d) 7

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

#include <stdio.h>
int main() {
    int x = 10;
    int y = x++;
    printf("%d %d", x, y);
    return 0;
}
a) 10 11
b) 11 10
c) 11 11
d) 10 10

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

#include <stdio.h>
void func(int x) {
    x = x + 2;
}
int main() {
    int x = 5;
    func(x);
    printf("%d", x);
    return 0;
}
a) 5
b) 7
c) 2
d) Compilation error

11. What will be the output of the following C code?

#include <stdio.h>
int main() {
    int x = 0, y = 0;
    while (++x < 5) y++;
    printf("%d %d", x, y);
    return 0;
}
a) 5 4
b) 5 5
c) 4 5
d) 4 4

12. What will be the output of the following C code?

#include <stdio.h>
int main() {
    int x = 5;
    int y = x = 0;
    printf("%d %d", x, y);
    return 0;
}
a) 0 0
b) 5 0
c) 5 5
d) 0 5

13. What will be the output of the following C code?

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

14. What will be the output of the following C code?

#include <stdio.h>
int main() {
    int i = 1;
    switch (i) {
        case 1:
        printf("One ");
        case 2:
        printf("Two ");
        default:
        printf("Default ");
    }
    return 0;
}
a) One
b) One Two
c) One Two Default
d) Two Default

15. What will be the output of the following C code that uses multidimensional arrays and loops?

#include <stdio.h>
int main() {
    int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", arr[i][j]);
        }
    }
    return 0;
}
a) 1 2 3 4 5 6
b) 1 4 2 5 3 6
c) 1 2 3
d) 4 5 6

16. What will be the output of the following C code?

#include <stdio.h>
int main() {
    char c = 255;
    int i = c;
    printf("%d", i);
    return 0;
}
a) 255
b) -1
c) 1
d) Error

17. What will be the output of the following C code?

#include <stdio.h>
int main() {
    int i = 10;
    do {
        printf("%d ", i);
        i++;
    } while (i < 10);
    return 0;
}
a) 10
b) 11
c) No output
d) Infinite loop

18. What will be the output of the following C code?

#include <stdio.h>
int main() {
    int a = 10, b = 20;
    const int *ptr = &a;
    ptr = &b;
    printf("%d", *ptr);
    return 0;
}
a) 10
b) 20
c) Compilation error
d) Undefined behavior

19. What will be the output of the following C code?

#include <stdio.h>
int main() {
    int arr[3] = {0, 1, 2};
    printf("%d", *(arr + 1));
    return 0;
}
a) 0
b) 1
c) 2
d) Error

20. What will be the output of the following C code?

#include <stdio.h>
int main() {
    int a = 5, b = 10, max;
    max = (a > b) ? a : b;
    printf("%d", max);
    return 0;
}
a) 5
b) 10
c) 0
d) Compilation error

21. What will be the output of the following C code?

#include <stdio.h>
int main() {
    int x = 1, y = 2, z = 3;
    printf("%d", x < y < z);
    return 0;
}
a) 1
b) 0
c) 2
d) 3

22. What will be the output of the following C code?

#include <stdio.h>
int main() {
    int nums[] = {5, 10, 15, 20, 25};
    int *ptr = nums + 2;
    printf("%d", *ptr);
    return 0;
}
a) 5
b) 10
c) 15
d) 20

23. What will be the output of the following C code?

#include <stdio.h>
int main() {
    for (int i = 0; i < 5; i++) {
        if (i == 3) continue;
        printf("%d ", i);
    }
    return 0;
}
a) 0 1 2 4
b) 0 1 2 3 4
c) 0 1 2 3
d) 0 1 2

24. What will be the output of the following C code?

#include <stdio.h>
int main() {
    int x = 10, y = 15;
    printf("%d", x & y);
    return 0;
}
a) 10
b) 5
c) 2
d) 0

25. What will be the output of the following C code?

#include <stdio.h>
int main() {
    int a = -10;
    unsigned int b = 6;
    printf("%d", a + b);
    return 0;
}
a) -4
b) 4
c) 4294967286
d) -4294967296

26. What will be the output of the following C code involving pointers and arrays?

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

27. What will be the output of the following C code using recursion?

#include <stdio.h>
int func(int n) {
    if (n == 0)
    return 1;
    else
    return n * func(n - 1);
}
int main() {
    printf("%d", func(5));
    return 0;
}
a) 120
b) 24
c) 1
d) 5

28. What will be the output of the following C code involving dynamic memory allocation?

#include <stdio.h>
#include <stdlib.h>
int main() {
    int *arr = malloc(5 * sizeof(int));
    for (int i = 0; i < 5; i++) {
        arr[i] = i;
    }
    printf("%d", arr[4]);
    free(arr);
    return 0;
}
a) 4
b) 0
c) 5
d) Random garbage value

29. What will be the output of the following C code that uses pointer arithmetic?

#include <stdio.h>
int main() {
    int nums[] = {10, 20, 30, 40, 50};
    int *ptr = nums + 3;
    printf("%d\n", *ptr);
    return 0;
}
a) 10
b) 20
c) 40
d) 30

30. What will be the output of the following C code using bitwise XOR?

#include <stdio.h>
int main() {
    int a = 4;  // Binary 0100
    int b = 5;  // Binary 0101
    printf("%d", a ^ b);
    return 0;
}
a) 1
b) 9
c) 0
d) 8

31. What will be the output of the following C code that manipulates pointers and structs?

    #include <stdio.h>
    typedef struct {
       int x;
       int y;
    } Point;
    int main() {
        Point p = {10, 20};
        Point *ptr = &p;
        printf("%d ", ptr->x);
        ptr->x = 30;
        printf("%d", ptr->x);
        return 0;
    }
a) 10 20
b) 10 30
c) 30 30
d) 30 20

32. What will be the output of the following C code involving pointer to a pointer?

#include <stdio.h>
int main() {
    int val = 5;
    int *ptr = &val;
    int **pptr = &ptr;
    printf("%d", **pptr);
    return 0;
}
a) 5
b) 0
c) Random garbage value
d) Address of val

33. What will be the output of the following C code using conditional operator?

#include <stdio.h>
int main() {
    int a = 10, b = 20;
    int result = a > b ? a : b;
    printf("%d", result);
    return 0;
}
a) 10
b) 20
c) 30
d) 0

34. What will be the output of the following C code using string manipulation?

#include <stdio.h>
#include <string.h>
int main() {
    char str[] = "hello";
    char rev[6];
    strcpy(rev, str);
    for (int i = 0; i < strlen(str)/2; i++) {
        char temp = rev[i];
        rev[i] = rev[strlen(str)-i-1];
        rev[strlen(str)-i-1] = temp;
    }
    printf("%s", rev);
    return 0;
}
a) hello
b) olleh
c) ehll
d) loleh

35. What will be the output of the following C code using a static variable in a function?

#include <stdio.h>
void func() {
    static int count = 0;
    count++;
    printf("%d ", count);
}
int main() {
    func();
    func();
    func();
    return 0;
}
a) 1 1 1
b) 1 2 3
c) 0 1 2
d) 2 3 4

Comments