|
1. Sample Code /* Eliminate all whitespace from the end of s. */ void rtrim (char * s) { char * start, ws, text; start = s, ws = text = s - 1; while (*s != '\0') { if (isspace(*s)) { ws = s; while (isspace(*s)) s++; } else { text = s; while (*s != '\0' && !isspace(*s)) s++; } } if (ws > text) *ws = '\0'; } The function rtrim(), defined above, contains an implementation error. Which one of the following describes it? - The comma operator has a higher precedence than assignment; this produces an unintended effect.
- rtrim() does not correctly handle the case where no character of s is whitespace.
- rtrim() does not correctly handle the case where every character of s is whitespace.
- ws and text are not assignment-compatible with s or NULL.
- The loop and its subordinate statements do not adequately detect an end-of-string condition on s in all cases.
2. How can I make sure that my program is the only one accessing a file? By using the sopen() function you can open a file in shared mode and explicitly deny reading and writing permissions to any other program but yours. This task is accomplished by using the SH_DENYWR shared flag to denote that your program is going to deny any writing or reading attempts by other programs. For example, the following snippet of code shows a file being opened in shared mode, denying access to all other files: /* Note that the sopen() function is not ANSI compliant... */ fileHandle = sopen(“C:DATASETUP.DAT”, O_RDWR, SH_DENYWR); By issuing this statement, all other programs are denied access to the SETUP.DAT file. If another program were to try to open SETUP.DAT for reading or writing, it would receive an EACCES error code, denoting that access is denied to the file. 3. /* fp points to an open stream. * Process a full stream of binary data. */ unsigned long process_binary_data(FILE *fp) { char ch; unsigned long count = 0UL; assert(fp != NULL); while ((ch = fgetc(fp)) != EOF) { process_byte(ch); count++; } return count; } The function process_binary_data(), defined above, may fail to process the entire stream under exceptional conditions. Which one of the following explains this error? - The disparity between the type of ch and the type of EOF causes the loop to execute forever.
- The loop may fail to process the entire stream underlying fp because it does not check for errors.
- It is incorrect to use the assignment operator in a loop condition. The programmer probably meant to use the equality operator.
- The function body contains incorrectly nested parentheses.
- count fails to contain the number of bytes actually processed.
4. What is a macro, and how do you use it? A macro is a preprocessor directive that provides a mechanism for token replacement in your source code. Macros are created by using the #define statement. Here is an example of a macro: Macros can also utilize special operators such as the stringizing operator (#) and the concatenation operator (##).The stringizing operator can be used to convert macro parameters to quoted strings, as in the following example: #define DEBUG_VALUE(v) printf(#v is equal to %d.n, v) In your program, you can check the value of a variable by invoking the DEBUG_VALUE macro: ... int x = 20; DEBUG_VALUE(x); ... The preceding code prints x is equal to 20. on-screen. This example shows that the stringizing operator used with macros can be a very handy debugging tool. 5. What is the easiest searching method to use? Just as qsort() was the easiest sorting method, because it is part of the standard library, bsearch() is the easiest searching method to use. If the given array is in the sorted order bsearch() is the best method. Following is the prototype for bsearch(): void *bsearch(const void *key, const void *buf, size_t num, size_t size, int (*comp)(const void *, const void*)); Another simple searching method is a linear search. A linear search is not as fast as bsearch() for searching among a large number of items, but it is adequate for many purposes. A linear search might be the only method available, if the data isn’t sorted or can’t be accessed randomly. A linear search starts at the beginning and sequentially compares the key to each element in the data set. 6. int factorial (int x) { extern jmp_buf jb; int fact, chk; if (!x) return 1; fact = x * (chk = factorial(x - 1)); if (chk > fact) longjmp(jb, -1); return fact; } int check_for_overflow (int x) { extern jmp_buf jb; if (setjmp(jb)) { printf("discovered overflow in factorial(%d)\n", x); return 0; } if (x < x =" 0;"> return factorial(x); } There is an error in check_for_overflow that may occasionally result in unexpected values in var jb. Which one of the following describes this error? - longjmp() cannot safely be used to escape from a recursive call chain.
- The argument to check_for_overflow() should be qualified with volatile to ensure correct error reporting.
- setjmp() and longjmp() must be invoked within the same stack frame. The result of longjmp() in this case is undefined.
- The calls to setjmp() and longjmp() operate on different jump buffers, and therefore may have an undefined effect.
- The factorial of zero (0) is incorrectly handled by factorial().
7. Sample Code char s1[100]; char s2[100]; gets( s1 ); fgets( s2, sizeof(s2), stdin); printf( "%d\n", strlen( s1 ) - strlen( s2 ) ); What will be printed when the above code is executed and the string "abcd" is entered twice on stdin? 8. class A { }; class B { protected: friend class A; }; class C { public: friend class B; }; Referring to the sample code above, assuming the above classes had data members, what names of C's members could be used in declarations of members of A? - Only private members
- Only protected members
- All of C's data members
- Only public members
- None of C's data members
9. Sample Code int x = 5; int y = 2; char op = '*'; switch (op) { default : x += 1; case '+' : x += y; case '-' : x -= y; } After the sample code above has been executed, what value will the variable x contain? 10. Sample Code void memset (void * buf, int c, size_t len) ??< unsigned char * b; register int i; assert(buf); debug("assert(buf) => TRUE??/n"); b = buf; for (i = 0; i <> ??> Consider the function memset(), defined above. Which one of the following is a completely true statement about the code shown above? - Some accesses performed on b are out-of-bounds.
- Providing that debug() is declared and correctly used, the code compiles correctly under a Standard C compiler.
- The three-character sequences starting with ?? were inserted by Microsoft Word.
- The source code file containing memset() has probably become corrupted. This code clearly does not compile.
- Assuming that debug() prints its string argument, the strings will run together on the same line.
|