1. 다음 클래스에 대해 물음에 답하라.
(1) private int a;
public void set(int a) { this.a = a; }
(2) private int a;
public void set(int a) { this.a = a; }
protected int b, c;
(3) private int a;
public void set(int a) { this.a = a; }
protected int b, c;
public int d, e;
(4) ① a = 1; // a는 private이므로 접근 불가
2. 자바의 모든 클래스가 반드시 상속받게 되어 있는 클래스는?
① Object
3. 상속을 이용하여 다음 클래스들을 간결한 구조로 재작성하라.
class Pen{ // 모든 펜의 공통 속성
private int amount;
public int getAmount() { return amount; }
public void setAmount(int amount) { this.amount = amount; }
}
class SharpPencil extends Pen{
private int width;
}
class BallPen extends Pen {
private String color;
public String getColor() { return color; }
public void setColor(String color) { this.color = color; }
}
class FountainPen extends BallPen{
public void refill(int n) { setAmount(n); }
}
4. 다음 중 설명에 적절한 단어를 기입하라.
자바에서 상속받는 클래스를 서브 클래스라고 부르며, extends 키워드를 이용하여 상속을 선언한다. 상속받은 클래스에 슈퍼 클래스의 멤버를 접근할 때 super 키워드를 이용한다. 한편, 객체가 어떤 클래스 타입 인지 알아내기 위해서는 instanceof 연산자를 이용하며, 인터페이스는 클래스와 달리 interface 키워드를 이용하여 선언한다.
5. 상속에 관련된 접근 지정자에 대한 설명이다. 틀린 것은?
② protected 멤버는 다른 패키지의 서브 클래스에서 접근 가능
6. 다음 빈칸에 적절한 한 줄의 코드를 삽입하라.
class TV {
private int size;
public TV(int n) {size=n;}
}
class ColorTV extends TV{
private int colors;
public ColorTV(int colors, int size){
super(size);
this.colors = colors;
}
}
7. 상속에 있어 생성자에 대해 묻는 문제이다. 실행될 때 출력되는 내용은 무엇인가?
A
B:11
8. 다음 코드에서 생성자로 인한 오류를 찾아내어 이유를 설명하고 오류를 수정하라.
1. 클래스 A에 매개변수가 없는 기본 생성자가 없으므로 public A() { } 추가
2. 클래스 B를 수정하고 싶다면 public B() {
super(0); // 클래스 A의 A(int i) 생성자 호출
b = 0;
}
9. 다음 추상 클래스의 선언이나 사용이 잘못된 것울 있는 대로 가려내고 오류를 지적하라.
① void f() -> abstract void f();
③ class C extends B -> abstract class C extends B
④ void f() -> int f() , 메소드 내부에 return 0; 추가
10. 추상 클래스를 구현하는 문제이다. 실행 결과와 같이 출력되도록 클래스 B를 완성하라.
public class B extends OddDetecter{
public B(int n) {
super(n);
}
public boolean isOdd() {
if(n%2 == 0) return false;
else return true;
}
public static void main(String [] args){
B b = new B(10);
System.out.println(b.isOdd());
}
}
11. 다음 상속 관계의 클래스들이 있다.
(1) ② ③
(2) true
false
(3) true
true
12. 동적 바인딩에 관한 문제이다. 다음 코드가 있을 때 질문에 답하라.
(1) Circle
(2) draw();
(3) super.draw();
13. 동적 바인딩에 대해 다음에 답하라.
(1) 추상 클래스는 객체 생성 X -> ② ④
(2)
class Circle extends Shape{
private int radius;
public Circle(int radius) { this.radius = radius; }
double getArea() { return 3.14*radius*radius }
public void draw() { System.out.println("반지름=" + radius); }
}
14. 다형성에 대한 설명 중 틀린 것은?
④
15. 다음 중 인터페이스의 특징이 아닌 것은?
②
16. 빈칸을 적절히 채우고, 실행 예시와 같이 출력되도록 클래스 TV에 필요한 메소드를 추가 작성하라.
interface Device {
void on();
void off();
}
public class TV implements Device{
public void on() {
System.out.println("켜졌습니다.");
}
public void off() {
System.out.println("종료합니다.");
}
public void watch() {
System.out.println("방송중입니다.");
}
public static void main(String [] args){
TV myTV = new TV();
myTV.on();
myTV.watch();
myTV.off();
}
}
'자바 > 명품 자바 프로그래밍' 카테고리의 다른 글
명품 자바 프로그래밍 6장 이론 문제 / 2021.11.20 (0) | 2021.11.20 |
---|---|
명품 자바 프로그래밍 5장 실습 문제 / 2021.11.16 (0) | 2021.11.16 |
명품 자바 프로그래밍 4장 실습 문제 / 2021.08.02 (0) | 2021.08.02 |
명품 자바 프로그래밍 4장 이론 문제 / 2021.07.31 (0) | 2021.07.31 |
명품 자바 프로그래밍 3장 실습 문제 / 2021.07.29 (1) | 2021.07.29 |