Java - 문제 미리보기

문제 2265

hard
다음 코드에서 출력 결과를 예측해보세요: ```java public class Book { String title; String author; public Book(String title) { _______(title, "Unknown"); // 빈칸 1 } public Book(String title, String author) { _______.title = title; // 빈칸 2 _______.author = author; // 빈칸 3 } public void display() { System.out.println(title + " by " + author); } public static void main(String[] args) { Book book = new Book("Java Guide"); book.display(); } } ``` 빈칸 1, 2, 3에 들어갈 올바른 코드 조합은?
A. `this`, `this`, `this`
B. `super`, `this`, `this`
C. `this`, `super`, `super`
D. `Book`, `this`, `this`

정답: A

⦁ 빈칸 1: `this(title, "Unknown")`로 같은 클래스의 다른 생성자를 호출합니다 ⦁ 빈칸 2, 3: `this.title`, `this.author`로 현재 객체의 멤버 변수에 접근합니다 ⦁ 생성자 체이닝을 통해 코드 중복을 피하고 기본값을 설정할 수 있습니다 ⦁ `Book("Java Guide")` 호출 → `this("Java Guide", "Unknown")` 호출 → 멤버 변수 초기화 ⦁ 최종적으로 title은 "Java Guide", author는 "Unknown"으로 설정됩니다

💡 학습 팁

이 문제를 포함한 Java 과목의 모든 문제를 순차적으로 풀어보세요. 진행상황이 자동으로 저장되어 언제든지 이어서 학습할 수 있습니다.