Interview Question for Software Engineer / Developers






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

for every array element write which constructor to be called.
Eg. A a[] = {A(1,2), A(), A(1) }, where A(1,2), A(), A(1) are constructor available for class A.

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

This solution is good only when you know what you need in that array at compile time. Assume that the content of the array should be detected at run time.

- fiddler.g June 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what do you mean by "content of the array should be detected at run time"? If you mean objects should be created at run time then use array of pointers(much better option will be to use vector<class*> )....

- Anonymous June 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here the length of the array and exact arguments of constructors are known at compile time. Suppose that you need to read the length and each argument from console. The elements will be allocated in sequential cells (in case of using array of pointers, the actual objects allocated in different locations).

- fiddler.g June 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use placement new

To Create array of data type A, do the following


void *mem = ::operator new(sizeof(A) * numOfArrElems);
A *a = static_cast<A*>(mem);
for(int i = 0; i < numOfArrElems; ++i) {
new (a[i]) A(args);
}

// Do your stuff....
// Call dtors on all elems and then delete the memory in the same fashion as was
// allocated
for(int i = 0; i < numOfArrElems; ++i) {
a[i].~A();
}
::operator delete((void*)a);

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

I really dont understand how this would prevent the default constructor from getting called?/

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

The question is not on how to prevent the default ctor to get called, but to create an array of objects of a class which doesn't provide default ctor, when you declare an array - e.g. A a[20] - the default ctor is called for all the 20 objects in array, but if the class doesn't provide one, u need to use a loop to call ctor of all the objects of array.

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

Dipkin,
Can you explain what you are doing in line
new (a[i]) A(args);?
As per your allocation of sizeof(A)*nomOffArrElem,
a[i] is the starting address of a block of memory as large as sizeof(A) and not the start address of a block of memory as large as 4 bytes (pointer to A).

So space allocation is already done.
Should the line
void *mem = ::operator new(sizeof(A) * numOfArrElems);
not be changed to
void **mem = ::operator new(sizeof(A*) * numOfArrElems);

and other changes accordingly?

- thisisveer@rediffmail.com June 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dipkin is using 'placement new'; see this post:

h**p://***.informit.com/guides/content.aspx?g=cplusplus&seqNum=304

- JeffD September 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How abt making the default constructor for that particular class private and use default arguments in some constructor??

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

I think you are implying the use of the Singleton to create some number of objects

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

You have to define an array of pointers. Then you can initialize each pointer separately by using "new":
C* ptrArray[10];
ptrArray[0]=new C(22);
ptrArray[1]=new C(4);
...
This is from "Effective C++" by Scott Meyers. He has another book "More Effective C++". In BB they always ask questions from these books.

- Anonymous July 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This will lead to compiler error
class A
{
A(int i):x(i) {}

private:
int x;

};


int main(int argc, char *argv[])
{

A array[10];
system("PAUSE");
return EXIT_SUCCESS;
}

Reason: no default constructor..

--------------------------------------
one best way to get aroung it is
class A
{
A(int i):x(i) {}

private:
int x;

};


int main(int argc, char *argv[])
{

A* array[10];
system("PAUSE");
return EXIT_SUCCESS;
}
Maintain an array of pointers so that constructors never have to be called

- Amit Priyadarshi September 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