Peter Swiss

Greenhorn
+ Follow
since Mar 03, 2011
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
6
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Peter Swiss

Hi guys,

This piece of code compiles fine



Class C does not implement Runnable. Still, object of C is passed to the Thread constructor.
Here is the API for Thread class.. http://download.oracle.com/javase/6/docs/api/java/lang/Thread.html

The constructors are no-arg, with a Runnable or with a String. How come this code compiles, when class C is not a Runnable nor a String?
Ok since we are saying "new C()" only C's object is created and because of constructor chaining, it changes the value of s thats declared in superclass A...

Thank you!!




In this line, "B() { super(); s += "b"; } " does class B have a local copy of s? Since, instance variables are inherited, s should refer to B's local copy of s.

And also on this line, "C() { s += "c"; } " s should refer to class C's local (inherited) copy of s.

But since the answer is -dabc, it seems all the changes are applied on the copy of s in superclass.




This question is from the K&B mock exam CD that comes with the textbook.

Nitesh Nandwana wrote:

At first i want to say that you should start from beginning.It's important to know java code conventions.Way you coded , is it really easy to read ?



I didnt write this code! This is exam question!!!

Riza Aktunc wrote:When a constructor is called, on the first line super() is called implicitly. There is just one exception. If the first line is this(...) or super(...), then super() is not called. Considering this information:

In main the constructor of C is called.
From C's constructor, B's constructor is called.
From B's constructor, A() is called.
From A(), A("d") is called.
A("d") makes s="-d"
A() makes s="-da"
B() makes s="-dab"
C() makes s="-dabc"
main prints "-dabc"



Riza, I know how constructor chaining works, my question was whether a subclass gets a local copy of the variable in superclass.
Are instance variables inherited? If yes, does a subclass have a local copy of the instance variable thats declared in superclass?



This program prints: -dabc

Can someone please explain this code?? Thanks a lot.
All right, thank you Vijay!!!
oh, yes!! LOL that was good!
Thanks Vijay!
Here is the question



Which sets of commands will compile and run without exception or error?

A. javac Antique.java
java Antique
B. javac Antique.java
java -ea Antique
C. javac -source 6 Antique.java
java Antique
D. javac -source 1.4 Antique.java
java Antique
E. javac -source 1.6 Antique.java
java -ea Antique


Answer says A and C are correct.
A is obvious.
What is the difference between C and D? Since Assertions were added to Java 1.4, options C and D are the same. So even D should be correct.

Please reply.

Thank you.

Vijay keshava wrote:
Please note that the A and B have a package declaration xcom.



How does that affect it?
package xcom; only means A.java and B.java both will be in package/directory xcom. How does that make option C correct?
In Q 3 on page 812 K&B,

Default classpath is : /foo

Directory structure is :


And these two files:



Which allows B.java to compile?

B. Set the current directory to xcom, then invoke javac -classpath . B.java
C. Set the current directory to test, then invoke javac -classpath . xcom/B.java

The answer says C is correct. But I think B should be correct.

Since class B extends A, to compile B.java compiler needs A.class
classpath is used only to search for .class files, not .java files. So in our example, we need classpath to get us to A.class

Based on these, Lets consider option B
We are in xcom directory
the classpath is "." , the current directory, "xcom" and A.class files is in xcom directory
And B.java is also in xcom directory

So B is correct.

In option C, we are in "test" directory.
the classpath is "." current directory "test" So classpath should have been "xcom" as A.class is in xcom
So C is incorrect.


Kindly explain... Thank you.
Hi guys,

A question from K&B text book has a parentheses ")" missing.. One of the options is "As the code stands, it will not compile"
Whereas, it turns out that it is just a typo and the code ran/compiled. So the correct answer was different.

In the actual exam, if you see a parentheses missing, should you take it as a compiler error? or just ignore it as a typo, and find the right answer (as in the K&B question above)?

Thank you.
Oh yes, static variable is associated with the class! Cool.

Thanks!