1z0-819 Exam QuestionsBrowse all questions from this exam

1z0-819 Exam - Question 23


Given:

and

What is the result?

Show Answer
Correct Answer: BC

The code provided defines a class `Person` with a private `String` field named `name` initialized to 'Green.' The `setName` method receives a parameter `name` but shadows the instance variable. It then concatenates 'Mr. ' with the local parameter name, not affecting the instance variable. The `toString` method returns the instance variable `name`, which remains 'Green.' Therefore, when `System.out.println(p);` is called, it outputs 'Green.'

Discussion

11 comments
Sign in to comment
RP384Option: B
Jan 25, 2023

GREEN will be printed

Leksh_geethOption: B
Nov 8, 2022

Correct Answer is Option B

SuperDabichoOption: B
Apr 26, 2023

setName() does not change the class variable, it changes the parameter name.

Mukes877Option: B
May 19, 2023

B is correct Because this.name is not used here so the parameter name is changed.

StavokOption: B
Jun 3, 2023

B is correct

OmnisumemOption: B
Sep 13, 2023

Tested: B.

UcefSoftOption: B
Nov 14, 2022

B is correct

NokuLOption: C
Jan 16, 2023

c is correct

JGR_77Option: B
Mar 28, 2023

Greenwill be printed

d7bb0b2Option: B
Jan 6, 2024

B is correct, setName(string name) shadow instance variable, not modified, if had another name this variable in mehtod setName(String otherName), then name of instance is modified. So print only "GREEN"

aruni_mishraOption: B
Jun 6, 2024

Tested: B