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

- Author: @baealex
- Published: 2018-05-28
- Updated: 2020-04-06
- 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-3%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>

#### 연습문제 3장

1. while문을 가진 다음 프로그램에 대해 물음에 답하라.

```java
int i = 0, sum = 0;
while(i<10) {
    i = i + 2;
    sum += i;
}
System.out.println(sum);
```

(1)무엇을 계산하는 프로그램이며, 실행 결과는? 
 <span style="color: rgb(255, 0, 0);">10보다 작은 2의 배수를 더함, 30</span>

(2)앞의 실행 결과와 동일한 실행 결과를 얻고자 할때 다음 빈칸을 채워라.

```java
int i = 0, sum = 0;
while(true) {
    i = i + 2;
    sum += i;
    if(/*i > 10*/) break;
}
System.out.println(sum);
```

```java
int i = 0, sum = 0;
do {
    i = i + 2;
    if(/*i > 10*/) continue;
    sum += i;
} while(i<10)
System.out.println(sum);
```

2. 다음 for문에 대해 물음에 답하라.

```java
double sum = 0.0;
double d[] = {1.0, 2.3, 3.4, 5.5};
for(int i=0; i<4; i++) sum += d[i];
System.out.println(sum);
```

(1)예상되는 실행 결과는 무엇인가? 
 <span style="color: rgb(255, 0, 0);">d 배열의 모든 값이 더해져 출력된다. [12.2]</span>

(2)for(int i=0; i<4; i++) 부분을 배열의 length 필드를 이용히여 수정하라. 
 <span style="color: rgb(255, 0, 0);">for(int i=0;
 i&lt;d.length; i++</span> <span style="color: rgb(255, 0, 0);">)</span>

(3)while문으로 바꾸어 작성하라.

```java
double sum = 0.0;
double d[] = {1.0, 2.3, 3.4, 5.5};
int i = 0;
while(i < d.length) sum += d[i++];
System.out.println(sum);
```

(4)do-whlie 문으로 바꾸어 작성하라.

```java
double sum = 0.0;
double d[] = {1.0, 2.3, 3.4, 5.5};
int i = 0;
do {
    sum += d[i++];
} while(i < d.length);
System.out.println(sum);
```

(5)for-each 문으로 바꾸어 작성하라.

```java
double sum = 0.0;
double d[] = {1.0, 2.3, 3.4, 5.5};
for(double i : d) sum+= i;
System.out.println(sum);
```

3. 배열을 선언하고 생성하는 다음 물음에 답하라.

(1)10개의 문자를 가지는 배열 c를 생성하는 코드를 쓰라. 
 <span style="color: rgb(255, 0, 0);">char c[] = new char[10];</span>

(2)0에서 5까지 정수 값으로 초기화된 정수 배열 n을 선언하라. 
 <span style="color: rgb(255, 0, 0);">int n[] = { 0, 1, 2, 3, 4, 5
 </span> <span style="color: rgb(255, 0, 0);">};</span>

(3)'일', '월', '화', '수', '목', '금', '토'로 초기화된 배열 day를 선언하라. 
 <span style="color: rgb(255, 0, 0);">char [] day = {
 '일', '월', '화', '수', '목', '금', '토' };</span>

(4)5행 4열 크기의 실수 배열 d를 선언하라. 
 <span style="color: rgb(255, 0, 0);">double[][] d = new double[4][5</span> <span style="color: rgb(255, 0, 0);">];</span>

(5)1에서 12까지 순서대로 정수로 초기화되는 3행 4열의 이차원 배열 var을 선언하라.

```java
int[][] var = { {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12} };
```

4. 다음 코드에 대해 실행 중 오류가 발생하는 보기는? 
 <span style="color: rgb(255, 0, 0);">②myArray[myArray.length] = 100;</span>

5. 다음 2차원 배열 선언문에서 문법적으로 잘못된 것은? 
 <span style="color: rgb(255, 0, 0);">④int [3][2] n = { {1, 2}, {3</span> <span style="color: rgb(255, 0, 0);">, 4</span> <span style="color: rgb(255, 0, 0);">}, {4</span> <span style="color: rgb(255, 0, 0);">,
 5</span> <span style="color: rgb(255, 0, 0);">}</span> <span style="color: rgb(255, 0, 0);"> };</span>

6. for-each 문을 이용하여 배열 b를 모두 출력하고자 한다. 빈칸에 적절한 코드를 삽입하라.

```java
boolean [] b = {true, false, true, true};
for(/*boolean boo : b*/)
    System.out.println(boo);
```

7. 다음은 사용자로부터 배열의 개수를 얻어서 배열을 생성하여 리턴하는 allocArray() 메소드를 작성하고자 한다.

```java
<1> allocArray() {
    Scanner scanner = new Scanner(System.in);
    double [] n = new double[scanner.nextInt()];
    <2> // 배열 리턴
```

(1)빈칸을 적절한 코드로 채워라. 
 <span style="color: rgb(255, 0, 0);">①static double[] </span> <span style="color: rgb(255, 0, 0);">②return
 n;</span>

(2)allocArray()를 호출하여 배열을 전달받는 한 줄의 코드를 작성하라. 
 <span style="color: rgb(255, 0, 0);">double [] d =
 allocArray();</span>

8.다음 코드에 대해 물음에 답하라.

(1)코드를 실행하면 출력되는 내용은?

<div class="txc-textbox" style="border-style: none; 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);">123<br/></span><span style="color: rgb(255, 255, 255);">계산을 끝냅니다.</span></p>
</div>

(2)s가 “23.5”일 때 앞의 코드를 실행한 결과 출력되는 내용은?

<div class="txc-textbox" style="border-style: none; 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);">23.5를 정수로 변환할 수 없습니다.<br/>계산을 끝냅니다.</span></p>
</div>
