# 명품 자바 에센셜 연습문제 4장(이론)

- Author: @baealex
- Published: 2018-07-29
- Updated: 2018-07-29
- Source: http://blex.me/@baealex/%EB%AA%85%ED%92%88-%EC%9E%90%EB%B0%94-%EC%97%90%EC%84%BC%EC%85%9C-%EC%97%B0%EC%8A%B5%EB%AC%B8%EC%A0%9C-4%EC%9E%A5%EC%9D%B4%EB%A1%A0
- Tags: 자바에센셜

---

<div class="txc-textbox" style="border-style: solid; border-width: 1px; border-color: rgb(254, 254, 184); background-color: rgb(254, 254, 184); padding: 10px;">
<p>문제와 해답은 직접 작성한 것으로 오타가 있을 수도 있으며 해답역시 틀린 부분이 있을 수 있습니다. 틀린 부분에 대한 지적은 적극 수용하도록 하겠습니다. 문제를 먼저 풀어보시고 참고용으로만
        봐주시길 바랍니다.<br/></p>
</div>

#### 연습문제 4장

1. 자바의 클래스와 객체에 대한 설명 중 틀린 것은? 
 ① 객체를 인스턴스라고도 부른다. 
 ② 클래스는 객체를 생성하기 위한 틀이다. 
 ③ 클래스는 필드와 메소드로 구성된다. 
 <span style="color: rgb(255, 0, 0);">④ 클래스의 필드들은 private 보다는 public으로 선언하는 것이 바람직하다.</span> 
 <span style="color: rgb(34, 116, 28);">객체
 지향에 위배되는 사항이다.</span>

2. 생성자에 대한 설명 중 잘못된 것은? 
 ① 한 클래스에 여러 개의 생성자가 있어도 된다. 
 ② this()는 생성자에서 다른 생성자를 호출하는 코드이다. 
 <span style="color: rgb(255, 0, 0);">③
 생성자에서는 this 레퍼런스를 사용할 수 없다.</span> 
 ④ 생성자는 객체 당 오직 한 번만 호출된다.

3. 다음 중 Circle 클래스의 배열 생성 문에서 오류가 없는 것은? 
 ① Circle c[] = new Circle[]; <span style="color: rgb(34, 116, 28);">Array
 initializer</span> 
 ② Circle []c = new Circle(); <span style="color: rgb(34, 116, 28);">Type mismatch</span> 
 <span style="color: rgb(255, 0, 0);">③ Circle []c = new Circle[5];</span> 
 ④ Circle c[] = new Circle[5](); <span style="color: rgb(34, 116, 28);">Type mismatch</span>

4. 다음은 static 멤버에 대한 설명이다. 보기에서 빈칸에 적절한 말을 삽입하라.

<div class="txc-textbox" style="border-style: solid; border-width: 1px; border-color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); padding: 10px;">
<p>static 멤버는 <u><span style="color: rgb(9, 0, 255);">클래스</span></u> 멤버라고도 불리며, 동일한 클래스의 모든 객체들에 의해 <u><span style="color: rgb(9, 0, 255);">공유</span></u>된다.
        static 멤머는 개체 레퍼런스나 클래스 이름으로도 접근할 수 있으며, 객체가 생성되기 전
        프로그램이 시작될 때 생성되어 있다. 특히 static 메소드는 <span style="color: rgb(9, 0, 255);"><u>static</u></span> 멤버들만 접근할 수
        있고, <span style="color: rgb(9, 0, 255);"><u>this</u></span>을(를) 사용할 수 없는 제약 사항이 있다.<br/></p>
</div>

5. 다음은 메소드 오버로딩이 성공한 사례인가, 실패한 사례인가?

```java
class A {
    int x;
    void f(int x) {this.x = x; }
    int f(int x, int y) { return x*y; }
}
```

<span style="color: rgb(34, 116, 28);">성공한 사례다. 메소드의 매개변수의 갯수가 다르기 때문이다.</span>

6. 다음은 객체 소멸에 대한 설명이다. 보기에서 빈칸에 적절한 말을 삽입하라.

<div class="txc-textbox" style="border-style: solid; border-width: 1px; border-color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); padding: 10px;">
<p>자바에서는 객체를 임의로 <span style="color: rgb(9, 0, 255);"><u>소멸시킬</u></span> 수 없으며, 이것은 개발자에게 매우 <span style="color: rgb(9, 0, 255);"><u>다행한</u></span>
        일이다. 참조하는 <span style="color: rgb(9, 0, 255);"><u>레퍼런스</u></span>가 하나도 없는 객체를 가비지라고 판단하고, 가비지를 사용 메모리로 자동
        수집하는 <span style="color: rgb(9, 0, 255);"><u>가비지 컬렉션</u></span>을 진행시킨다. 응용프로그램에서 자바 플랫폼에 이 과정을 지시하고자 하면 <u><span style="color: rgb(9, 0, 255);">System.gc()</span></u>코드를 호출하면 된다.<br/></p>
</div>

7. 다음 소스에 오류는 없지만, 객체 지향 프로그래밍 관점에서 바람직한 코드로 수정하라.

```java
class Person {
    public int age;
}

public class Example {
    public static void main(String args[]) {
        Person a = new Person();
        a.age = 17;
    }
}
```

위 코드를 아래와 같이 변경

```java
class Person {
    private int age;
    Person(int age) {
        this.age = age;
    }
}

public class Example {
    public static void main(String args[]) {
        Person a = new Person(17);
    }
}
```

8. 다음 프로그램의 실행 결과는 무엇인가?

```java
class Number {
    int n;
    public Number(int n) { this.n = n; }
}

public class Example {
    static void plusTen(Number x) { x.n += 10; }
    public static void main(String args[]) {
        Number ob = new Number(5);
        plusTem(ob);
        system.out.println(ob.n);
    }
} 
```

<div class="txc-textbox" style="border-style: solid; border-width: 1px; border-color: rgb(0, 0, 0); background-color: rgb(0, 0, 0); padding: 10px;">
<p><span style="color: rgb(255, 255, 255);">15</span><br/></p>
</div>

9. 다음 Rectangle 클래스의 두 생성자를 this()가 없는 코드로 바꾸어라.

```java
public class Rectangle {
    int w,h;
    Rectangle(int w, int h) {
        this.w = w; this.h = h;
    }
    Rectangle(int w) {
        this(w,2); // Change to a different code instead of the this()
        // My Answer : this.w = w; this.h = 2;
    }
    Rectangle() {
        this(1); // Change to a different code instead of the this()
        // My Answer : this.w = 1; this.h = 2;
    }
}
```

10. 다음 중 가비지가 발생하는 것은? <span style="font-size: 10pt;">(생략)</span> 
 ①②③ <span style="color: rgb(255, 0, 0);">④</span>

11. 다음 멤버 함수의 사용이 잘못된 것은?

```java
public class StaticTest {
    static int a;
    static int getA() { return a; } // 1
    int b;
    int getB() { return b; } // 2
    int f() { return getA(); } // 3
    static int g() { return getB(); } // 4
}
```

<span style="color: rgb(34, 116, 28);">④: g()함수는 static이지만 getB()는 static으로 생성되지 않았기 때문</span>

12. 다음 코드에서 잘못된 문장은?

```java
class StaticSample {
    int x;
    static int y;
    static int f() { return y; }
}

public class UsingStatic {
    public static void main(Stirng [] args) {
        StaticSample.x = 5; // 1
        StaticSample.y = 10; // 2
        int tmep = StaticSample.f(); // 3
        StaticSample a = new StaticSample();
        temp = a.y; // 4
    }
}
```

<span style="color: rgb(34, 116, 28);">①: 변수 x는 static이 아니므로 동적생성 후 사용해야 한다.</span>
