Posts

Showing posts from March, 2010

Good collection of C n C++ interview written Questions and answers

Download Questions in PDF : Download C n c++ written questions and Ans(PDF) C Aptitude 1. void main() int const * p=5; printf("%d",++(*p)); Answer: Compiler error: Cannot modify a constant value. Explanation: p is a pointer to a "constant integer". But we tried to change the value of the "constant integer". 2. main() char s[ ]="man"; int i; for(i=0;s[ i ];i++) printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]); Answer: mmmm aaaa nnnn Explanation: s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea. Generally array name is the base address for that array. Here s is the base address. i is the index number/displacement from the base address. So, indirecting it with * is same as s[i]. i[s] may be surprising. But in the case of C it is same as 3. main() float me = 1.1; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); Answer: I hate U Explana

C++ Class Member Initialization order....

Sample Code.... class Test { int x; int y; public:Test(int z) { x=y=z; } }; class Test1 { Test t; const int b; Test1(int z):t(1),b(10) { } }; CSomeClass::CSomeClass() { x=0; y=1; } and in other places I see it written this way: CSomeClass::CSomeClass() : x(0), y(1) { } Some of my programmer friends say that it's better to do it the second way, but no one knows why. Can you tell me the difference between these two methods of initializing class members? A Technically your friends are right, but in most cases it makes no difference. There are two reasons to use the second syntax, which is called a member initialization list: one is necessity, the other efficiency. Let's examine the first reasonĂ¢€"necessity. Suppose you have a class member that is itself a class or struct, and this class only has one constructor that requires an argument. class CMember { public: CMember(int x) { ... } }; Since CMember has an explicit constructor, the compiler do