Quiz Space

Programming Concepts using Java · Quiz 2 · 4 Aug 2024 · May 2024 term

Question 6: Consider the Java code given below. What will the output …

Question 6

+6 marksOne correct option

Consider the Java code given below.

java
import java.util.*;
class Book implements Cloneable {
String title;
String author;
public Book(String t, String a) {
this.title = t;
this.author = a;
}
public Book clone() throws CloneNotSupportedException {
return (Book) super.clone();
}
public String toString() {
return title + ":" + author;
}
}
class Library implements Cloneable {
String libraryName;
List<Book> books;
public Library(String l, List<Book> b) {
this.libraryName = l;
this.books = b;
}
public Library clone() throws CloneNotSupportedException {
Library clonedLibrary = (Library) super.clone();
clonedLibrary.books = new ArrayList<>();
for (Book book : this.books) {
clonedLibrary.books.add(book.clone());
}
return clonedLibrary;
}
}
public class TestCloning {
public static void main(String[] args) throws CloneNotSupportedException {
List<Book> books = new ArrayList<>();
books.add(new Book("1984", "George Orwell"));
books.add(new Book("Outline", "Rachel Cusk"));
Library library1 = new Library("Central Library", books);
Library library2 = library1.clone();
library2.books.get(0).title = "Hanging";
System.out.println(library1.books);
System.out.println(library2.books);
}
}

What will the output be?

  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • D

Question 6 of 15 in the IIT Madras BS Programming Concepts using Java (Java) Quiz 2 paper sat on 4 Aug 2024, in the May 2024 term (IIT M DIPLOMA AN EXAM QDD2 4 Aug 2024). It carries 6 marks.

More questions from this paper

  1. Q1Consider the Java code given below. The method boolean containsKey (Object key) in the class Map returns true if and on…
  2. Q2Consider two Java files located in two different packages as shown below. Vehicle.java: Car.java Choose the correct opt…
  3. Q3Consider the Java code given below. Choose the correct option when the program is executed as:\ java -ea Test
  4. Q4Consider the Java code given below. Choose the correct option.
  5. Q5Consider the Java code given below. Choose the correct option(s) to fill in place of CODE BLOCK so that the output is:\…
  6. Q7Consider the Java code given below. What will the output be?
  7. Q8Consider the Java code given below. Identify the appropriate option to fill in place of LINE X such that the output is:…
  8. Q9Consider the Java code given below. After type erasure, which of the following correctly represents the ArrayOperations…
  9. Q10Consider the Java code given below. Choose the correct option.
  10. Q11Consider the Java code given below. Choose the correct option to be filled in place of LINE 1 so that the output is:\ […
  11. Q12From among the options, choose the code segment that gives the same output as is given by the Java code inside the CODE…
  12. Q13Consider the Java code given below. What will the output be?
  13. Q14Consider the Java code given below. Identify the appropriate option to fill in place of LINE X such that the output is:…
  14. Q15Consider the Java code given below that prints the most impressive act among a set of given Performer objects. From amo…