Basic Objectives

1. What is the difference between the following 2 codes?

    #include <stdio.h> //Program 1
    int main()
    {
        int d, a = 1, b = 2;
        d =  a++ +++b;
        printf("%d %d %d", d, a, b);
    }
    #include <stdio.h> //Program 2
    int main()
    {
        int d, a = 1, b = 2;
        d =  a++   +  ++b;
        printf("%d %d %d", d, a, b);
    }
a) No difference as space doesn’t make any difference, values of a, b, d are same in both the     case
b) No difference as space doesn’t make any difference, values of a, b, d are different
c) Program 1 has syntax error, program 2 prints 4 2 3
d) Program 2 has syntax error, program 1 prints 4 2 3


Answer:c

2. What is the output of this C code?

    #include <stdio.h>
    int main()
    {
        int a = 1, b = 1, c;
        c = a++ + b;
        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


Answer:b

3. What is the output of this C code?

    #include <stdio.h>
    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
b) 9, 6, 9
c) 9, 3, 5
d) 6, 4, 6


Answer:a

4. For which of the following, “PI++;” code will fail?
a) #define PI 3.14
b) char *PI = “A”;
c) float PI = 3.14;
d) Both (A) and (B)


5. What is the output of this C code?

    #include <stdio.h>
    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
d) a = 5, b = 8


Answer:c

6. What is the output of this C code?


    #include <stdio.h>
    int main()
    {
        int i = 0;
        int j = i++ + i;
        printf("%d\n", j);
    }
a) 0
b) 1
c) 2
d) Compile time error


Answer:a

7. What is the output of this C code?

    #include <stdio.h>
    int main()
    {
        int i = 2;
        int j = ++i + i;
        printf("%d\n", j);
    }
a) 6
b) 5
c) 4
d) Compile time error


Answer:a

8. Comment on the output of this C code?

    #include <stdio.h>
    int main()
    {
        int i = 2;
        int i = i++ + i;
        printf("%d\n", i);
    }
a) = operator is not a sequence point
b) ++ operator may return value with or without side effects
c) it can be evaluated as (i++)+i or i+(++i)
d) Both a and b


Answer:a


9. What is the output of this C code?

    #include <stdio.h>
    int main()
    {
        int c = 2 ^ 3;
        printf("%d\n", c);
    }
a) 1
b) 8
c) 9
d) 0


Answer: a

10. What is the output of this C code?

    #include <stdio.h>
    int main()
    {
        unsigned int a = 10;
        a = ~a;
        printf("%d\n", a);
    }
a) -9
b) -10
c) -11
d) 10


Answer:c

11. What is the output of this C code?

    #include <stdio.h>
    int main()
    {
        if (7 & 8)
        printf("Honesty");
            if ((~7 & 0x000f) == 8)
                printf("is the best policy\n");
    }
a) Honesty is the best policy
b) Honesty
c) is the best policy
d) No output


Answer:c
12. What is the output of this C code?

    #include <stdio.h>
    int main()
    {
        int a = 2;
        if (a >> 1)
           printf("%d\n", a);
    }
a) 0
b) 1
c) 2
d) No Output.


Answer:c

13. Comment on the output of this C code?

    #include <stdio.h>
    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) Arithmetic Shift right
d) bitwise exclusive OR


Answer:b

14. What is the output of this C code?

    #include <stdio.h>
    void main()
    {
        int x = 97;
        int y = sizeof(x++);
        printf("x is %d", x);
    }
a) x is 97
b) x is 98
c) x is 99
d) Run time error


Answer:a

15. What is the output of this C code?

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


Answer:d

16. What is the output of this C code?

    #include <stdio.h>
    void main()
    {
        int x = 4;
        int *p = &x;
        int *k = p++;
        int r = p - k;
        printf("%d", r);
    }
a) 4
b) 8
c) 1
d) Run time error


Answer:c


17. What is the output of this C code?

    #include <stdio.h>
    int main()
    {
        int i = -3;
        int k = i % 2;
        printf("%d\n", k);
    }
a) Compile time error
b) -1
c) 1
d) Implementation defined


Answer:b

18. What is the output of this C code?

     
    #include <stdio.h>
    int main()
    {
        int i = 3;
        int l = i / -2;
        int k = i % -2;
        printf("%d %d\n", l, k);
        return 0;
    }
a) Compile time error
b) -1 1
c) 1 -1
d) Implementation defined


Answer:b

19. What is the output of this C code?

    #include <stdio.h>
    int main()
    {
        int i = 5;
        i = i / 3;
        printf("%d\n", i);
        return 0;
    }
a) Implementation defined
b) 1
c) 3
d) Compile time error


Answer:b

20. What is the output of this C code?

   #include <stdio.h>
    int main()
    {
        int i = -5;
        i = i / 3;
        printf("%d\n", i);
        return 0;
    }
a) Implementation defined
b) -1
c) -3
d) Compile time error


Answer:b

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


    #include <stdio.h>
    void main()
    {
        int x = 5 * 9 / 3 + 9;
    }
a) 3.75
b) Depends on compiler
c) 24
d) 3


Answer:c

22. What is the output of this C code?

    #include <stdio.h>
    void main()
    {
        int x = 5.3 % 2;
        printf("Value of x is %d", x);
    }
a) Value of x is 2.3
b) Value of x is 1
c) Value of x is 0.3
d) Compile time error


Answer:d

23. What is the output of this C code?

    #include <stdio.h>
    void main()
    {
        int y = 3;
        int x = 5 % 2 * 3 / 2;
        printf("Value of x is %d", x);
    }
a) Value of x is 1
b) Value of x is 2
c) Value of x is 3
d) Compile time error


Answer:a

24. Comment on the output of this C code?

    #include <stdio.h>
    int main()
    {
        int a[5] = {1, 2, 3, 4, 5};
        int i;
        for (i = 0; i < 5; i++)
            if ((char)a[i] == '5')
                printf("%d\n", a[i]);
            else
                printf("FAIL\n");
    }
a) The compiler will flag an error
b) Program will compile and print the output 5
c) Program will compile and print the ASCII value of 5
d) Program will compile and print FAIL for 5 times


Answer:d

25. The format identifier ‘%i’ is also used for _____ data type?
a) char
b) int
c) float
d) double


Answer:b

26. Which data type is most suitable for storing a number 65000 in a 32-bit system?
a) short
b) int
c) long
d) double


Answer:a


27. Which of the following is a User-defined data type?
a) typedef int Boolean;
b) typedef enum {Mon, Tue, Wed, Thu, Fri} Workdays;
c) struct {char name[10], int age};
d) all of the mentioned


Answer:d

28. What is the size of an int data type?
a) 4 Bytes
b) 8 Bytes
c) Depends on the system/compiler
d) Cannot be determined


Answer:c

29. What is the output of this C code?

    #include  <stdio.h>
    int main()
    {
       char chr;
       chr = 128;
       printf("%d\n", chr);
       return 0;
    }
a) 128
b) -128
c) Depends on the compiler
d) None of the mentioned


Answer:b

30. Comment on the output of this C code?

    #include  <stdio.h>
    int main()
    {
        char c;
        int i = 0;
        FILE *file;
        file = fopen("test.txt", "w+");
        fprintf(file, "%c", 'a');
        fprintf(file, "%c", -1);
        fprintf(file, "%c", 'b');
        fclose(file);
        file = fopen("test.txt", "r");
        while ((c = fgetc(file)) !=  -1)
            printf("%c", c);
        return 0;
    }
a) a
b) Infinite loop
c) Depends on what fgetc returns
d) Depends on the compiler


Answer:a

31. Which of the following is not a valid variable name declaration?
a) int _a3;
b) int a_3;
c) int 3_a;
d) int _3a

Answer:c
Explanation:Variable name cannot start with a digit.

32. Which of the following is not a valid C variable name?
a) int number;
b) float rate;
c) int variable_count;
d) int $x;
ans :d
explanation:
White spaces and special symbols(!,@,#,$,^,&) are not allowed   except underscore( _ ) .


33. What is the output of this C code?

    #include  <stdio.h>
    int main()
    {
       char chr;
       chr = 128;
       printf("%d\n", chr);  
       return 0;
    }
a) 128
b) -128
c) Depends on the compiler
d) None of the mentioned

Answer:b

34. What is short int in C programming?

a) Basic datatype of C
b) Qualifier
c) short is the qualifier and int is the basic datatype
d) All of the mentioned

Answer:c