Choosing how to earn college credit? CLEP vs AP guide
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 3: Class Creation
Start Practice TestPractice Test
About Exam
hard

Variable Shadowing Trap

Mark
< Prev
Next >

A method parameter is given the same name as an instance field, causing the parameter to shadow the field during assignment.

What is printed when the following Java program is compiled and run?

public class QuestionTrace {

    public static void main(String[] args) {
        Item item = new Item(20);
        item.setVal(50);
        System.out.println(item.val);
    }
}

class Item {

    public int val;

    public Item(int val) {
        this.val = val;
    }

    public void setVal(int val) {
        val = val;
    }
}
A

50

B

0

C

70

D

20

Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Not enough data yet to show leaderboard.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Instagram: @_apfive_|Privacy Policy|