AP CSA Unit 3 Day 16: Tostring Override

Unit 3, Classes & Objects • Cycle 2
Day 16 Advanced Practice • Harder Difficulty
Focus: toString Override Hard toString Override

Advanced Practice Question

Format: toString Override

What is the output?
public class Book {
    private String title;
    
    public Book(String t) {
        title = t;
    }
    
    public String toString() {
        return title;
    }
}

// In another class:
Book b = new Book("Java");
System.out.println(b);
Difficulty: Hard  |  Topic: toString Override  |  Cycle: 2 (Advanced)
Why This Answer?

When you print an object, Java automatically calls its toString() method. Since we overrode it to return title, it prints "Java".

Common Mistake
Watch Out!

Not knowing that println(object) automatically calls toString().

AP Exam Strategy

System.out.println(obj) is the same as System.out.println(obj.toString()).

Master This Topic

This Cycle 2 HARD question tests tostring override. Review Unit 3 concepts to build mastery of classes & objects.

  • Understanding tostring override
  • Tracing code execution accurately
  • Avoiding common pitfalls
View Unit 3 Study Guide

Ready for More Challenges?

Cycle 2 questions prepare you for the hardest AP CSA exam questions.

Study Games Practice FRQs
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.