• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

scjp 5 tough questions

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
)class base{
base(){
test();
}
public void test(){
System.out.println("This is base");
}
}
class sub extends base{
sub(){
}
public void test(){
System.out.println("This is sub");
}
}
public static void main(String a[]){
base b=new sub();
}

}

2)predict the output .(leave all the ,; problems if there correct

class break{
}

will the above program compile ?

3)class test{
static{
System.out.println("This is test");

}
}

what happens when you try and compile the above program?

4)class Integer{

}

will the above program compile ?

5.class Integer{

}
class Long extends Integer{
}
will the above program compile?

visit me at www.examsguide.com
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Kr SCJP"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct me if I had misinterpreted these questions:

Question 1: What is the output of the following code:


The code does compile and since the method test() is overriden the ouput would be: "This is sub"

2. Will the Program Compile??
class break{
}
Think it should be fine!! (Compile.. right..)
3. class test{
static{
System.out.println("This is test");

}
}
Will print "This is test"

4. class Integer{

} should compile..

Not sure whether Class Long extends Integer {} would compile..
Once again if i am wrong please do correct me..
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In part 2, I don't think it will compile since break is a keyword, and it's use in this case is inconsistent with the way break should be used.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

All the above Examples except one ( class break{} ) compile fine.
The class break{} give a compilation problem, break being a keyword in Java.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

Question 1 complies fine and the output would be - " This is sub " , the reason being the method test is overriden.

Question 2 wont complie as break is a reserved keyword.

Question 3 , 4 and 5 should compile without any problem.

Please correct me if i was wrong.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am dissapointed. I do not beleive these are the 5 tough questions in the SCJP exam.

We all know reserved keywords cannot be used as class name identifiers.

java.lang.Integer it is not the same as Integer in the default package and java.lang.Long it is not the same as Long in the default package, so declaring those clases simply hide those from the java.lang package, preventing the programmer from using the simple names, if the programmer wants to create a Java Integer now will have to declare it as java.lang.Integer.

And the inheritance example would have been a little more interesting like this: What is printed to the output?



In order to demonstrate the difference between method overriding of non-satic methods and method hinding of static ones.
[ January 12, 2006: Message edited by: Edwin Dalorzo ]
 
mani mathesh
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Edwin
I think You are pro in java.I just finished my scjp.thx for your explanation .I purposely declared class with Integer just to test the knowledge of people who are preparing for certification
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice.

By the way change Kr, your name violates the site policy, you should change it accordingly.


Now you answer my tricky question I just posted out there! :-)
[ January 12, 2006: Message edited by: Edwin Dalorzo ]
 
mani mathesh
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i did
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the answer is
I am child
I am Parent
My Nick is Luke
My Nick is Luke
I am child
I am Parent
for static methods there is no concept of overriding
the method being invoked depends on the compile time type of the object
 
Rajesh Kumar MadhanaGopal
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Edwin
Could you please explain me how the method SayYourName is called even after setting the reference to Null ?

According to my understanding , i thought that it would throw a null pointer execption after the refrence is set to null.



public class Test {static class Parent{public static void sayYourName(){System.out.println("I am Parent");}public void sayMyNick(){System.out.println("Anakin");}}static class Child extends Parent{public static void sayYourName(){System.out.println("I am child");}public void sayMyNick(){System.out.println("My Nick is Luke");}}public static void main(String args[]){Child theChild = new Child();Parent theParent = theChild;theChild.sayYourName();theParent.sayYourName();theChild.sayMyNick();theParent.sayMyNick();theChild = null;theParent = null;theChild.sayYourName();theParent.sayYourName();}}

--------------------------------------------------------------------------------



In order to demonstrate the difference between method overriding of non-satic methods and method hinding of static ones.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Rajesh

Well, the example demonstrates that the static methods do not depend on the existance of an actual reference, because they belong to the class.

Hence, it would have been the same if I had written:



So, the point is: You do not need an object reference to call a static method, although the reference is null, you can call the static method, because it just depends on the refence type and not in its real existance.

The other point that is clear, as it was stated by Sivaram, there is not such thing as inheritance of static methods, there is just method hiding. So if the child reference acceses its "supossedly overriden" static methods through the parent reference you will always get the execution of the Parent class definition of the method, once again, because static methods depend on the type of the reference or the class itself, and not on the existance of the actual object reference, hence there is not dynamic method lookup in this case.

I hope I have explained myself, English is not my native language.

Thanks to all of you guys. I had fun with this too.

Regards,
Edwin Dalorzo

[ January 13, 2006: Message edited by: Edwin Dalorzo ]
[ January 13, 2006: Message edited by: Edwin Dalorzo ]
 
Rajesh Kumar MadhanaGopal
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Edwin
Thanks a lot ,your explanation was very clear.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic