Google Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

It should print B A C.

confirmed it on an actual program!

- johnny drama November 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well my C# code produced "A B C".
Where class C "has an instance of A as its private member" <- this line is a little a ambiguous in the problem description.

- CameronWills November 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Actually, even if class B has an instance of A as its private member.. The output was still "A B C"

- CameronWills November 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well Mr. Wills product of your code was right but not the language. As far as I know Google mostly uses Java, C/C++, Python. C++ will return BAC.

- NameNo November 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

correct when A is instantiated during declaration

- mm January 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 5 vote

ABC, when A is instantiated during declaration

BAC, when A is instantiated in C's constructor

class A
{
public A()
{
Console.Write("A");
}
}

class B
{
public B()
{
Console.Write("B");
}
}

class C : B
{
A a = new A();

// A a;

public C()
{
//a = new A();
Console.Write("C");
}
}

- siva November 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

BAC when A is instantiated during declaration
BCA when A is instantiated in the constructor

- mm January 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

The ouput is B A C.
This how the code looks likes:
class A
{
A() { cout<<"A"<<endl;}
};
class B
{
B() {cout<<"B"<<endl;
A a1;
}
};
class C:public B
{
C()
{ cout<<"c"<<endl; }
};

int main()
{
C c1;
return 0;
}

- Prashanth November 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Since problem is clearly saying C is having the instance of A class as private, so we are not considering it should be in constructor of C class:-

Output will be:- BAC
Code:-

public class A {
public A(){
System.out.println("A");
}
}

public class B {

public B(){
System.out.println("B");
}
}

public class C extends B{
private A a=new A();
public C(){
System.out.println("C");
}

public static void main(String arr[]){
C obj=new C();

}
}

- Abhishek January 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Since problem is clearly saying C is having the instance of A class as private, so we are not considering it should be in constructor of C class:-

Output will be:- BAC
Code:-

public class A {
	public A(){
		System.out.println("A");
	}
}

public class B {

	public B(){
		System.out.println("B");
	}
}

public class C extends B{
	private A a=new A();
	public C(){
		System.out.println("C");
	}
	
	public static void main(String arr[]){
		C obj=new C();
		
	}
}

- Abhishek January 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Just adding to the BAC consensus with my explanation.

If the interviewer asked why does it have to be this way?
My answer would be:
1) The parent class B must be constructed first to enable access to inherited elements during C's initialization => B;
2) The member variable *instance* A ("instance" means A is not an uninitialized reference*) of the class C must be called before the classes constructor or they can't be operated on in the constructer => A
3) The constructer C has the pre requisites required and can be invoked => C

* As some have pointed out *if* A was a pointer its constructer would not be called until A is explicitly created. But a pointer to A is not equivalent to an "instance of A" which the question references.

- gvp March 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Why it should print A? C only has an instance of A,it is not saying that we use it anywhere...

- vag November 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

It also depends on how you create A. If it's a simple C member (non dynamic), then you would probably initialize it in C's constructor. Also, C is a subclass of B. Then output would be B A C

- Olster November 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is a very simple question to determine whether candidate knows about RAII and inheritance in C++. I think it is not related to C#.

- A.Y.R. November 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

It should print ABC without any if.

- artaka December 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

When B is called it process all the statements in its constructor

- Prashanth November 23, 2012 | 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