• 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

Please help with these questions

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand the answer and rationale behind these two questions

I purchased the e practice exam and don't understand bits of 2 questions in the first practice exam.

QUESTION 4 Exam 1

1. interface Animal {
2. void eat();
3. }
4.
5. // insert code here
6.
7. public class HouseCat extends Feline {
8. public void eat() { }
9. }


abstract class Feline implements Animal { }
-- FIRST OF ALL WHY IS THIS A LEGAL IMPLEMENTATION OF THE interface it doesn't implement the eat() method.

Question 8 Exam 1

Given:

1. class Banana {
2. int x = 1;
3. public static void main(String [] args) {
4. int x = 2;
5. Banana b = new Banana();
6. b.go();
7. }
8. { x += x; }
9. void go() {
10. ++x;
11. System.out.println(x);
12. }
13. }


Whis is the { x += x; } part of this code snippet legal???
 
ranger
Posts: 17347
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

-- FIRST OF ALL WHY IS THIS A LEGAL IMPLEMENTATION OF THE interface it doesn't implement the eat() method.



because the class is defined as abstract. So it isn't required to implement any of the interface methods. That is still going to be the responsibility of any class that extends the abstract class and is not abstract itself.

Whis is the { x += x; } part of this code snippet legal???



Not a great answer, but because it is the "+=" is a legal operator/assigner. And you know x = x; is a legal assignment.

Usually x += x; is equivalent to x = x + x. You should try it and see what the results are. Do a simple class that in the main takes int x = 1; then x += x; and see what x is after that statement.

But besides that, there is much more going on in that code besides the assignment. Like where is this code, what method, etc...

Mark

[ July 13, 2005: Message edited by: Mark Spritzler ]
[ July 13, 2005: Message edited by: Mark Spritzler ]
 
Dibbo Khan
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mark, but my question about the second question is a little different

{ X += X } was not part of a method, anonymous method or a constructor, so how come such a statement can exist between two methods like this.

Thanks for you help in advance.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unlabeled blocks (like { x += x; } in the above example) contain "initializer code." These blocks are executed upon object instantiation. If the blocks are preceded with the keyword "static," then they are "static blocks" or "static initializer code," and are executed upon class loading.
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dibbo,

it is an initializer, consider it as a part of the constructor which is run even before the constructor code steps in.

Wei-ju
 
Dibbo Khan
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you everyone
 
It was the best of times. It was the worst of times. It was a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic