Programming Concepts using Java, Quiz 2
Consider the Java code given below.
import java.util.*;class Address implements Cloneable{ private String city; private int pinCode; // constructor here to instantiate city, pinCode public void setCity(String c){ city = c; }}class Temple implements Cloneable{ private Address adr; private String tempName; // constructor here to instantiate adr, tempName public Address getAddress(){ return adr; } public void setTempName(String t){ tempName = t;
} public Temple clone() throws CloneNotSupportedException{ Temple t = (Temple) super.clone(); return t; } //Overrides the method toString() // to return tempName + ":"+ city + ":" + pinCode;}public class Main{ public static void main(String[] args){ Temple t1 = new Temple(new Address("Madurai", 625001), "Meenakshi temple"); try{ Temple t2 = t1.clone(); t2.setTempName("Golden temple"); t2.getAddress().setCity("Amritsar"); System.out.println(t1); System.out.println(t2); } catch(CloneNotSupportedException e){ System.out.println("Cloning not supported"); } }}What will the output be?
Consider the Java code given below. import java.util.*; class Address implements Cloneable{ private String city; private int pinCode; // constructor here to instantiate city, pinCode public void setCity(String c){ city = c; } } class Temple implements Cloneable{ private Address adr; private String tempName; // constructor here to instantiate adr, tempName public Address getAddress(){ return adr; } public void setTempName(String t){ tempName = t; } public Temple clone() throws CloneNotSupportedException{ Temple t = (Temple) super.clone(); return t; } //Overrides the method toString() // to return tempName + ":"+ city + ":" + pinCode; } public class Main{ public static void main(String[] args){ Temple t1 = new Temple(new Address("Madurai", 625001), "Meenakshi temple"); try{ Temple t2 = t1.clone(); t2.setTempName("Golden temple"); t2.getAddress().setCity("Amritsar"); System.out.println(t1); System.out.println(t2); } catch(CloneNotSupportedException e){ System.out.println("Cloning not supported"); } } } What will the output be? Consider the Java code given below. import java.util.*; public class Test{ public static void main(String[] args){ var bill_march = new LinkedHashMap<String, Integer>(); bill_march.put("suresh", 100); bill_march.put("mukesh", 150); bill_march.put("ganesh", 80); bill_march.put("pranay", 200); var bill_april = new LinkedHashMap<String, Integer>(); bill_april.put("suresh", 200); bill_april.put("mukesh", 100); bill_april.put("ganesh", 100); bill_april.put("pranay", 100); var totalBill = new LinkedHashMap<String, Integer>(); for(Map.Entry<String, Integer> e : bill_march.entrySet()) totalBill.put(e.getKey(), e.getValue()); for(Map.Entry<String, Integer> e : bill_april.entrySet()) totalBill.merge(e.getKey(), e.getValue(), (x, y) -> y + x); System.out.println(totalBill); } } Choose the correct option. Consider the following code. public class ArrayOperations{ public <T extends Comparable> T min(T[] arr){ // code for finding the minimum } public <T extends Number> T sum(T[] arr){ // code for finding the sum of elements } public <T> int count(T[] arr){ // code for finding the count of elements } } What is the class ArrayOperations converted to after type erasure?