Thursday, October 4, 2012

100 C Questions and Answers


(Q :  1)
Code:
int z,x=5,y=-10,a=4,b=2; 
z = x++ - --y * b / a;
What number will z in the sample code above contain? 
Option 1
Option 2
Option 3
10 [Ans] Corrected by buddy by running the program
Option 4
11 
Option 5
12 
____________________________________________________________

(Q :  2)
With every use of a memory allocation function, what function should be used to release allocated memory which is no longer needed? 

Option 1
unalloc() 
Option 2
dropmem() 
Option 3
dealloc() 
Option 4
release() 
Option 5
free() [Ans]
____________________________________________________________

(Q :  3)
Code:
void *ptr;
myStruct myArray[10];
ptr = myArray;
Which of the following is the correct way to increment the variable "ptr"? 
Option 1
ptr = ptr + sizeof(myStruct); [Ans]
Option 2
++(int*)ptr; 
Option 3
ptr = ptr + sizeof(myArray); 
Option 4
increment(ptr); 
Option 5
ptr = ptr + sizeof(ptr); 
____________________________________________________________

(Q :  4)
Code:
char* myFunc (char *ptr)
{
 ptr += 3;
 return (ptr);
int main()
{
 char *x, *y;
 x = "HELLO";
 y = myFunc (x);
 printf ("y = %s \n", y);
 return 0;
}
What will print when the sample code above is executed? 
Option 1
y = HELLO 
Option 2
y = ELLO 
Option 3
y = LLO 
Option 4
y = LO [Ans]
Option 5
x = O 
____________________________________________________________

(Q :  5)
Code:
struct node *nPtr, *sPtr;    ( pointers for a linked list. ) 
for (nPtr=sPtr; nPtr; nPtr=nPtr->next)
{    
 free(nPtr);
}
The sample code above releases memory from a linked list. Which of the choices below accurately describes how it will work? 
Option 1
It will work correctly since the for loop covers the entire list. 
Option 2
It may fail since each node "nPtr" is freed before its next address can be accessed. 
Option 3
In the for loop, the assignment "nPtr=nPtr->next" should be changed to "nPtr=nPtr.next". 
Option 4
This is invalid syntax for freeing memory. 
Option 5
The loop will never end. 
____________________________________________________________

(Q :  6)
What function will read a specified number of elements from a file? 
Option 1
fileread() 
Option 2
getline() 
Option 3
readfile() 
Option 4
fread() 
Option 5
gets()
____________________________________________________________

(Q :  7)
"My salary was increased by 15%!" 
Select the statement which will EXACTLY reproduce the line of text above. 
Option 1
printf("\"My salary was increased by 15/%\!\"\n"); 
Option 2
printf("My salary was increased by 15%!\n"); 
Option 3
printf("My salary was increased by 15'%'!\n"); 
Option 4
printf("\"My salary was increased by 15%%!\"\n");[Ans] 
Option 5
printf("\"My salary was increased by 15'%'!\"\n"); 
____________________________________________________________

(Q :  8)
What is a difference between a declaration and a definition of a variable? 
Option 1
Both can occur multiple times, but a declaration must occur first. 
Option 2
There is no difference between them. 
Option 3
A definition occurs once, but a declaration may occur many times. 
Option 4
A declaration occurs once, but a definition may occur many times. [Ans] 
Option 5
Both can occur multiple times, but a definition must occur first. 
____________________________________________________________

(Q :  9) 
int testarray[3][2][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; 
What value does testarray[2][1][0] in the sample code above contain? 
Option 1
Option 2
Option 3
Option 4
Option 5
11[Ans]
____________________________________________________________ 

(Q :  10)
Code:
int a=10,b;
b=a++ + ++a;
printf("%d,%d,%d,%d",b,a++,a,++a);
what will be the output when following code is executed
Option 1
12,10,11,13
Option 2
22,10,11,13
Option 3
22,11,11,11
Option 4
12,11,11,11
Option 5
22,13,13,13[Ans]
____________________________________________________________

(Q :  11)
Code:
int x[] = { 1, 4, 8, 5, 1, 4 }; 
int *ptr, y; 
ptr  = x + 4; 
y = ptr - x;
What does y in the sample code above equal? 
Option 1
-3 
Option 2
Option 3
4[Ans] 
Option 4
4 + sizeof( int ) 
Option 5
4 * sizeof( int 
____________________________________________________________ 

(Q :  12)
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? 
Option 1
1, 2, 3, 4, 5, 5, 
Option 2
4, 3, 2, 1, 0, 0, 
Option 3
5, 4, 3, 2, 1, 0, 
Option 4
0, 0, 1, 2, 3, 4, [Ans] 
Option 5
0, 1, 2, 3, 4, 5, 
____________________________________________________________ 

(Q :  13)
11 ^ 5 
What does the operation shown above produce? 
Option 1
Option 2
Option 3
Option 4
14 [Ans] 
Option 5
15 
____________________________________________________________ 

(Q :  14)
#define MAX_NUM 15 
Referring to the sample above, what is MAX_NUM? 
Option 1
MAX_NUM is an integer variable. 
Option 2
MAX_NUM is a linker constant. 
Option 3
MAX_NUM is a precompiler constant. 
Option 4
MAX_NUM is a preprocessor macro. [Ans] 
Option 5
MAX_NUM is an integer constant. 
____________________________________________________________ 

(Q :  15)
Which one of the following will turn off buffering for stdout? 
Option 1
setbuf( stdout, FALSE ); 
Option 2
setvbuf( stdout, NULL ); 
Option 3
setbuf( stdout, NULL ); 
Option 4
setvbuf( stdout, _IONBF ); 
Option 5
setbuf( stdout, _IONBF ); 
____________________________________________________________ 

(Q :  16)
What is a proper method of opening a file for writing as binary file? 
Option 1
FILE *f = fwrite( "test.bin", "b" ); 
Option 2
FILE *f = fopenb( "test.bin", "w" ); 
Option 3
FILE *f = fopen( "test.bin", "wb" ); 
Option 4
FILE *f = fwriteb( "test.bin" ); 
Option 5
FILE *f = fopen( "test.bin", "bw" ); 
____________________________________________________________ 

(Q :  17)
Which one of the following functions is the correct Option for moving blocks of binary data that are of arbitrary size and position in memory? 
Option 1
memcpy() 
Option 2
memset() 
Option 3
strncpy() 
Option 4
strcpy() 
Option 5
memmove()[Ans] 
____________________________________________________________ 

(Q :  18)
int x = 2 * 3 + 4 * 5; 
What value will x contain in the sample code above? 
Option 1
22 
Option 2
26[Ans] 
Option 3
46 
Option 4
50 
Option 5
70 
____________________________________________________________

(Q :  19)
Code:
void * array_dup (a, number, size) 
  const void * a; 
  size_t number; 
  size_t size; 
  void * clone; 
  size_t bytes; 
  assert(a != NULL); 
  bytes = number * size; 
  clone = alloca(bytes); 
  if (!clone) 
    return clone; 
  memcpy(clone, a, bytes); 
  return clone; 
}
The function array_dup(), defined above, contains an error. Which one of the following correctly analyzes it?  
Option 1
If the arguments to memcpy() refer to overlapping regions, the destination buffer will be subject to memory corruption. 
Option 2
array_dup() declares its first parameter to be a pointer, when the actual argument will be an array. 
Option 3
The memory obtained from alloca() is not valid in the context of the caller. Moreover, alloca() is nonstandard. 
Option 4
size_t is not a Standard C defined type, and may not be known to the compiler. 
Option 5
The definition of array_dup() is unusual. Functions cannot be defined using this syntax. 
____________________________________________________________

(Q :  20)
int var1; 
If a variable has been declared with file scope, as above, can it safely be accessed globally from another file?  
Option 1
Yes; it can be referenced through the register specifier. 
Option 2
No; it would have to have been initially declared as a static variable. 
Option 3
No; it would need to have been initially declared using the global keyword.[Ans] 
Option 4
Yes; it can be referenced through the publish specifier. 
Option 5
Yes; it can be referenced through the extern specifier. 
____________________________________________________________

(Q :  21)
time_t t; 
Which one of the following statements will properly initialize the variable t with the current time from the sample above? 
Option 1
t = clock();[Ans] 
Option 2
time( &t ); 
Option 3
t = ctime(); 
Option 4
t = localtime(); 
Option 5
None of the above 
____________________________________________________________

(Q :  22)
Which one of the following provides conceptual support for function calls? 
Option 1
The system stack[Ans] 
Option 2
The data segment 
Option 3
The processor's registers 
Option 4
The text segment 
Option 5
The heap 
____________________________________________________________
(Q :  23)
C is which kind of language? 
Option 1
Machine 
Option 2
Procedural[Ans] 
Option 3
Assembly 
Option 4
Object-oriented 
Option 5
Strictly-typed 
____________________________________________________________

(Q :  24)
Code:
int i,j; 
int ctr = 0; 
int myArray[2][3]; 
for (i=0; i<3; 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? 
Option 1
Option 2
Option 3
Option 4
Option 5
5 [Ans] 00,10,01,11,12 
____________________________________________________________

(Q :  25)
Code:
int x = 0; 
for (x=1; x<4; x++); 
printf("x=%d\n", x);
What will be printed when the sample code above is executed? 
Option 1
x=0 
Option 2
x=1 
Option 3
x=3 
Option 4
x=4[Ans] 
Option 5
x=5 
____________________________________________________________

(Q :  26)
Code:
int x = 3; 
if( x == 2 );
  x = 0; 
if( x == 3 )
 x++; 
else x += 2;
What value will x contain when the sample code above is executed? 
Option 1
Option 2
2[Ans] 
Option 3
Option 4
Option 5
____________________________________________________________

(Q :  27)
Code:
char *ptr; 
char myString[] = "abcdefg"; 
ptr = myString; 
ptr += 5;
What string does ptr point to in the sample code above? 
Option 1
fg [Ans](because string)
Option 2
efg 
Option 3
defg 
Option 4
cdefg 
Option 5
None of the above 
____________________________________________________________

(Q :  28)
Code:
double x = -3.5, y = 3.5; 
printf( "%.0f : %.0f\n", ceil( x ), ceil( y ) ); 
printf( "%.0f : %.0f\n", floor( x ), floor( y ) );
What will the code above print when executed?
ceil =>rounds up 3.2=4 floor =>rounds down 3.2=3 
Option 1
-3 : 4 
-4 : 3 [Ans] 
Option 2
-4 : 4 
-3 : 3 
Option 3
-4 : 3 
-4 : 3 
Option 4
-4 : 3 
-3 : 4 
Option 5
-3 : 3 
-4 : 4 
____________________________________________________________

(Q :  29)
Which one of the following will declare a pointer to an integer at address 0x200 in memory? 
Option 1
int *x; 
*x = 0x200;[Ans] 
Option 2
int *x = &0x200; 
Option 3
int *x = *0x200; 
Option 4
int *x = 0x200; 
Option 5
int *x( &0x200 ); 
____________________________________________________________

(Q :  30)
Code:
int x = 5; 
int y = 2; 
char op = '*'; 
switch (op) 
  default : x += 1; 
  case '+' : x += y; (It will go to all the cases)
  case '-' : x -= y; 
}
After the sample code above has been executed, what value will the variable x contain? 
Option 1
Option 2
Option 3
6 [Ans] 
Option 4
Option 5
____________________________________________________________

(Q :  31)
Code:
x = 3, counter = 0; 
while ((x-1)) 
   ++counter; 
   x--; 
}
Referring to the sample code above, what value will the variable counter have when completed? 
Option 1
Option 2
Option 3
2[Ans] 
Option 4
Option 5
____________________________________________________________

(Q :  32)
char ** array [12][12][12]; 
Consider array, defined above. Which one of the following definitions and initializations of p is valid? 
Option 1
char ** (* p) [12][12] = array; [Ans]
Option 2
char ***** p = array; 
Option 3
char * (* p) [12][12][12] = array; 
Option 4
const char ** p [12][12][12] = array; 
Option 5
char (** p) [12][12] = array; 
____________________________________________________________

(Q :  33)
void (*signal(int sig, void (*handler) (int))) (int); 
Which one of the following definitions of sighandler_t allows the above declaration to be rewritten as follows: 
sighandler_t signal (int sig, sighandler_t handler); 
Option 1
typedef void (*sighandler_t) (int);[Ans] 
Option 2
typedef sighandler_t void (*) (int); 
Option 3
typedef void *sighandler_t (int); 
Option 4
#define sighandler_t(x) void (*x) (int) 
Option 5
#define sighandler_t void (*) (int) 
____________________________________________________________

(Q :  34)
All of the following choices represent syntactically correct function definitions. Which one of the following represents a semantically legal function definition in Standard C? 
Option 1
Code:
 int count_digits (const char * buf) { 
  assert(buf != NULL); 
  int cnt = 0, i; 
  for (i = 0; buf[i] != '\0'; i++) 
    if (isdigit(buf[i])) 
      cnt++; 
  

 return cnt; 
}
Option 2
Code:
 int count_digits (const char * buf) { 
  int cnt = 0; 
  assert(buf != NULL); 
  for (int i = 0; buf[i] != '\0'; i++) 
    if (isdigit(buf[i])) 
      cnt++; 
  return cnt; 
}
Option 3

Code:
 int count_digits (const char * buf) { 
  int cnt = 0, i; 
  assert(buf != NULL); 
  for (i = 0; buf[i] != '\0'; i++) 
    if (isdigit(buf[i])) 
      cnt++; 
  return cnt; 
}
Option 4

Code:
 int count_digits (const char * buf) { 
  assert(buf != NULL); 
  for (i = 0; buf[i] != '\0'; i++) 
    if (isdigit(buf[i])) 
      cnt++; 
  return cnt; 
}
Option 5

Code:
 int count_digits (const char * buf) { 
  assert(buf != NULL); 
  int cnt = 0; 
  for (int i = 0; buf[i] != '\0'; i++) 
    if (isdigit(buf[i])) 
      cnt++; 
  return cnt; 
}
____________________________________________________________

(Q :  35)
struct customer *ptr = malloc( sizeof( struct customer ) ); 
Given the sample allocation for the pointer "ptr" found above, which one of the following statements is used to reallocate ptr to be an array of 10 elements? 
Option 1
ptr = realloc( ptr, 10 * sizeof( struct customer)); [Ans] 
Option 2
realloc( ptr, 9 * sizeof( struct customer ) ); 
Option 3
ptr += malloc( 9 * sizeof( struct customer ) ); 
Option 4
ptr = realloc( ptr, 9 * sizeof( struct customer ) ); 
Option 5
realloc( ptr, 10 * sizeof( struct customer ) ); 
____________________________________________________________

(Q :  36)
Which one of the following is a true statement about pointers? 
Option 1
Pointer arithmetic is permitted on pointers of any type. 
Option 2
A pointer of type void * can be used to directly examine or modify an object of any type. 
Option 3
Standard C mandates a minimum of four levels of indirection accessible through a pointer. 
Option 4
A C program knows the types of its pointers and indirectly referenced data items at runtime. 
Option 5
Pointers may be used to simulate call-by-reference. 
____________________________________________________________

(Q :  37)
Which one of the following functions returns the string representation from a pointer to a time_t value? 
Option 1
localtime 
Option 2
gmtime 
Option 3
strtime 
Option 4
asctime 
Option 5
ctime 
____________________________________________________________

(Q :  38)
Code:
 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? 
Option 1
It will not compile because not enough initializers are given. 
Option 2
Option 3
Option 4
12 
Option 5
24 [Ans]
____________________________________________________________

(Q :  39)
char buf [] = "Hello world!"; 

char * buf = "Hello world!"; 
In terms of code generation, how do the two definitions of buf, both presented above, differ? 
Option 1
The first definition certainly allows the contents of buf to be safely modified at runtime; the second definition does not. 
Option 2
The first definition is not suitable for usage as an argument to a function call; the second definition is. 
Option 3
The first definition is not legal because it does not indicate the size of the array to be allocated; the second definition is legal. 
Option 4
They do not differ -- they are functionally equivalent. [Ans]
Option 5
The first definition does not allocate enough space for a terminating NUL-character, nor does it append one; the second definition does. 
____________________________________________________________

(Q :  40)
In a C expression, how is a logical AND represented? 
Option 1
@@ 
Option 2
|| 
Option 3
.AND. 
Option 4
&& [Ans]
Option 5
.AND 
____________________________________________________________

(Q :  41)
How do printf()'s format specifiers %e and %f differ in their treatment of floating-point numbers? 
Option 1
%e always displays an argument of type double in engineering notation; %f always displays an argument of type double in decimal notation. [Ans] 
Option 2
%e expects a corresponding argument of type double; %f expects a corresponding argument of type float. 
Option 3
%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. 
Option 4
%e displays an argument of type double with trailing zeros; %f never displays trailing zeros. 
Option 5
%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 code. 
____________________________________________________________

(Q :  42)
Which one of the following Standard C functions can be used to reset end-of-file and error conditions on an open stream? 
Option 1
clearerr() 
Option 2
fseek() 
Option 3
ferror() 
Option 4
feof() 
Option 5
setvbuf() 
____________________________________________________________

(Q :  43)
Which one of the following will read a character from the keyboard and will store it in the variable c? 
Option 1
c = getc(); 
Option 2
getc( &c ); 
Option 3
c = getchar( stdin ); 
Option 4
getchar( &c ) 
Option 5
c = getchar(); [Ans] 
____________________________________________________________

(Q :  44)
Code:
#include <stdio.h> 
int i; 
void increment( int i ) 
   i++; 

int main() 
   for( i = 0; i < 10; increment( i ) ) 
   { 
   } 
   printf("i=%d\n", i); 
   return 0; 
}
What will happen when the program above is compiled and executed? 
Option 1
It will not compile. 
Option 2
It will print out: i=9. 
Option 3
It will print out: i=10. 
Option 4
It will print out: i=11. 
Option 5
It will loop indefinitely.[Ans] 
____________________________________________________________

(Q :  45)
Code:
char ptr1[] = "Hello World"; 
char *ptr2 = malloc( 5 ); 
ptr2 = ptr1;
What is wrong with the above code (assuming the call to malloc does not fail)? 
Option 1
There will be a memory overwrite. 
Option 2
There will be a memory leak. 
Option 3
There will be a segmentation fault. 
Option 4
Not enough space is allocated by the malloc. 
Option 5
It will not compile. 
____________________________________________________________

(Q :  46)
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? 
Option 1
i = 5[Ans] 
Option 2
i = 8 
Option 3
i = 9 
Option 4
i = 10 
Option 5
i = 18 
____________________________________________________________

(Q :  47)
Which one of the following C operators is right associative? 
Option 1
= [Ans] 
Option 2
Option 3
[] 
Option 4
Option 5
-> 
____________________________________________________________

(Q :  48) 
What does the "auto" specifier do? 
Option 1
It automatically initializes a variable to 0;. 
Option 2
It indicates that a variable's memory will automatically be preserved.[Ans] 
Option 3
It automatically increments the variable when used. 
Option 4
It automatically initializes a variable to NULL. 
Option 5
It indicates that a variable's memory space is allocated upon entry into the block. 
____________________________________________________________

(Q :  49)
How do you include a system header file called sysheader.h in a C source file? 
Option 1
#include <sysheader.h> [Ans]
Option 2
#incl "sysheader.h" 
Option 3
#includefile <sysheader> 
Option 4
#include sysheader.h 
Option 5
#incl <sysheader.h> 

____________________________________________________________

(Q :  50) 
Which one of the following printf() format specifiers indicates to print a double value in decimal notation, left aligned in a 30-character field, to four (4) digits of precision? 
Option 1
%-30.4e 
Option 2
%4.30e 
Option 3
%-4.30f 
Option 4
%-30.4f [Ans] decimal notation 
Option 5
%#30.4f 
____________________________________________________________

(Q :  51)
Code:
int x = 0; 
for ( ; ; ) 
 if (x++ == 4)
  break; 
 continue; 
printf("x=%d\n", x);
What will be printed when the sample code above is executed? 
Option 1
x=0 
Option 2
x=1 
Option 3
x=4 
Option 4
x=5[Ans] 
Option 5
x=6 
____________________________________________________________

(Q :  52)
According to the Standard C specification, what are the respective minimum sizes (in bytes) of the following three data types: short; int; and long? 
Option 1
1, 2, 2 
Option 2
1, 2, 4 
Option 3
1, 2, 8 
Option 4
2, 2, 4[Ans] 
Option 5
2, 4, 8 
____________________________________________________________

(Q :  53)
Code:
int y[4] = {6, 7, 8, 9}; 
int *ptr = y + 2; 
printf("%d\n", ptr[ 1 ] );  (ptr+1 == ptr[1])
What is printed when the sample code above is executed? 
Option 1
Option 2
Option 3
Option 4
9[Ans] 
Option 5
The code will not compile. 
____________________________________________________________

(Q :  54)
penny = one 
nickel = five 
dime = ten 
quarter = twenty-five 
How is enum used to define the values of the American coins listed above? 
Option 1
enum coin {(penny,1), (nickel,5), (dime,10), (quarter,25)}; 
Option 2
enum coin ({penny,1}, {nickel,5}, {dime,10}, {quarter,25}); 
Option 3
enum coin {penny=1,nickel=5,dime=10,quarter=25};[Ans] 
Option 4
enum coin (penny=1,nickel=5,dime=10,quarter=25); 
Option 5
enum coin {penny, nickel, dime, quarter} (1, 5, 10, 25); 
____________________________________________________________

(Q :  55)
char txt [20] = "Hello world!\0"; 
How many bytes are allocated by the definition above? 
Option 1
11 bytes 
Option 2
12 bytes 
Option 3
13 bytes 
Option 4
20 bytes[Ans] 
Option 5
21 bytes 
____________________________________________________________

(Q :  56)
Code:
int i = 4; 
int x = 6; 
double z; 
z =  x / i; 
printf("z=%.2f\n", z);
What will print when the sample code above is executed? 
Option 1
z=0.00 
Option 2
z=1.00[Ans] 
Option 3
z=1.50 
Option 4
z=2.00 
Option 5
z=NULL 
____________________________________________________________

(Q :  57)
Which one of the following variable names is NOT valid? 
Option 1
go_cart 
Option 2
go4it 
Option 3
4season[Ans] 
Option 4
run4 
Option 5
_what 
____________________________________________________________

(Q :  58)
int a [8] = { 0, 1, 2, 3 }; 
The definition of a above explicitly initializes its first four elements. Which one of the following describes how the compiler treats the remaining four elements? 
Option 1
Standard C defines this particular behavior as implementation-dependent. The compiler writer has the freedom to decide how the remaining elements will be handled. 
Option 2
The remaining elements are initialized to zero(0).[Ans] 
Option 3
It is illegal to initialize only a portion of the array. Either the entire array must be initialized, or no part of it may be initialized. 
Option 4
As with an enum, the compiler assigns values to the remaining elements by counting up from the last explicitly initialized element. The final four elements will acquire the values 4, 5, 6, and 7, respectively. 
Option 5
They are left in an uninitialized state; their values cannot be relied upon. 
____________________________________________________________

(Q :  59)
Which one of the following is a true statement about pointers? 
Option 1
They are always 32-bit values. 
Option 2
For efficiency, pointer values are always stored in machine registers. 
Option 3
With the exception of generic pointers, similarly typed pointers may be subtracted from each other. 
Option 4
A pointer to one type may not be cast to a pointer to any other type. 
Option 5
With the exception of generic pointers, similarly typed pointers may be added to each other. 
____________________________________________________________

(Q :  60)
Which one of the following statements allocates enough space to hold an array of 10 integers that are initialized to 0? 
Option 1
int *ptr = (int *) malloc(10, sizeof(int)); 
Option 2
int *ptr = (int *) calloc(10, sizeof(int)); 
Option 3
int *ptr = (int *) malloc(10*sizeof(int)); [Ans]
Option 4
int *ptr = (int *) alloc(10*sizeof(int)); 
Option 5
int *ptr = (int *) calloc(10*sizeof(int)); 
____________________________________________________________

(Q :  61)
What are two predefined FILE pointers in C? 
Option 1
stdout and stderr 
Option 2
console and error 
Option 3
stdout and stdio 
Option 4
stdio and stderr 
Option 5
errout and conout 
____________________________________________________________

(Q :  62)
Code:
u32 X (f32 f)
 union { 
        f32 f; 
        u32 n; 
       } u; 
 u.f = f; 
 return u.n; 
}
Given the function X(), defined above, assume that u32 is a type-definition indicative of a 32-bit unsigned integer and that f32 is a type-definition indicative of a 32-bit floating-point number. 
Which one of the following describes the purpose of the function defined above? 
Option 1
X() effectively rounds f to the nearest integer value, which it returns. 
Option 2
X() effectively performs a standard typecast and converts f to a roughly equivalent integer. 
Option 3
X() preserves the bit-pattern of f, which it returns as an unsigned integer of equal size. 
Option 4
Since u.n is never initialized, X() returns an undefined value. This function is therefore a primitive pseudorandom number generator. 
Option 5
Since u.n is automatically initialized to zero (0) by the compiler, X() is an obtuse way of always obtaining a zero (0) value. 
____________________________________________________________

(Q :  63)
Code:
long factorial (long x) 
 ???? 
 return x * factorial(x - 1); 
}
With what do you replace the ???? to make the function shown above return the correct answer? 
Option 1
if (x == 0) return 0; 
Option 2
return 1; 
Option 3
if (x >= 2) return 2; 
Option 4
if (x == 0) return 1; 
Option 5
if (x <= 1) return 1; [Ans]{more probable} 
____________________________________________________________

(Q :  64)
Code:
( Increment each integer in the array 'p' of * size 'n'. ) 
void increment_ints (int p [(n)], int n) 
  assert(p != NULL);  ( Ensure that 'p' isn't a null pointer. ) 
  assert(n >= 0);  ( Ensure that 'n' is nonnegative. ) 
  while (n)  ( Loop over 'n' elements of 'p'. ) 
  { 
    *p++;          ( Increment *p. ) 
    p++, n--;      ( Increment p, decrement n. ) 
  } 
}
Consider the function increment_ints(), defined above. Despite its significant inline commentary, it contains an error. Which one of the following correctly assesses it? 
Option 1
*p++ causes p to be incremented before the dereference is performed, because both operators have equal precedence and are right associative. 
Option 2
An array is a nonmodifiable lvalue, so p cannot be incremented directly. A navigation pointer should be used in conjunction with p. 
Option 3
*p++ causes p to be incremented before the dereference is performed, because the autoincrement operator has higher precedence than the indirection operator. 
Option 4
The condition of a while loop must be a Boolean expression. The condition should be n != 0. 
Option 5
An array cannot be initialized to a variable size. The subscript n should be removed from the definition of the parameter p. 
____________________________________________________________

(Q :  65)
How is a variable accessed from another file? 
Option 1
The global variable is referenced via the extern specifier.[Ans] 
Option 2
The global variable is referenced via the auto specifier. 
Option 3
The global variable is referenced via the global specifier. 
Option 4
The global variable is referenced via the pointer specifier. 
Option 5
The global variable is referenced via the ext specifier. 
____________________________________________________________

(Q :  66)
When applied to a variable, what does the unary "&" operator yield? 
Option 1
The variable's value 
Option 2
The variable's binary form 
Option 3
The variable's address [Ans] 
Option 4
The variable's format 
Option 5
The variable's right value 
____________________________________________________________

(Q :  67)
Code:
( sys/cdef.h ) 
#if     defined(__STDC__) || defined(__cplusplus) 
#define __P(protos)   protos 
#else 
#define __P(protos)   () 
#endif 
( stdio.h ) 
#include <sys/cdefs.h> 
div_t div __P((int, int));
The code above comes from header files for the FreeBSD implementation of the C library. What is the primary purpose of the __P() macro? 
Option 1
The __P() macro has no function, and merely obfuscates library function declarations. It should be removed from further releases of the C library. 
Option 2
The __P() macro provides forward compatibility for C++ compilers, which do not recognize Standard C prototypes. 
Option 3
Identifiers that begin with two underscores are reserved for C library implementations. It is impossible to determine the purpose of the macro from the context given. 
Option 4
The __P() macro provides backward compatibility for K&R C compilers, which do not recognize Standard C prototypes. 
Option 5
The __P() macro serves primarily to differentiate library functions from application-specific functions. 
____________________________________________________________

(Q :  68)
Which one of the following is NOT a valid identifier? 
Option 1
__ident 
Option 2
auto [Ans]
Option 3
bigNumber 
Option 4
g42277 
Option 5
peaceful_in_space 
____________________________________________________________

(Q :  69)
( Read an arbitrarily long string. ) 
Code:
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; 
}
The function read_long_string(), defined above, contains an error that may be particularly visible under heavy stress. Which one of the following describes it? 
Option 1
The write to *buf is blocked by the const qualifications applied to its type. 
Option 2
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. 
Option 3
The symbol stdin may not be defined on some ANCI C compliant systems. 
Option 4
The else causes fwd to contain an errant address. 
Option 5
If the call to realloc() fails during any iteration but the first, all memory previously allocated by the loop is leaked. 
____________________________________________________________

(Q :  70)
Code:
FILE *f = fopen( fileName, "r" ); 
readData( f ); 
if( ???? ) 
 puts( "End of file was reached" ); 
}
Which one of the following can replace the ???? in the code above to determine if the end of a file has been reached? 
Option 1
f == EOF[Ans] 
Option 2
feof( f ) 
Option 3
eof( f ) 
Option 4
f == NULL 
Option 5
!f 
____________________________________________________________

(Q :  71)
Global variables that are declared static are ____________. 
Which one of the following correctly completes the sentence above? 
Option 1
Deprecated by Standard C 
Option 2
Internal to the current translation unit 
Option 3
Visible to all translation units 
Option 4
Read-only subsequent to initialization 
Option 5
Allocated on the heap[Ans]
____________________________________________________________

(Q :  72)
( Read a double value from fp. ) 
Code:
double read_double (FILE * fp) { 
  double d; 
  assert(fp != NULL); 
  fscanf(fp, " %lf", d); 
  return d; 
}
The code above contains a common error. Which one of the following describes it? 
Option 1
fscanf() will fail to match floating-point numbers not preceded by whitespace. 
Option 2
The format specifier %lf indicates that the corresponding argument should be long double rather than double.  
Option 3
The call to fscanf() requires a pointer as its last argument. 
Option 4
The format specifier %lf is recognized by fprintf() but not by fscanf(). 
Option 5
d must be initialized prior to usage. 
____________________________________________________________

(Q :  73)
Which one of the following is NOT a valid C identifier? 
Option 1
___S 
Option 2
1___ [Ans]
Option 3
___1 
Option 4
___ 
Option 5
S___ 
____________________________________________________________

(Q :  74)
According to Standard C, what is the type of an unsuffixed floating-point literal, such as 123.45? 
Option 1
long double 
Option 2
Unspecified 
Option 3
float[Ans] 
Option 4
double 
Option 5
signed float 
____________________________________________________________

(Q :  75)
Which one of the following is true for identifiers that begin with an underscore? 
Option 1
They are generally treated differently by preprocessors and compilers from other identifiers. 
Option 2
They are case-insensitive. 
Option 3
They are reserved for usage by standards committees, system implementers, and compiler engineers. 
Option 4
Applications programmers are encouraged to employ them in their own code in order to mark certain symbols for internal usage. Option 5
They are deprecated by Standard C and are permitted only for backward compatibility with older C libraries. 
____________________________________________________________

(Q :  76)
Which one of the following is valid for opening a read-only ASCII file? Option 1
fileOpen (filenm, "r"); 
Option 2
fileOpen (filenm, "ra"); 
Option 3
fileOpen (filenm, "read"); 
Option 4
fopen (filenm, "read"); 
Option 5
fopen (filenm, "r");[Ans] 
____________________________________________________________

(Q :  77)
f = fopen( filename, "r" ); 
Referring to the code above, what is the proper definition for the variable f? 
Option 1
FILE f; 
Option 2
FILE *f;[Ans] 
Option 3
int f; 
Option 4
struct FILE f; 
Option 5
char *f; 
____________________________________________________________

(Q :  78)
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? 
Option 1
flush() 
Option 2
output() 
Option 3
fflush() 
Option 4
dump() 
Option 5
write() 
____________________________________________________________

(Q :  79)
short int x; ( assume x is 16 bits in size ) 
What is the maximum number that can be printed using printf("%d\n", x), assuming that x is initialized as shown above? 
Option 1
127 
Option 2
128 
Option 3
255 
Option 4
32,767 [Ans] 
Option 5
65,536 
____________________________________________________________

(Q :  80)
Code:
void crash (void) 
 printf("got here"); 
 *((char *) 0) = 0; 
}
The function crash(), defined above, 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? 
Option 1
The C standard says that dereferencing a null pointer causes undefined behavior. This may explain why printf() apparently fails. 
Option 2
If the standard output stream is buffered, the library buffers may not be flushed before the crash occurs. 
Option 3
printf() always buffers output until a newline character appears in the buffer. Since no newline was present in the format string, nothing is printed. 
Option 4
There is insufficient information to determine why the output fails to appear. A broader context is required. 
Option 5
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. 
____________________________________________________________

(Q :  81)
Code:
char * dwarves [] = { 
  "Sleepy", 
  "Dopey" "Doc", 
  "Happy", 
  "Grumpy" "Sneezy", 
  "Bashful", 
};
How many elements does the array dwarves (declared above) contain? Assume the C compiler employed strictly complies with the requirements of Standard C. 
Option 1
Option 2
5[Ans]
Option 3
Option 4
Option 5
____________________________________________________________

(Q :  82)
Which one of the following can be used to test arrays of primitive quantities for strict equality under Standard C? 
Option 1
qsort() 
Option 2
bcmp() 
Option 3
memcmp() 
Option 4
strxfrm() 
Option 5
bsearch() 
____________________________________________________________

(Q :  83)
Code:
int debug (const char * fmt, ...) { 
  extern FILE * logfile; 
  va_list args; 
  assert(fmt); 
  args = va_arg(fmt, va_list); 
  return vfprintf(logfile, fmt, args); 
}
The function debug(), defined above, contains an error. Which one of the following describes it? 
Option 1
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 code. 
Option 2
vfprintf() does not conform to ISO 9899: 1990, and may not be portable. 
Option 3
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(). 
Option 4
The argument list args has been improperly initialized. 
Option 5
Variadic functions are discontinued by Standard C; they are legacy constructs from K&R C, and no longer compile under modern compilers. 
____________________________________________________________

(Q :  84)
Code:
char *buffer = "0123456789"; 
char *ptr = buffer; 
ptr += 5; 
printf( "%s\n", ptr ); 
printf( "%s\n", buffer );
What will be printed when the sample code above is executed? 
Option 1
0123456789 
56789 
Option 2
5123456789 
5123456789 
Option 3
56789 
56789 
Option 4
0123456789 
0123456789 
Option 5
56789 
0123456789 [Ans] 
____________________________________________________________

(Q :  85)
( Get the rightmost path element of a Unix path. ) 
Code:
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; 
}
The function get_rightmost(), defined above, contains an error. Which one of the following describes it? 
Option 1
The calls to memset() and memcpy() illegally perform a pointer conversion on rightmost without an appropriate cast. 
Option 2
The code does not correctly handle the situation where a directory separator '/' is the final character. 
Option 3
The if condition contains an incorrectly terminated character literal. 
Option 4
memcpy() cannot be used safely to copy string data. 
Option 5
The return value of get_rightmost() will be invalid in the caller's context. 
____________________________________________________________

(Q :  86)
What number is equivalent to -4e3? 
Option 1
-4000 [Ans] 
Option 2
-400 
Option 3
-40 
Option 4
.004 
Option 5
.0004 
____________________________________________________________

(Q :  87)
Code:
void freeList( struct node *n ) 
 while( n ) 
 { 
  ???? 
 } 
}
Which one of the following can replace the ???? for the function above to release the memory allocated to a linked list? 
Option 1
n = n->next; 
free( n ); 
Option 2
struct node m = n; 
n = n->next; 
free( m ); 
Option 3
struct node m = n; 
free( n ); 
n = m->next; 
Option 4
free( n ); 
n = n->next; 
Option 5
struct node m = n; 
free( m ); 
n = n->next; 
____________________________________________________________

(Q :  88)
How does variable definition differ from variable declaration? 
Option 1
Definition allocates storage for a variable, but declaration only informs the compiler as to the variable's type. 
Option 2
Declaration allocates storage for a variable, but definition only informs the compiler as to the variable's type. 
Option 3
Variables may be defined many times, but may be declared only once.[Ans] 
Option 4
Variable definition must precede variable declaration. 
Option 5
There is no difference in C between variable declaration and variable definition. 
____________________________________________________________

(Q :  89)
Code:
int x[] = {1, 2, 3, 4, 5}; 
int u; 
int *ptr = x; 
???? 
for( u = 0; u < 5; u++ )
{
 printf("%d-", x[u]); 
}
printf( "\n" ); 
Which one of the following statements could replace the ???? in the code above to cause the string 1-2-3-10-5- to be printed when the code is executed? 
Option 1
*ptr + 3 = 10; 
Option 2
*ptr[ 3 ] = 10; 
Option 3
*(ptr + 3) = 10;[Ans] 
Option 4
(*ptr)[ 3 ] = 10; 
Option 5
*(ptr[ 3 ]) = 10; 
____________________________________________________________

(Q :  90)
Code:
(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;
}
The function sum_array(), defined above, contains an error. Which one of the following accurately describes it? 
Option 1
The range of the loop does not match the bounds of the array d. 
Option 2
The loop processes the incorrect number of elements. 
Option 3
total is initialized with an integer literal. The two are not compatible in an assignment. 
Option 4
The code above fails to compile if there are no registers available for i. 
Option 5
The formal parameter d should be declared as double * d to allow dynamically allocated arrays as arguments. 
____________________________________________________________

(Q :  91)
Code:
#include <stdio.h> 
void func() 
   int x = 0; 
   static int y = 0; 
   x++; y++; 
   printf( "%d -- %d\n", x, y ); 

int main() 
   func(); 
   func(); 
   return 0; 
}
What will the code above print when it is executed? 
Option 1
1 -- 1 
1 -- 1 
Option 2
1 -- 1 
2 -- 1 
Option 3
1 -- 1 
2 -- 2 
Option 4
1 -- 0 
1 -- 0 
Option 5
1 -- 1 
1 -- 2 [Ans]
____________________________________________________________

(Q :  92) $$No other seems that sound$$
Code:
int fibonacci (int n)
 switch (n)
 { 
  default: 
      return (fibonacci(n - 1) + fibonacci(n - 2)); 
  case 1: 
  case 2: 
 } 
  return 1; 
}
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 above? 
Option 1
For some values of n, the environment will almost certainly exhaust its stack space before the calculation completes.
[Ans] 
Option 2
An error in the algorithm causes unbounded recursion for all values of n. 
Option 3
A break statement should be inserted after each case. Fall-through is not desirable here. 
Option 4
The fibonacci() function includes calls to itself. This is not directly supported by Standard C due to its unreliability. 
Option 5
Since the default case is given first, it will be executed before any case matching n. 
____________________________________________________________

(Q :  93)
When applied to a variable, what does the unary "&" operator yield? 
Option 1
The variable's address [Ans]
Option 2
The variable's right value 
Option 3
The variable's binary form 
Option 4
The variable's value 
Option 5
The variable's format 
____________________________________________________________

(Q :  94)
short int x; ( assume x is 16 bits in size ) 
What is the maximum number that can be printed using printf("%d\n", x), assuming that x is initialized as shown above? 
Option 1
127 
Option 2
128 
Option 3
255 
Option 4
32,767[Ans] 
Option 5
65,536 
____________________________________________________________

(Q :  95)
int x = 011 | 0x10; 
What value will x contain in the sample code above? 
Option 1
Option 2
13 
Option 3
19 
Option 4
25 
Option 5
27 
____________________________________________________________

(Q :  96)
Which one of the following calls will open the file test.txt for reading by fgetc? 
Option 1
fopen( "test.txt", "r" ); 
Option 2
read( "test.txt" ) 
Option 3
fileopen( "test.txt", "r" ); 
Option 4
fread( "test.txt" ) 
Option 5
freopen( "test.txt" ) 
____________________________________________________________

(Q :  97)
Code:
int m = -14; 
int n = 6; 
int o; 
o = m % ++n; 
n += m++ - o; 
m <<= (o ^ n) & 3;
Assuming two's-complement arithmetic, which one of the following correctly represents the values of m, n, and o after the execution of the code above? 
Option 1
m = -26, n = -7, o = 0 
Option 2
m = -52, n = -4, o = -2 
Option 3
m = -26, n = -5, o = -2 
Option 4
m = -104, n = -7, o = 0 
Option 5
m = -52, n = -6, o = 0 
____________________________________________________________

(Q :  98)
How do printf()'s format specifiers %e and %f differ in their treatment of floating-point numbers? 
Option 1
%e displays an argument of type double with trailing zeros; %f never displays trailing zeros. 
Option 2
%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. 
Option 3
%e always displays an argument of type double in engineering notation; %f always displays an argument of type double in decimal notation. [Ans]
Option 4
%e expects a corresponding argument of type double; %f expects a corresponding argument of type float. 
Option 5
%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 code. 
____________________________________________________________

(Q :  99) $$Except 1 all choices are O.K.$$
c = getchar(); 
What is the proper declaration for the variable c in the code above? 
Option 1
char *c; 
Option 2
unsigned int c; 
Option 3
int c; 
Option 4
unsigned char c; 
Option 5
char c;[Ans]
____________________________________________________________

(Q :  100)
Which one of the following will define a function that CANNOT be called from another source file? 
Option 1
void function() { ... } 
Option 2
extern void function() { ... } 
Option 3
const void function() { ... } 
Option 4
private void function() { ... } 
Option 5
static void function() { ... } 
____________________________________________________________    

No comments:

Post a Comment

Live

Your Ad Here