Sunday, January 10, 2010

What does the following program print?

#include

void main()
{

static struct S1
{
char c[4], *s;
} s1 = {"abc", "def"};

printf("%c %c\n", s1.c[0], *s1.s);
printf("%s %s", s1.c, s1.s);
}

a) Compiler error

b) a def
    abc def

c) a d
    abc def

d) a d
    a def


What is the output of following program?

#include
int main()
{
printf("%c\n", '1' + 1);
return EXIT_SUCCESS;
}

a) ASCII value of '1' is required to find out the answer
b) 2
c) 50
d) Syntax Error

#include
void func()
{
int x = 0;
static int y = 0;
x++; y++;
printf( "%d -- %d\n", x, y );
}

int main()
{
func();
func();
return 0;
}

What will the code above print when it is executed?

a) 1 -- 1
1 -- 1
b) 1 -- 1
2 -- 1
c) 1 -- 1
2 -- 2
d) 1 -- 0
1 -- 0
e) 1 -- 1
1 -- 2

Which one of the following statements could replace the ???? in the code above to cause the string 1-2-3-10-5- to be printed when the code is executed?

int x[] = {1, 2, 3, 4, 5};
int u;
int *ptr = x;
????
for( u = 0; u < 5; u++ ) { printf("%d-", x[u]); } printf( "\n" ); a) *ptr + 3 = 10; b) *ptr[ 3 ] = 10; c) *(ptr + 3) = 10; d) (*ptr)[ 3 ] = 10; e) *(ptr[ 3 ]) = 10;

With what do you replace the ???? to make the function shown below return the correct answer?

long factorial (long x)
{
????
return x * factorial(x - 1);
}

a) if (x == 0) return 0;
b) return 1;
c) if (x >= 2) return 2;
d) if (x == 0) return 1;
e) if (x <= 1) return 1;