Interview Question


Country: United States




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

It will print Orange for sure. It reads Y as the object of class W and prints the Z belonging to the class W. Had the name of the object of Class W been something else, it would have given Apple as output.

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

why it reads Y as object of class W.??? in class X, there is a static class also. so why they dont read the static class Y???? plz give the proper reason..

- SanBits December 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@SanBits: The memory allocation of static variables and static classes is different. The Y object (of class W) is like a static variable to Class X and is like a static variable to class X. So, this Y (the object) is allocated in the heap. The allocation of an inner class is not in the heap (I don't remember the name of the area where it is stored). So, when Y is called, the compiler first searches for a Y in the heap (according to the normal flow). If it does not find a Y in the heap (case when the object name of W is changed), it accesses the Z of the static inner class. You can try all the above if you have a JVM.

- alex December 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think that the only reason Y is that this order of precedence (static field over static class) is defined in the JLS.

- Igory December 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@lgory: agreed. I'm actually surprised that Java allows this. That means if you're working on a class and you add in a static field that has the same name as an inner class (by mistake), you could actually inadvertently modify the behavior of existing code that references the inner class. It's an unlikely case, but still nasty.

- eugene.yarovoi December 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Well I do Seriously Doubt on what @alex has told up.. People having Java Env Can check with changing the variable name as mentioned by him.

However, being more of the C++ coder, I have a firm idea about what is happening but need to get it verified as Java Don't support pointer.

IMHO, when stack object of Y is being created in the memory stack, the member variable Z will have its address to the offset of 0 bytes from the object of Y.. But as object Y is constructing another object W on the top of its stack, it MAY have this object being created to the offset of 0 bytes to the memory address of the static class Y.

As a Consequence , When we put X.Y.Z.. Compiler goes to the base address of Y and then tries to find the variable Z which after the W's object created lies in the offset of 0 bytes to the base object.

Now the beauti lies in assigning the variable Z which itself is static here.. So as the object W is being created after the Z assignment inside Y, it will get overridden to new value of Z as mentioned in class W.

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

Checking what Alex has said, the program does in fact print "Orange". You could have checked that in ideone if you don't have a Java setup on your computer. In fairness to you though, Alex's response is not completely right because the behavior has nothing to do with where something is stored, but everything to do with the rules of the language. What section of memory a variable or class is stored in is an implementation detail.

The explanation you gave doesn't make a huge amount of sense to me even from a C++ perspective, but even supposing it did, that sort of explanation probably wouldn't extend to Java.

I believe the reason it prints Orange and not Apple is because the language rules assign precedence to associating the . operator with a field (and not a class). If you change the field name to a different name, the program does print "Apple" as one might expect.

- eugene.yarovoi December 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@eugene.yarovoi: The different types of memory allocation is the reason for a higher precedence of one over the other.

- alex December 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@eugene.. Thanks for your insight .. but I would rather like to tell you that in C++ the memory Layout happens EXACTLY what I said.. I am Device Driver Programmer.. and most of the time.. I use this memory management layout to coordinate different process accessing the same memory location..

Well if U know what I meant to say that U understood it.. :D

- hprem991 December 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Alex: I very much doubt that where something is stored in memory is the reason for those precedence rules. That seems like a somewhat extraordinary claim given that AFAIK where something is stored in memory is an implementation detail and may not be consistent across all implementations. Can you point to any sources that would support your position?

@hprem991: Your explanation was unclear to me and I wasn't really able to follow you, so that's why I gave some benefit of the doubt and said "even supposing [it makes sense]". C++ doesn't support inner classes (well maybe the new versions do, I don't know), so I'm not completely sure how you can even attempt a comparison of this situation with any situation in C++. Regardless of the merits of any claims you make about C++ though, this is a Java question.

- eugene.yarovoi December 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Eugene... Who Said C++ Does not Support nested Class ? Search google.. Anyways.. For your information about language.. if your are experienced programmer .. tell me one question.. if you have the language limitation or language operatibility limitation.. will you use or promote language ?? But than how come Java /C++ stand out ?? So what you are claiming here now DOES NOT MAKE SENSE..
There is not benefit of doubt .. in fact for programmer like me there should not be and if there it is U need to clear it out.
So For your info.. here it is.. the only diff with Java from C++ is Memory management.. How java Does MM is BYREFERENCE (no pointer in laymen term).. so even there is one java type version of C++ .. most people called it as C#.. it does the exact same kinda MM what Java Does..
So from my previous expr with Microsoft where I was working on Windows Phone 7 Logger .. I can tell you that the MM layout does follow up almost everything what C++ MM does.. however there is some advance concepts being taken care.. but for question like this no doubt it does.
And I do apologize for my explanation coz I am not a management guy to explain bit and pieces in tech forum and I expect that people reading / commenting have atleast the gap filling knowledge to understand what I meant. :)

- hprem991 December 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

I think that you have created in class X:
--1-- a new class Y
AND
--2-- an object named Y of the class W

X.Y.Z calls (2) , which is a variable = an object named Y of class W
That's why it prints "Orange"

If you wanted to print "Apple" you should use :
X.Y obj = new X.Y();
System.out.println(obj.Z);
This calls on object of the class Y .

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

-1 for giving yourself a +1

- eugene.yarovoi December 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am new here.. I was testing to see if careercup allows me to vote for my self... which it does... I agree it is a bad thing.. I consider it a bug :P

- Kamy December 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I should give you +1 for thinking about corner cases :)

- eugene.yarovoi December 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 3 vote

Well I am not much of the Java Code but looking at the code it should print Apple.

Since its a Static Class so no need to make an Object of class and X.Y.Z refer to the static string Z of the Static Class Y.

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

No. it vl print Orange. but y?? still confusing..!

- SanBits December 14, 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