다라다라V
article thumbnail
728x90
반응형

 

💡 

다음 클래스에 대해 물음에 답하라.

class A {
   private int a;
   public void set(int a) { this.a = a; }
}
class B extends A {
   protected int b, c;
}
class C extends B {
   public int d, e;
}

(1) A objA = new objA() 의해 생성되는 객체 objA 멤버들을 모두 나열하라.

(2) B objB = new objB(); 의해 생성되는 객체 objB 멤버들을 모두 나열하라.

(3) C objC = new objC(); 의해 생성되는 객체 objC 멤버들을 모두 나열하라.

(4) 클래스 0 다음과 같이 작성하였을 , 오류가 발생하는 라인을 모두 찾아라.

 

[정답]

(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;


[해설]

(p255) 서브 클래스는 슈퍼 클래스와 서브 클래스이 멤버를 모두 가지고 태어나게 된다.

 

💡 

자바의 모든 클래스가 반드시 상속받게 되어 있는 클래스는?

 

[정답] Object


[해설]

(p256) 사용자가 만들든, 자바 패키지에서 제공하든, 자바의 모든 클래스는 java.lang.Object클래스를 상속받도록 컴파일된다.

 

💡 

상속을 이용하여 다음 클래스들을 간결한 구조로 재작성하라.

class SharpPencil { // 샤프펜슬
   private int width; // 펜의 굵기
   private int amount; // 남은 량
   public int getAmount() { return amount; }
   public void setAmount (int amount) { this.amount = amount; }
}

class Ballpen  { // 볼펜
   private int amount; // 남은 량
   private String color; // 볼펜의 색
   public int getAmount() { return amount; }
   public void setAmount (int amount) { this.amount = amount; }
   public String getColor() { return color; }
   public void setColor(String color) { this.color = color; }
}

class FountainPen { // 만년필
   private int amount; // 남은 량
   private String color; // 만년필의 색
   public int getAmount() { return amount; }
   public void setAmount (int amount) { this.amount = amount; }
   public String getColor() { return color; }
   public void setColor(String color) { this.color = color; }
   public void refill (int n) { setAmount(n); }
}

 

[정답]

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); }
}

 

 

💡 

다음 중 설명에 적절한 단어를 기입하라.

자바에서 상속받는 클래스를 ________라고 부르며, _____ 키워드를 이용하여 상속을 선언한다. 상속받은 클래스에서 상속해준 클래스의 멤버를 접근할 때 ______ 키워드를 이용한다. 한편, 객체가 어떤 클래스의 타입인지 알아내기 위해서는 ________ 연산자를 이용하며, 인터페이스는 클래스와 달리 ________ 키워드를 이용하여 선언한다.

 

[정답] 서브 클래스 / extends / super / instanceof / inteface

 

💡 

상속에 관련된 접근 지정자에 대한 설명이다. 틀린 것은?

 

[정답] 슈퍼 클래스의 protected 멤버는 같은 패키지에 있는 서브 클래스에서만 접근할 수 있다.


[해설]

(p258) protected는 같은 패키지에 있는 클래스, 같은 패키지에 있는 서브 클래스, 다른 패키지에 있는 ㄷ서브 클래스만 접근 가능하다. 단, 다른 패키지에 있는 클래스는 접근 불가능하다.

 

💡 

다음 빈칸에 적절한 한 줄의 코드를 삽입하라.

class TV {
   private int size;
   public TV(int n) { size = n; }
}
class ColorTV extends TV {
   private int colors;
   public ColorTV(int colors, int size) {
      ___________
      this.colors = colors;
   }
}

 

[정답] super(size);


[해설]

(p265) 서브 클래스의 생성자에서 super()를 명시적으로 사용하면, 슈퍼 클래스 생성자를 명시적으로 선택할 수 있다. super()는 슈퍼 클래스 생성자를 호출하는 코드이다.

 

💡 

상속에 있어 생성자에 대해 묻는 문제이다. 실행될 때 출력되는 내용은 무엇인가?

class A {
   public A() { System.out.println("A"); }
   public A(int x) { System.out.println("A: " + x); }
}
class B extends A {
   public B() { super(100); }
   public B(int x) { System.out.println("B: " + x); }
}
public class Example {
   public static void main(String[] args) {
      B b = new B(11);
   }
}

 

 

[정답] 

A

B: 11

 

 

💡 

다음 코드에서 생성자로 인한 오류를 찾아내어 이유를 설명하고 오류를 수정하라.

class A {
   private int a;
   protected A(int i) { a = i; }
}
class B extends A {
   private int b;
   public B() { b = 0; }
}

 

[정답] public A() {} 생성


[해설]

 

💡 

다음 추상 클래스의 선언이나 사용이 잘못된 것을 있는 대로 가려내고 오류를 지적하라.

 

[정답] 

 

 

💡 

추상 클래스를 구현하는 문제이다. 실행 결과와 같이 출력되도록 클래스 B를 완성하라.

abstract class OddDetector {
   protected int n;
   public OddDetector (int n) {
      this.n = n;
   }
   public abstract boolean isOdd(); // 홀수이면 true 리턴
}
public class B extends OddDetector {
   public B(int n) {
      super(n);
   }
   public static void main(String[] args) {
      B b = new B(10);
      System.out.println(b.isOdd());
    }
}



[정답]

abstract class OddDetector {
    protected int n;
    public OddDetector (int n) {
        this.n = n;
    }
    public abstract boolean isOdd(); // 홀수이면 true 리턴
}
public  class B extends OddDetector {
    public B(int n) {
        super(n);
    }

    public boolean isOdd() {
        if(n%2==1)
            return true;
        else return false;
    }

    public static void main(String[] args) {
        B b = new B(10);
        System.out.println(b.isOdd());
    }
}

 

💡 

다음 상속 관계의 클래스들이 있다.

class A {
   int i;
}
class B extends A {
   int j;
}
class C extends B {
   int k;
}
class D extends B {
   int m;
}

(1) 다음 중 업캐스팅을 모두 골라라

(2) 다음 코드를 실행한 결과는?

A x = new D();
System.out.println(x instanceof B);
System.out.println(x instanceof C);

(3) 다음 코드를 실행한 결과는?

System.out.println(new D() instanceof Object);
System.out.println("Java" instanceof Object);

 

[정답]

(1) B b = new C(); A a = new D();

(2)

true
false

(3)

true
true

 

 

💡 

동적 바인딩에 관한 문제이다. 다음 코드가 있을 때 질문에 답하라.

class Shape {
   public void draw() { System.out.println("Shape"); }
}
class Circle extends Shape {
   public void paint() {
      __________________
   }
   public void draw() { System.out.println("Circle"); }
}

(1) Shape s = new Circle(); s.draw()가 호출되면 출력되는 내용은?

(2) s.paint()가 호출되면 "Circle"이 출력되도록 빈 칸에 적절한 코드를 삽입하라.

(3) s.paint()가 호출되면 "Shape"이 출력되도록 빈 칸에 적절한 코드를 삽입하라.

 

[정답] 

(1) Circle

(2) draw();

(3) super.draw();

 

💡 

동적 바인딩에 대해 다음에 답하라.

abstract class Shape {
   public void paint() { draw(); } 
   abstract public void draw();
}
class Circle extends Shape {
   private int radius;
   public Circle(int radius) { this.radius = radius; }
   double getArea() { return 3.14 * radius * radius; }
}

(1) 다음 중 오류가 발생하는 것을 있는 대로 골라라.

(2) 다음 코드의 실행 결과 "반지름=10"이 출력되도록 Circle 클래스를 수정하라.

Circle p = new Circle(10);
p.paint();

 

[정답]

(1) ② Shape s = new Shape();

(2) 

abstract class Shape {
   public void paint() { draw(); } 
   abstract public void draw();
}
class Circle extends Shape {
   private int radius;
   public Circle(int radius) { this.radius = radius; }
   double getArea() { return 3.14 * radius * radius; }
   System.out.println("반지름="+radius);
}

 

 

💡 

다형성에 대한 설명 중 틀린 것은?

 

[정답] 자바에서 다형성은 모호한(ambiguous) 문제를 일으키므로 사용하지 않는 것이 바람직하다.

 

 

💡 

다음 중 인터페이스의 특징이 아닌 것은?

 

[정답] 인터페이스의 추상 메소드는 자동으로 public이다


[해설]

(p297) 인터페이스 내의 추상 메소드는 public abstract로 정해져 있다.

 

 

💡 

빈칸을 적절히 채우고, 실행 예시와 같이 출력되도록 클래스 TV에 필요한 메소드를 추가 작성하라.

________ Device {
   void on();
   void off();
}
public class TV _________ Device {
   public static void main(String[] args) {
      TV myTV = new TV();
      myTV.on();
      myTV.watch();
      myTV.off();
   }
}

 

[정답]

interface Device {
    void on();
    void off();
}
public class TV implements Device {
    public static void main(String[] args) {
        TV myTV = new TV();
        myTV.on();
        myTV.watch();
        myTV.off();
    }

    public void on() {
        System.out.println("켜졌습니다.");
    }

    public void off() {
        System.out.println("종료합니다.");
    }

    public void watch() {
        System.out.println("방송중입니다.");
    }
}
 
반응형
profile

다라다라V

@DaraDaraV

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!