Programming Concepts using Java, Quiz 2
Consider the Java code given below that prints the price of laptops. From among the options, identify the appropriate function header for the function printPrice that takes as input a list of laptops and prints their prices.
import java.util.*;class Laptop { private double price; public Laptop(double p) { price = p; } public double getPrice() { return price; }}class Dell extends Laptop { public Dell(double p) { super(p); }}class HP extends Laptop { public HP(double p) { super(p); }}
public class Test { // FUNCTION HEADER for function printPrice { for (int i = 0; i < lst.size(); i++) { System.out.println(lst.get(i).getPrice()); } }
public static void main(String[] args) { List<Dell> d = new ArrayList<Dell>(); d.add(new Dell(1000.00)); d.add(new Dell(1200.00));
List<HP> h = new ArrayList<HP>(); h.add(new HP(900.00)); h.add(new HP(1100.00));
printPrice(d); printPrice(h); }
}Choose the correct option.
Consider the Java code given below that prints the price of laptops. From among the options, identify the appropriate function header for the function `printPrice` that takes as input a list of laptops and prints their prices. import java.util.*; class Laptop { private double price; public Laptop(double p) { price = p; } public double getPrice() { return price; } } class Dell extends Laptop { public Dell(double p) { super(p); } } class HP extends Laptop { public HP(double p) { super(p); } } public class Test { // FUNCTION HEADER for function printPrice { for (int i = 0; i < lst.size(); i++) { System.out.println(lst.get(i).getPrice()); } } public static void main(String[] args) { List<Dell> d = new ArrayList<Dell>(); d.add(new Dell(1000.00)); d.add(new Dell(1200.00)); List<HP> h = new ArrayList<HP>(); h.add(new HP(900.00)); h.add(new HP(1100.00)); printPrice(d); printPrice(h); } } Choose the correct option. Consider the Java code given below. interface Purchasable { public default void purchase() { System.out.println("Product purchased"); } } interface Reviewable { public default void review() { System.out.println("Product reviewed"); } } class Headphone implements Purchasable, Reviewable { public void purchase() { System.out.println("Headphone purchased successfully"); } } public class Test { public static void main(String[] args) { Purchasable p1 = new Headphone(); p1.purchase(); p1.review(); } } Choose the correct option. The following code maps a set of athletes names to the number of medals they have won and categorizes the athletes based on whether they are medalists or not. import java.util.*; public class Olympics { TreeSet<String> t1 = new TreeSet<String>(); TreeSet<String> t2 = new TreeSet<String>(); public boolean hasMedal(int medals) { if(medals > 0){ return true; } return false; } public void filterAthletes(TreeMap<String, Integer> medals) { for (Map.Entry<String, Integer> entry : medals.entrySet()) { if (hasMedal(entry.getValue())) { t1.add(entry.getKey()); } else { t2.add(entry.getKey()); } } } public void display() { System.out.println("Medalists: " + t1); System.out.println("Non-Medalists: " + t2); } public static void main(String[] args) { TreeMap<String, Integer> medals = new TreeMap<String, Integer>(); medals.put("Bolt", 2); medals.put("Sindhu", 0); medals.put("Manu", 1); medals.put("Rahul", 0); medals.put("Gulan", 3); Olympics o = new Olympics(); o.filterAthletes(medals); o.display(); } } Choose the correct option.