• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Dan's mock exam#1

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
First of all, I just wanted to verify that Dan's famous exams are available from
this link: http://www.danchisholm.net/apr18/comprehensive/index.html.
i.e. I just wanted to make sure that I'm attempting the right exams?
2) I gave the first mock exam. I have 2 doubts.
a) Like, what should be the decent score on these mock exams. For e.g.
I got 26/31. Is it decent or really bad? What's the difficulty level
of the real exam compared to those exams?
b) Although I ran the program and verified that the answer is correct
but I can't understand why? In the following code,
why the printS1() of class P is invoked? I thought the context is an object
of class Q. So shouldn't Q's printS1() should be invoked? I know the difference
between static hidden methods and instance overriden methods but still here
we are not invoking the method using reference of type P. If we were invoking
by using P p = new Q() and then p.printS1S2() then I had no problems but ???

Question 25 (mock exam#1):
UBB code
class P {
static void printS1(){System.out.print("P.printS1 ");}
void printS2() {System.out.print("P.printS2 ");}
void printS1S2(){printS1();printS2();}
}
class Q extends P {
static void printS1(){System.out.print("Q.printS1 ");}
void printS2(){System.out.print("Q.printS2 ");}
public static void main(String[] args) {
new Q().printS1S2();
}
}

What is the result of attempting to compile and run the above program?
ans rints: P.printS1 Q.printS2

Thanks,
Deep
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Since the method printS1S2() is declared in the base class, the call to the static method printS1() will invoke only the base class static method, since the re-declared method printS1() is hidden(not known to the base class) from the base class method printS1S2().
Try overriding the method printS1S2(), in the derived class. It will invoke only the derived class's static method, since the base class method is obviously hidden. Hope my understanding is right.
Regards,
Uma....
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deep: I just wanted to make sure that I'm attempting the right exams
Yup, you're on the right track
Deep:
2) I gave the first mock exam. I have 2 doubts.
a) Like, what should be the decent score on these mock exams. For e.g.
I got 26/31. Is it decent or really bad? What's the difficulty level
of the real exam compared to those exams?

my $0.02, if you're getting consistently more than 60-65% you're ready for the exam.

My explanation: You're creating an instance of class Q and invoking printS1S2() method.
void printS1S2(){printS1();printS2();}
Notice that printS1() is a static method, and static methods are not overriden. So, even if you have a method with the same name in the subclass, it's definitely not overriden, just happen-to-have the same name. Therefore, P.printS1 of class P will be used.
Looking at printS2(), we can see that the method is overriden, so the method to use is the one defined in class Q.
hope it helps
 
Stop it! You're embarassing me! And you are embarrassing this tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic