Variable Shadowing in a Java Class
Consider the following class definition. What is printed when the subsequent code segment is executed?
public class Student {
private String name;
private int age;
public Student(String name, int age) {
name = name;
age = age;
}
public void setAge(int age) {
age = age + 1;
}
public void printInfo() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
Student s = new Student("Alice", 18);
s.setAge(20);
s.printInfo();
A
Name: Alice, Age: 20
B
Name: null, Age: 0
C
Name: Alice, Age: 18
D
Name: Alice, Age: 21
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | sgarv2513 | 1 | 4 | 0m 00s | 70 |
| #2 | kaisuki | 1 | 4 | 0m 00s | 70 |
| #3 | bommasam000 | 0 | 3 | 1m 29s | -119 |
Items per page:
10
1 – 3 of 3
APFIVE