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. 15Java Labb. 15c. Java Labd. Compilation Error
Question
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. 15Java Labb. 15c. Java Labd. Compilation Error
Solution
The correct answer is:
a. 15Java Lab
Explanation:
The given code is a simple example of Generics in Java. The class Test is a generic class with a type parameter T. It has a constructor that takes an argument of type T and a method getObject() that returns an object of type T.
In the main method, two instances of Test class are created - one with Integer type and another with String type. The `getObject
Similar Questions
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
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); }}
Analyze the following code carefully.public class Test { int x; public Test(String t) { System.out.println("Test"); } public static void main(String[] args) { Test test = new Test(); System.out.println(test.x); }}Group of answer choicesThe program has a compile error because you cannot create an object from the class that defines the object.The program has a compile error because Test does not have a default constructor.The program has a compile error because System.out.println method cannot be invoked from the constructor.The program has a compile error because x has not been initialized.
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
Analyze the following code carefully. Please select the one that applies.public class Test { public static void main(String[] args) { A a = new A(); a.print(); }}class A { String s; A(String s) { this.s = s; }void print() { System.out.println(s); }}
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.