objective type question for c

50
Question 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? 1) 5 2) 6 3) 10 [Ans] Corrected by buddy by running the program 4) 11 5) 12 Question 2 With every use of a memory allocation function, what function should be used to release allocated memory which is no longer needed? 1) unalloc() 2) dropmem() 3) dealloc() 4) release() 5) free() [Ans] Question 3 Code: void *ptr; myStruct myArray[10]; ptr = myArray; Which of the following is the correct way to increment the variable "ptr"? 1) ptr = ptr + sizeof(myStruct); [Ans] 2) ++(int*)ptr; 3) ptr = ptr + sizeof(myArray); 4) increment(ptr); 5) ptr = ptr + sizeof(ptr); Question 4 Code: char* myFunc (char *ptr) { ptr += 3; return (ptr); } int main() { char *x, *y; x = "HELLO";

Upload: mohnishcsitdurg

Post on 08-Apr-2015

860 views

Category:

Documents


10 download

TRANSCRIPT

Page 1: Objective Type Question for C

Question 1Code: 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?1) 52) 63) 10 [Ans] Corrected by buddy by running the program4) 115) 12 Question 2With every use of a memory allocation function, what function should be used to release allocated memory which is no longer needed?1) unalloc()2) dropmem()3) dealloc()4) release()5) free() [Ans]Question 3Code:void *ptr;myStruct myArray[10];ptr = myArray;Which of the following is the correct way to increment the variable "ptr"?1) ptr = ptr + sizeof(myStruct); [Ans]2) ++(int*)ptr;3) ptr = ptr + sizeof(myArray);4) increment(ptr);5) ptr = ptr + sizeof(ptr); Question 4Code: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?1) y = HELLO2) y = ELLO

Page 2: Objective Type Question for C

3) y = LLO4) y = LO [Ans]5) x = O Question 5Code: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?1) It will work correctly since the for loop covers the entire list.2) It may fail since each node "nPtr" is freed before its next address can be accessed.3) In the for loop, the assignment "nPtr=nPtr->next" should be changed to "nPtr=nPtr.next".4) This is invalid syntax for freeing memory.5) The loop will never end. Question 6What function will read a specified number of elements from a file?1) fileread()2) getline()3) readfile()4) fread()5) gets()Question 7"My salary was increased by 15%!"Select the statement which will EXACTLY reproduce the line of text above.1) printf("\"My salary was increased by 15/%\!\"\n");2) printf("My salary was increased by 15%!\n");3) printf("My salary was increased by 15'%'!\n");4) printf("\"My salary was increased by 15%%!\"\n");[Ans]5) printf("\"My salary was increased by 15'%'!\"\n");Question 8What is a difference between a declaration and a definition of a variable?1) Both can occur multiple times, but a declaration must occur first.2) There is no difference between them.3) A definition occurs once, but a declaration may occur many times.4) A declaration occurs once, but a definition may occur many times. [Ans]5) Both can occur multiple times, but a definition must occur first.Question 9int 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?1) 32) 53) 74) 95) 11[Ans]

Page 3: Objective Type Question for C

Question 10Code: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 executed1) 12,10,11,132) 22,10,11,133) 22,11,11,114) 12,11,11,115) 22,13,13,13[Ans] Question 11Code: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?1) -32) 03) 4[Ans]4) 4 + sizeof( int )5) 4 * sizeof( int Question 12Code: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?1) 1, 2, 3, 4, 5, 5,2) 4, 3, 2, 1, 0, 0,3) 5, 4, 3, 2, 1, 0,4) 0, 0, 1, 2, 3, 4, [Ans]5) 0, 1, 2, 3, 4, 5, Question 1311 ^ 5What does the operation shown above produce?1) 12) 6

Page 4: Objective Type Question for C

3) 84) 14 [Ans]5) 15Question 14#define MAX_NUM 15Referring to the sample above, what is MAX_NUM?1) MAX_NUM is an integer variable.2) MAX_NUM is a linker constant.3) MAX_NUM is a precompiler constant.4) MAX_NUM is a preprocessor macro. [Ans]5) MAX_NUM is an integer constant.Question 15Which one of the following will turn off buffering for stdout?1) setbuf( stdout, FALSE );2) setvbuf( stdout, NULL );3) setbuf( stdout, NULL );4) setvbuf( stdout, _IONBF );5) setbuf( stdout, _IONBF );Question 16What is a proper method of opening a file for writing as binary file?1) FILE *f = fwrite( "test.bin", "b" );2) FILE *f = fopenb( "test.bin", "w" );3) FILE *f = fopen( "test.bin", "wb" );4) FILE *f = fwriteb( "test.bin" );5) FILE *f = fopen( "test.bin", "bw" );Question 17Which one of the following functions is the correct choice for moving blocks of binary data that are of arbitrary size and position in memory?1) memcpy()2) memset()3) strncpy()4) strcpy()5) memmove()[Ans]Question 18int x = 2 * 3 + 4 * 5;What value will x contain in the sample code above?1) 222) 26[Ans]3) 464) 505) 70Question 19Code:void * array_dup (a, number, size) const void * a; size_t number; size_t size;

Page 5: Objective Type Question for C

{ 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?1) If the arguments to memcpy() refer to overlapping regions, the destination buffer will be subject to memory corruption.2) array_dup() declares its first parameter to be a pointer, when the actual argument will be an array.3) The memory obtained from alloca() is not valid in the context of the caller. Moreover, alloca() is nonstandard.4) size_t is not a Standard C defined type, and may not be known to the compiler.5) The definition of array_dup() is unusual. Functions cannot be defined using this syntax. Question 20int var1;If a variable has been declared with file scope, as above, can it safely be accessed globally from another file?1) Yes; it can be referenced through the register specifier.2) No; it would have to have been initially declared as a static variable.3) No; it would need to have been initially declared using the global keyword.[Ans]4) Yes; it can be referenced through the publish specifier.5) Yes; it can be referenced through the extern specifier.Question 21time_t t;Which one of the following statements will properly initialize the variable t with the current time from the sample above?1) t = clock();[Ans]2) time( &t );3) t = ctime();4) t = localtime();5) None of the aboveQuestion 22Which one of the following provides conceptual support for function calls?1) The system stack[Ans]2) The data segment3) The processor's registers4) The text segment5) The heap

Page 6: Objective Type Question for C

Question 23C is which kind of language?1) Machine2) Procedural[Ans]3) Assembly4) Object-oriented5) Strictly-typedQuestion 24Code: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?1) 12) 23) 34) 45) 5 [Ans] 00,10,01,11,12 Question 25Code: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?1) x=02) x=13) x=34) x=4[Ans]5) x=5 Question 26Code: 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?1) 12) 2[Ans]3) 3

Page 7: Objective Type Question for C

4) 45) 5 Question 27Code:char *ptr; char myString[] = "abcdefg"; ptr = myString; ptr += 5;What string does ptr point to in the sample code above?1) fg [Ans]because string2) efg3) defg4) cdefg5) None of the above Question 28Code: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=31) -3 : 4, -4 : 3 [Ans]2) -4 : 4, -3 : 33) -4 : 3, -4 : 34) -4 : 3, -3 : 45) -3 : 3, -4 : 4 Question 29Which one of the following will declare a pointer to an integer at address 0x200 in memory?1) int *x;*x = 0x200;[Ans]2) int *x = &0x200;3) int *x = *0x200;4) int *x = 0x200;5) int *x( &0x200 );Question 30Code: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?1) 4

Page 8: Objective Type Question for C

2) 53) 6 [Ans]4) 75) 8 Question 31Code:x = 3, counter = 0; while ((x-1)) { ++counter; x--; }Referring to the sample code above, what value will the variable counter have when completed?1) 02) 13) 2[Ans]4) 35) 4 Question 32char ** array [12][12][12];Consider array, defined above. Which one of the following definitions and initializations of p is valid?1) char ** (* p) [12][12] = array; [Ans]2) char ***** p = array;3) char * (* p) [12][12][12] = array;4) const char ** p [12][12][12] = array;5) char (** p) [12][12] = array;Question 33void (*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);1) typedef void (*sighandler_t) (int);[Ans]2) typedef sighandler_t void (*) (int);3) typedef void *sighandler_t (int);4) #define sighandler_t(x) void (*x) (int)5) #define sighandler_t void (*) (int)Question 34All of the following choices represent syntactically correct function definitions. Which one of the following represents a semantically legal function definition in Standard C?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]))

Page 9: Objective Type Question for C

cnt++;

return cnt; }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; }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; }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; }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; }Question 35struct 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?1) ptr = realloc( ptr, 10 * sizeof( struct customer)); [Ans]2) realloc( ptr, 9 * sizeof( struct customer ) );3) ptr += malloc( 9 * sizeof( struct customer ) );

Page 10: Objective Type Question for C

4) ptr = realloc( ptr, 9 * sizeof( struct customer ) );5) realloc( ptr, 10 * sizeof( struct customer ) ); Question 36Which one of the following is a true statement about pointers?1) Pointer arithmetic is permitted on pointers of any type.2) A pointer of type void * can be used to directly examine or modify an object of any type.3) Standard C mandates a minimum of four levels of indirection accessible through a pointer.4) A C program knows the types of its pointers and indirectly referenced data items at runtime.5) Pointers may be used to simulate call-by-reference.Question 37Which one of the following functions returns the string representation from a pointer to a time_t value?1) localtime2) gmtime3) strtime4) asctime5) ctimeQuestion 38Code: 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) 63) 74) 125) 24 [Ans] Question 39char buf [] = "Hello world!";

char * buf = "Hello world!";In terms of code generation, how do the two definitions of buf, both presented above, differ?1) The first definition certainly allows the contents of buf to be safely modified at runtime; the second definition does not.2) The first definition is not suitable for usage as an argument to a function call; the second definition is.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.4) They do not differ -- they are functionally equivalent. [Ans]5) The first definition does not allocate enough space for a terminating NUL-character, nor does it append one; the second definition does.Question 40In a C expression, how is a logical AND represented?1) @@

Page 11: Objective Type Question for C

2) ||3) .AND.4) && [Ans]5) ANDQuestion 41How do printf()'s format specifiers %e and %f differ in their treatment of floating-point numbers?1) %e always displays an argument of type double in engineering notation; %f always displays an argument of type double in decimal notation. [Ans]2) %e expects a corresponding argument of type double; %f expects a corresponding argument of type float.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.4) %e displays an argument of type double with trailing zeros; %f never displays trailing zeros.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.Question 42Which one of the following Standard C functions can be used to reset end-of-file and error conditions on an open stream?1) clearerr()2) fseek()3) ferror()4) feof()5) setvbuf()Question 43Which one of the following will read a character from the keyboard and will store it in the variable c?1) c = getc();2) getc( &c );3) c = getchar( stdin );4) getchar( &c )5) c = getchar(); [Ans]Question 44Code:#include <stdio.h> int i; void increment( int i ) { i++; }

int main() { for( i = 0; i < 10; increment( i ) ) { }

Page 12: Objective Type Question for C

printf("i=%d\n", i); return 0; }What will happen when the program above is compiled and executed?1) It will not compile.2) It will print out: i=9.3) It will print out: i=10.4) It will print out: i=11.5) It will loop indefinitely.[Ans] Question 45Code: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)?1) There will be a memory overwrite.2) There will be a memory leak.3) There will be a segmentation fault.4) Not enough space is allocated by the malloc.5) It will not compile.

Question 46Code: 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[Ans]2) i = 8

Page 13: Objective Type Question for C

3) i = 94) i = 105) i = 18

Question 47Which one of the following C operators is right associative?1) = [Ans]2) ,3) []4) ^5) -> Question 48What does the "auto" specifier do?1) It automatically initializes a variable to 0;.2) It indicates that a variable's memory will automatically be preserved.[Ans]3) It automatically increments the variable when used.4) It automatically initializes a variable to NULL.5) It indicates that a variable's memory space is allocated upon entry into the block.Question 49How do you include a system header file called sysheader.h in a C source file?1) #include <sysheader.h> [Ans]2) #incl "sysheader.h"3) #includefile <sysheader>4) #include sysheader.h5) #incl <sysheader.h>Question 50Which 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?1) %-30.4e2) %4.30e3) %-4.30f4) %-30.4f [Ans] decimal notation5) %#30.4f

Question 51Code: 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?1) x=0

Page 14: Objective Type Question for C

2) x=13) x=44) x=5[Ans]5) x=6 Question 52According to the Standard C specification, what are the respective minimum sizes (in bytes) of the following three data types: short; int; and long?1) 1, 2, 22) 1, 2, 43) 1, 2, 84) 2, 2, 4[Ans]5) 2, 4, 8Question 53Code: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?1) 62) 73) 84) 9[Ans]5) The code will not compile. Question 54penny = onenickel = fivedime = tenquarter = twenty-fiveHow is enum used to define the values of the American coins listed above?1) enum coin {(penny,1), (nickel,5), (dime,10), (quarter,25)};2) enum coin ({penny,1}, {nickel,5}, {dime,10}, {quarter,25});3) enum coin {penny=1,nickel=5,dime=10,quarter=25};[Ans]4) enum coin (penny=1,nickel=5,dime=10,quarter=25);5) enum coin {penny, nickel, dime, quarter} (1, 5, 10, 25);Question 55char txt [20] = "Hello world!\0";How many bytes are allocated by the definition above?1) 11 bytes2) 12 bytes3) 13 bytes4) 20 bytes[Ans]5) 21 bytesQuestion 56Code:int i = 4; int x = 6; double z;

Page 15: Objective Type Question for C

z = x / i; printf("z=%.2f\n", z);What will print when the sample code above is executed?1) z=0.002) z=1.00[Ans]3) z=1.504) z=2.005) z=NULL Question 57Which one of the following variable names is NOT valid?1) go_cart2) go4it3) 4season[Ans]4) run45) _whatQuestion 58int 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?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.2) The remaining elements are initialized to zero(0).[Ans]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.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.5) They are left in an uninitialized state; their values cannot be relied upon.Question 59Which one of the following is a true statement about pointers?1) They are always 32-bit values.2) For efficiency, pointer values are always stored in machine registers.3) With the exception of generic pointers, similarly typed pointers may be subtracted from each other.4) A pointer to one type may not be cast to a pointer to any other type.5) With the exception of generic pointers, similarly typed pointers may be added to each other.Question 60Which one of the following statements allocates enough space to hold an array of 10 integers that are initialized to 0?1) int *ptr = (int *) malloc(10, sizeof(int));2) int *ptr = (int *) calloc(10, sizeof(int));3) int *ptr = (int *) malloc(10*sizeof(int)); [Ans]4) int *ptr = (int *) alloc(10*sizeof(int));5) int *ptr = (int *) calloc(10*sizeof(int));Question 61What are two predefined FILE pointers in C?

Page 16: Objective Type Question for C

1) stdout and stderr2) console and error3) stdout and stdio4) stdio and stderr5) errout and conoutQuestion 62Code: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?1) X() effectively rounds f to the nearest integer value, which it returns.2) X() effectively performs a standard typecast and converts f to a roughly equivalent integer.3) X() preserves the bit-pattern of f, which it returns as an unsigned integer of equal size.4) Since u.n is never initialized, X() returns an undefined value. This function is therefore a primitive pseudorandom number generator.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. Question 63Code: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?1) if (x == 0) return 0;2) return 1;3) if (x >= 2) return 2;4) if (x == 0) return 1;5) if (x <= 1) return 1; [Ans]{more probable} Question 64Code: Increment each integer in the array 'p' of * size 'n'. void increment_ints (int p [n], int n) {

Page 17: Objective Type Question for C

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?1) *p++ causes p to be incremented before the dereference is performed, because both operators have equal precedence and are right associative.2) An array is a nonmodifiable lvalue, so p cannot be incremented directly. A navigation pointer should be used in conjunction with p.3) *p++ causes p to be incremented before the dereference is performed, because the autoincrement operator has higher precedence than the indirection operator.4) The condition of a while loop must be a Boolean expression. The condition should be n != 0.5) An array cannot be initialized to a variable size. The subscript n should be removed from the definition of the parameter p. Question 65How is a variable accessed from another file?1) The global variable is referenced via the extern specifier.[Ans]2) The global variable is referenced via the auto specifier.3) The global variable is referenced via the global specifier.4) The global variable is referenced via the pointer specifier.5) The global variable is referenced via the ext specifier.Question 66When applied to a variable, what does the unary "&" operator yield?1) The variable's value2) The variable's binary form3) The variable's address [Ans]4) The variable's format5) The variable's right valueQuestion 67Code: 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?

Page 18: Objective Type Question for C

1) The __P() macro has no function, and merely obfuscates library function declarations. It should be removed from further releases of the C library.2) The __P() macro provides forward compatibility for C++ compilers, which do not recognize Standard C prototypes.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.4) The __P() macro provides backward compatibility for K&R C compilers, which do not recognize Standard C prototypes.5) The __P() macro serves primarily to differentiate library functions from application-specific functions. Question 68Which one of the following is NOT a valid identifier?1) __ident2) auto [Ans]3) bigNumber4) g422775) peaceful_in_spaceQuestion 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?1) The write to *buf is blocked by the const qualifications applied to its type.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.3) The symbol stdin may not be defined on some ANCI C compliant systems.4) The else causes fwd to contain an errant address.

Page 19: Objective Type Question for C

5) If the call to realloc() fails during any iteration but the first, all memory previously allocated by the loop is leaked. Question 70Code: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?1) f == EOF[Ans]2) feof( f )3) eof( f )4) f == NULL5) !f Question 71Global variables that are declared static are ____________.Which one of the following correctly completes the sentence above?1) Deprecated by Standard C2) Internal to the current translation unit3) Visible to all translation units4) Read-only subsequent to initialization5) Allocated on the heap[Ans]Question 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?1) fscanf() will fail to match floating-point numbers not preceded by whitespace.2) The format specifier %lf indicates that the corresponding argument should be long double rather than double.3) The call to fscanf() requires a pointer as its last argument.4) The format specifier %lf is recognized by fprintf() but not by fscanf().5) d must be initialized prior to usage. Question 73Which one of the following is NOT a valid C identifier?1) ___S2) 1___ [Ans]3) ___1

Page 20: Objective Type Question for C

4) ___5) S___Question 74According to Standard C, what is the type of an unsuffixed floating-point literal, such as 123.45?1) long double2) Unspecified3) float[Ans]4) double5) signed floatQuestion 75Which one of the following is true for identifiers that begin with an underscore?1) They are generally treated differently by preprocessors and compilers from other identifiers.2) They are case-insensitive.3) They are reserved for usage by standards committees, system implementers, and compiler engineers.4) Applications programmers are encouraged to employ them in their own code in order to mark certain symbols for internal usage.5)They are deprecated by Standard C and are permitted only for backward compatibility with older C libraries.Question 76Which one of the following is valid for opening a read-only ASCII file?1) fileOpen (filenm, "r");2) fileOpen (filenm, "ra");3) fileOpen (filenm, "read");4) fopen (filenm, "read");5) fopen (filenm, "r");[Ans] Question 77f = fopen( filename, "r" );Referring to the code above, what is the proper definition for the variable f?1) FILE f;2) FILE *f;[Ans]3) int f;4) struct FILE f;5) char *f;Question 78If there is a need to see output as soon as possible, what function will force the output from the buffer into the output stream?1) flush()2) output()3) fflush()4) dump()5) write()Question 79short 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

Page 21: Objective Type Question for C

initialized as shown above?1) 1272) 1283) 2554) 32,767 [Ans]5) 65,536Question 80Code: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?1) The C standard says that dereferencing a null pointer causes undefined behavior. This may explain why printf() apparently fails.2) If the standard output stream is buffered, the library buffers may not be flushed before the crash occurs.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.4) There is insufficient information to determine why the output fails to appear. A broader context is required.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. Question 81Code: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.1) 42) 5[Ans]3) 64) 75) 8 Question 82Which one of the following can be used to test arrays of primitive quantities for strict equality under Standard C?1) qsort()

Page 22: Objective Type Question for C

2) bcmp()3) memcmp()4) strxfrm()5) bsearch()Question 83Code: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?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.2) vfprintf() does not conform to ISO 9899: 1990, and may not be portable.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().4) The argument list args has been improperly initialized.5) Variadic functions are discontinued by Standard C; they are legacy constructs from K&R C, and no longer compile under modern compilers. Question 84Code: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?1) 0123456789567892) 512345678951234567893) 56789567894) 012345678901234567895) 567890123456789 [Ans] Question 85 Get the rightmost path element of a Unix path. Code:char * get_rightmost (const char * d){

Page 23: Objective Type Question for C

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?1) The calls to memset() and memcpy() illegally perform a pointer conversion on rightmost without an appropriate cast.2) The code does not correctly handle the situation where a directory separator '/' is the final character.3) The if condition contains an incorrectly terminated character literal.4) memcpy() cannot be used safely to copy string data.5) The return value of get_rightmost() will be invalid in the caller's context. Question 86What number is equivalent to -4e3?1) -4000 [Ans]2) -4003) -404) .0045) .0004

Question 87Code: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?1) n = n->next;free( n );2) struct node m = n;n = n->next;free( m );

Page 24: Objective Type Question for C

3) struct node m = n;free( n );n = m->next;4) free( n );n = n->next;5) struct node m = n;free( m );n = n->next; Question 88How does variable definition differ from variable declaration?1) Definition allocates storage for a variable, but declaration only informs the compiler as to the variable's type.2) Declaration allocates storage for a variable, but definition only informs the compiler as to the variable's type.3) Variables may be defined many times, but may be declared only once.[Ans]4) Variable definition must precede variable declaration.5) There is no difference in C between variable declaration and variable definition.Question 89Code: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?1) *ptr + 3 = 10;2) *ptr[ 3 ] = 10;3) *(ptr + 3) = 10;[Ans]4) (*ptr)[ 3 ] = 10;5) *(ptr[ 3 ]) = 10; Question 90Code:sum_array() has been ported from FORTRAN. No * logical changes have been madedouble 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;}

Page 25: Objective Type Question for C

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

Page 26: Objective Type Question for C

} 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?1) For some values of n, the environment will almost certainly exhaust its stack space before the calculation completes.[Ans]2) An error in the algorithm causes unbounded recursion for all values of n.3) A break statement should be inserted after each case. Fall-through is not desirable here.4) The fibonacci() function includes calls to itself. This is not directly supported by Standard C due to its unreliability.5) Since the default case is given first, it will be executed before any case matching n. Question 93When applied to a variable, what does the unary "&" operator yield?1) The variable's address [Ans]2) The variable's right value3) The variable's binary form4) The variable's value5) The variable's formatQuestion 94short 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?1) 1272) 1283) 2554) 32,767[Ans]5) 65,536Question 95int x = 011 | 0x10;What value will x contain in the sample code above?1) 32) 133) 194) 255) 27Question 96Which one of the following calls will open the file test.txt for reading by fgetc?1) fopen( "test.txt", "r" );2) read( "test.txt" )3) fileopen( "test.txt", "r" );4) fread( "test.txt" )5) freopen( "test.txt" )Question 97Code:int m = -14;

Page 27: Objective Type Question for C

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?1) m = -26, n = -7, o = 02) m = -52, n = -4, o = -23) m = -26, n = -5, o = -24) m = -104, n = -7, o = 05) m = -52, n = -6, o = 0 Question 98How do printf()'s format specifiers %e and %f differ in their treatment of floating-point numbers?1) %e displays an argument of type double with trailing zeros; %f never displays trailing zeros.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.3) %e always displays an argument of type double in engineering notation; %f always displays an argument of type double in decimal notation. [Ans]4) %e expects a corresponding argument of type double; %f expects a corresponding argument of type float.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.Question 99$$Except 1 all choices are O.K.$$c = getchar();What is the proper declaration for the variable c in the code above?1) char *c;2) unsigned int c;3) int c;4) unsigned char c;5) char c;[Ans]Question 100Which one of the following will define a function that CANNOT be called from another source file?1) void function() { ... }2) extern void function() { ... }3) const void function() { ... }4) private void function() { ... }5) static void function() { ... }

Question 101 What will be output of following c program?

#include<stdio.h>void main(){

Page 28: Objective Type Question for C

char far *p=(char far *)0x55550005;char far *q=(char far *)0x53332225;*p=25;(*p)++;printf("%d",*q);

}Output: 26Explanation:Far address of p and q are representing same physical address. Physical address of 0x55550005 = 0x5555*ox10 + ox0005 = 0x55555 Physical address of 0x53332225 = 0x5333 * 0x10 + ox2225 = 0x55555*p =25, means content at memory location 0x55555 is assigning value 25(*p)++ means increase the content by one at memory location 0x5555 so now content at memory location 0x55555 is 26*q also means content at memory location 0x55555 which is 26

Question 102 What will be output of following c program?

#include<stdio.h>void main(){

char i=13;while(i){i++;}printf("%d",i);

}Output: 0Explanation:while(i) will true if i non negative. while(i) will false only when i=0. Since i is char type of data which is cyclic in nature. i will get maximum value to 127 the minimum value -128 then it will be zero .When i=0 ,while condition will false and i will come outside the loop and at that time value of I is zero.

Question 103 Write a c program without using == operator compare two numbers?Answer:#include<stdio.h>void main(){

int a=5,b=5;if(b^a)printf("EQUAL”);

}Output: EQUALQuestion 104 What will be output of following c program?

#include<stdio.h>void main(){

char a='\x82';

Page 29: Objective Type Question for C

printf("%d",a);}

Output:-126Explanation: \x symbol indicate the hexadecimal number system in character constant. So 82 is hexadecimal number its equivalent binary value is 130. 130 is beyond the range of signed char. Since signed char is cyclic in nature so its value will be130-256=-126

Question 105 What will be output of following c program?#include<stdio.h>void main(){

enum {a,b=32766,c,d,e=0};printf("%d",a);

}Output: errorExplanation: Value of enum constant must be within range of signed int .Maximum value of signed int is 32767 while value of d=32768

Question 106 What will be output of following c program?#include<stdio.h>#define char doublevoid main(){

char a=9;printf("%d",sizeof(a));

}Output: 8Explanation: #define is preprocessor directive which process before the actual compilation. #define paste value in the in place of macro in the program before actual compilation. New source code will be:#include<stdio.h>void main(){

double a=9;printf("%d",sizeof(a));

}Now a is double type of data which size is 8 byte.

Question 107 How you will copy the content of one array to another in another array by one line statement?Answer:

#include<stdio.h>void main(){

int i;

Page 30: Objective Type Question for C

struct arr1{int a[8];} ;struct arr1 p={1,2,3,4,5,6,7,8},q;q=p; //copy in one linefor(i=0;i<8;i++){printf("%d",q.a[i]); }

}Output: 12345678

Question 108 What will be output of following c program?#include<stdio.h>void main() {

char arr[8]={'S','A','V','R','I','Y','A'}; char *p; p=(char *)(arr+2)[2]; printf("%c",p);

}

Output: I

Question 109 What will be output of following c program?

#include<stdio.h>void main() {

float a=5e4;printf("%f",a);

}

Output: 50000.000000Explanation:5e4 is in exponential form is (5*10^4)

Question 110 What will be output of following c program?

#include<stdio.h>void main(){

float a=5e40; printf("%f",a);

}

Output: +INFExplanation: 5e40 is beyond the range of float so output is plus infinity.

Question 111 What will be output of following c program?

Page 31: Objective Type Question for C

#include<stdio.h>int * operation(int,int);void main() {

int p=6,q=9,*r; r=operation (p,q);printf("%d",*r);

} int * operation(int p,int q) {

int a=1,b; b=!(p)+ a * q++; return &b;

}Output: garbageExplanation:Variable b is auto type. Its scope is within the operation function. When control will transfer to the operation function b will die. We are returning address of variable b to r. *r means content at location control will transfer to the operation function b will die. We are returning address of variable b to r. When control transfer to main function assigning the address of b till now variable b has dead and r will store some garbage value.*r means content at location &r so it will show wrong output. To find write output declare b as static data type. Scope of static data type is entire the program.

#include<stdio.h>int * operation(int,int);void main(){

int p=6,q=9,*r;r=operation(p,q); printf("%d",*r);

} int * operation(int p,int q){

int a=1;static int=b;b=!(p)+ a * q++;return &b;

}

Output: 9

Question 112 What will be output of following c program?#include<stdio.h>void main() {

extern int p; printf("%d",p);

} int p=34; Output: 34Explanation:

Page 32: Objective Type Question for C

extern storage class search the value of p in outside the function.

Question 113 What will be output of following c program?#include<stdio.h>void main() {

extern int p=23;printf("%d",p);

} Output: Compilation errorExplanation: Extern variable cannot be initialized due to for extern variable has not any storage space.#include<stdio.h>void main() {

extern int p; printf("%d",p);

}int p;Output: 0Explanation: Default value of extern data type is zero.

Question 114 What will be output of following c program?#include<stdio.h>void main(){

int p; p= 4>7 ? return 5:return 6;printf("%d",p);

}Output: Compilation errorExplanation:We cannot use return keyword in conditional operator.

Question 115 What will be output of following c program?#include<stdio.h>void main(){

int p=0;p= 4>7 ? p=11:p=22;printf("%d",p);

}Output: errorExplanation: Assignment operator = has less precedence than conditional operator. Condition 4>7 is false .So false i.e. p=12 will execute but since = has less precedence so first value of p will assign 0 because initial value of p is. Now statement is0=22We cannot assign a value to constant. So error will Lvalue required. To solve such problem use () operator it has higher precedence than conditional operator.

Page 33: Objective Type Question for C

Question 116 What will be output of following c program?#include<stdio.h>void main(){

int p=0;p= 4>7 ? (p=11):(p=22);printf("%d",p);

}Output: 22

Question 117 What will be output of following c program?#include<stdio.h>void main(){

int p=0;p= 11>7 ? (p=11):p=22;printf("%d",p);

}

Output: 11Explanation:Condition operator evaluates only true part 11>7 is true. So it will assign 11 to p .It doesn’t check p=22 is write or wrong. It only checks its syntax is correct or not.Question 118 A function can not be overloaded only by its return type.a. Trueb. False

Question 119 A function can be overloaded with a different return type if it has all the parameters same.a. Trueb. False

Question 120) Inline functions involves some additional overhead in running time.a. Trueb. False

Question 121) A function that calls itself for its processing is known asa. Inline Functionb. Nested Functionc. Overloaded Functiond. Recursive Function

Question 122) We declare a function with ______ if it does not have any return typea. longb. doublec. voidd. int

Page 34: Objective Type Question for C

Question 123:Arguments of a functions are separated witha. comma (,)b. semicolon (c. colon (d. None of these Question 124:Variables inside parenthesis of functions declarations have _____ level access.a. Localb. Globalc. Moduled. Universal Question 125:Observe following function declaration and choose the best answer:int divide ( int a, int b = 2 )a. Variable b is of integer type and will always have value 2b. Variable a and b are of int type and the initial value of both variables is 2c. Variable b is international scope and will have value 2d. Variable b will have value 2 if not specified when calling function Question 126:The keyword endla. Ends the execution of program where it is writtenb. Ends the output in cout statementc. Ends the line in program. There can be no statements after endld. Ends current line and starts a new line in cout statement. Question 127:Strings are character arrays. The last index of it contains the null-terminated charactera. \nb. \tc. \0d. \1Question 128:The void specifier is used if a function does not have return type.a. Trueb. False Question 129:You must specify void in parameters if a function does not have any arguments.a. Trueb. False Question 130:

Page 35: Objective Type Question for C

Type specifier is optional when declaring a functiona. Trueb. False Question 131:Study the following piece of code and choose the best answerint x=5, y=3, z;a=addition(x,y)a. The function addition is called by passing the valuesb. The function addition is called by passing reference Question 132:In case of arguments passed by values when calling a function such as z=addidion(x,y),a. Any modifications to the variables x & y from inside the function will not have any effect outside the function.b. The variables x and y will be updated when any modification is done in the functionc. The variables x and y are passed to the function additiond. None of above are valid. Question 133:If the type specifier of parameters of a function is followed by an ampersand (& , that function call isa. pass by valueb. pass by reference Question 134:In case of pass by referencea. The values of those variables are passed to the function so that it can manipulate themb. The location of variable in memory is passed to the function so that it can use the same memory area for its processingc. The function declaration should contain ampersand (& in its type declarationd. All of above Question 135:Overloaded functions area. Very long functions that can hardly runb. One function containing another one or more functions inside it.c. Two or more functions with the same name but different number of parameters or type.d. None of above Question 136:Functions can be declared with default values in parameters. We use default keyword to specify the value of such parameters.a. Trueb. False

Page 36: Objective Type Question for C

Question 137:Examine the following program and determine the output#include <iostream>using namespace std;int operate (int a, int b){ return (a * b);}float operate (float a, float b){ return (a/b);}int main(){ int x=5, y=2; float n=5.0, m=2.0; cout << operate(x,y) <<"\t";cout << operate (n,m);return 0;} a. 10.0 5.0b. 5.0 2.5c. 10.0 5d. 10 2.5Question 138Identify the correct statementa. Programmer can use comments to include short explanations within the source code itself.b. All lines beginning with two slash signs are considered comments.c. Comments very important effect on the behaviour of the programd. both Question 139The directives for the preprocessors begin witha. Ampersand symbol (&b. Two Slashes (//)c. Number Sign (#)d. Less than symbol (< Question 140The file iostream includesa. The declarations of the basic standard input-output library.b. The streams of includes and outputs of program effect.c. Both of thesed. None of these

Page 37: Objective Type Question for C

Question 141There is a unique function in C++ program by where all C++ programs start their executiona. Start()b. Begin()c. Main()d. Output() Question 142Every function in C++ are followed bya. Parametersb. Parenthesisc. Curly bracesd. None of these Question 143Which of the following is false?a. Cout represents the standard output stream in c++.b. Cout is declared in the iostream standard filec. Cout is declared within the std namespaced. None of above Question 144Every statement in C++ program should end witha. A full stop (.)b. A Comma (,)c. A Semicolon (d. A colon ( Question 145Which of the following statement is true about preprocessor directives?a. These are lines read and processed by the preprocessorb. They do not produce any code by themselvesc. These must be written on their own lined. They end with a semicolon Question 146A block comment can be written bya. Starting every line with double slashes (//)b. Starting with /* and ending with */c. Starting with //* and ending with *//d. Starting with <!- and ending with -!> Question 147When writing comments you cana. Use code and /* comment on the same lineb. Use code and // comments on the same line

Page 38: Objective Type Question for C

c. Use code and //* comments on the same lined. Use code and <!- comments on the same line Question 148What is the correct value to return to the operating system upon the successful completion of a program?A. -1B. 1C. 0D. Programs do not return a value. Question 149What is the only function all C++ programs must contain?A. start()B. system()C. main()D. program() Question 150What punctuation is used to signal the beginning and end of code blocks?A. { }B. -> and <-C. BEGIN and ENDD. ( and ) Question 151What punctuation ends most lines of C++ code?A. . (dot)B. ; (semi-colon)C. : (colon)D. ' (single quote) Question 152Which of the following is a correct comment?A. */ Comments */B. ** Comment **C. /* Comment */D. { Comment } Question 153Which of the following is not a correct variable type?A. floatB. realC. intD. double Question 154

Page 39: Objective Type Question for C

Which of the following is the correct operator to compare two variables?A. :=B. =C. equalD. == Question 155Which of the following is true?A. 1B. 66C. .1D. -1E. All of the above Question 156Which of the following is the boolean operator for logical-and?A. &B. &&C. |D. |& Question 157Evaluate !(1 && !(0 || 1)).A. TrueB. FalseC. Unevaluatable

Question 158 cin extraction stops execution as soon as it finds any blank space character a. trueb. false Question 159 Observe the following statements and decide what do they do. string mystring; getline(cin, mystring); a. reads a line of string from cin into mystringb. reads a line of string from mystring into cinc. cin can’t be used this wayd. none of above

Page 40: Objective Type Question for C

Question 160 Regarding stringstream identify the invalid statement a. stringstream is defined in the header file <sstream>b. It allows string based objects treated as streamc. It is especially useful to convert strings to numerical values and vice versa.d. None of above Question 161 Which of the header file must be included to use stringstream? a. <iostream>b. <string>c. <sstring>d. <sstream> Question 162 Which of the following header file does not exist? a. <iostream>b. <string>c. <sstring>d. <sstream> Question 163 If you use same variable for two getline statements a. Both the inputs are stored in that variableb. The second input overwrites the first onec. The second input attempt fails since the variable already got its valued. You can not use same variable for two getline statements Question 164 The “return 0;” statement in main function indicates a. The program did nothing; completed 0 tasksb. The program worked as expected without any errors during its executionc. not to end the program yet.d. None of above Question 165

Page 41: Objective Type Question for C

Which of the following is not a reserve keyword in C++?a. mutableb. defaultc. readabled. volatile Question 166 The size of following variable is not 4 bytes in 32 bit systemsa. intb. long intc. short intd. float Question 167 Identify the correct statement regarding scope of variables a. Global variables are declared in a separate file and accessible from any program.b. Local variables are declared inside a function and accessible within the function only.c. Global variables are declared inside a function and accessible from anywhere in program.d. Local variables are declared in the main body of the program and accessible only from functions.