Exam 1z0-809 All QuestionsBrowse all questions from this exam
Question 223

Given:

public class Test {

private T t;

public T get () {

return t;

}

public void set (T t) {

this.t = t;

}

public static void main (String args [ ] ) {

Test type = new Test<>();

Test type 1 = new Test (); //line n1

type.set("Java");

type1.set(100); //line n2

System.out.print(type.get() + " " + type1.get());

}

}

What is the result?

    Correct Answer: C

    The given code tries to create a generic class Test that can handle objects of type T. From the code, type is defined as Test<String> and type1 is defined as Test. The problem arises when type1.set(100) is called. The raw type Test (without specifying the type parameter) is being used, which causes unsafe operations. In a strict coding environment, this should lead to a compilation error. To rectify, Test should be given a specific type, e.g., Test<Integer> type1 = new Test<>(); and the appropriate adjustments should be made in the code.

Discussion
adnano1234

It prints "Java 100". Tested

DestroyerOption: A

It should print "Java 100"

iSnoverOption: A

A -> prints "Java 100"