Morgan Stanley Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

Dynamic polymorphism is applicable to methods but not to variables. So the output will be "0 and In Child"

- Naresh Parimi May 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I agree with u Broda

- SRINIVAS May 16, 2014 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

here the statement :

ParentTest s = new ChildTest();

is evaluated as

ParentTest s ;
	s= new ChildTest();

even though the variable s refers an instance of class ChildTest, the s.x still evaluates to "0". Because variables names in Java are resolved by the reference type (it is ParentTest in this code), not the object they are referencing.this is known as Shadowing in Java

ChildTest extends ParentTest and overrides its print() method and when we call print() method from a reference variable of type ParentTest, it doesn't call print() method from ParentTest class instead it calls print() method from ChildTest subclass because object referenced by ParentTest type is a ChildTest object. This resolution happens only at runtime because object only created during runtime and called dynamic binding in Java AKA Dynamic Method Dispatch.

- Harshil Raval May 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ParentTest s = new ChildTest();
It is using dynamic binding concept but s variable is parent type so s is access to x variable of parent class.

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

It would result in compilation error as you have 2 public classes in a single file. If that was not intentional or the classes are assumed to be in different files, the answer should be zero i.e base class' value of that variable.

- phalgun May 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I know little about java, but in c++ in this case, there is no virtual function. So the result should be "0 and In Parent".
And I verfy it on my ubuntu. here is my code in c++:

class A
{
  public:
    int x;
    void print()
    {
      printf("In parent, x= %d\n", x);
    }
    A(): x(0){}
};

class B: public A
{
  public:
    int x;
    void print()
    {
      printf("In child, x= %d\n", x);
    }
    B():x(1){}
};

int main()
{
  A* p = new B;
  p->print();
  delete p;
  return 0;
}

- AcesLau May 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.


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