Part3 – Interview Questions - Interview Questions
- The write to *buf is blocked by the const qualifications applied to its type.
- if the null pointer for char is not zero-valued on the host machine, the implicit comparisons to zero (0) may introduce undesired behavior. Moreover, even if successful, it introduces machine-dependent behavior and harms portability.
- The symbol stdin may not be defined on some ANCI C compliant systems.
- The else causes fwd to contain an errant address.
- If the call to realloc() fails during any iteration but the first, all memory previously allocated by the loop is leaked.
- if == EOF[Ans]
- feof( f )
- eof( f )
- if == NULL
- !f
- Deprecated by Standard C
- Internal to the current translation unit
- Visible to all translation units
- Read-only subsequent to initialization
- Allocated on the heap[Ans]
- fscanf() will fail to match floating-point numbers not preceded by whitespace.
- The format specifier %lf indicates that the corresponding argument should be long double rather than double.
- The call to fscanf() requires a pointer as its last argument.
- The format specifier %lf is recognized by fprintf() but not by fscanf().
- d must be initialized prior to usage.
- ___S
- 1___ [Ans]
- ___1
- ___
- S___
- long double
- Unspecified
- loat[Ans]
- double
- signed float
- They are generally treated differently by preprocessors and compilers from other identifiers.
- They are case-insensitive.
- They are reserved for usage by standards committees, system implementers, and compiler engineers.
- Applications programmers are encouraged to employ them in their own in order to mark certain symbols for internal usage.
- They are deprecated by Standard C and are permitted only for backward compatibility with older C libraries.
- fileOpen (filenm, “r”);
- fileOpen (filenm, “ra”);
- fileOpen (filenm, “read”);
- fopen (filenm, “read”);
- fopen (filenm, “r”);[Ans]
- FILE f;
- FILE *f;[Ans]
- int f;
- struct FILE f;
- char *f;
- flush()
- output()
- fflush()
- dump()
- write()
- 127
- 128
- 255
- 32,767 [Ans]
- 65,536
- The C standard says that dereferencing a null pointer causes undefined behavior. This may explain why printf() apparently fails.
- If the standard output stream is buffered, the library buffers may not be flushed before the crash occurs.
- printf() always buffers output until a newline character appears in the buffer. Since no newline was present in the format string, nothing is printed.
- There is insufficient information to determine why the output fails to appear. A broader context is required.
- printf() expects more than a single argument. Since only one argument is given, the crash may actually occur inside printf(), which explains why the string is not printed. puts() should be used instead.
- 4
- 5[Ans]
- 6
- 7
- 8
- qsort()
- bcmp()
- memcmp()
- strxfrm()
- bsearch()
- The ellipsis is a throwback from K&R C. In accordance with Standard C, the declaration of args should be moved into the parameter list, and the K&R C macro va_arg() should be deleted from the.
- vfprintf() does not conform to ISO 9899: 1990, and may not be portable.
- Library routines that accept argument lists cause a fault on receipt of an empty list. The argument list must be validated with va_null() before invoking vfprintf().
- The argument list args has been improperly initialized.
- Variadic functions are discontinued by Standard C; they are legacy constructs from K&R C, and no longer compile under modern compilers.
- 0123456789
56789 - 5123456789
5123456789 - 56789
56789 - 0123456789
0123456789 - 6789
0123456789 [Ans] - The calls to memset() and memcpy() illegally perform a pointer conversion on rightmost without an appropriate cast.
- The does not correctly handle the situation where a directory separator ‘/’ is the final character.
- The if condition contains an incorrectly terminated character literal.
- memcpy() cannot be used safely to copy string data.
- The return value of get_rightmost() will be invalid in the caller’s context.
- -4000 [Ans]
- -400
- -40
- .004
- .0004
- n = n->next;
free( n ); - struct node m = n;
n = n->next;
free( m ); - struct node m = n;
free( n );
n = m->next; - free( n );
n = n->next; - struct node m = n;
free( m );
n = n->next; - Definition allocates storage for a variable, but declaration only informs the compiler as to the variable’s type.
- Declaration allocates storage for a variable, but definition only informs the compiler as to the variable’s type.
- Variables may be defined many times, but may be declared only once.[Ans]
- Variable definition must precede variable declaration.
- There is no difference in C between variable declaration and variable definition.
- *ptr + 3 = 10;
- *ptr[ 3 ] = 10;
- *(ptr + 3) = 10;[Ans]
- (*ptr)[ 3 ] = 10;
- *(ptr[ 3 ]) = 10;
- The range of the loop does not match the bounds of the array d.
- The loop processes the incorrect number of elements.
- total is initialized with an integer literal. The two are not compatible in an assignment.
- The above fails to compile if there are no registers available for i.
- The formal parameter d should be declared as double * d to allow dynamically allocated arrays as arguments.
- 1 -- 1
1 -- 1 - 1 -- 1
2 -- 1 - 1 -- 1
2 -- 2 - 1 -- 0
1 -- 0 - 1 -- 1
1 -- 2 [Ans] - For some values of n, the environment will almost certainly exhaust its stack space before the calculation completes.
[Ans] - An error in the algorithm causes unbounded recursion for all values of n.
- A break statement should be inserted after each case. Fall-through is not desirable here.
- The fibonacci() function includes calls to itself. This is not directly supported by Standard C due to its unreliability.
- The variable's address [Ans]
- The variable's right value
- The variable's binary form
- The variable's value
- The variable's format
- 127
- 128
- 255
- 32,767[Ans]
- 65,536
- 3
- 13
- 19
- fopen( "test.txt", "r" );
- read( "test.txt" )
- fileopen( "test.txt", "r" );
- fread( "test.txt" )
- freopen( "test.txt" )
- m = -26, n = -7, o = 0
- m = -52, n = -4, o = -2
- m = -26, n = -5, o = -2
- m = -104, n = -7, o = 0
- m = -52, n = -6, o = 0
- %e displays an argument of type double with trailing zeros; %f never displays trailing zeros.
- %e displays a double in engineering notation if the number is very small or very large. Otherwise, it behaves like %f and displays the number in decimal notation.
- %e always displays an argument of type double in engineering notation; %f always displays an argument of type double in decimal notation. [Ans]
- %e expects a corresponding argument of type double; %f expects a corresponding argument of type float.
- %e and %f both expect a corresponding argument of type double and format it identically. %e is left over from K&R C; Standard C prefers %f for new .
- char *c;
- unsigned int c;
- int c;
- unsigned char c;
- char c;[Ans]
- void function() { ... }
- extern void function() { ... }
- const void function() { ... }
- private void function() { ... }
- static void function() { ... }
The function read_long_string(), defined below, contains an error that may be particularly visible under heavy stress. Which one of the following describes it?
int read_long_string (const char ** const buf) {
char * p = NULL;
const char * fwd = NULL;
size_t len = 0;
assert(buf);
do
{
p = realloc(p, len += 256);
if (!p)
return 0;
if (!fwd)
fwd = p;
else
fwd = strchr(p, '\0');
} while (fgets(fwd, 256, stdin));
*buf = p;
return 1;
}
Which one of the following can replace the ???? in the below to determine if the end of a file has been reached?
FILE *f = fopen( fileName, "r" );
readData( f );
if( ???? )
{
puts( "End of file was reached" );
}
Global variables that are declared static are ____________.
Which one of the following correctly completes the sentence above?
The below contains a common error. Which one of the following describes it?
Read a double value from fp.
double read_double (FILE * fp) {
double d;
assert(fp != NULL);
fscanf(fp, " %lf", d);
return d;
}
Which one of the following is NOT a valid C identifier?
According to Standard C, what is the type of an unsuffixed floating-point literal, such as 123.45?
Which one of the following is true for identifiers that begin with an underscore?
Which one of the following is valid for opening a read-only ASCII file?
Referring to the below, what is the proper definition for the variable f?
f = fopen( filename, "r" );
If there is a need to see output as soon as possible, what function will force the output from the buffer into the output stream?
What is the maximum number that can be printed using printf(“%d\n”, x), assuming that x is initialized as shown below?
short int x; assume x is 16 bits in size
The function crash(), defined below, triggers a fault in the memory management hardware for many architectures. Which one of the following explains why “got here” may NOT be printed before the crash?
void crash (void)
{
printf("got here");
*((char *) 0) = 0;
}
How many elements does the array dwarves (declared below) contain? Assume the C compiler employed strictly complies with the requirements of Standard C.
char * dwarves [] = {
"Sleepy",
"Dopey" "Doc",
"Happy",
"Grumpy" "Sneezy",
"Bashful",
};
Which one of the following can be used to test arrays of primitive quantities for strict equality under Standard C?
The function debug(), defined below, contains an error. Which one of the following describes it?
int debug (const char * fmt, ...) {
extern FILE * logfile;
va_list args;
assert(fmt);
args = va_arg(fmt, va_list);
return vfprintf(logfile, fmt, args);
}
What will be printed when the sample below is executed?
char *buffer = "0123456789";
char *ptr = buffer;
ptr += 5;
printf( "%s\n", ptr );
printf( "%s\n", buffer );
The function get_rightmost(), defined above, contains an error. Which one of the following describes it?
char * get_rightmost (const char * d)
{
char rightmost [MAXPATHLEN];
const char * p = d;
assert(d != NULL);
while (*d != '\0')
{
if (*d == '/')
p = (*(d + 1) != '\0') ? d + 1 : p;
d++;
}
memset(rightmost, 0, sizeof(rightmost));
memcpy(rightmost, p, strlen(p) + 1);
return rightmost;
}
hat number is equivalent to -4e3?
Which one of the following can replace the ???? for the function below to release the memory allocated to a linked list?
void freeList( struct node *n )
{
while( n )
{
????
}
}
How does variable definition differ from variable declaration?
Which one of the following statements could replace the ???? in the above to cause the string 1-2-3-10-5- to be printed when the is executed?
int x[] = {1, 2, 3, 4, 5};
int u;
int *ptr = x;
????
for( u = 0; u < 5; u++ )
{
printf("%d-", x[u]);
}
printf( "\n" );
The function sum_array(), defined below, contains an error. Which one of the following accurately describes it?
sum_array() has been ported from FORTRAN. No * logical changes have been made
double sum_array(double d[],int n)
{
register int i;
double total=0;
assert(d!=NULL);
for(i=l;i<=n;i++)
total+=d[i];
return total;
}
What will the above print when it is executed?
#include
void func()
{
int x = 0;
static int y = 0;
x++; y++;
printf( "%d -- %d\n", x, y );
}
int main()
{
func();
func();
return 0;
}
The function above has a flaw that may result in a serious error during some invocations. Which one of the following describes the deficiency illustrated below?
int fibonacci (int n)
{
switch (n)
{
default:
return (fibonacci(n - 1) + fibonacci(n - 2));
case 1:
case 2:
}
return 1;
}
Since the default case is given first, it will be executed before any case matching n.
hen applied to a variable, what does the unary "&" operator yield?
What is the maximum number that can be printed using printf("%d\n", x), assuming that x is initialized as shown below?
short int x; assume x is 16 bits in size
What value will x contain in the sample below?
int x = 011 | 0x10;
25
27
Which one of the following calls will open the file test.txt for reading by fgetc?
Assuming two's-complement arithmetic, which one of the following correctly represents the values of m, n, and o after the execution of the below?
int m = -14;
int n = 6;
int o;
o = m % ++n;
n += m++ - o;
m <<= (o ^ n) & 3;
How do printf()'s format specifiers %e and %f differ in their treatment of floating-point numbers?
What is the proper declaration for the variable c in the below?
$$Except 1 all choices are O.K.$$
c = getchar();
Which one of the following will define a function that CANNOT be called from another source file?