|
1. Which one of the following is the result type of the sizeof operator? - unsigned char
- int
- char
- unsigned int
- size_t
2. Can static variables be declared in a header file? You can’t declare a static variable without defining it as well (this is because the storage class modifiers static and extern are mutually exclusive). A static variable can be defined in a header file, but this would cause each source file that included the header file to have its own private copy of the variable, which is probably not what was intended. 3. Which one of the following functions returns the string representation from a pointer to a time_t value? - localtime
- gmtime
- ctime
- strtime
- asctime
4. Is it better to use malloc() or calloc()? Both the malloc() and the calloc() functions are used to allocate dynamic memory. Each operates slightly different from the other. malloc() takes a size and returns a pointer to a chunk of memory at least that big: void *malloc( size_t size ); calloc() takes a number of elements, and the size of each, and returns a pointer to a chunk of memory at least big enough to hold them all: void *calloc( size_t numElements, size_t sizeOfElement ); There’s one major difference and one minor difference between the two functions. The major difference is that malloc() doesn’t initialize the allocated memory. The first time malloc() gives you a particular chunk of memory, the memory might be full of zeros. If memory has been allocated, freed, and reallocated, it probably has whatever junk was left in it. That means, unfortunately, that a program might run in simple cases (when memory is never reallocated) but break when used harder (and when memory is reused). calloc() fills the allocated memory with all zero bits. That means that anything there you’re going to use as a char or an int of any length, signed or unsigned, is guaranteed to be zero. Anything you’re going to use as a pointer is set to all zero bits. That’s usually a null pointer, but it’s not guaranteed.Anything you’re going to use as a float or double is set to all zero bits; that’s a floating-point zero on some types of machines, but not on all. The minor difference between the two is that calloc() returns an array of objects; malloc() returns one object. Some people use calloc() to make clear that they want an array. 5. Which one of the following is a true statement about non-generic (void *) pointers? - For efficiency, pointer values are always stored in machine registers.
- A pointer to one type may not be cast to a pointer to any other type.
- Similarly typed pointers may be subtracted from each other.
- They are always 32-bit values.
- Similarly typed pointers may be added to each other.
6. Can the size of an array be declared at runtime? No. In an array declaration, the size must be known at compile time. You can’t specify a size that’s known only at runtime. For example, if i is a variable, you can’t write code like this: char array[i]; /* not valid C */ Some languages provide this latitude. C doesn’t. If it did, the stack would be more complicated, function calls would be more expensive, and programs would run a lot slower. If you know that you have an array but you won’t know until runtime how big it will be, declare a pointer to it and use malloc() or calloc() to allocate the array from the heap. 7. Is it possible to execute code even after the program exits the main() function? The standard C library provides a function named atexit() that can be used to perform cleanup operations when your program terminates. You can set up a set of functions you want to perform automatically when your program exits by passing function pointers to the at exit() function. 8. Are pointers integers? No, pointers are not integers.A pointer is an address.It is merely a positive number and not an integer. 9. Which of the following correctly enumerate predefined streams? - stddev, stdprn, and stdmon
- stdin, stdout, and stderr
- stdin, stdout, and stdterm
- stdio and stderr
- stdin, stdlib, and stderr
10. How do you override a defined macro? You can use the #undef preprocessor directive to undefine (override) a previously defined macro.
|