Sail E0 Webinar

MCQs

Total Questions : 10
Question 1.


What is the output of this program?


import java.util.*;
public class genericstack {
Stack stk = new Stack ();
public void push(E obj) {
stk.push(obj);
}
public E pop() {
E obj = stk.pop();
return obj;
}
}
class Output {
public static void main(String args[]) {
genericstack gs = new genericstack();
gs.push(36);
System.out.println(gs.pop());
}
}
  1.    H
  2.    Hello
  3.    Runtime Error
  4.    Compilation Error
 Discuss Question
Answer: Option D. -> Compilation Error

genericstack's object gs is defined to contain a string parameter but we are sending 

an integer parameter, which results in compilation error.
Output:
$ javac Output.javac
$ java Output
Hello


Question 2.


What is the output of this program?


import java.util.*;
class Output {
public static void addNumbers(List
  1.    1
  2.    2
  3.    3
  4.    6
 Discuss Question
Answer: Option A. -> 1

None.
Output:
$ javac Output.javac
$ java Output
1


Question 3.


What is the output of this program?


import java.util.*;
class Output {
public static double sumOfList(List
  1.    5.0
  2.    7.0
  3.    8.0
  4.    6.0
 Discuss Question
Answer: Option B. -> 7.0

None.
Output:
$ javac Output.javac
$ java Output
7.0


Question 4.


What is the output of this program?


import java.util.*;
class Output {
public static double sumOfList(List
  1.    0
  2.    4
  3.    5.0
  4.    6.0
 Discuss Question
Answer: Option D. -> 6.0

None.
Output:
$ javac Output.javac
$ java Output
6.0


Question 5.

Which of the following keywords are used for lower bounding a wild card?


  1.    extends
  2.    super
  3.    class
  4.    lower
 Discuss Question
Answer: Option B. -> super

A lower bounded wildcard is expressed using the wildcard character ('?'), following 

by the super keyword, followed by its lower bound: .


Question 6.

Which of the following is incorrect statement regarding the use of generics and 

parameterized types in Java?


  1.    Generics provide type safety by shifting more type checking responsibilities to the compiler.
  2.    Generics and parameterized types eliminate the need for down casts when using Java Collections.
  3.    When designing your own collections class (say, a linked list), generics and parameterized types allow you to achieve type safety with just a single class definition as opposed to defining multiple classes.
  4.    All of the mentioned
 Discuss Question
Answer: Option C. -> When designing your own collections class (say, a linked list), generics and parameterized types allow you to achieve type safety with just a single class definition as opposed to defining multiple classes.

None.


Question 7.

Which of these is wildcard symbol?


  1.    ?
  2.    !
  3.    %
  4.    &
 Discuss Question
Answer: Option A. -> ?

In generic code, the question mark (?), called the wildcard, represents an unknown type.


Question 8.

Which of these is an correct way making a list that is upper bounded by class 

Number?


  1.    List
  2.    List
  3.    List(? extends Number)
  4.    List(? UpperBounds Number)
 Discuss Question
Answer: Option A. -> List

None.


Question 9.

What is use of wildcards?


  1.    It is used in cases when type being operated upon is not known.
  2.    It is used to make code more readable.
  3.    It is used to access members of super class.
  4.    It is used for type argument of generic method
 Discuss Question
Answer: Option A. -> It is used in cases when type being operated upon is not known.

The wildcard can be used in a variety of situations: as the type of a parameter, field, or 

local variable; sometimes as a return type (though it is better programming practice to 

be more specific). The  wildcard  is  never  used  as  a  type argument  for a generic 

method invocation, a generic class instance creation, or a supertype.


Question 10.

Which of these keywords is used to upper bound a wildcard?


  1.    stop
  2.    bound
  3.    extends
  4.    implements
 Discuss Question
Answer: Option C. -> extends

None.


Latest Videos

Latest Test Papers