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;

How many bytes are allocated by the definition below?

char txt [20] = "Hello world!\0";

a) 11 bytes
b) 12 bytes
c) 13 bytes
d) 20 bytes
e) 21 bytes

In terms of code generation, how do the two definitions of buf, both presented above, differ?

char buf [] = "Hello world!";
char * buf = "Hello world!";

a) The first definition certainly allows the contents of buf to be safely modified at runtime; the second definition does not
b) The first definition is not suitable for usage as an argument to a function call; the second definition is
c) The first definition is not legal because it does not indicate the size of the array to be allocated; the second definition is legal
d) They do not differ -- they are functionally equivalent
e) The first definition does not allocate enough space for a terminating NUL-character, nor does it append one; the second definition does

Which one of the following will declare a pointer to an integer at address 0x200 in memory?

a) int *x;
*x = 0x200;
b) int *x = &0x200;
c) int *x = *0x200;
d) int *x = 0x200;
e) int *x( &0x200 );


What string does ptr point to in the sample code below?

char *ptr;
char myString[] = "abcdefg";
ptr = myString;
ptr += 5;

a) fg
b) efg
c) defg
d) cdefg
e) None of the above

What value will x contain when the sample code below is executed?

int x = 3;
if( x == 2 );
x = 0;
if( x == 3 )
x++;
else x += 2;

a) 1
b) 2
c) 3
d) 4
e) 5