What is the output of the following program:class Test { // A Generic method example static <T> void genericDisplay(T element) { System.out.println(element.getClass().getName() + " = " + element); } // Driver method public static void main(String[] args) { // Calling generic method with Integer argument genericDisplay(11); // Calling generic method with String argument genericDisplay("Java Lab"); // Calling generic method with double argument genericDisplay(1.0); }}Select one:a. Compilation errorb.11Java Lab1.0c. No outputd.java.lang.Integer = 11java.lang.String = Java Labjava.lang.Double = 1.0
Question
What is the output of the following program:class Test { // A Generic method example static <T> void genericDisplay(T element) { System.out.println(element.getClass().getName() + " = " + element); } // Driver method public static void main(String[] args) { // Calling generic method with Integer argument genericDisplay(11); // Calling generic method with String argument genericDisplay("Java Lab"); // Calling generic method with double argument genericDisplay(1.0); }}Select one:a. Compilation errorb.11Java Lab1.0c. No outputd.java.lang.Integer = 11java.lang.String = Java Labjava.lang.Double = 1.0
Solution
The correct answer is:
d. java.lang.Integer = 11 java.lang.String = Java Lab java.lang.Double = 1.0
This is because the program is using a generic method that accepts any type of argument and prints out the class name of the argument (which is the type of the argument) and the argument itself. So when you pass in an Integer, a String, and a Double, it prints out their class names and their values.
Similar Questions
class Test { // A Generic method example static <T> void genericDisplay(T element) { System.out.println(element.getClass().getName() + " = " + element); } // Driver method public static void main(String[] args) { // Calling generic method with Integer argument genericDisplay(11); // Calling generic method with String argument genericDisplay("Java Lab"); // Calling generic method with double argument genericDisplay(1.0); }}
What is the output of the following program:class Test<T> { // An object of type T is declared T obj; Test(T obj) { this.obj = obj; } // constructor public T getObject() { return this.obj; }} // Driver class to test abovepublic class Main { public static void main(String[] args) { // instance of Integer type Test<Integer> iObj = new Test<Integer>(15); System.out.println(iObj.getObject()); // instance of String type Test<String> sObj = new Test<String>("Java Lab"); System.out.println(sObj.getObject()); }}Select one:a. 15b. Java Labc. Compilation Errord. 15Java Lab
What is the Output of following Java Program?abstract class Demo{ public int a; Demo() { a = 10; } abstract public void set(); }class Test extends Demo{ final public void get() { System.out.println("a = " + a); } public static void main(String[] args) { Test obj = new Test(); obj.get(); }}Select one:a.Compile Time Errorb.a=10c.Runtime Exception
What is the output of the following code? _____public class Test { public static void main(String[] args) { new Person().printPerson(); new Student().printPerson(); }}class Student extends Person { @Override public String getInfo() { return "Student"; }}class Person { public String getInfo() { return "Person"; } public void printPerson() { System.out.println(getInfo()); }}A. Person PersonB. Person StudentC. Stduent StudentD. Student Person
Given:class FourWheeler { public FourWheeler () { System.out.print(1); } } class Car extends FourWheeler{ public Car() { System.out.print(2); } } class Audi extends Car{ public Audi() {System.out.print(3); } } public class Driver{ public static void main( String[] argv ) {new Audi(); } } What is the result when this code is executed?Select one:a.321b.The code runs with no outputc.123d.3
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.