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.


Lesson(2) in class constructor with example codes

Sphere: Related Content Constructor Overloading:
===================
As soon as we provide our own constructor, compiler does not provide any implicit default constructor.
Now it is our headache to provide the class constructors with proper argument list and there definitions as well.
e.g.

class Account{
public:
Account(){cout << "this is default constructor"<< "\n";} Account(int n) {cout << "Value is " <<>

the output:

this is default constructor
Value is 15
The name is C++

The above declarations like:
Account();
Account(int n);
Account(const char *name);

are nothing but constructor overloading.

Constructor Overloading:
===================
Like any other function, a constructor can also be overloaded with more than one function. Those functions will have the same name but different types or number of parameters. For overloaded functions the compiler will call the one whose parameters match the arguments used in the function call. In the case of constructors, which are automatically called when an object is created, the one executed is the one that matches the arguments passed on the object declaration.

**Note:
Remember if we don't declare *any* constructor, compiler will assume that class to have default constructor, and program will compile successfully e.g

class Account{
public:

private:
char *_name;

};

int main()
{
Account acc1;
return 0;
}

But the following program won't compile successfully.

class Account{
public:
//Account(){cout << "this is default constructor"<< "\n";}
Account(int n) {cout << "Value is " <<>
};

int main()
{
Account acc1;
Account acc2(15);
return 0;
}
It will show following compilation error:

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
Prac\const_1.cpp:
Error E2285 Prac\const_1.cpp 19: Could not find a match for 'Account::Account()' in function main()
Warning W8004 Prac\const_1.cpp 23: 'acc1' is assigned a value that is never used in function main()
*** 1 errors in Compile ***

reason is we have already provided a constructor for the class, so compiler has stopped providing or assuming that class to have any default constructor. Now no more default constructor. Programmer needs to provide the constructor explicitly.

By the way,
Account acc1() implies a function acc1() which is returning Account. Please don't be confuse this with constructor declaration.
Account acc1(); //wrong
Account acc1; //right




Friday, June 27, 2008

Lesson(1) in class constructor with example codes

Sphere: Related Content Default Constructor:
===============
If no constructor is declared in the class definition then compiler assumes class to have default constructors without any arguments.
e.g.

class Account
{

public:
private:
char *_name;

unsigned int _acct_num;
double _balance;
};

int main()
{
Account acc;
return 0;
}


The above program will compile successfully. Though there is no class constructor. In this case compiler will assume that Account class has default constructor. So we can declare a object of this class by simply declaring object without any arguments. like
Account acc;

Eventually we can write our own default constructor
e.g.

class Account{
public:
Account() {cout << "this is default constructor"<<>
private:
char *_name;
unsigned int _acct_num;
double _balance;
};

int main()
{
Account acc;
return 0;
}



Now output of the program is :
this is default constructor

So in this way we can provide our own default constructor.

Wednesday, June 18, 2008

I need quota for myself-The Gujjar ways

Sphere: Related Content I need quota for me, I need quota for my family, I need quota for my community. After seeing Gujjars I am also sure that I can have reservation for myself only thing I need support from few people, support from media and obviously few politician friends. Gujjars were demanding quota or reservation because they were feeling that they had been marginalized by the uprising of Meena community in Rajasthan. Meenas have more representative in Rajasthan Govt. office. Moreover Mrs. Raje also quite responsible for this situation. Because she promised about the reservation to secure Gujjar vote bank to come into power. Now sha has to bite the bullet. As a result 5% special quota has been reversed for the Gujjar in the OBC category. But Gujjar had paid much for this achievement. Few Gujjar people died in the agitation since last year. Now they have the much-awaited quota in their hand. But now I am afraid that this trend must have some serious impact on other national issues like demand for Telengana, Gorkhaland etc. The rule is simple. A group of few people need to block all major roads, highways, railways, show constant non-cooperation to Govt, do some vandalism for a long time. No need to call media. They will come automatically because they need breaking news. Basically you need to capture the headline of the media. Then you need to refuse any peace request from Govt. for several times and apply all the means to disturb the normal public life. This way you can get into the nerve of Govt. After a long time you will be awarded with your reservation. Really Gujjar has shown the way how to get reservation. Now I am waiting for few more incidents like. Are not you???

Now it is called hunger !!!

Sphere: Related Content There are recently 2 news really drive me crazy 1) Asia will face bandwidth crisis by 2011, 2) World will out of internet address by 2010. Two situations are closely inter-related and that is penetration of internet like never before. People were thinking what would be the killer application that will drive people crazy. Now we got the answer youtube, orkut, facebook and other social networking sites. People are crazy for online streaming sites like youtube etc etc. Now-a-days there are so many news portals are there which provide online streaming of there channel needless to say that it is real time. So need more and more bandwidth. Moreover, mobile internet are achieving greater market share day-by-day. 3G enabled mobile phone are not lesser than PC-based internet. Connected thro' WiFi or whatever means you know and start browsing internet. So need more and more IPs. So guys need to learn IPv6 to survive. Seeing the situation, all internet users must move to an upgraded platform - called Internet Protocol version 6 - to access the 340 trillion-trillion-trillion new addresses needed to connect not only billions of new users but also the trillions of sensor devices that will require networking as technology takes greater control of people's lives. "IPv6 provides more addresses in cyberspace than there are grains of sands on the world's beaches," news.com.au quoted Viviane Reding, EU Commissioner for the Information Society, as saying. I don't know when we will be satisfied or rather I am doubtful that any infrastucture can ever satisfy this ever growing need for more bandwidth ??