Java SE 8 Programmer II Exam
Question
Given: Book.java: public class Book{ private String read(String bname) {return "Read" + bname} } EBook.java: public class EBook extends Book{ public class String read (String url){return "View" + url} } Test.java: public class Test{ public static void main (String[] args){ Book b1 = new Book(); b1.read("Java Programing"); Book b2 = new EBook(); b2.read("http://ebook.com/ebook"); } } What is the result?
Answers
Explanations
Click on the arrows to vote for the correct answer
A. B. C. D.D.
The code provided is creating a class hierarchy with a base class Book
and a derived class EBook
. The Book
class has a private method named read
which takes a String
argument bname
and returns the concatenated string "Read" followed by the bname
parameter. The EBook
class extends Book
and has a public method named read
which takes a String
argument url
and returns the concatenated string "View" followed by the url
parameter.
In the Test
class, two objects are created: b1
is an instance of Book
and b2
is an instance of EBook
. Then the read
method is called on both of these objects with the respective parameters.
The first call to read
on b1
with the argument "Java Programming" will return the string "ReadJava Programming" because the Book
class's read
method is private, so it can only be accessed within the Book
class itself.
The second call to read
on b2
with the argument "http:/ ebook.com/ebook" will return the string "Viewhttp:/ ebook.com/ebook" because the EBook
class's read
method is public and overrides the Book
class's read
method.
Therefore, the output of the program will be:
javascriptReadJava Programming Viewhttp:/ ebook.com/ebook
The correct answer is option A: "Read Java Programming View http:/ ebook.com/ebook".