C INTERVIEW QUESTIONS Part 1

C INTERVIEW QUESTIONS
Question: Difference between arrays and pointers?
Answer: Pointers are used to manipulate data using the address. Pointers use * operator to access the data pointed to by them
Arrays use subscripted variables to access and manipulate data. Array variables can be equivalently written using pointer expression.

Question: What is the purpose of realloc ( )?
Answer: 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 n specifies the
new size. The size may be increased or decreased. If n is greater than the old size and if sufficient space is not available subsequent to the old region, the function realloc ( )
may create a new region and all the old data are moved to the new region.

Question: What is static memory allocation and dynamic memory allocation?
Answer: Static memory allocation: The compiler allocates the required memory space for a declared variable. By using the address of operator, the reserved address is obtained and this address may be assigned to a pointer variable. Since most of the declared variable has static memory, this way of assigning pointer value to a pointer variable is known as static memory allocation. Memory is assigned during compilation time.

Dynamic memory allocation: It 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 functions are assigned to pointer variables, such assignments are known as dynamic memory allocation. Memory is assigned during run time.

Question: How are pointer variables initialized?
Answer: Pointer variable are initialized by one of the following two ways
Ø Static memory allocation
Ø Dynamic memory allocation

Question: What is a pointer variable?
Answer: A pointer variable is a variable that may contain the address of another variable or any valid address in the memory.

Question: What is a pointer value and address?
Answer: A pointer value is a data object that refers to a memory location. Each memory location is numbered in the memory. The number attached to a memory location is called the address of the location.

Question: What are the advantages of the functions?
Answer :Ø 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: What is the purpose of main( ) function?
Answer :The function main( ) invokes other functions within it.It is the first function to be called when the program starts execution.
Ø It is the starting function
Ø It returns an int value to the environment that called the program
Ø Recursive call is allowed for main( ) also.
Ø It is a user-defined function
Ø Program execution ends when the closing brace of the function main( ) is reached.
Ø It has two arguments 1)argument count and 2) argument vector (represents strings passed).
Ø Any user-defined name can also be used as parameters for main( ) instead of argc and argv

Question: What is a function and built-in function?
Answer: A large program is subdivided into a number of smaller programs or subprograms. Each subprogram specifies one or more actions to be performed for a large program. Such subprograms are functions.
The function supports only static and extern storage classes. By default, function assumes extern storage class. Functions have global scope. Only register or auto storage class is allowed in the function parameters. Built-in functions that predefined and supplied along with the compiler are known as built-in functions. They are also known as library functions.

Question: What is modular programming?
Answer: 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.

Question: When does the compiler not implicitly generate the address of the first element of an array?
Answer: Whenever an array name appears in an expression such as
Ø array as an operand of the sizeof operator
Ø array as an operand of & operator
Ø array as a string literal initializer for a character array
Then the compiler does not implicitly generate the address of the address of the first element of an array.

Question: What are the characteristics of arrays in C?
Answer :1) An array holds elements that have the same data type

2) Array elements are stored in subsequent memory locations

3) Two-dimensional array elements are stored row by row in subsequent memory locations.

4) Array name represents the address of the starting element

5) Array size should be mentioned in the declaration. Array size must be a constant expression and not a variable.

Question: Differentiate between a linker and linkage?
Answer: A linker converts an object code into an executable code by linking together the necessary build in functions. The form and place of declaration where the variable is declared in a program determine the linkage of variable.

Question: What are the advantages of auto variables?
Answer : 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: What is storage class and what are storage variable?
Answer: A storage class is an attribute that changes the behavior of a variable. It controls the lifetime, scope and linkage.
There are five types of storage classes
1) auto
2) static
3) extern
4) register
5) typedef

Question: Which expression always return true? Which always return false?
Answer: expression if (a=0) always return false
expression if (a=1) always return true.

Question: Write the equivalent expression for x%8?
Answer:x&7

Question: why n++ executes faster than n+1?
Answer: 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: what is a modulus operator? What are the restrictions of a modulus operator?
Answer: 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.

Question: Can the sizeof operator be used to tell the size of an array passed to a function?
Answer: No. There’s no way to tell, at runtime, how many elements are in an array parameter just by looking at the array parameter itself. Remember, passing an array to a function is exactly the same as passing a pointer to the first element.