Interview Questions

C Questions only (7)

C Interview Questions and Answers


(Continued from previous question...)

C Questions only (7)


58.
Sample Code
struct node {
int id;
int length;
struct node * next;
struct node * prev;
unsigned char data [1];
}

Consider struct node, an aggregate type defined above. Which one of the following might explain the declaration of its peculiar member data?

1 There is no difference between character unsigned char data and array unsigned char data [1], since each allocates only a single byte. Identical operations can be performed on both quantities. The choice was one of preference.
2 The programmer is declaring a bit field called data, which consists of only a single bit. struct node probably represents some hardware device.
3 data is probably used in conjunction with length and malloc() to create objects of variable size. struct node is essentially a header for an object of indeterminate size.
4 The information provided by the definition of struct node is insufficient to formulate a guess about the purpose of the member data or its strange declaration.
5 Clearly the programmer has made a typo. If the programmer had intended to allocate only a single byte, he or she would have declared data as unsigned char data instead.


59.
Sample Code
char ca[]="abc\012\0x34";

Refer to the above sample code. What does strlen(ca) return, using a Standard C compiler?

1 3
2 4
3 5
4 10
5 12


60.
sizeof(L"Hello world!") == x

Consider the code fragment above. For which value of x is the expression above true?

1 sizeof(char *)
2 sizeof(wchar_t [])
3 12
4 13
5 13 * sizeof(wchar_t);


61.
unsigned i, *ip = &i;
*ip = *ip & ~*ip;

What does the above sample code do?

1 It sets ip to 0.
2 It sets all the bits of i to 0.
3 It sets i to the value of UINT_MAX.
4 It sets ip to NULL.
5 It produces an undefined result.


62.
Sample Code
char*(*(*x)(void))[];

What does the above statement declare?

1 An array of pointers to a function with no parameters returning a pointer to a string pointer
2 An array of pointers to a pointer to a function with no parameters returning a string
3 A pointer to an array of functions with no parameters returning a string
4 A pointer to a function with no parameters that returns a pointer to an array of strings
5 It is a syntactically incorrect statement.


63.
#include<string.h>
#include <stdio.h>
void f(int x) {
char b[] = "1234567";
strncpy(b, "abc", x);
{
int i=0;
for(; i<sizeof(b); i++)
printf("%c", b[i] ? b[i] : '*');
}
}
int main() {
f(2); f(6);
return 0;
}

What does the above program print?

1 ab34567abc4567
2 ab*4567*abc***7*
3 ab34567*abc***7*
4 ab*4567abc***7
5 ab*4567*abc*567*


64.
Sample Code
char c = '\101';

What will the variable c contain in the code above?

1 It will not compile.
2 The four characters '\', '1', '0', '1'
3 A backslash '\'.
4 65 (which is the letter A in ASCII)
5 101 (which is the letter e in ASCII)


65.
Sample Code
void myFunc (int x)
{
if (x > 0) myFunc(--x);
printf("%d, ", x);
}

int main()
{
myFunc(5);
return 0;
}

What will the above sample code produce when executed?

1 5, 4, 3, 2, 1, 0,
2 0, 0, 1, 2, 3, 4,
3 4, 3, 2, 1, 0, 0,
4 1, 2, 3, 4, 5, 5,
5 0, 1, 2, 3, 4, 5,


66.
Sample Code
char s1[100];
char s2[100];

gets( s1 );
fgets( s2, sizeof(s2), stdin);
printf( "%d\n", strlen( s1 ) - strlen( s2 ) );

What will be printed when the above code is executed and the string "abcd" is entered twice on stdin?

1 -1
2 0
3 1
4 2
5 4

(Continued on next question...)

Other Interview Questions