0% found this document useful (0 votes)
30K views

What Is The Output of This C Code

The document contains multiple code snippets in C language and their corresponding output options. The code snippets cover various arithmetic, logical, relational and bitwise operators in C. Some key concepts tested include operator precedence, type conversions during expressions, side effects in expressions, short circuit evaluation, precedence of operators etc. The correct outputs are chosen from the given options for each code snippet based on the rules and semantics of C language.

Uploaded by

arunkumar_a8131
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30K views

What Is The Output of This C Code

The document contains multiple code snippets in C language and their corresponding output options. The code snippets cover various arithmetic, logical, relational and bitwise operators in C. Some key concepts tested include operator precedence, type conversions during expressions, side effects in expressions, short circuit evaluation, precedence of operators etc. The correct outputs are chosen from the given options for each code snippet based on the rules and semantics of C language.

Uploaded by

arunkumar_a8131
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

What is the output of this C code?

    int main()
    {
        int i = -5;
        int k = i %4;//-5%4->-1
        printf("%d\n", k);
    }

 A. Compile time error

 B. -1

 C. 1

 D. None

2.
What is the output of this C code?

    int main()
    {
        int i = 5;
        int l = i / -4; // 5/-4 -> -1
        int k = i % -4; // 5 % -4->1
        printf("%d %d\n", l, k);
        return 0;
    }

 A. Compile time error

 B. -1  1

 C. 1  -1

 D. Run time error

3. What is the output of this C code?

    int main()
    {
        int i = 7;
        i = i / 4;//7/4 -> 1
        printf("%d\n", i);
       return 0;
    }

 A. Run time error

 B. 1
 C. 3

 D. Compile time error

4. What is the value of x in this C code?

    void main()
    {
        int x = 4 *5 / 2 + 9;//20/2+9//10+9->19
    }

 A. 6.75

 B. 1.85

 C. 19

 D. 3

5. What is the output of this C code?

    void main()
    {
        int x = 4.3 % 2;
        printf("Value of x is %d", x);
    }

 A. Value of x is 1.3

 B. Value of x is 2

 C. Value of x is 0.3

 D. Compile time error

6.
What is the output of this C code?

    void main()
    {
        int y = 3;
        int x = 7 % 4 * 3 / 2;//3*3/2->9/2->4
        printf("Value of x is %d", x);
    }

 A. Value of x is 1

 B. Value of x is 2

 C. Value of x is 4

 D. Compile time error


8. The precedence of arithmetic operators is (from highest to lowest)?

 A. %, *, /, +, -

 B. %, +, /, *, -

 C. +, -, %, *, /

 D. %, +, -, *, /

9. Which of the following is not an arithmetic operation?

 A. a *= 20;

 B. a /= 30;

 C. a %= 40;

 D. a != 50;

10. Which of the following data type will throw an error on modulus operation(%)?

 A. char

 B. short

 C. float

 D. int

11. What is the output of this C code?

    int main()
    {
        int a = 20;
        double b = 15.6;
        int c;
        c = a + b;
        printf("%d", c);
    }

 A. 35(c)

 B. 36

 C. 35.6

 D. 30

12. What is the output of this C code?

    int main()
    {
        int a = 20, b = 15, c = 5;
        int d;
        d = a == (b + c);
        printf("%d", d);
    }

 A. 1

 B. 40

 C. 10

 D. 5

13.
What is the output of this C code?

    void main()
    {
        int x = 0;
        if (x = 0)
            printf("Its zero\n");
        else
            printf("Its not zero\n");
    }

 A. Its not zero(C)

 B. Its zero

 C. Run time error

 D. None

14. What is the output of this C code?

    void main()
    {
        int k = 8;
        int x = 0 == 1 && k++;
        printf("%d%d\n", x, k);
    }

 A. 0   9

 B. 0   8(c)

 C. 1   9

 D. 1   8

15. What is the output of this C code?


    void main()
    {
        char a = 'a';
        int x = (a % 10)++;
        printf("%d\n", x);
    }

 A. 6

 B. Junk value

 C. Compile time error(c)

 D. 7

16. What is the output of this C code?

    void main()

    {
        1 < 2 ? return 1: return 2;
    }

 A. returns 1

 B. returns 2

 C. varies

 D. Compile time error(c)

19. What is the output of this C code?

    int main()
    {
        int x = 2, y = 2;
        x /= x / y;
        printf("%d\n", x);
        return 0;
    }

 A. 2(c)

 B. 1

 C. 0.5

 D. Undefined behaviour

21. What is the type of the below assignment expression if x is of type float, y is of type
int?
        y = x + y;

 A. Int(c)
 B. float

 C. There is no type for an assignment expression

 D. double

22. What is the value of the below assignment expression


      (x = foo())!= 1 considering foo() returns 2

 A. 2

 B. true

 C. 1(c)

 D. 0

24. for c = 2, value of c after c <<= 1;

 A. c = 1;

 B. c = 2;

 C. c = 3;

 D. c = 4;(c)

25. What is the output of this C code?

    int main()
    {
        int a = 1, b = 2;
        a += b -= a;
        printf("%d %d", a, b);
    }

 A. 1   1

 B. 1   2

 C. 2   1

 D. 2   2

26.
What is the output of this C code?

    int main()
    {
        int a = 4, n, i, result = 0;
        scanf("%d", &n);
        for (i = 0;i < n; i++)
        result += a;
    }

 A. Addition of a and n.

 B. Subtraction of a and n.

 C. Multiplication of a and n.(c)

 D. Division of a and n.

27. Which of the following is an invalid assignment operator?

 A. a %= 10;

 B. a /= 10;

 C. a |= 10;

 D. None of the mentioned(c)

28. What is the output of this C code?

    int main()
    {
        int c = 2 ^ 3;
        printf("%d\n", c);
    }

 A. 1

 B. 8

 C. 9

 D. 0

29. What is the output of this C code?

    int main()
    {
        unsigned int a = 10;
        a = ~a;
        printf("%d\n", a);
    }

 A. -9

 B. -10

 C. -11

 D. 10
31. What is the output of this C code?

    int main()
    {
        int a = 2;
        if (a >> 1)
           printf("%d\n", a);
    }

 A. 0

 B. 1

 C. 2

 D. No output

32. Comment on the output of this C code?

    int main()
    {
        int i, n, a = 4;
        scanf("%d", &n);
        for (i = 0; i < n; i++)
            a = a * 2;
    }

 A. Logical Shift left

 B. No output(c)

 C. Arithmetic Shift right

 D. bitwise exclusive OR

33. What is the output of this C code?

    void main()
    {
        int x = 97;
        int y = sizeof(x++);
        printf("x is %d", x);
    }

 A. x is 97(c)

 B. x is 98

 C. x is 99

 D. Run time error

34.
What is the output of this C code?

    void main()
    {
        int x = 4, y, z;
        y = --x;//3
        z = x--;//3 2
        printf("%d%d%d", x, y, z);
    }

 A. 3  2  3

 B. 2  2  3

 C. 3  2  2

 D. 2  3  3(c)

35. What is the output of this C code?

    void main()
    {
        int x = 4;
        int *p = &x;
        int *k = p++;
        int r = p - k;
        printf("%d", r);
    }

 A. 4

 B. 8

 C. 1(c)

 D. Run time error

36. What is the output of this C code?

    void main()
    {
        int a = 5, b = -7, c = 0, d;
        d = ++a && ++b || ++c;
        printf("\n%d%d%d%d", a, b, c, d);
    }

 A. 6  -6  0  0

 B. 6  -5  0  1

 C. -6  -6  0  1
 D. 6  -6  0  1(c)

37. What is the output of this C code?

    void main()
    {
        int a = -5;
        int k = (a++, ++a); -5,-3
        printf("%d\n", k);
    }

 A. -3(c)

 B. -5

 C. 4

 D. Undefined

38. What is the output of this C code?

    int main()
    {
        int x = 2;
        x = x << 1;
        printf("%d\n", x);
    }

 A. 4(c)

 B. 1

 C. Depends on the compiler

 D. Depends on the endianness of the machine

9. What is the output of this C code?

    int main()
    {
        int x = -2;
        x = x >> 1;
        printf("%d\n", x);
    }

 A. 1

 B. -1(c)

 C. 2 ^ 31 – 1 considering int to be 4 bytes

 D. Either (b) or (c)

40. What is the output of this C code?


    int main()
    {
        if (~0 == 1)
            printf("yes\n");
        else
            printf("no\n");
    }

 A. Yes

 B. No(c)

 C. Compile time error

 D. Undefined

41. What is the output of this C code?

    int main()
    {
        int x = -2;
        if (!0 == 1)
            printf("yes\n");
        else
            printf("no\n");
    }

 A. Yes

 B. No

 C. Run time error

 D. Undefined

42. What is the output of this C code?

    int main()
    {
        int y = 0;
        if (1 |(y = 1))
            printf("y is %d\n", y);
        else
            printf("%d\n", y);
 
    }

 A. 1

 B. 0

 C. Run time error

 D. Y is 1(c)
#include <stdio.h>

int main()

int y = 1;

if(y & (y=2))printf("true %d\n",y);

else printf("false %d\n",y);

return 0;

 A. true 2(c)

 B. false 2

 C. Either option a or option b

 D. true 1

45. What is the output of this C code?

    int main()
    {
        int a = 1, b = 1, c;
        c = a++ + b;//1 + 1 =2 a=2
        printf("%d, %d", a, b);
    }

 A. a = 1, b = 1

 B. a = 2, b = 1

 C. a = 1, b = 2

 D. a = 2, b = 2

46. What is the output of this C code?

    int main()
    {
        int a = 1, b = 1, d = 1;
        printf("%d, %d, %d", ++a + ++a+a++, a++ + ++b, ++d + d++ + a++);
    }

 A. 15, 4, 5(c)

 B. 9, 6, 9

 C. 9, 3, 5
 D. 6, 4, 6

48.
What is the output of this C code?

    int main()
    {
        int a = 10, b = 10;
        if (a = 5)
        b--;
        printf("%d, %d", a, b--);
    }

 A. a = 10, b = 9

 B. a = 10, b = 8

 C. a = 5, b = 9(c)

 D. a = 5, b = 8

49. What is the output of this C code?

    int main()
    {
        int i = 0;
        int j = i++ + i;
        printf("%d\n", j);
    }

 A. 0

 B. 1(c)

 C. 2

 D. Compile time error

50. What is the output of this C code?

    int main()
    {
        int i = 2;
        int j = ++i + i;
        printf("%d\n", j);
    }

 A. 6(c)

 B. 5
 C. 4

 D. Compile time error

You might also like