Technical Interview Questions

C Programming Interview Questions-C- Advanced

|   100952

1.Can we have a pointer to a function?

a. Not possible  

b. Possible

c. Depends on the return value

d. Depends on the # of arguments

Answer:b.Possible

void func(int a)
{
}
void main()
{
void (*fp)(int);
fp=func;
fp(1);
}

2.Write a function to swaps the values of two integers.

a. No solution exist.  

b. Solution need 2 extra variables  

c Solution exist without any extra variables  

d. Solution need 1 extra variable 

Solution c Solution exist without any extra variables

1. void swap(int a, int b)
{
int c;
c=a;
a=b;
b=c;
}

2. void swap (int a, int b)
{

a=a+b;

b=a-b;

a=a-b;

}
Solution2 is the best solution since no extra variable is required.

3. Which of the following Bitwise operators can be used efficiently to swap two numbers?

a.   &      

b. ^      

c. |  

d. ||

Solution:b ^

a=a^b
b=a^b
a=a^b

Now ‘a’ will have ‘b’s initial value and wise-versa. 

4.Do you find any issue with the above snippet of code?

 a.  No issues   

b. P is a bad pointer  

c P is a void pointer  

d. Both 2& 3

Answer: b. P is a bad pointer

When a pointer is first allocated, it does not have a pointee. The pointer is "uninitialized" or simply "bad". A de-reference operation on a bad pointer is a serious run-time error. If you are lucky, the de-reference operation will crash or halt immediately (Java behaves this way). If you are unlucky, the bad pointer de-reference will corrupt a random area of memory, slightly altering the operation of the program so that it goes wrong some indefinite time later. Each pointer must be assigned a pointee before it can support de-reference operations. Before that, the pointer is bad and must not be used. Bad pointers are very common. In fact, every pointer starts out with a bad value. Correct code overwrites the bad value with a correct reference to a pointee, and thereafter the pointer works fine. There is nothing automatic that gives a pointer a valid pointee.

void BadPointer()
{

int* p;            // allocate the pointer, but not the pointee

*p = 42;        // this de-reference is a serious runtime error

}

5.  void add(int a, int b)

{

int c;

c =  a + b;

add (1,1);       

}

What is the result of above funtion?

a. Sum of a,b,1   

b. Results in Buffer Overflow    

c Results in Compiler Error

d Results in Stack Overflow

Answer: d. Results in Stack Overflow

When a function is called recursively, sometimes infinite recursions occur which results in STACK OVERFLOW. What does it mean? Well when a function is called,

1. First it will evaluate actual parameter expressions.

2. Then, memory is allocated to local variables.

3. Store caller’s current address of execution (return address of the current function) and then continue execute the recursive call.

4. Then it executes rest of the function body and reaches end and returns to the caller’s address.

Now when a function is infinitely called (recursively) without a proper condition to check its recursive, then only first 3 steps keep executing and function will never reach step 4 to finish execution and return to previous function. In this way, function will keep allocating memory and at some point of time it will go out of memory or reaches stack limit and will never be able to accommodate another function and hence crashes. This is called stack overflow.

6. Which of the following will initialize the new memory to 0 ?

a. malloc      

b.     free      

c.   new    

d    delete

Answer: c. new

“new” will initlize the new memory to 0 but “malloc()” gives random value in the new alloted memory location

7.Which of the following standard C library converts a string to a long integer and reports any .leftover. numbers that could not be converted.

a.   atol     

b.   atoi   

c.  stol   

d.  strtol

Answer: d. strtol

strtol() Converts a string to a long integer and reports any .leftover. numbers that could not be converted.
atoi() Converts a string to an integer.
atol() Converts a string to a long integer. 

 

 

 

feedback