fbpx

Typical Bloomberg phone interview questions with answers

(Last Updated On: May 12, 2010)

Typical Bloomberg phone interview questions with answers

Typical Bloomberg phone interview questions:
I got phone interviewed with Bloomberg.
1)diff bet class and structure
2)syntax of copy constructor
3)why we need copy constructor?
4)why we need overloaded = operator?
5)signature of overloaded = operator for a class?
e.g. Myclass & operator =(const Myclass &)
6)why & used in return type of = operator ?
7)suppose :

class Myclass{
public:
Myclass(int a){m=a;};

private :
int m;
};

a)how will u create an array of objects of Myclass on stack?
b)how will u crete an array of objects on heap?

8)what precautions should be taken to write a class’s destructure?
9)what will u do to prevent others from creating more than one instances of ur class?
10)how will u make singleton thread safe?
10.1) what problem may occure with singleton at run time?
11)what is diff between “operator new” and “new” ?
12)what happens if new fails to allocate memory?

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.

Reply to Comment
john on May 16, 2009 |Edit | Edit

7a: Myclass myclass[10];

7b:
Myclass *myclass = new MyClass[10];
int i;
for (i = 0; i < 10; i++) myclass[i] = new MyClass(); Cookie on May 17, 2009 |Edit | Edit @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}; Correct:) 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. Jey on June 10, 2009 |Edit | Edit Is it can be some thing like this for stack? MyClass objMyClass[] = {MyClass(1), MyClass(2)}; bbs59 on June 12, 2009 |Edit | Edit 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)} leakymemory on September 27, 2009 |Edit | Edit That is correct of myClass[10] = {1,2,3,4,5,6,7,8,9,10} I tested in vc++ and it totally worked! creation on December 09, 2009 |Edit | Edit 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 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)}; Tim on December 12, 2009 |Edit | Edit 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; 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 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. 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. 7a. The following code is working fine: #include
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(); }

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!
Don't miss out!
Get my TRADE LIKE A BOSS 2 PDF Books

You will received instantly the download links.

Invalid email address
Give it a try. You can unsubscribe at any time.

NOTE!

Check NEW site on stock forex and ETF analysis and automation

Scroll to Top