• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Marcus mock exam #57

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is actually one of the high-quality questions I can find in the free mocks:

What code placed after the comment //Here will result in calling the getFields method resulting in the output of the string
"Agg"?

1) System.out.println(a.getFields());
2) System.out.println(a.name);
3) System.out.println((Base) a.getFields());
4) System.out.println( ((Agg) a).getFields());
The right answer is No4, But the explanation is completely irrelevant:The Base type reference to the instance of the class Agg needs to be cast from Base to Agg to get access to its methods.The
method invoked depends on the object itself, not on the declared type. So, a.getField() invokes getField() in the Base class,
which displays Base. But the call to ((Agg)a).getField() will invoke the getField() in the Agg class. You will be unlucky to
get a question as complex as this on the exam.

I think Marcus just didn't update the explanation with new devised question.
I first chose No1, which actually will cause compiler error. Even though a actually refer to type Agg, but the compiler doesn't know that. It only knows a is Base reference. Since there is no getField() method in Base class. It sent an error message. This question is a little tricky, but really tests the concept of inheritance thoroughly.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The explanation given by Marcus looks perfectly fine. What seems to be wrong ?
-Sandeep Nachane
------------------
Visit my homepage at
www.tipsmart.com
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Dear
i already had given the answer to this question just check it at
in Mock Exam Errata > marcus GreenExam 3Q 57, question was asked by Rebecca YY on feb 9.
just go through it i think your doubt will be cleared.
lokesh mahajan
 
Tom Tang
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, Sandeep:
Marcus said " So, a.getField()invokes getField() in the Base class,which displays Base.". I believe he originally had a getField() which prints "Base" in the Base class, but remove it to make the question more tricky.
Tom
 
Tom Tang
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, Lokesh:
Thank you for your information.
I saw your posting immediately after puting on this one. Actually, I think Rebecca's thread should be in this forum, and mine should be in the Errata forum.
 
Don't touch me. And dont' touch this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic