Interview Questions

C Questions only (5)

C Interview Questions and Answers


(Continued from previous question...)

C Questions only (5)


40.
Sample Code
char buf[ 50 ] = "Hello World";
char *ptr = buf + 5;

From the sample above, what is the proper way to copy 20 bytes from the location pointed to by ptr to the beginning of buf?
1 memcpy( buf, ptr, 20 );
2 It cannot be done because the source and destination overlap.
3 strncpy( buf, ptr, 20 );
4 That may cause an illegal memory access because it will read memory past the end of the string.
5 memmove( buf, ptr, 20 );


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

1 int *x = &0x200;
2 int *x = *0x200;
3 int *x( &0x200 );
4 int *x;
*x = 0x200;
5 int *x = 0x200;


42.
int *x;
x = (int *) 15;

Is the above code legal?

1 Yes; upon initialization, the number 15 is stored in a special pointer memory address space.
2 Yes; a new memory space is allocated to hold the number 15.
3 No; this syntax is not allowed.
4 Yes; the pointer x points at the integer in memory location 15.
5 No; this assigns the number 15 to an unallocated space in memory.


43.
x[2] = 5 vs.
2[x] = 5

Are x[2] and 2[x] identical in the sample code above? Why or why not?

1 Yes; both are identical because they are resolved to pointer references.
2 Yes; both are identical because the compiler will identify the x variable and make adjustments.
3 No; x[2] is correct, but 2[x] is invalid syntax.
4 No; both variable assignments have invalid syntax.
5 No; 2[x] is correct, but x[2] is invalid syntax.


44.
Sample Code
int x=1;
int y=x+++ ++x;

Refer to the above sample code. What is the value of y?

1 2
2 3
3 4
4 5
5 The value is implementation dependent because it is not defined in Standard C.


45.
Which one of the following functions is the Standard C functional equivalent of the AT&T Unix function rindex(), which returns a pointer to the last occurrence of a character in a string or NULL if no such character exists?

1 strrchr()
2 stridx()
3 strcspn()
4 strchr()
5 strridx()


46.
Which one of the following declarations ensures that ptr CANNOT be used to modify the string to which it points (although ptr itself may be changed to point to another string)?
1 const char *ptr = "Hello";
2 char *ptr = const "Hello";
3 char * static ptr = "Hello";
4 static char *ptr = "Hello";
5 char * const ptr = "Hello";


47.
Sample Code
char * strdup (const char * s) {
char * buf;
int len;

assert(s != NULL);

len = strlen(s);
buf = (char *) calloc(len + 1, sizeof(char));
memcpy(buf, s, len);

return buf;
}

The proposed implementation of strdup() above contains a runtime error that may NOT appear consistently with each invocation. Which one of the following accurately describes this error?

1 The arguments to calloc() do not cause enough memory to be allocated for storing the contents of s.
2 If memory is scarce, calloc() may fail and return NULL. The code does not anticipate this condition.
3 memcpy() may corrupt data if used to copy ASCII strings.
4 buf is never NUL-terminated, and therefore cannot be used by C library functions affecting strings.
5 The function returns a pointer to dynamic memory. This practice should be avoided and always constitutes a memory leak.


48.
Sample Code
int a[5] = {1, 2, 3, 4, 5};
int *aPtr;
aPtr = a;
printf("element=%d\n", *(aPtr + 2));

What will be printed when the sample code above is executed?

1 element=1
2 element=2
3 element=3
4 element=4
5 element=5

(Continued on next question...)

Other Interview Questions