C Programming Test And Answer 10

1.Point out the error, if any in the program.

#include
int main()
{
    int P = 10;
    switch(P)
    {
       case 10:
       printf("Case 1");

       case 20:
       printf("Case 2");
       break;

       case P:
       printf("Case 2");
       break;
    }
    return 0;
}

A. Error: No default value is specified
B. Error: Constant expression required at line case P:
C. Error: There is no break statement in each case.
D. No error will be reported.

Explanation:

The compiler will report the error “Constant expression required” in the line case P: . Because, variable names cannot be used with case statements.

The case statements will accept only constant expression.

2.What is the output of the program?

#include
int main()
{
    union a
    {
        int i;
        char ch[2];
    };
    union a u;
    u.ch[0] = 3;
    u.ch[1] = 2;
    printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
    return 0;
}

A. 3, 2, 515
B. 515, 2, 3
C. 3, 2, 5
D. None of these

Explanation:

printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i); It prints the value of u.ch[0] = 3, u.ch[1] = 2 and it prints the value of u.i means the value of entire union size.

C Programming Test And Answer 10_第1张图片

So the output is 3, 2, 515.

你可能感兴趣的:(sanfoundry)