What is the result of the following?

Given the cache class:

public class Cache {

private T t;

public void setValue (T t) { this.t=t; }

public T getValue() {return t; }

}

What is the result of the following?

Cache<> c = new Cache<Integer>(); // Line 1

Given the cache class:

public class Cache {

private T t;

public void setValue (T t) { this.t=t; }

public T getValue() {return t; }

}

What is the result of the following?

Cache<> c = new Cache<Integer>(); // Line 1

A.
SetValue(100); // Line 2
System.out.print(c.getValue().intValue() +1); // Line 3

B.
101

C.
Compilation fails at line 1.

D.
Compilation fails at line 2.

E.
Compilation fails at line 3.

Explanation:
Compilation failure at line:
illegal start of type
type cache.Cache does not take parameters.



Leave a Reply 18

Your email address will not be published. Required fields are marked *


Martin

Martin

C)

it must be:
Cache c = new Cache(); // Line 1

instead of:
Cache c = new Cache(); // Line 1

ses

ses

Answer is 101;
here the correct source-code:

public class Cache {

private T t;

public void setValue(T t) {
this.t = t;
}

public T getValue() {
return t;
}

public static void main(String[] args) {
Cache c = new Cache(); // Line 1
c.setValue(100); // Line 2
System.out.print(c.getValue().intValue() + 1); // Line 3
}
}

Serg

Serg

This isn’t correct source-code.

“T cannot be resolved to a type”

must be :

public class Cache { …

Serg

Serg

public class Cache{ …

Serg

Serg

Serg

Serg

The HTML has error for viewing angle brackets

Ray

Ray

Below is the correct code.

public class Cache{
private T t;
public void setValue(T t){
this.t=t;
}

public T getValue(){
return t;
}

public static void main(String[] args){
Cache c=new Cache();
c.setValue(100);
System.out.print(c.getValue().intValue()+1);
}
}

Ray

Ray

The HTML has error for viewing angle brackets

Ray

Ray

Becaue they are empty in the first angle brackets of Line 1, so it Compilation fails at line 1.
The correct answer is C.

Charlie

Charlie

Should be
public class Cache
Instead of
public class Cache

And
Cache c = new Cache();
Instead of
Cache c = new Cache();

Charlie

Charlie

Angle brackets disappeared as Ray said!!!

hafid

hafid

1. The type Cache is not generic; it cannot be parameterized with arguments
2. The class Cache not compile : T cannot be resolved to a type for field and method

This question is incomplete

Jan

Jan

it should be like this:

public class Cache <T> {
private T t;

public void setValue (T t) { this.t=t; }

public T getValue() { return t; }

public static void main(String[] args) {
Cache<Integer> c = new Cache<>(); // Line 1
c.setValue(100); // Line 2
System.out.print(c.getValue().intValue() +1); // Line 3
}
}

Correct Answer: C.
Compilation fails at line 1.

Sarah

Sarah

I agree with C because of 2 things:
1-The Cache class doesn’t have a type T
2-When creating a List for example the diamond presentation is like this:
List list = new ArrayList();