Question: Consider the following Java program: class Ref { private T x; Ref ( T x ) { this.x = x; } T get ( )

Consider the following Java program:
class Ref {
private T x;
Ref(T x){ this.x = x; }
T get(){ return this.x; }
void set(T x){ this.x = x; }
}
public class Question {
static void f(Ref r){
r.set("b");
}
static void g(Ref r){
r = new Ref<>("c");
r.set("d");
}
public static void main(String[] args){
Ref s = new Ref<>("a");
f(s);
System.out.println(s.get());
g(s);
System.out.println(s.get());
}
}
What is printed when this program is run?
Options:
b b
a d
a c
b d
a a

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!