Saturday, June 28, 2008

Technical Interview Question (1)

Sphere: Related Content

1. What is Callback Function?

Another use for function pointers is setting up "listener" or "callback" functions that are invoked when a particular event happens. The function is called, and this notifies your code that something of interest has taken place.

Why would you ever write code with callback functions? You often see it when writing code using someone's library. One example is when you're writing code for a a graphical user interface (GUI). Most of the time, the user will interact with a loop that allows the mouse pointer to move and that redraws the interface. Sometimes, however, the user will click on a button or enter text into a field. These operations are "events" that may require a response that your program needs to handle. How can your code know what's happening? Using Callback functions! The user's click should cause the interface to call a function that you wrote to handle the event.

2. What is function pointer?

#include

void (*foo)(int);

void my_int_func(int x)

{

printf( "%d\n", x );

}


int main()

{

foo = &my_int_func;

/* call my_int_func (note that you do not need to write (*foo)(2) ) */

foo( 2 );

/* but if you want to, you may */

(*foo)( 2 );

return 0;

}


3. what will be the output:

#include

#include

#include

void printfunc(char *);

#define LEN 50

int main()

{

#undef LEN

#define LEN 70

char *p = "abcdefg";

char q[]= "abcdefg";

char *s= (char *)malloc(sizeof(char)*5);

int *r, a = 10;

r = &a;

printf("len is %d\n", LEN);

printf("len is p %d\n", strlen(p));

printf("len is p %d\n", sizeof(p));

printf("len is q %d\n", sizeof(q));

printf("len is s %d\n", sizeof(s));

printf("len is r %d\n", sizeof(r));

printfunc(q);

}

void printfunc(char *q)

{

printf("in func len is q %d\n", sizeof(q));

}

Output:

len is 70

len is p 7

len is p 4

len is q 8

len is s 4

len is r 4

in func len is q 4.

4. Difference between Data Structure and Algorithm?

Data structure refers to the ways data is stored in memory

An algorithm refers to how it is manipulated.

5. What is the difference in using function call-by-Reference and function call-by-Pointers?

Answer: pointer can be null, whereas a reference always refers to something. If you want to allow the possibility of a null argument your only option is a pointer parameter.

6. What is the advantage and disadvantage of Inline function?

Different compilers apply different rules to determine whether a function defined as inline will have its calls replaced by the inline code. Obviously making a function “inline” is likely to be unproductive, especially if it’s called many times in a program.

Inline functions are best used for small functions such as accessing private data members. The main purpose of these inline functions (usually one or two lines of code) is to return state information about objects; short functions are sensitive to the overhead of function calls. Longer functions spend proportionately less time in the calling/returning sequence and benefit less from inlining. An inline function typically saves the overhead of:

  • Function calls (including parameter passing and placing the object's address on the stack)
  • Preservation of caller's stack frame
  • New stack-frame setup
  • Return-value communication
  • Old stack-frame restore
  • Return

7. Difference between macro and inline function?

Inline functions are similar to macros because they both are expanded at compile time, but the macros are expanded by the preprocessor, while inline functions are parsed by the compiler. There are several important differences:

  • Inline functions follow all the protocols of type safety enforced on normal functions.
  • Inline functions are specified using the same syntax as any other function except that they include the inline keyword in the function declaration.
  • Expressions passed as arguments to inline functions are evaluated once. In some cases, expressions passed as arguments to macros can be evaluated more than once.

8. When should I use macro and when inline functions?

Besides the difference already pointed out, you also must have in mind that because macros are expanded at pre-compile time, you cannot use them for debugging, but you can use inline functions.

Code:

#define max(a,b) (a>b?a:b)

class foo{

public:

inline int maxim(int a, int b);

};

inline int foo::maxim(int a, int b)

{

return a > b ? a : b;

}

int main(){

foo f;

int x = max(1,2);

int y = f.maxim(1,2);

return 0;

}


In this example you can put a breakpoint in foo::maxim and step into the method, though it is an inline function. (if maxim was defined in the body of the foo class the keyword inline would have not been necessary) However, because macros are expanded before compilation actually starts, you cannot do that with the macro max. It is expanded at pre-compile time to:

Code:

int main()

{

foo f;

int x = 1>2?1:2;

int y = f.maxim(1,2);

return 0;

}

Also you must be carefully with macros, because they can have the arguments evaluated more than once. Here is an example:

Code:

#include

using namespace std;

#define max(a,b) (a>b?a:b)

int main()

{

int a = 0;

int b = 1;

int c = max(a++, b++);

cout <<>

return 0;

}


The intention is to have the program print 1 and 2, but because the macro is expanded to:

Code:

int c = a++ > b++ ? a++ : b++;

b will be incremented twice, and the program prints 1 and 3.


No comments: