Output of C programs | Set 34

Ques 1. Assume that the size of an integer is 4 bytes and size of character is 1 byte. What will be the output of following program?

#include <stdio.h>
union test {
    int x;
    char arr[8];
    int y;
} u;
int main()
{
    printf("%u", sizeof(u));
    return 0;
}

options :
A)12
B)16
C)8
D)4

Answer - C

Explanation : In union data type, the memory required to store a union variable is the memory required for the largest element of an union.

Ques 2. What will be the output of following program?

#include <stdio.h>
int main()
{
    int n;
    for (n = 9; n != 0; n--)
        printf("%d", n--);
}

options :
A)9 7 5 3 1
B)9 8 7 6 5 4 3 2 1
C)Infinite loop
D)9 7 5 3

Answer - C

Explanation : The loop will run infinite time because n will never be equal to 0.

Ques 3. What will be the output of following program?


#include <stdio.h>
int main()
{
    int x = 1;
    if (x = 0)
        printf("Geeks");
    else
        printf("Geeksforgeeks");
}

Options :
A)Geeks
B)runtime error
C)Geeksforgeeks
D)compile time error

Answer  - C

Explanation : Here we are assigning(=) and not comparing(==) x with 0 which is not true so the else part will execute and print Geeksforgeeks.

Ques 4.What will be output of following c code?


#include <stdio.h>
int main()
{
    int i = 2, j = 2;
    while (i + 1 ? --i : j++)
        printf("%d", i);
    return 0;
}

Options :
A)1
B)2
C)0
D)No output

Answer : A

Explanation:
Consider the while loop condition:

i + 1 ? -- i : ++j

In first iteration:
i + 1 = 3 (True), So ternary operator will return

 -–i i.e. 1

In C, 1 means true so while condition is true. Hence printf statement will print 1
In second iteration:
i + 1 = 2 (True), So ternary operator will return

-–i i.e. 0

In C, zero means false so while condition is false. Hence program control will come out of the while loop.

Ques 5. Assume that the size of an integer is 4 bytes and size of character is 1 byte. What will be the output of following program?

#include <stdio.h>
struct test {
    int x;
    char arr[8];
    int y;
} u;
int main()
{
    printf("%u", sizeof(u));
    return 0;
}

options :
A)12
B)16
C)8
D)4

Answer - B

Explanation : In structure data type, The amount of memory required to store a structure variable is the sum of memory size of all members.

This article is contributed by Rishabh Jain. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:







Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.