exam questions

Exam 1z0-808 All Questions

View all questions & answers for the 1z0-808 exam

Exam 1z0-808 topic 1 question 183 discussion

Actual exam question from Oracle's 1z0-808
Question #: 183
Topic #: 1
[All 1z0-808 Questions]

Given the definitions of the Bird class and the Peacock class:
and the code fragment:

Which code snippet can be inserted to print Fly.Dance. ?

  • A. Bird p = new Peacock();
  • B. Bird b = new Bird(); Peacock p = (Peacock) b;
  • C. Peacock b = new Peacock (); Bird p = (Bird) b;
  • D. Bird b = new Peacock (); Peacock p = (Peacock) b;
Show Suggested Answer Hide Answer
Suggested Answer: D 🗳️

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
MPignaProTech
2 months, 3 weeks ago
D is the good answer
upvoted 1 times
...
BelloMio
7 months, 1 week ago
It's not B because with Bird B = new Bird() and peacock p = (peacock) B; it's as if this becomes peacock p = new bird(); which is wrong
upvoted 1 times
...
7df49fb
9 months, 3 weeks ago
only D
upvoted 1 times
...
DriftKing
1 year, 4 months ago
Selected Answer: D
Tested. Answer is - D. Bird b = new Peacock (); Peacock p = (Peacock) b;
upvoted 2 times
...
amigo31
2 years, 1 month ago
I tested it. For B option is taken cast exception. D option worked correctly..
upvoted 2 times
...
carloswork
2 years, 2 months ago
Selected Answer: D
Tested. Answer is D. To test: class Bird { public void fly() { System.out.print("Fly."); } } class Peacock extends Bird { public void dance() { System.out.print("Dance."); } } public class Test { public static void main(String[] args) { Bird b = new Peacock (); Peacock p = (Peacock) b; p.fly(); p.dance(); } }
upvoted 3 times
...
UAK94
2 years, 2 months ago
Answer is D. class Bird{ public void fly() { System.out.println("fly"); } } public class Peacock extends Bird { public void dance() { System.out.println("dance"); } public static void main(String[] args) { Bird b= new Peacock(); Peacock p= (Peacock) b; // Bird b= new Bird(); // Peacock p= (Peacock) b; // ClassCastException p.fly(); p.dance(); } }
upvoted 3 times
...
iSnover
2 years, 3 months ago
Selected Answer: B
The correct answer is the letter B, you need to create an object B and then do a casting for it, making the same be able to access the method of the child class.
upvoted 1 times
DriftKing
1 year, 4 months ago
You're are wrong. Correct answer is D. Option B is not how downcasting works. Option D is the textbook definition of downcating. With option our object type is peacock and class type of reference variable is also Peacock and peacock is a child class. Child class peacock can access it's method as well as Parent class Bird method.
upvoted 1 times
DriftKing
1 year, 4 months ago
*With option D
upvoted 1 times
...
...
...
alex_au
2 years, 3 months ago
Correct answer is D. For option B there is ClassCastException as class Bird cannot be casted to class Peacock
upvoted 4 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago