Sei sulla pagina 1di 22

1. #include <iostream.h> #include <stdlib.

h> #define Random() rand int main() { int n = 0; do { cout << "hello " << ++n << "\n"; } while( Random() != 0 ); return 0; } 2. #include <stdio.h> class X { public: int *px; X( int init ) { px = new int; *px = init; } ~X() { delete px; } }; void print( X x ) { printf( "%d\n", *x.px ); } int main() { X x(15); print( x ); X y(16); print( x ); print( y ); return 0; } 3. #include <stdio.h> void print_sum( double a[], int n ) { int i; double sum = 0; for( i = 0; i < n; i++ ) sum += a[i]; printf( "Sum = %d\n", sum ); } int main() { double x[3] = { 1.0, 2.0, 3.0 };

print_sum( x, 3 ); return 0; } 4. #include <stdio.h> void print_mod(int i, int n) { if( n == 0 && i == 0 ) return; printf( "%d mod %d == %d\n", i, n, i % n ); } int main() { for( int i = 0; i < 10; i++ ) for( int j = 0; j < 10; j++ ) print_mod( i, j ); return 0; } 5. #include <iostream.h> void out( int n ) { cout << n << "\n"; } void show( int a, int b, int c ) { out( a ); out( b ); out( c ); } int main() { int i = 1; show( i++, i++, i++ ); return 0; } 6. #include <iostream.h> char *WineList[] = { "Cabernet Sauvignon", "Pinot Noir" "Chardonnay", 0 }; int main()

{ cout << "The wine choices today are:\n"; for( int i = 0; WineList[i]; i++ ) cout << WineList[i] << "\n"; return 0; } 7. #include <stdio.h> #include <string.h> class S { int n; char *s; public: S(char *p) { n = strlen(p); s = p; } operator char*() const; }; S::operator char*() const { return s; } int main() { S s("abc"); printf( "%s\n", s ); return 0; } 8. class S { char *p; int n; public: S( int k = 0 ) { n = k; if( n ) p = new char[n]; } ~S() { delete[] p; } }; int main() { S s; return 0; }

9. Code time_t currentTime = time( NULL ); printf("%s\n", ???? ); Which of the following can replace ???? in order for the above sample code to print out the current time as a human-readable string? Choice 1 ctime(&currentTime) Choice 2 strtime(currentTime) Choice 3 asctime(currentTime) Choice 4 timestr(currentTime) Choice 5 asctime(&currentTime) 10. Code double x = 1/2.0 + 1/2; printf("x=%.2f\n", x); What will be printed when the sample code above is executed? Choice 1 x=0.00 Choice 2 x=0.25 Choice 3 x=0.50 Choice 4 x=0.75 Choice 5 x=1.00 11. Code void *ptr;

What would be the correct way to cast ptr in the code above to be a pointer to a 3 element array of function pointers with a return value of int and a single paramter of a pointer to char? Choice 1 ((int *)(*[3])(char *))ptr Choice 2 (int (*(*))(char *)[3])ptr Choice 3 (int (*(*)[3])(char *))ptr Choice 4 (int ((*)[3])(char *))ptr Choice 5 (int *(*)[3](char *))ptr 12 Code struct computer { int cpuSpeed; char cpuType[10]; } comp[] = { { 400, "Pentium" }, { 266, "PowerPC" }, { 333, "Sparc" }, }; Given the array of structures in the sample code above, which of the following expressions will evaluate to the number of structures in the array (in this case it would be 3)? Choice 1 sizeof( *comp ) Choice 2 sizeof( comp ) Choice 3 sizeof( struct computer ) Choice 4 sizeof( comp ) / sizeof( *comp ) Choice 5 sizeof( *comp ) / sizeof( comp ) C, Question 5 of 40 Take a 15 minute break AFTER this question (one per test)

Copyright 2000 Brainbench, Inc. All rights reserved. 13. Code int counter = 0; int aMatrix[5][5]; register int *aPtr; int i, j; for (i=0; i<5; i++) for (j=0; j<5; j++) aMatrix[i][j] = counter++; aPtr = &aMatrix[1][1]; printf("%d\n", aPtr[2]); Referring to the sample code above, what will be the value of "aPtr[2]", after execution? Choice 1 5 Choice 2 6 Choice 3 7 Choice 4 8 Choice 5 9 C, Question 8 of 40 Take a 15 minute break AFTER this question (one per test)

14.. Which of the following would compute the square of x in C? Choice 1 x**2; Choice 2 power(x, 2); Choice 3 pow(x, 2); Choice 4

x^2; Choice 5 pow(2, x); C, Question 9 of 40 15. According to the ANSI specification, what are the respective minimum sizes (in bytes) of the following three data types: short; int; and long. Choice 1 1, 2, 2 Choice 2 1, 2, 4 Choice 3 1, 2, 8 Choice 4 2, 2, 4 Choice 5 2, 4, 8 C, Question 10 of 40 16. Code int x = 1; int *ptr = malloc(sizeof(int)); ptr = &x; x = 2; *ptr = 3; Is there anything wrong with the above code? Choice 1 No, x will be set to 1 Choice 2 No, x will be set to 2 Choice 3 No, x will be set to 3 Choice 4 Yes, There will be a memory overwrite. Choice 5 Yes, There will be a memory leak. C, Question 12 of 40

17. 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? Choice 1 0123456789 0123456789 Choice 2 56789 0123456789 Choice 3 5123456789 5123456789 Choice 4 56789 56789 Choice 5 0123456789 56789 C, Question 13 of 40

18. Code char *aA = "aA"; char *aC = "aC"; printf( "%d\n", strcmp( aA, aC ) ); What will be printed when the above code is executed? Choice 1

0 Choice 2 1 Choice 3 -1 Choice 4 a negative number Choice 5 a positive number C, Question 14 of 40 19. How could stderr be redirected from within a program to force all error messages to be written to the end of the text file "error.log" instead of the location specified by the operating system? Choice 1 stderr = fopen( "error.log", "a" ); Choice 2 freopen( stderr, "error.log", "w" ); Choice 3 stderr = fopen( "error.log", "w" ); Choice 4 stderr = freopen( "error.log", "a", stderr ); Choice 5 freopen( "error.log", "a", stderr ); C, Question 15 of 40

20. What is a variable DECLARATION (as opposed to definition)? Choice 1 The assignment of properties to a variable Choice 2 The assignment of properties and storage space to a variable Choice 3 The assignment of storage space to a variable Choice 4 The assignment of storage space to a variable whose properties have been specified external to the current file scope.

Choice 5 The assignment of properties and identification to a variable C, Question 16 of 40 21. How is time defined by the POSIX standard? Choice 1 Time = number of seconds since GMT, 1/1/1900 Choice 2 Time = number of seconds since GMT, 1/1/1940 Choice 3 Time = number of seconds since GMT, 1/1/1950 Choice 4 Time = number of seconds since GMT, 1/1/1960 Choice 5 Time = number of seconds since GMT, 1/1/1970 C, Question 17 of 40

22.. Code f = fopen( fileName, "r" ); if( ???? ) { fprintf( stderr, "Cound not open file!" ); exit( -1 ); } What should replace the ???? in the code above to determine if the file could not be opened? Choice 1 f == NULL Choice 2 fclose( f ) Choice 3 f != 0; Choice 4 f == EOF Choice 5

f == -1 C, Question 18 of 40 23. Which of the following functions returns the string representation from a pointer to a time_t value? Choice 1 localtime Choice 2 gmtime Choice 3 strtime Choice 4 asctime Choice 5 ctime C, Question 19 of 40 24. Code int var1; If a variable has been declared with file scope, as above, can it safely be accessed globally from another file? Choice 1 Yes. It can be referenced through the extern specifier. Choice 2 Yes. It can be referenced through the register specifier. Choice 3 No. It would have to have been initially declared as a static variable. Choice 4 No. It would need to have been initially declared using the global keyword. Choice 5 Yes. It can be referenced through the publish specifier. 25. Code struct customer *ptr = malloc( sizeof( struct customer ) ); Given the sample allocation for the pointer "ptr" found above, which statement would be used to reallocate ptr to be an array of 10 elements?

Choice 1 ptr += malloc( 9 * sizeof( struct customer ) ); Choice 2 ptr = realloc( ptr, 9 * sizeof( struct customer ) ); Choice 3 ptr = realloc( ptr, 10 * sizeof( struct customer ) ); Choice 4 realloc( ptr, 9 * sizeof( struct customer ) ); Choice 5 realloc( ptr, 10 * sizeof( struct customer ) ); C, Question 21 of 40 26. Which of the following will read a character from the stdin stream and store it in the variable c? Choice 1 c = getchar( stdin ); Choice 2 c = getchar(); Choice 3 getc( &c ); Choice 4 getchar( &c ); Choice 5 c = getc(); C, Question 2 of 40 Take a 15 minute break AFTER this question (one per test) 27. Code char *ptr; char myString[] = "abcdefg"; ptr = myString; ptr += 5; What string does ptr point to in the sample code above? Choice 1 fg Choice 2 efg Choice 3 defg

Choice 4 cdefg Choice 5 None of the above C, Question 22 of 40

28. Which of the following is valid for opening a read-only ASCII file? Choice 1 fileOpen (filenm, "r"); Choice 2 fileOpen (filenm, "ra"); Choice 3 fopen (filenm, "r"); Choice 4 fopen (filenm, "read"); Choice 5 fileOpen (filenm, "read"); C, Question 23 of 40

29. Code char *x = "car"; Given the definition for x above, which of the following will NOT print the letter r? Choice 1 printf( "%c\n", *( x + 2 ) ); Choice 2 printf( "%c\n", *x + 2 ); Choice 3 printf( "%c\n", (&x)[0][2] ); Choice 4 printf( "%c\n", 2[x] ); Choice 5 printf( "%c\n", x[2] ); C, Question 24 of 40

30. What does invoking the C compiler accomplish? Choice 1 It executes applications at run-time. Choice 2 It only provides code evaluation. You must use the linker to assemble and link programs. Choice 3 It interprets files at run-time. Choice 4 It creates machine code. Choice 5 It links together object files. C, Question 25 of 40 31. Code void getFileLength( char *s ) { FILE *f; if( f = fopen( s, "r" ) ) { fseek( f, SEEK_END, 0 ); printf( "%d\n", ????? ); fclose( f ); } } Which of the following could replace the ????? in the code above to cause the function to print the length of the file when a valid file name is passed to it? Choice 1 filelen( f ) Choice 2 position( f ) Choice 3 ftell( f ) Choice 4 tell( f ) Choice 5 fposition( f ) C, Question 26 of 40

32. Code void listFile( FILE *f ) { int c; while( c = fgetc( f ) != EOF ) { printf( "%d", c ); } printf( "\n" ); } What will be printed when the function above is called with pointer to an open file that contains the three characters abc? Choice 1 abc Choice 2 111 Choice 3 000 Choice 4 The characters ab followed by an infinite number of c's Choice 5 656667 C, Question 27 of 40

33. Statements A) A definition allocates storage for the variable. B) A declaration allocates storage for the variable. C) A definition can occur muliple times D) A declaration can occur multiple times Regarding the difference between a declaration and a definition of a variable, which of the above stataments are true?

Choice 1 A and C only Choice 2 A and D only Choice 3 B and C only Choice 4 B and D only Choice 5 none of the above 34. Statements A) A definition allocates storage for the variable. B) A declaration allocates storage for the variable. C) A definition can occur muliple times D) A declaration can occur multiple times Regarding the difference between a declaration and a definition of a variable, which of the above stataments are true? Choice 1 A and C only Choice 2 A and D only Choice 3 B and C only Choice 4 B and D only Choice 5 none of the above C, Question 28 of 40 35. Code #include <math.h> static double (*funcs[])( double ) = { sin, cos, tan, asin, acos, atan, sinh, cosh, tanh

}; double computeTrigFunction( int index, double argument ) { return ????; } Referring to the sample code above which should compute the value of a trigonometric function based on its index, what would be a replacement for the ???? to make the correct call? Choice 1 (*funcs[ index ])( argument ) Choice 2 funcs[index](argument) Choice 3 *funcs[ index ]( argument ) Choice 4 funcs(argument)[index] Choice 5 (*funcs)[ index ]( argument ) C, Question 30 of 40

36. Code void freeList( struct node *n ) { while( n ) { ???? } } Which of the following can replace the ???? for the function above to release the memory allocated to a linked list? Choice 1 struct node m = n; free( m ); n = n->next; Choice 2 n = n->next; free( n ); Choice 3 struct node m = n; free( n );

n = m->next; Choice 4 free( n ); n = n->next; Choice 5 struct node m = n; n = n->next; free( m ); C, Question 31 of 40

37. Code char var1[10]; char var2[5] = "Hello"; strcpy( var1, var2 ); What will happen when the above code is executed? Choice 1 The linker will reject this as an array overflow error. Choice 2 The variable var will contain the string "Hello", but a run-time message will identify an array overflow. Choice 3 The variable var2 will contain the word "Hello", but the behavior of strcpy is undefined. Choice 4 The compiler will reject it because only string pointers can be initialized this way. Choice 5 The variable var1 and var2 will contain the string "Hello" and no problems will occur. C, Question 32 of 40 38. All standard C library <math.h> functions require what data type? Choice 1 float Choice 2 int Choice 3

double Choice 4 precision Choice 5 decimal C, Question 33 of 40 39. Code char *x = NULL; printf("%s\n", x); What will be the behavior of the sample code above? Choice 1 This will not compile. A pointer cannot be initialized to NULL. Choice 2 The result will fall into "undefined behavior" and be unpredictable. Choice 3 A blank line will be printed. Choice 4 Nothing will be printed. Choice 5 A "0" will be printed. 40.. Code char buf[ 50 ] = "Hello World"; char *ptr = buf + 5; From the sample above, which would be the proper way to copy 20 bytes from the location pointed to by ptr to the beginning of buf? Choice 1 strncpy( buf, ptr, 20 ); Choice 2 memmove( buf, ptr, 20 ); Choice 3 It cannot be done, because the source and destination overlap. Choice 4 That may cause an illegal memory access, because it will read memory past the end of the string. Choice 5 memcpy( buf, ptr, 20 ); C, Question 34 of 40

41. What is the largest value an integer can hold in an ANSI compiler? Choice 1 32767 Choice 2 65536 Choice 3 2147483647 Choice 4 INT_MAX Choice 5 1 << INT_BITS C, Question 36 of 40 42. Code void printTime( time_t *t ) { ???? } Which of the following can replace the ???? in the code above to print the time passed in t in human readable form? Choice 1 char s[ 100 ]; ctime( t, s ); printf( "%s\n", s ); Choice 2 printf( "%s\n", asctime( t ) ); Choice 3 printf( "%s\n", ctime( t ) ); Choice 4 printf( "%s", t ); Choice 5 char *s = ctime( t ); printf( "%s\n", s ); free( s ); C, Question 37 of 40

43. Which is more correct: 'int main (int argc, char** argv)' or 'int main (int argc, char* argv[])'? Choice 1 int main (int argc, char* argv[]) Choice 2 Both are equally correct. Choice 3 Both are equally wrong. Choice 4 int main (int argc, char** argv) Choice 5 Neither is correct. C, Question 38 of 40 44. Code #include <stdio.h> char *format = "%d"; int main() { int x; myFunc( scanf, &x ); printf( "%d\n", x ); return 0; } Referring to the sample code above, which of the following would be a correct implementation for myFunc? Choice 1 void myFunc( int (*y)(const char*, ... ), int *x ) { (*y)( format, x ); } Choice 2 void myFunc( int *y(const char*, ... ), int *x ) { (*y)( format, &x ); } Choice 3 void myFunc( int *y(const char*, ... ), int *x ) { (*y)( format, x ); } Choice 4 void myFunc( int (*y)(const char*, ... ), int *x )

{ (*y)( format, &x ); } Choice 5 void myFunc( *(int y(const char*, ... )), int *x ) { (*y)( format, x ); } 45. In a #if preprocessor directive, what is the syntax for performing an "else if"? Choice 1 #elif (..) Choice 2 #else if (..) Choice 3 #elseother (..) Choice 4 #elseif (..) Choice 5 #fi C, Question 40 of 40

46. US Coins penny = one nickel = five dime = ten quarter = twenty-five How would enum be used to define the values of the American coins listed above? Choice 1 enum coin {penny=1, nickel=5, dime=10, quarter=25}; Choice 2 enum coin {(penny,1), (nickel,5), (dime,10), (quarter,25)}; Choice 3 enum coin (penny=1, nickel=5, dime=10, quarter=25); Choice 4 enum coin {penny, nickel, dime, quarter} (1, 5, 10, 25); Choice 5 enum coin ({penny,1}, {nickel,5}, {dime,10}, {quarter,25});

Potrebbero piacerti anche