Goldman Sachs Interview Question for Software Engineer / Developers






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

It gives compile time error: can not convert from B to A&

- BM February 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

correct, type doesn't match

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

correct, type doesn't match

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

if you want to get away from the compile time error use reinterpret_cast as below.
A &a =reinterpret_cast<A&>(b);

- BJ September 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No. It will not give any error. this code will work

- Shrikant Badiger September 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ Please ignore above Qn. here is correct one

output of following Program
class A
{
int n;
public:
virtual void Fun1(int no=10)
{
n = no;
cout<<"A::Fun1"<<n;
}
};

class B
{
int m;
public:
virtual void Fun1(int no=20)
{
m = no;
cout<<"B::Fun1()"<<m;
}
}


int main()
{
B b;
A &a =b;
a.Fun1();
}

- RS February 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

output - B::Fun1 10

- RS February 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

no, it won`t because that`s no inheritance there. If add to B class " : public A" then yes, ur answer is correct.

- Anonymous February 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

My be RB forgot to put following in the code

class B: public A

If he is not forgotten then its a compiler error.

at A &a =b;

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

Looks like they forgot inheritance in the above code. If B is inherited from A then the output is B::Func1.a The confusing part is what happens if its not a reference and just an object of A ?

ie., A a = b; /// in this class object gets sliced and caling
a.func1() would result int A::Func1..

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

@ above
Sorry guys I forgot to add class B: public A.. assume class B is publicly inherited from A.

- RS March 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I tried compiling and it worked.

case 1:
int main()
{
B b;
A &a =b;
a.Fun1();
getch();
return 0;
}

output : B::Fun1<>10

case 2:


int main()
{
B b;
A a =b;
a.Fun1();
getch();
return 0;
}

output : A::Fun1<>10

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

shouldn't first output shd be;
cout<<"B::Fun1()"<<m;

B::Fun1() 20

- bills March 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, the reason behind this is

B b;
A &a =b;
a.Fun1();

function resolution happens at run time. hence a.Func1() will invoke the derived class Func1(). i.e., dynamic binding. whereas the initialization of the variable happens at the compile time.

So at compile time
a.Func1() will have references to Base class Func1
virtual void Func1(int no=10)
{
n = no;
cout<<"A::Fun1"<<n;
}

and hence no=10.

- Nit March 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Functions are dynamically bind in case of virtual but the default parameter values are statically bind. Refer More Effective C++.

- Richa March 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

good questions and good answer by Nit above

- Sirius April 29, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes..!! this function will give error. Invalid assignment for the refenece in A &a;

- Shrikant Badiger September 19, 2013 | 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