• +91 9971497814
  • info@interviewmaterial.com

Computer Interview Questions Answers

C Interview Questions Answers

Question - 51 : -  I had the definition char a[6] in one source file, and in another I declared extern char *a. Why didn't it work?

Answer - 51 : - In one source file you defined an array of characters and in the other you declared a pointer to characters. The declaration extern char *a does not declare an array and therefore does not match the actual definition. The type pointer-to-type-T is not the same as array-of-type-T. Use extern char a[].

Question - 52 : - How do you use a pointer to a function?

Answer - 52 : - The hardest part about using a pointer-to-function is declaring it. Consider an example. You want to create a pointer, pf, that points to the strcmp() function. The strcmp() function is declared in this way: int strcmp(const char *, const char * ) To set up pf to point to the strcmp() function, you want a declaration that looks just like the strcmp() function’s declaration, but that has *pf rather than strcmp: int (*pf)( const char *, const char * ); After you’ve gotten the declaration of pf, you can #include and assign the address of strcmp() to pf: pf = strcmp;

Question - 53 : - Can the size of an array be declared at runtime?

Answer - 53 : - 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.

Question - 54 : - What are the advantages of auto variables?

Answer - 54 : - 1)The same auto variable name can be used in different blocks 2)There is no side effect by changing the values in the blocks 3)The memory is economically used 4)Auto variables have inherent protection because of local scope

Question - 55 : - What are the advantages of the functions?

Answer - 55 : - - Debugging is easier - It is easier to understand the logic involved in the program - Testing is easier - Recursive call is possible - Irrelevant details in the user point of view are hidden in functions - Functions are helpful in generalizing the program

Question - 56 : - When would you use a pointer to a function?

Answer - 56 : - Pointers to functions are interesting when you pass them to other functions. A function that takes function pointers says, in effect, Part of what I do can be customized. Give me a pointer to a function, and I’ll call it when that part of the job needs to be done. That function can do its part for me. This is known as a callback. It’s used a lot in graphical user interface libraries, in which the style of a display is built into the library but the contents of the display are part of the application. As a simpler example, say you have an array of character pointers (char*s), and you want to sort it by the value of the strings the character pointers point to. The standard qsort() function uses function pointers to perform that task. qsort() takes four arguments, - a pointer to the beginning of the array, - the number of elements in the array, - the size of each array element, and - a comparison function, and returns an int.

Question - 57 : - What are Templates

Answer - 57 : - C++ Templates allow u to generate families of functions or classes that can operate on a variety of different data types, freeing you from the need to create a separate function or class for each type. Using templates, u have the convenience of writing a single generic function or class definition, which the compiler automatically translates into a specific version of the function or class, for each of the different data types that your program actually uses. Many data structures and algorithms can be defined independently of the type of data they work with. You can increase the amount of shared code by separating data-dependent portions from data-independent portions, and templates were introduced to help you do that.

Question - 58 : - How to Copy constructor ?

Answer - 58 : - A copy constructor is used to create an instanc... A copy constructor is used to create an instance from another instance, e.g.: MyClass A; MyClass B (A); When B is instantiated, the following constructor is called: B (B const & object); Use the body of the copy constructor to copy member data from the source object. e.g.: class Ints { int x, y; Ints (Ints const & object) { x = object.x; y = object.y; } };

Question - 59 : - Why n++ executes faster than n+1?

Answer - 59 : - The expression n++ requires a single machine instruction such as INR to carry out the increment operation whereas, n+1 requires more instructions to carry out this operation.

Question - 60 : - 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().

Answer - 60 : - 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

Computer Contributors

krishan
Rajeev Katiyar
Yes Baroda

Share your email for latest updates

Name:
Email:

Our partners