Interview Questions

C Questions only (8)

C Interview Questions and Answers


(Continued from previous question...)

C Questions only (8)


67.
Which one of the following correctly declares a pointer to an array of 12 characters?
1 char (* a) [12];
2 char (* a) [11];
3 char * a [11];
4 char * (a [12]);
5 char * (a [11]);


68.
Sample Code
void zero_array (char a [20]) {
size_t size;

assert(a);

size = sizeof(a);
memset(a, 0, size);
}

The function zero_array(), defined above, contains an error. Which one of the following describes it?

1 The result of sizeof(a) may be subject to a type cast that causes size to acquire an erroneous value. The call to memset() will probably clear the wrong number of bytes.
2 The assert() macro may incorrectly cause the code to abort if the host machine uses a constant other than zero (0) to represent the null pointer natively.
3 Standard C does not permit programmers to declare the size of the leftmost dimension of an array parameter. The compiler should print an error when parsing zero_array().
4 sizeof(a) evaluates to sizeof(char *) rather than twenty (20). The call to memset() will probably clear the wrong number of bytes.
5 The second and third arguments to memset() are probably reversed.


69.
Sample Code
time_t currentTime = time( NULL );
printf("%s\n", ???? );

Which one of the following can replace ???? in order for the above sample code to print out the current time as a human-readable string?

1 ctime(¤tTime)
2 asctime(¤tTime)
3 asctime(currentTime)
4 timestr(currentTime)
5 strtime(currentTime)

70.
Sample Code
#include <math.h>
static double (*funcs[])( double ) =
{
sin, cos, tan, asin, acos, atan, sinh, cosh, tanh
};

double computeTrigFunction( int index, double argument )
{
return ????;
}

Referring to the sample code above that should compute the value of a trigonometric function based on its index, what would be a replacement for the ???? to make the correct call?

1 *funcs[ index ]( argument )
2 (*funcs)[ index ]( argument )
3 funcs(argument)[index]
4 *(funcs[index](argument))
5 funcs[index](argument)


71.
Sample Code
file inc.h contains only the following line:
struct x {int d; char c;}

file main.c contains only the three following lines:

#include <stdlib.h>
#include "inc.h"
main(int n, char**a) { exit(0); }

Refer to the above sample code. The program is supposed to be Standard C. What is the problem with main in this code?

1 main does not return a value.
2 The parameters of main must be named argc and argv.
3 main is missing a return type.
4 The parameters of main are not used.
5 The type of the second parameter of main is invalid.


72.
Sample Code
int x = 5;
int y = 2;
char op = '*';
switch (op)
{
default : x += 1;
case '+' : x += y;
case '-' : x -= y;
}

After the sample code above has been executed, what value will the variable x contain?

1 4
2 5
3 6
4 7
5 8


1.
class A {
};

class B {
protected:
friend class A;
};

class C {
public:
friend class B;
};

Referring to the sample code above, assuming the above classes had data members, what names of C's members could be used in declarations of members of A?
1) Only private members
2) Only protected members
3) All of C's data members
4) Only public members
5) None of C's data members

(Continued on next question...)

Other Interview Questions