Object Oriented Programming using Java Questions and Answers – Generic Class

This set of Object Oriented Programming (OOPs) using Java Multiple Choice Questions & Answers (MCQs) focuses on “Generic Class”.

1. What will be the output of the following Java code?

class Generic<T> 
{ 
    T t; 
    Generic(T t) 
    {  
        this.t = t;  
    } 
    public T getT()  
    {
        return this.t;         
    } 
} 
class MainCall 
{ 
    public static void main (String[] args) 
    { 
        Generic <Integer> i = new Generic<Integer>(25); 
        System.out.println(i.getT());
        Generic <Double> j = new Generic<Double>(37); 
        System.out.println(j.getT());
    } 
}

a)

25
37

b)

advertisement
advertisement
25
37.0

c) Compilation Error
d) Runtime Error
View Answer

Answer: c
Explanation: Generic classes are designed in Java which allow a type or method to operate on objects of various types. Since double needs to have a decimal point in the number passed to it, when 37 is passed to j, it leads to a compilation error.
Output:

$ javac MainCall.java
$ java MainCall
error: incompatible types: int cannot be converted to Double

2. What will be the output of the following Java code?

class Generic<T> 
{ 
    T t; 
    Generic(T t) 
    {  
        this.t = t;  
    } 
    public T Position()  
    {
        return this.t;         
    } 
} 
class StringGeneric 
{ 
    public static void main (String[] args) 
    { 
        Generic <String> i = new Generic<String>("Sanfoundry"); 
        System.out.println(i.Position().charAt(3));
    } 
}

a) Garbage Value
b) n
c) f
d) Compilation Error
View Answer

Answer: c
Explanation: The function Position returns the string object which stores the value “Sanfoundry”. The third index of the string is ‘f’ as indexing starts from 0, it is printed.
Output:

advertisement
$ javac StringGeneric.java
$ java StringGeneric
f

3. What will be the output of the following Java code?

advertisement
class Generic<T,U> 
{ 
    T t; 
    U u;
    Generic(T t, U u) 
    {  
        this.t = t;  
        this.u = u;
    } 
    public void append()  
    {
        System.out.print(u);
        System.out.print(t);
    } 
} 
class GenericString 
{ 
    public static void main (String[] args) 
    { 
        Generic <String, String> i = new Generic<String, String>("San", "foundry"); 
        i.append();
    } 
}

a) Compilation Error
b) Runtime Error
c) Sanfoundry
d) foundrySan
View Answer

Answer: d
Explanation: When function append is called, the strings “San” and “foundry” are currently stored in t and u respectively. Since they are printed in the order of u followed by t, the resulting output is foundrySan.
Output:

$ javac GenericString.java
$ java GenericString
foundrySan

4. What will be the output of the following Java code?

class Generic<T> 
{ 
    T t; 
    Generic(T t) 
    {  
        this.t = t;  
    } 
    public T getT()  
    {
        return this.t;
    } 
} 
class GenericString2 
{ 
    public static void main (String[] args) 
    { 
        Generic <String> i = new Generic<String>("Sanfoundry"); 
        System.out.println(i.getClass());
        System.out.println(i.getT().getClass());
    } 
}

a)

class T
class Generic

b)

class Generic
class String 

c)

class Generic
class java.lang.String

d)

class T
class java.lang.String
View Answer
Answer: c
Explanation: A class that can refer to any type is known as a generic class. In the above program, ‘i’ is an object of the class Generic, and getT returns the string object which stores the string “Sanfoundry”, which is an object of the java.lang.String class.
Output:

$ javac GenericString2.java
$ java GenericString2
class Generic
class java.lang.String

5. What will be the output of the following Java code?

class Generic<T> 
{ 
    T t; 
    Generic(T t) 
    {  
        this.t = t;  
    } 
    public T getT()  
    {
        return this.t;
    } 
} 
class GenericInt 
{ 
    public static void main (String[] args) 
    { 
        Generic <Integer> i = new Generic<Integer>(100);
        Generic <Generic> j = new Generic<Generic>(i); 
        System.out.println(j.getClass());
        System.out.println(j.getT().getClass());
    } 
}

a)

class Generic
class Integer

b)

class Generic
class java.io.Integer

c)

class Generic 
class java.lang.Integer

d)

class Generic
class Generic
View Answer
Answer: d
Explanation: Here j is an object of the class Generic, with the value i, which inturn is an object of the class Generic with an integer value of 100. Hence both the getClass() functions give class Generic.
Output:

$ javac GenericInt.java
$ java GenericInt
class Generic
class Generic

6. What will be the output of the following Java code?

class Generic<T> 
{ 
    T t; 
    Generic(T t) 
    {  
        this.t = t;  
    } 
    public T getT()  
    {
        return this.t;
    } 
} 
class GenericProg 
{ 
    public static void main (String[] args) 
    { 
        Generic <String> i = new Generic<String>("Sanfoundry");
        System.out.println(i);
    } 
}

a) Compilation Error
b) “Sanfoundry”
c) Generic@2a139a55
d) GenericProg@2a139a55
View Answer

Answer: c
Explanation: When an object of a class is printed, it is returned in the format of ClassName@Hashcode. Here an object of the Generic class is called, hence it returns Generic@2a139a55.
Output:

$ javac GenericProg.java
$ java GenericProg
Generic@2a139a55

7. What will be the output of the following Java code?

class Generic <T>
{
    Stack <T> t = new Stack <T>();
    public void push(T i) 
    {
        t.push(i);
    }
    public T pop() 
    {           
        return t.pop();
    }
    public Stack Array()
    {
        return t;
    }
}
public class GenericPop
{
    public static void main(String args[])
    {
        Generic <String> i = new Generic<String>();
        i.push("San");
        i.push("foundry");
        System.out.println(i.Array());
        System.out.println(i.pop());
        System.out.println(i.pop());
 
    }
}

a)

Sanfoundry
Sanfoundry

b) Compilation Error
c)

[San, foundry]
San
foundry

d)

[San, foundry]
foundry
San
View Answer
Answer: d
Explanation: The push function of a stack adds an element to the rightmost of the stack, while the pop function removes the rightmost element and returns it. Hence the items are obtained in reverse order.
Output:

$ javac GenericPop.java
$ java GenericPop
[San, foundry]
foundry
San

8. What will be the output of the following Java code?

class Generic <T>
{
    Stack <T> t = new Stack <T>();
    public void push(T i) 
    {
        t.push(i);
    }
    public T pop() 
    {           
        return t.pop();
    }
}
public class GenericStack
{
    public static void main(String args[])
    {
        Generic <String> i = new Generic<String>();
        i.push("37");
        i.push(37.0);
        System.out.println(i.pop());
        System.out.println(i.pop());
    }
}

a)

37
37.0

b)

"37"
37.0

c) Runtime Error
d) Compilation Error
View Answer

Answer: d
Explanation: The object i of the class Generic is defined to have values of type String. the second push command passes a double value and hence leads to a compilation error.
Output:

$ javac GenericStack.java
$ java GenericStack
error: incompatible types: double cannot be converted to String

9. What will be the output of the following Java code?

class Generic <T>
{
    Stack <T> t = new Stack <T>();
    public void push(T i) 
    {
        t.push(i);
    }
    public T pop() 
    {           
        return t.pop();
    }
}
public class GenericStackProg
{
    public double multiply(double a, double b)
    {
        return a*b;
    }
    public static void main(String args[])
    {
        Generic <Double> i = new Generic<Double>();
        i.push(21.0);
        i.push(32.0);
        i.push(40.0);
        GenericStackProg j = new GenericStackProg();
        System.out.println(j.multiply(i.pop(),i.pop()));
    }
}

a) 840.0
b) 1280.0
c) 672.0
d) Compilation Error
View Answer

Answer: b
Explanation: The pop() method is used to remove the object at the top of this stack and returns that object as the value of this function. In this case, the pop() function removes the rightmost element of a stack, hence sending the last 2 values into the multiply function which are 32.0 and 40.0.
Output:

$ javac GenericStackProg.java
$ java GenericStackProg
1280.0

10. What will be the output of the following Java code?

class Generic <T>
{
    Stack <T> t = new Stack <T>();
    public void push(T i) 
    {
        t.push(i);
    }
    public T pop() 
    {           
        return t.pop();
    }
    public int search(String s)
    {
        return t.search(s);
    }
}
public class GenericStackMultiply
{
    public double multiply(double a, double b)
    {
        return a*b;
    }
    public static void main(String args[])
    {
        Generic <String> i = new Generic<String>();
        i.push("San");
        i.push("Fou");
        i.push("Nd");
        String k = i.pop();
        i.push("nd");
        System.out.println(i.search("Nd"));
    }
}

a) 2
b) 1
c) -1
d) 0
View Answer

Answer: c
Explanation: Push function adds an element to the rightmost of a stack and pop function removes the rightmost element of a stack. When pop is called, the string “Nd” is removed and after the push command, “nd” is added in that place. Hence the search function returns -1, which implies that the element wasn’t found in the stack.
Output:

$ javac GenericStackMultiply.java
$ java GenericStackMultiply
-1

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming (OOPs) using Java, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.