Bloomberg LP Interview Question for Financial Software Developers






Comment hidden because of low score. Click to expand.
1
of 1 vote

A* a =new A();//default constructor
foo(*a); //copy constr
A b=*a; //copy constr
b=*a; //assignment constr

- beyondfalcon August 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My answer is 2. But I have tested it, the actual number should be 3. I understand it called copy ctor for foo(*a) and A b=*a;
But why does it call copy ctor after assignment operator of b=*a; Can some one explain plz?

- Huangxinyu June 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You are right. It's two.

b=*a should be assignment operator.

Not sure how you thought it could be 3?

- xankar June 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I used the following class to test.
The output of above code is:

Default Ctor
Copy Ctor
Copy Ctor
Assignment Operator
Copy Ctor

No sure why.

class A{
public:
A(int ii=0);
A(const A& a);
A operator=(const A& a);
//int get() const;
private:
int i;
};

A::A(int ii){
i=ii;
cout<<"Default Ctor"<<endl;
}

A::A(const A &a){
i=a.i;
cout<<"Copy Ctor"<<endl;
}

A A::operator =(const A &a){
i=a.i;
cout<<"Assignment Operator"<<endl;
return *this;
}

- Huangxinyu June 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

foo(*a);
calling this function causes actual argument *a assigned to formal argument of foo().
2 you got + this =3

- Amit June 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Because you are returning by value in assignment operator tahn by reference

Ur assignment operator shud be like this

A& A::operator =(const A &a){
i=a.i;
cout<<"Assignment Operator"<<endl;
return *this;
}

The code should only call 2 copy ctors once ur assignment operator is fixed

- Dipkin June 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Exactly, thanks

- Huangxinyu June 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Always a temporary object is created when not returned by reference then later it is copied to caller object using copy constructor.

- Karthik Raj June 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

so the final output should be 2.

- raja roy July 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

So the answer is: It depends on the implementation of the copy constructor. Copy Constructor is definitely called twice, for lines 2 (when foo is called) and 3, but a poorly implemented assignment operator might call the copy constructor when when returning or being called.

- Anonymous September 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry, I meant ". . . on the implementation Assignment Operator . . ."

- Anonymous September 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

9838790459

- mod baseer March 23, 2016 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More