Question

Question 1 Consider the following code snippet: public class Box<E> { private E data; public Box()...

Question 1

Consider the following code snippet:

public class Box<E>

{

private E data;

public Box() { . . . }

public void insert(E value) { . . . }

public E getData() { . . . }

}

What will result from executing the following code?

Box<String> box = new Box<>();

. . .

box.insert("blue Box");

String b = box.getData();

A.   run-time error

B.   compiler warning

C.   no error

D.   compiler error

Question 2

What is used as a wildcard indicator that matches any class in a type parameter?

A.   &

B.   any

C.   *

D.   ?

Question 3

Suppose that Car is a subclass of Vehicle. Which statement is true about ArrayList<Car> and ArrayList<Vehicle>?

A.   ArrayList<Car> is a subclass of ArrayList<Vehicle>

B.   Any Vehicle object can be added into an ArrayList<Car>

C.   An ArrayList<Car> object can be cast to an ArrayList<Vehicle>

D.   Any Car object can be added into an ArrayList<Vehicle>

Question 4

Which of the following statements about using generic programming is NOT correct?

A.   Using type parameters makes generic code easier to read and understand.

B.   Type parameters cannot be used with interfaces.

C.   Using type parameters makes generic code safer.

D.   Using type parameters will potentially avoid some class cast exceptions that may occur when using collections without type parameters.

Question 5

Consider the following code snippet:

ArrayList<Double> arr = new ArrayList<>();
. . .

String element = arr.get(0);

Is there an error in this code?

A.   Yes, a run-time error will occur because a Double value cannot be cast to a String value.

B.   Yes, a compile-time error will occur because you cannot create an ArrayList of type Double.

C.   No run-time error or compile-time errors will occur.

D.   Yes, a compile-time error will occur because you cannot assign a Double value to a String variable.

Question 6

Which of the following statements about generic methods is correct?

A.   A generic method must have a generic type parameter.

B.   The generic type parameter of a generic method designates the method's return type.

C.   A generic method must reside in a generic class.

D.   When calling a generic method, you need to instantiate the type parameters.

Question 7

Consider the following code snippet:

public static <E extends Comparable<E>> E min(ArrayList<E> objects)

What can we conclude about the return type of this method?

A.   The return type is a class that implements the Comparable interface.

B.   The return type is a class that extends the Comparable interface.

C.   The return type is a subclass of the ArrayList class.

D.   The return type is an array list of generic objects.

Question 8

Internally within the JVM, type parameters are replaced by their bounds (or Object, if unbounded) to become ordinary non-generic classes. What is this simplification process called?

A.   Type erasure

B.   Type casting

C.   Type simplification

D.   Type conversion

Question 9

Where do you specify the type parameters of a generic method?

A.   Between the return type and the method name

B.   Immediately before the parentheses with the parameters

C.   First, before any modifiers such as "public" or "static"

D.   Immediately before the return type

Question 10

Which of the following is NOT a typical advantage of generic methods?

A.   Faster execution during run-time

B.   No need for casting of specified return types

C.   Compile-time enforcement of specified argument types

D.   Backward-compatibility with older non-generic code

0 0
Add a comment Improve this question Transcribed image text
Answer #1

There are a lot of questions to answer. I have tried to explain all the answers but if you have some doubts leave a comment I will explain more. Most of the questions are about Generics. Generics allow Errors to be caught at much earlier compile time rather than at runtime.

  1. The Program will run without any error. As we have created Box class as Generic we can pass any Object while creating instance of Box class so Box<String> box = new Box<>(); is completely valid and insert and get will work without any problem

Answer C .

  1. What is used as a wildcard indicator that matches any class in a type parameter. In Java Classes can be declared as Generic with Parameters . In java “?” means a wildcard in generics that represent an unknown type.

Answer ; D

  1. Suppose that Car is a subclass of Vehicle. Which statement is true about ArrayList<Car> and ArrayList<Vehicle>? You can not add any instance of Vehicle to car but other way is possible as Car is subclass of Vehicle so you can add instance of Car to Vehicle. Actually there is no relation in the ArrayList<Car> and ArrayList<Vehicle>

Answer: D

  1. Answer: B Type parameters can be be used with interfaces. All the other options are actually its advantages.
  2. Answer : B.   Yes, a compile-time error will occur because you cannot create an ArrayList of type Double. As Double we can not convert Double to String directly. You need to use String.valueOf(arr.get(0));
  3. Answer A: A Generic Method should have a generic Type Parameter without it that will be a non generic method.
  4. Answer The return type is a class that implements the Comparable interface.

public static <E extends Comparable<E>> E min(ArrayList<E> objects)

Here the return type is <E extends Comparable<E>> that means any class that implements Comparable interface

  1. Internally within the JVM, type parameters are replaced by their bounds (or Object, if unbounded) to become ordinary non-generic classes. What is this simplification process called?

Answer: A . Type Erasure in Generics replaces all the type parameters in generic types with their bounds or Object if the type parameters are unbounded.

  1. Where do you specify the type parameters of a generic method?

Answer: D Immediately before the return type. The type Parameter must precedes the method's return type. Eg.

public static < E > void test ( E input ) {

  1. Which of the following is NOT a typical advantage of generic methods? Generics provides Generics also provide compile-time type safety and makes execution faster.

Answer D Backward-compatibility with older non-generic code. Actually Generics do not provide backward compatibilty.

Add a comment
Know the answer?
Add Answer to:
Question 1 Consider the following code snippet: public class Box<E> { private E data; public Box()...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 8 0 Consider the following class definitions, public class Class public String getValue() return "A";...

    Question 8 0 Consider the following class definitions, public class Class public String getValue() return "A"; public void showValue() System.out.print(getValue(); public class Classe extends Class public String getValue() return "B"; The following code segment appears in a class other than ClassA or Classe. ClassA obj = new Class(); obj.showValue(); What, if anything, is printed when the code segment is executed? c) AB ta PendulumPhysical.pdf ^ IN SHM Pendulums ....doc Moh PendulumPhysical.pdf Type here to search o Bi e а в...

  • For Questions 1-3: consider the following code: public class A { private int number; protected String...

    For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {    super();    System.out.println(“B() called”);...

  • For Questions 1-3: consider the following code: public class A { private int number; protected String...

    For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {   super();   System.out.println(“B() called”); } public...

  • Convert this java code to scala public class Main { public static String checkType (Object obj)...

    Convert this java code to scala public class Main { public static String checkType (Object obj) { //check if the given obj is of type Person if (obj instanceof Person) { Person p = (Person) obj; //type cast to Person if (p.vehicle instanceof Car) { Car c = (Car) p.vehicle; return "This is a car with plate: " + C.plate; } else if (p.vehicle instanceof Truck) { Truck t = (Truck) p.vehicle; return "This is a truck with places: "...

  • Java Programming Answer 60a, 60b, 60c, 60d. Show code & output. public class Employee { private...

    Java Programming Answer 60a, 60b, 60c, 60d. Show code & output. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return...

  • Given the following Java code snippet from a rental car management system class Car{ //code omitted...

    Given the following Java code snippet from a rental car management system class Car{ //code omitted public boolean pickPassenger(Passenger psg, int psgID) {           //code omitted           Passenger localPassenger = psg;           //code omitted          } } The pickPassenger() method implements code for picking up a Passenger object for a ride by a Car object. Given the Passenger object reference is local to the pickPassenger() method and is not required for any other method or field of class Car, the...

  • Consider the following snippet of code :   ArrayList <String> temp = new ArrayList<String>();   temp.add(“a”);   temp.add(”b”);   temp.add(“c”);...

    Consider the following snippet of code :   ArrayList <String> temp = new ArrayList<String>();   temp.add(“a”);   temp.add(”b”);   temp.add(“c”);   temp.remove(“z”);   temp.add(2, “d”);   temp.set(0, “e”);   temp.remove(1);   System.out.println(temp.size() + ” “+ temp.get(1)); What will print on the screen? 3 b 3 d 4 a 4 e Runtime Error

  • Giving the following code snippet: Discuss the reasons of using private access specifier in some places...

    Giving the following code snippet: Discuss the reasons of using private access specifier in some places of the code and public in other places. Additionally, discuss how can you test this code. public class Book {      privateStringbookName;      privateinted;      private String author;           public Book (String n, int e, String a)      {            bookName = n;            ed = e;            author = a;      }           /**      * Gets the book name.     ...

  • Provided code Animal.java: public class Animal {    private String type;    private double age;   ...

    Provided code Animal.java: public class Animal {    private String type;    private double age;       public Animal(String aT, double anA)    {        this.type = aT;        if(anA >= 0)        {            this.age = anA;        }    }    public String getType()    {        return this.type;    }    public double getAge()    {        return this.age;    } } Provided code Zoo.java: public class Zoo {...

  • need java code for this question Question 2 (15 marks) (a) Does the following class successfully...

    need java code for this question Question 2 (15 marks) (a) Does the following class successfully compile? Explain your answer. public class MyClass public static void main(String arge) if(Integer.parseInt(args[0]) < 0) throw new RuntimeException(); C { 1 } If the class does compile, describe what will happen when we run it with command: java MyClass -10 (5 marks) (b) Write a complete definition of the method with the heading given below: public static double calculate insurance Premium double carValue, int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT