Programming Concepts using Java, Quiz 2
Consider the Java code given below.
interface Computable{ void compute();}class Phone implements Computable{ public void compute() { System.out.println("Phone compute"); }}class Laptop implements Computable{ public void compute() { System.out.println("Laptop compute"); }}class DeviceList{ private Object[] cArr = {new Phone(), new Laptop()}; public void getCompute(){ for(int i = 0; i < cArr.length; i++){ //LINE 1 } }}public class Test{ public static void main(String[] args) { DeviceList dList = new DeviceList(); dList.getCompute(); }}Identify the appropriate option to fill in place of LINE 1 such that the output is
Phone compute
Laptop compute
Consider the Java code given below. interface Computable{ void compute(); } class Phone implements Computable{ public void compute() { System.out.println("Phone compute"); } } class Laptop implements Computable{ public void compute() { System.out.println("Laptop compute"); } } class DeviceList{ private Object[] cArr = {new Phone(), new Laptop()}; public void getCompute(){ for(int i = 0; i < cArr.length; i++){ //LINE 1 } } } public class Test{ public static void main(String[] args) { DeviceList dList = new DeviceList(); dList.getCompute(); } } Identify the appropriate option to fill in place of LINE 1 such that the output is Phone compute\ Laptop compute Consider the Java code given below. public class Example{ public <T extends Comparable> void sortArray(T[] obj){ // Sorts obj } public <T> void elementDisplay(T[] arr){ // Displays the elements of arr } public <T extends Number> T sum(List<T> lst){ // Returns the sum of elements of lst } } How does class Example look after type erasure? Consider the code given below interface Shape{ public abstract int area(); } class Square implements Shape, Cloneable{ int side; public Square(int s){ side = s; } public int area(){ return side * side; } public Square clone() throws CloneNotSupportedException{ return (Square)super.clone(); } } public class Test{ public static void main(String[] args){ Square s1 = new Square(5); try{ Square s2 = s1.clone(); s1.side = 3; System.out.print(s1.area() + s2.area()); } catch(CloneNotSupportedException e){ System.out.println("Cloning not supported"); } } } What will the output be?