C Interview Questions and Answers
Part:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
(Continued from previous part...)
C Questions only (3)
20.
Sample Code
void memset (void * buf, int c, size_t len) ??<
unsigned char * b;
register int i;
assert(buf);
debug("assert(buf) => TRUE??/n");
b = buf;
for (i = 0; i < len; i++) ??<
b??(i??) = c;
debug("b??(i??) <- c??/n");
??>
??>
Consider the function memset(), defined above. Which one of the following is a completely true statement about the code shown above?
1 Some accesses performed on b are out-of-bounds.
2 Providing that debug() is declared and correctly used, the code compiles correctly under a
Standard C compiler.
3 The three-character sequences starting with ?? were inserted by Microsoft Word.
4 The source code file containing memset() has probably become corrupted. This code clearly
does not compile.
5 Assuming that debug() prints its string argument, the strings will run together on the same
line.
21.
Which one of the following is the result type of the sizeof operator?
1 unsigned char
2 int
3 char
4 unsigned int
5 size_t
22.
Which one of the following best describes the Standard C convention regarding variables with names that start with an underscore (_)?
1 They are reserved for usage by standards committees, system implementers, and compiler
engineers.
2 They are deprecated by Standard C and are permitted only for backward compatibility with
older C libraries.
3 Applications programmers are encouraged to employ them in their own code in order to mark
certain symbols for internal usage.
4 They are case-insensitive.
5 They are generally treated differently by preprocessors and compilers from other
identifiers.
23.
Sample Code
int i,j;
int ctr = 0;
int myArray[2][3];
for (i=0; i<; i++)
for (j=0; j<2; j++)
{
myArray[j][i] = ctr;
++ctr;
}
What is the value of myArray[1][2]; in the sample code above?
1 1
2 2
3 3
4 4
5 5
24.
Sample Code
int i = 4;
switch (i)
{
default:
;
case 3:
i += 5;
if ( i == 8)
{
i++;
if (i == 9) break;
i *= 2;
}
i -= 4;
break;
case 8:
i += 5;
break;
}
printf("i = %d\n", i);
What will the output of the sample code above be?
1 i = 5
2 i = 8
3 i = 9
4 i = 10
5 i = 18
25.
Which one of the following functions returns the string representation from a pointer to a time_t value?
1 localtime
2 gmtime
3 ctime
4 strtime
5 asctime
26.
Which one of the following is a true statement about an lvalue?
1 An lvalue is the result of an arithmetic operation involving quantities of type long int.
2 All lvalues can be used on the right side of an assignment statement.
3 An lvalue is, by definition, the value appearing on the rightmost side of an assignment
statement.
4 By definition, an lvalue is the storage space indirectly referenced by a pointer.
5 An lvalue is any quantity capable of appearing on the left side of a shift operator.
27.
Which one of the following is a true statement about non-generic (void *) pointers?
1 For efficiency, pointer values are always stored in machine registers.
2 A pointer to one type may not be cast to a pointer to any other type.
3 Similarly typed pointers may be subtracted from each other.
4 They are always 32-bit values.
5 Similarly typed pointers may be added to each other.
28.
Which one of the following will read a character from the keyboard and will store it in the variable c?
1 c = getchar();
2 c = getchar( stdin );
3 getchar( &c )
4 getc( &c );
5 c = getc();
29.
Which one of the following is NOT a valid statement?
1 ++d+++e++;
2 f*=g+=h=5;
3 l-->>m<<--n;
4 int a(int a),b=0,c=a((c=b,++b));
5 i->j<-k;
30.
int factorial (int x) {
extern jmp_buf jb;
int fact, chk;
if (!x) return 1;
fact = x * (chk = factorial(x - 1));
if (chk > fact) longjmp(jb, -1);
return fact;
}
int check_for_overflow (int x) {
extern jmp_buf jb;
if (setjmp(jb)) {
printf("discovered overflow in factorial(%d)\n", x);
return 0;
}
if (x < 0) x = 0;
return factorial(x);
}
There is an error in check_for_overflow that may occasionally result in unexpected values in var jb. Which one of the following describes this error?
1 longjmp() cannot safely be used to escape from a recursive call chain.
2 The argument to check_for_overflow() should be qualified with volatile to ensure correct
error reporting.
3 setjmp() and longjmp() must be invoked within the same stack frame. The result of longjmp()
in this case is undefined.
4 The calls to setjmp() and longjmp() operate on different jump buffers, and therefore may
have an undefined effect.
5 The factorial of zero (0) is incorrectly handled by factorial().
(Continued on next part...)
Part:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|