Bloomberg LP Interview Question for Software Engineer / Developers






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

Um... actually the answer is:
b.x = 5
because in this case, the copy constructor is called. In no case could the result be 4 since "=" symbol forces either the copy constructor or the assignment operator to be invoked. The statements
Someclass c(2);
c = a;
will give the result 3, not 4 because the assignment operator will be called rather than either of the constructors.

- Wandering programmer February 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

For the second case, I think you are talking about 1 instead of 3. Because "=" is called.

- Mimi March 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No... the assignment operator will be called to assign a to c. In this context the value of 2 contained in c.x will be overwritten by the value of 4 contained in a.x. This new value will then be decremented to 3. I suggest you compile and run the following example
#include <iostream>

using namespace std;

// Online Test
class Someclass {
public:
int x;
public :
Someclass(int xx) : x(xx) { }
Someclass(const Someclass& a) { x = a.x ; x++;}
Someclass& operator =(const Someclass& a1) { x = a1.x ; x--;}
};

int main( )
{

Someclass a(4);
Someclass b = a;
Someclass c(2);

c = a;

cout << "a.x = " << a.x << endl;
cout << "b.x = " << b.x << endl;
cout << "c.x = " << c.x << endl;
}

- Wandering programmer March 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

"=" is such a b*tch! :)

I actually compiled your (dfmcia) program. You are right-
b.x =5. I expected b.x=3.

You said for "=" operator either the copy or assignment constructor is called, but i always got b.x=5 when I ran the program multiples. How do i make sure only assignment operator is called when "=" is used, given that both of them are defined?

- chethanjs May 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Forget it, I figured it out.

1) When you are using "=" when initializing a class, then always copy constructor is called.
Eg: Someclass b = a;


2) When you are using "=" to assign to an already initialized class, then assignment operator is called.
Eg: b = a or c = a.

- xankar May 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

first, there is an unclosed comment (/*) so it won't compile.

Second, There will be no output, because there is no output line.

Third, a.x = 4 because the instantiation of a uses the constructor that takes an int. b.x = 5 because the instatiation of b uses the copy constructor; the call:

SomeClass b = a;

is equivalent to

SomeClass b(a);

Both use the copy constructor. The assignment operator would only be called with:

SomeClass b;
b = a;

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

nice

- siva.sai.2020 May 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Someclass {
public:
int x;
public :
Someclass(int xx) : x(xx) { }
Someclass(const Someclass& a) { x = a.x ; x++;}
Someclass& operator =(const Someclass& a1) { x = a1.x ; x--;}
};

int main( )
{

Someclass a(4);
Someclass b = a;
Someclass c(2);

c = a;

cout << "a.x = " << a.x << endl;
cout << "b.x = " << b.x << endl;
cout << "c.x = " << c.x << endl;
}

- Anonymous March 19, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class SomeClass {

int x;

public: SomeClass(int xx) : x(xx) { } }; int main() { SomeClass a(10);

SomeClass b(a);

return 0;

}

- Anonymous May 28, 2022 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class someclass{
int x;
public: someclass (int xx0 : x(xx0 ()};
int main(){ someclass a(10);
some class b(a);
return 0;
}

- Anonymous July 24, 2022 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

b.x = 4

- Anonymous February 26, 2010 | 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