Monday, December 1, 2008

c++ placement interview Question with solution

1.class complex{
double re;
double im;
public:
complex() : re(0),im(0) {}
complex(double n) { re=n,im=n;};
complex(int m,int n) { re=m,im=n;}
void print() { cout<<
};

void main(){
complex c3;
double i=5;
c3 = i;
c3.print();
}

Answer: 
5,5 

Explanation:
Though no operator= function taking complex, double is defined, the double on the rhs is converted into a temporary object using the single argument constructor taking double and assigned to the lvalue.


2.void main()
{
int a, *pa, &ra;
pa = &a;
ra = a;
}

Answer : 
Compiler Error: 'ra',reference must be initialized

Explanation : 
Pointers are different from references. One of the main 
differences is that the pointers can be both initialized and assigned,
whereas references can only be initialized. So this code issues an error.

No comments:

Post a Comment

Thanks to given comments.......

My Blog List