Câu hỏi trắc nghiệm ngôn ngữ lập trình C (tiếp theo)
Which one of the following is valid for opening a read-only ASCII file?
Choice 1
fileOpen (filenm, "r");
Choice 2
fileOpen (filenm, "ra");
Choice 3
fileOpen (filenm, "read");
Choice 4
fopen (filenm, "read");
Choice 5
fopen (filenm, "r");[Ans]
Q92:
f = fopen( filename, "r" );
Referring to the code above, what is the proper definition for the variable f?
Choice 1
FILE f;
Choice 2
FILE *f;[Ans]
Choice 3
int f;
Choice 4
struct FILE f;
Choice 5
char *f;
Q93:
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?
Choice 1
flush()
Choice 2
output()
Choice 3
fflush()
Choice 4
dump()
Choice 5
write()
Q94:
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?
Choice 1
127
Choice 2
128
Choice 3
255
Choice 4
32,767 [Ans]
Choice 5
65,536
Q95:
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?
Choice 1
The C standard says that dereferencing a null pointer causes undefined behavior. This may explain why printf() apparently fails.
Choice 2
If the standard output stream is buffered, the library buffers may not be flushed before the crash occurs.
Choice 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.
Choice 4
There is insufficient information to determine why the output fails to appear. A broader context is required.
Choice 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.
Q96:
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.
Choice 1
4
Choice 2
5[Ans]
Choice 3
6
Choice 4
7
Choice 5
8
Q97:
Which one of the following can be used to test arrays of primitive quantities for strict equality under Standard C?
Choice 1
qsort()
Choice 2
bcmp()
Choice 3
memcmp()
Choice 4
strxfrm()
Choice 5
bsearch()
Q98:
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?
Choice 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.
Choice 2
vfprintf() does not conform to ISO 9899: 1990, and may not be portable.
Choice 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().
Choice 4
The argument list args has been improperly initialized. Choice 5
Variadic functions are discontinued by Standard C; they are legacy constructs from K&R C, and no longer compile under modern compilers.
Q99:
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
56789
Choice 2
5123456789
5123456789
Choice 3
56789
56789
Choice 4
0123456789
0123456789
Choice 5
56789
0123456789 [Ans]
Q100:
/* 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?
Choice 1
The calls to memset() and memcpy() illegally perform a pointer conversion on rightmost without an appropriate cast.
Choice 2
The code does not correctly handle the situation where a directory separator '/' is the final character.
Choice 3
The if condition contains an incorrectly terminated character literal.
Choice 4
memcpy() cannot be used safely to copy string data.
Choice 5
The return value of get_rightmost() will be invalid in the caller's context.
NGUYỄN THỊ BẢO TRANG
» Tin mới nhất:
» Các tin khác: