Interview Questions

C Questions only (1)

C Interview Questions and Answers


(Continued from previous question...)

C Questions only (1)

1.
Sample Code
#include <stdio.h>
int main(void) {
{int i=10; goto lbl;}
{int i=20; {int i=30; lbl:;}
printf("%i", ++i);}
}

Refer to the above sample code. What does this program print?
1 10
2 11
3 21
4 31
5 An undefined value


2.
short testarray[4][3] = { {1}, {2, 3}, {4, 5, 6} };
printf( "%d\n", sizeof( testarray ) );

Assuming a short is two bytes long, what will be printed by the above code?
1 It will not compile because not enough initializers are given.
2 6
3 7
4 12
5 24


3.
Which of the following choices most accurately describes variable DEFINITION (as opposed to declaration)?

1 The assignment of properties and storage space to a variable
2 The identification of a variable that resides elsewhere in the program
3 The assignment of storage space to a variable
4 The assignment of properties to a variable
5 The assignment of storage space to a variable whose
properties have been specified external to the current file scope


4.
How do you include a system header file called
sysheader.h in a C source file?

1 #includefile <sysheader>
2 #include sysheader.h
3 #incl "sysheader.h"
4 #include <sysheader.h>
5 #incl <sysheader.h>


5.
Sample Code
f = fopen( fileName, "r" );

From the sample above, which one of the following statements will set the file position for the file f to cause the next byte read from the file to be the last byte?

1 f = feof( f ) - 1;
2 fsetpos( f, EOF - 1 );
3 fseek( f, -1, SEEK_SET );
4 fseek( f, -1, EOF );
5 fseek( f, -1, SEEK_END );


6.
/* Initialize an array with zero values. */
void bzero (void * b, size_t len) {
char *buf = b;
int i;

for (i = 0; i <= len; i++)
*(buf + i) = 0;
}

The function bzero(), defined above, contains a common programming error.
Which one of the following correctly describes it?
1 The loop writes beyond the end of buf.
2 buf refers to an array and should be indexed as one; the pointer
notation applied by the loop causes the wrong address to be
dereferenced.
3 It is not permitted to declare objects of type size_t; this type
is reserved for exclusive usage by the standard library's internal
routines.
4 A generic pointer cannot be cast to a pointer of a more specific type.
5 The comparison between int i and size_t len is not permitted by Standard C.


7.
Sample Code
long factorial (long x)
{
return x * factorial(x - 1);
}

What will the function above return if called with x equal to 5?

1 0
2 15
3 120
4 The program will not compile.
5 The function will never return.


8.
Which one of the following describes a C character string?

1 A sequence of char objects in EBCDIC format
2 A sequence of printable ASCII characters
3 Any pointer to type char
4 An array of bytes terminated by NUL
5 A sequence of char objects in ASCII format


9.
#include %lt;stdlib.h>
int f(TYPE x) {
/*Allocates an array of two integers; returns 0
if the allocation succeeded, 1 otherwise.
Returns the allocated array in x.
*/
int *ret = (int*)malloc(sizeof(int[2]));
return ret ? *x=ret, 0 : 1;
}
Referring to the sample code above, how should TYPE be declared?

1 typedef int**TYPE;
2 typedef int*TYPE;
3 typedef int[]TYPE;
4 typedef &(int*)TYPE;
5 typedef int[]*TYPE;


10.
Which one of the following is a valid variable name?

1 Account_Number
2 Account Number
3 "Account Number"
4 Account^Number
5 Account-Number

(Continued on next question...)

Other Interview Questions