SQLD프로그래밍 언어 활용난이도 3MCQ

SQLD 프로그래밍 언어 활용 기출문제 #3069

문제

다음 Java 프로그램의 실행 결과는?

interface Drawable { void draw(); default void display() { System.out.print("Interface "); } } class Rectangle implements Drawable { int width = 4, height = 6; public void draw() { System.out.print(width * height); } public void display() { System.out.print("Rectangle "); } } public class Main { public static void main(String[] args) { Drawable d = new Rectangle(); d.display(); d.draw(); } }

① Interface 24 ② Rectangle 24 ③ Rectangle 0 ④ 컴파일 오류 (인터페이스 메서드 재정의 불가)

정답

2

해설

Rectangle 클래스에서 인터페이스의 default 메서드 display()를 오버라이드하여 "Rectangle "을 출력하고, draw() 메서드에서 width * height = 4 * 6 = 24를 출력한다. 인터페이스의 default 메서드는 구현 클래스에서 재정의 가능하다.

이런 문제 20~50개를 한 번에 풀어보세요

매번 새로 추가되는 모의고사 + 오답 자동 복습 + 회차별 실력 추적. 회원가입 후 무료 이용.

[SQLD] 프로그래밍 언어 활용 기출 #3069 | sqldpass