1.

Give the outputs of the following code segments, if any and justify your answers.

Answer»

(i) #define CUBE(x)

(x * x * x)

main( ) {

printf(“%d”, CUBE(4+5));

}

(ii) int j = 5;

printf(“%d”, j = j == 6);

printf(“%d”, j = ++j == 6);

(iii)for (j = 0; j = 3; j++)

printf(“%d”, j);

(iv)main( )

{

static char a[ ] = “Test String”;

static char *b = “Test String”;

printf(“%d %d”, sizeof(a), sizeof(b));

}

(v) main( )

{

enum test

{RED, BLUE, GREEN}; enum test t = BLUE;

printf(“%d”, t); }

(vi) main( ) {

union U { int j; char c; float f;}

u; u.j = 10; u.c = ‘A’; u.f = 99.99;

printf(“u.j = %d u.c = %c u.f = %f”, u.j, u.c, u.f);

}

(i) Output is: 49 macro cube(x *x*x) is expanded into (4+5*4+5*4+5) which is evaluated as 4+20+20+5=49 due to priority of operators. So output is 49.

(ii) Output is: 0 0 Both the expressions are evaluated as 0 so the output is 0.

(iii) Output is: infinite loop There is an incorrect assignment, j=3 in test expression.

(iv) Output is:12 2, The printf is printing the size of char array a and char b, null char is included.

(v) Output is:1, The compiler automatically assigns integer digits beginning with 0 to all the enumeration constants so BLUE has value 1.

(vi) Output is: u.j=1311,u.c=’β’,u.f=99.989998 The first two outputs are erroneous output which is machine dependent. During accessing a union member, we should make sure that we are accessing the member whose value is currently stored otherwise we will get errors. 



Discussion

No Comment Found

Related InterviewSolutions