|
1. What is a modulus operator? What are the restrictions of a modulus operator? A Modulus operator gives the remainder value. The result of x%y is obtained by (x-(x/y)*y). This operator is applied only to integral operands and cannot be applied to float or double. 2. int a [25]; int * p = a + 3; Which one of the following is functionally equivalent to the code fragment above? - int a [25];
int * p = &a[3]; - int a [25];
int * p = &(a + 3); - int a [25];
int * p = &a[2]; - int a [25];
int * p = (&a)[3]; - int a [25];
int * p = a[3];
3. What is the difference between text and binary modes? Streams can be classified into two types: text streams and binary streams. Text streams are interpreted, with a maximum length of 255 characters. With text streams, carriage return/line feed combinations are translated to the newline n character and vice versa. Binary streams are uninterpreted and are treated one byte at a time with no translation of characters. Typically, a text stream would be used for reading and writing standard text files, printing output to the screen or printer, or receiving input from the keyboard. A binary text stream would typically be used for reading and writing binary files such as graphics or word processing documents, reading mouse input, or reading and writing to the modem. 4. x[2] = 5 vs. 2[x] = 5 Are x[2] and 2[x] identical in the sample code above? Why or why not? - Yes; both are identical because they are resolved to pointer references.
- Yes; both are identical because the compiler will identify the x variable and make adjustments.
- No; x[2] is correct, but 2[x] is invalid syntax.
- No; both variable assignments have invalid syntax.
- No; 2[x] is correct, but x[2] is invalid syntax.
5. How do you determine whether to use a stream function or a low-level function? Stream functions such as fread() and fwrite() are buffered and are more efficient when reading and writing text or binary data to files. You generally gain better performance by using stream functions rather than their unbuffered low-level counterparts such as read() and write(). In multi-user environments, however, when files are typically shared and portions of files are continuously being locked, read from, written to, and unlocked, the stream functions do not perform as well as the low-level functions. This is because it is hard to buffer a shared file whose contents are constantly changing. Generally, you should always use buffered stream functions when accessing nonshared files, and you should always use the low-level functions when accessing shared files 6. How can I sort things that are too large to bring into memory? A sorting program that sorts items that are on secondary storage (disk or tape) rather than primary storage (memory) is called an external sort. Exactly how to sort large data depends on what is meant by too large to fit in memory. If the items to be sorted are themselves too large to fit in memory (such as images), but there aren’t many items, you can keep in memory only the sort key and a value indicating the data’s location on disk. After the key/value pairs are sorted, the data is rearranged on disk into the correct order. If too large to fit in memory means that there are too many items to fit into memory at one time, the data can be sorted in groups that will fit into memory, and then the resulting files can be merged. A sort such as a radix sort can also be used as an external sort, by making each bucket in the sort a file. Even the quick sort can be an external sort. The data can be partitioned by writing it to two smaller files. When the partitions are small enough to fit, they are sorted in memory and concatenated to form the sorted file. 7. Sample Code int z; int x = 5; int y = -10; int a = 4; int b = 2; z = x++ + ++y * b / a; What number will z in the sample code above contain? 8. What are the standard predefined macros? The ANSI C standard defines six predefined macros for use in the C language: Macro Name Purpose _ _LINE_ _ Inserts the current source code line number in your code. _ _FILE_ _ Inserts the current source code filename in your code. _ _ Inserts the current date of compilation in your code. _ _TIME_ _ Inserts the current time of compilation in your code. _ _STDC_ _ Is set to 1 if you are enforcing strict ANSI C conformity. _ _cplusplus Is defined if you are compiling a C++ program. 9. How can you determine the maximum value that a numeric variable can hold? For integral types, on a machine that uses two’s complement arithmetic (which is just about any machine you’re likely to use), a signed type can hold numbers from 2(number of bits 1) to +2(number of bits 1) 1. An unsigned type can hold values from 0 to +2(number of bits) 1. For instance, a 16-bit signed integer can hold numbers from 2^15 (32768) to +2^15 1 (32767). 10. How do you print only part of a string? /* Use printf() to print the first 11 characters of source_str. */ printf(First 11 characters: ‘%11.11s’n, source_str);
|