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

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

    A compilation error occurs. The issue is that line n1 does not specify a generic type, which will lead to a raw type usage causing compatibility issues. To rectify this, line n1 should be replaced with: Test<Integer> type1 = new Test<>(), which correctly specifies the type parameter.

Discussion
jduarteOption: A

Answer A. tested

DangNHHOption: A

Answer A At line n1, Generic type data was not defined. Thus, line n2 is valid. The generic type will be set as an integer. Test type 1 = new Test (); //line n1 type1.set(100); //line n2 Besides, the variable name "type 1" should be removed space.

asdfjhfgjuaDCVOption: A

A is the correct answer public class Test<T> { private T t; public T get() { return t; } public void set(T t) { this.t = t; } public static void main(String args[]) { Test<String> type = new Test<>(); Test type1 = new Test<>(); // corrected line n1 type.set("Java"); type1.set(100); // corrected line n2 System.out.print(type.get() + " " + type1.get()); } }

steefaandOption: A

A is correct.

r1muka5Option: C

Correct answer is C, the space should be removed.

WilsonKKerllOption: A

Answer is A.

YasinGaberOption: C

I think answer should be C, because at line #1 there is a space in (type 1), it has to be either (type1) or (type<Integer>1): public class TestCode<T> { private T t; public T get () { return t; } public void set (T t) { this.t = t; } public static void main(String[] args) { TestCode<String> type = new TestCode<>(); TestCode<Integer> type1 = new TestCode (); //line n1 type.set("Java"); type1.set(100); //line n2 System.out.print(type.get() + " " + type1.get()); } }