Social Icons

Monday, December 10, 2012

C LANGUAGE INTERVIEW PAPERS ANSWERS & QUESTIONS

36.What is generic pointer in C?
Ans:
In C, void* acts as a generic pointer. When other pointer types are assigned to generic pointer, conversions are applied automatically (implicit conversion).


37.Are the expressions arr and &arr same for an array of integers?
Ans:
Yes for array of integers they are same.


38.How pointer variables are initialized ?
Ans:
Pointer variables are initialized by one of the following ways.
I. Static memory allocation
II. Dynamic memory allocation


39.What is static memory allocation ?
Ans:
Compiler allocates memory space for a declared variable. By using the address of operator, the reserved address is obtained and this address is assigned to a pointer variable. This way of assigning pointer value to a pointer variable at compilation time is known as static memory allocation.


40.What is dynamic memory allocation?
Ans:
A dynamic memory allocation uses functions such as malloc() or calloc() to get memory dynamically. If these functions are used to get memory dynamically and the values returned by these function are assigned to pointer variables, such an way of allocating memory at run time is known as dynamic memory allocation. 


41.What is the purpose of realloc ?
Ans:
It increases or decreases the size of dynamically allocated array. The function realloc(ptr,n)
uses two arguments. The first argument ptr is a pointer to a block of memory for which the size
is to be altered. the second argument specifies the new size. The size may be increased or
decreased. If sufficient space is not available to the old region the function may create a new
region. 


42.What is pointer to a pointer.
Ans:
If a pointer variable points another pointer value. such a situation is known as a pointer to a
pointer.  Ex.
int *p1,**p2,v=10;
P1=&v; p2=&p1;
Here p2 is a pointer to a pointer.


43.What is an array of pointers ?
Ans:
if the elements of an array are addresses, such an array is called an array of pointers.


44.Difference between linker and linkage ?
Ans:
Linker converts an object code into an executable code by linking together the necessary built in functions. The form and place of declaration where the variable is declared in a program determine the linkage of variable.


45.Is it possible to have negative index in an array?
Ans:
Yes it is possible to index with negative value provided there are data stored in these  location. Even if it is illegal to refer to the elements that are out of array bounds, the compiler will not produce error because C has no check on the bounds of an array.


46.Why is it necessary to give the size of an array in an array declaration ?
Ans:
When an array is declared ,the compiler allocates a base address and reserves enough space in memory for all the elements of the array. The size is required to allocate the required space and
hence size must be mentioned. 


47.What modular programming ?
Ans:
If a program is large, it is subdivided into a number of smaller programs that are called modules or subprograms. If a complex problem is solved using more modules, this approach is known as modular programming.


48.What is a function ?
Ans:
A large program is subdivided into a number of smaller programs or subprograms. Each subprogram specifies one or more actions to be performed for the larger program. such sub programs are called functions.


49.What is an argument ?
Ans:
An argument is an entity used to pass data from the calling to a called function.


50.What are built in functions ?
Ans:
The functions that are predefined and supplied along with the compiler are known as built-in
functions. They are also known as library functions.


51.Difference between formal argument and actual argument ?
Ans:
Formal arguments are the arguments available in the function definition. They are preceded by their own data type. Actual arguments are available in the function call. These arguments are given as constants or variables or expressions to pass the values to the function.


52.Is it possible to have more than one main() function in a C program ?
Ans:
The function main() can appear only once. The program execution starts from main.


53.What is the difference between an enumeration and a set of pre-processor # defines?
Ans:
There is hardly any difference between the two, except that a #define has a global
effect (throughout the file) whereas an enumeration can have an effect local to the block if
desired. Some advantages of enumeration are that the numeric values are automatically assigned
whereas in #define we have to explicitly define them. A disadvantages is that we have no control
over the size of enumeration variables.


54.How are Structure passing and returning implemented by the complier?
Ans:
When structure are passed as argument to functions, the entire structure is typically pushed on the stack. To avoid this overhead many programmer often prefer to pass pointers to structure  instead of actual structures. Structures are often returned from functions in a location pointed  to by an extra, compiler-supported ‘hidden’ argument to the function. 


55.What is the similarity between a Structure, Union and enumeration?
Ans:
All of them let the programmer to define new data type.


56.Can a Structure contain a Pointer to itself?
Ans:
Yes such structures are called self-referential structures.


57.How can we read/write Structures from/to data files?
Ans:
To write out a structure we can use fwrite() as Fwrite( &e,sizeof(e),1,fp);Where e is a structure variable. A corresponding fread() invocation can read the structure back from file. On calling fwrite() it writes out sizeof(e) bytes from the address &e. Data files written as memory images with fwrite(), however ,will not be portable, particularly if they contain floating point fields or Pointers. This is because memory layout of structures is machine and compiler dependent. Therefore, structures written as memory images cannot necessarily be read back by programs running on other machine, and this is the important concern if the data files you’re writing will ever be interchanged between machines.

No comments:

Post a Comment