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

Independent Object States

Mark
< Prev
Next >

Two distinct objects are created and modified separately, demonstrating the independence of their instance variables.

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

public class QuestionTrace {

    public static void main(String[] args) {
        Item i1 = new Item(10);
        Item i2 = new Item(20);
        i1.add(5);
        System.out.print(i1.get() + i2.get());
    }
}

class Item {

    private int val;

    public Item(int v) {
        val = v;
    }

    public void add(int v) {
        val = val + v;
    }

    public int get() {
        return val;
    }
}
A

35

B

30

C

40

D

15

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|