Bloomberg LP Interview Question for Software Engineer / Developers






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

1. By default class has private access specifier and structure has public in C++.
2. MyClass(const MyClass &)
{
}
3. To create a copy of existing object.
4. By default = does a shallow copy so if your class has pointers to some other data type except inbuilt one then a shallow copy would be done and it may result in dangling pointers.
5. const MyClass& operator = (const MyClass &)
{
}
6. So that chaining can be possible like below
x = y = z = 10;
8. The class destructor should never throw an exception. A destructor can be called in 2 ways.One when an object goes out of scope and 2nd when stack unwinding happens.So if a destructor is called due to stack unwinding and it throws an exception then C++ unexpected is called which calles terminate which terminates the whole application.Nothing is destroyed not even local objects.
9. Make class Singleton.
10. Use Mutex to warp the code where instantiation happens.
11. Operator new allocates raw memory only.It is similar to malloc() in C.
new first allocates raw memory using operator new and then calls constructor for the class.
12. bad_alloc exception is thrown.

- Cookie May 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi John,

Your 7a is already corrected by Cookie
Your '7b' is also incorrect.
---> " Myclass *myclass = new MyClass[10]; " here new will allocate memory and creates object as well, by calling its default constructor. During compilation, You will encounter 'No appropriate default constructor available' error.
Rest of the code is also error.

The correct answer to 7b is:

MyClass* mc[10];
for (int i=0; i < 10; i++)
  mc[i] = new MyClass(i); <--- it will call user-defined constructor here.

- Maninder Singh June 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Is it can be some thing like this for stack?

MyClass objMyClass[] = {MyClass(1), MyClass(2)};

- Jey June 10, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Jey is right,
the way like MyClass myClass[10] = {1,2,3,4,5,6,7,8,9,10} is not correct.
you must do it like
MyClass myClass[10] = {MyClass(1),MyClass(2),...,MyClass(10)}

- bbs59 June 12, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That is correct of myClass[10] = {1,2,3,4,5,6,7,8,9,10}
I tested in vc++ and it totally worked!

- leakymemory September 27, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That is correct of myClass[10] = {1,2,3,4,5,6,7,8,9,10}
I tested in vc++ and it totally worked!

The constructor is not defined as explicit, it should be OK above

- creation December 09, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

just remember aggregation initialization: Type MyArray[size] = {Elem1, Elem2,...};
e.x, int m[] = {1,3,5,7};
So,

On Stack: MyClass myArray[] = {MyClass(1), MyClass(2),...,MyClass(10)};

On Heap: MyClass* p[] = {new MyClass(1), new MyClass(2),..., new MyClass(10)};

- bbs59 June 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Not really a general solution. What if it's 1000000 instead of 10?

How about this?

7a. MyClass* mc = (MyClass*)(new char[10*sizeof(MyClass)]);

7b. char stackMem[10*sizeof(MyClass)];
    MyClass* mc = (MyClass*)stackMem;

- Tim December 12, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Jey:

The correct answer for allocating the objects on heap would be using placement new operator i guess.

create a chunk of memory (char*) with normal new[]

offset it to point to account object using placement new

note placement new wont allocate any memory but calls ctor

in this way u dont need a array of pointers

deletion could be little tricky

first explicitly call the dtor for each object and delete the chuck as a whole

- contactjey June 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

To point #8 I would add. Make it virtual if any other function is virtual

- wihenao August 13, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

To point#8, it is necessary declare it as "virtual" also if the destructor is called by a base pointer:

class A {...};
class B: public A {...};

...

A* pA = new B();
...
delete pA;

If A destructor is not virtual only A destructor is called.

- Pedro August 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

10) simplest answer is use RAII and lock the object creation, something like,

MyInstance* MyInstance::Instance()
{
Lock lock(m_dataMutex);
if(!pInstance)
{
pInstance = new MyInstance;
}
return pInstance;
}

but this is not multi-thread safe and also is less optimized. Read about double-check locking pattern to know more.

- ac November 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

7a. The following code is working fine:

#include <iostream>
using namespace std;

class Any
{
  public:
	   int var;

	  //Any() {  }

	  Any(int v)
	  {
		  var = v;
	  }

	  Any(Any &q)
	  {
		  var = q.var;
	  }

	  void prnt()
	  {
		  std::cout << "\n Var: " << var << "\n";		  
	  }
	  
};

void main()
{
  Any m[10] = {1,2,3,4,5,6,7,8,9,10};
  
  for(int x=0; x<10; x++)
      m[x].prnt();

}

- Rajika November 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

7a: Myclass myclass[10];

7b:
Myclass *myclass = new MyClass[10];
int i;
for (i = 0; i < 10; i++)
myclass[i] = new MyClass();

- john May 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@John

Myclass myclass[10]; will not work.This statement would give a compile time error as the class lacks a default constructor.

To construct the array on stack it would be done like below

int i[] = {1,2,3,4,5,6,7,8,9,10};

MyClass myClass[10] = {1,2,3,4,5,6,7,8,9,10};

- Cookie May 17, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Correct:)

- Anonymous June 10, 2009 | Flag


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