본문 바로가기
자바(Java)/자바 AP

[자바AP.04] 반복문(Iteration)

by 긱펀 2024. 3. 24.
반응형

[자바AP.04] 반복문(Iteration)

 

[문제 정답은 제일 아래 "더보기" 클릭으로 확인 가능]

 

 

*04.Iteration(Loops)에 대한 복습 내용은 아래 "더보기" 클릭

더보기

In this chapter you learned about loops. Loops are used to repeat a statement or block of statements inside a pair of curly braces.

 

  • Body of a Loop - The single statement or a block of statements that can be repeated (a loop may not execute at all if the condition is false to start with). In Java the body of the loop is either the first statement following a while or for loop is the body of the loop or a block of statements enclosed in { and }.
  • For Loop - A loop that has a header with 3 optional parts: initialization, condition, and change. It does the initialization one time before the body of the loop executes, executes the body of the loop if the condition is true, and executes the change after the body of the loop executes before checking the condition again.
  • While Loop - A loop that repeats while a Boolean expression is true.
  • Infinite Loop - A loop that never ends.
  • Loop - A way to repeat one or more statements in a program.
  • Nested Loop - One loop inside of another.
  • Out of Bounds error - A run-time error that occurs when you try to access past the end of a string or list in a loop.
  • while - used to start a while loop
  • for - used to start a for loop or a for each loop
  • System.out.println(variable) - used to print the value of the variable. This is useful in tracing the execution of code and when debugging.

 


 

[문제1]

What is the output after the code has been executed?

1
2
3
4
5
6
7
8
9
int n = 35;
int result = 1;
while (n > 0)
{
    int d = n % 10;
    result *= d;
    n /= 10;
}
System.out.println(result);
cs

 

A. 35
B. 15
C. 10
D. 8
E. 33

 

[문제2]

Consider the following codeWhich of the following could replace the missing loop header to ensure that the code segment will work as intended to print out the even numbers from 0 to 8? segment which is intended to print out the even numbers from 0 to 8 (including 8).

 

1
2
3
4
5
6
7
8
9
int count = 0;
/* missing loop header */
{
    if (count % 2 == 0)
    {
        System.out.println(count);
    }
    count++;
}
cs

 

A. while (count > 0)
B. while (count >= 8)
C. while (count < 8)
D. while (count < 10)
E. while (count <= 10)

 

[문제3]

Consider the following code segment.

1
2
3
4
5
6
7
8
9
10
int count = 0, sum = 0;
while (count <= 6)
{
    count++;
    if (count % 2 == 0)
    {
        sum += count;
    }
}
System.out.println(sum);
cs

 

Which of the following code segments will produce the same output as the code segment above?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
I.  int sum = 0;
    for(int count = 0; count <= 6; count++)
    {
        count++;
        if (count % 2 == 0)
        {
             sum += count;
        }
    }
    System.out.println(sum);
 
II. int sum = 0;
    for(int i = 0; i <= 6; i += 2)
    {
       sum += i;
    }
    System.out.println(sum);
 
III. int sum = 0;
     for(int j = 7; j > 1; j--)
     {
        if (j % 2 == 0)
        {
             sum += j;
        }
     }
     System.out.println(sum);
cs

 

A. I and II only
B. II and III only
C. I and III only
D. III only
E. I, II, and III

 

[문제4]

Which of the following best explains how changing the for loop header to for (int i = 4; i <= 6; i += 2) affects the output of the code segment?

1
2
3
4
5
6
int result = 1;
for(int i = 3; i < 6; i += 2)
{
  result *= i;
}
System.out.println(result);
cs

 

A. The output of the code segment will be unchanged.
B. The output will be the same, but the new loop will iterate more times.
C. The output will be different, but both versions of the loop will iterate two times.
D. The output will be different, and the new loop will iterate more times.
E. This will cause an error.

 

[문제5]

What does the following code print?

1
2
3
4
for (int i = 3; i <= 12; i++)
{
   System.out.print(i + " ");
}
cs

 

A. 5 6 7 8 9
B. 4 5 6 7 8 9 10 11 12
C. 3 5 7 9 11
D. 3 4 5 6 7 8 9 10 11 12

 

[문제6]

How many times does the following method print a *?

1
2
3
4
for (int i = 3; i < 9; i++)
{
   System.out.print("*");
}
cs

 

A. 9
B. 7
C. 6
D. 10

 

[문제7]

What does the following code print?

1
2
3
4
5
6
int x = -5;
while (x < 0)
{
   x++;
   System.out.print(x + " ");
}
cs

 

A. 5 4 3 2 1
B. -5 -4 -3 -2 -1
C. -4 -3 -2 -1 0

 

[문제8]

How many times does the following method print a *?

1
2
3
4
for (int i = 5; i <= 12; i++)
{
   System.out.print("*");
}
cs

 

[문제9]

What does the following code print?

1
2
3
4
5
int x = 3;
while (x < 9)
{
   System.out.print(x + " ");
}
cs

 

A. 3 4 5 6 7 8
B. 3 4 5 6 7 8 9
C. 0 1 2 3 4 5 6 7 8
D. 0 1 2 3 4 5 6 7 8 9
E. It is an infinite loop

 

[문제10]

What does the following code print?

1
2
3
4
5
6
int x = 0;
while (x <= 5)
{
   System.out.print(x + " ");
   x++;
}
cs

 

A. 1 2 3 4
B. 1 2 3 4 5
C. 0 1 2 3 4
D. 0 1 2 3 4 5

 

[문제 11]

How many stars are output when the following code is executed?

1
2
3
4
for (int i = 0; i < 5; i++) {
   for (int j = 0; j < 5; j++)
      System.out.println("*");
}
cs

 

A. 10
B. 5
C. 25
D. 50
E. 15

 

[문제12]

Which of the following code segments will produce the displayed output?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[displayed output]
1
22
333
4444
55555
 
 
I.   for (int i = 1; i <= 5; i++) {
        for (int j = i; j > 0; j--) {
           System.out.print(i);
        }
        System.out.println();
     }
 
II.  for (int i = 0; i < 5; i++) {
        for (int j = 0; j < i; j++) {
           System.out.print(i);
        }
        System.out.println();
     }
 
III. for (int i = 1; i < 5; i++) {
        for (int j = i; j > 0; j--) {
           System.out.print(i);
        }
        System.out.println();
     }
 
IV.  for (int i = 1; i < 6; i++) {
        for (int j = 0; j < i; j++) {
           System.out.println(i);
        }
     }
 
V.   for (int i = 0; i < 5; i++) {
        for (int j = 0; j < i; j++) {
           System.out.print(i+1);
        }
        System.out.println();
     }
cs

 

A. I
B. II
C. III
D. IV
E. V

 

[문제13]

What is printed as a result of the following code segment?

1
2
3
4
for (int k = 0; k < 20; k+=2) {
   if (k % 3 == 1)
      System.out.print(k + " ");
}
cs

 

A. 0 2 4 6 8 10 12 14 16 18
B. 4 16
C. 0 6 12 18
D. 1 4 7 10 13 16 19
E. 4 10 16

 

[문제14]

Which of the following code segments will produce the displayed output?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[displayed output]
11111
2222
333
44
5
 
 
I.   for (int j = 1; j <= 5; j++) {
        for (int k = 5; k >= j; k--) {
           System.out.print(j);
        }
        System.out.println();
     }
 
II.  for (int j = 1; j <= 5; j++) {
        for (int k = 5; k >= 1; k--) {
           System.out.print(j);
        }
        System.out.println();
     }
 
III. for (int j = 1; j <= 5; j++) {
        for (int k = 1; k <= j; k++) {
           System.out.print(j);
        }
        System.out.println();
     }
 
IV.  for (int j = 1; j <= 5; j++) {
        for (int k = 1; k <= 5; k++) {
           System.out.println(j);
        }
     }
 
V.   for (int j = 1; j <= 5; j++) {
        for (int k = j; k <= 5; k++) {
           System.out.print(k);
        }
        System.out.println();
     }
cs

 

A. I
B. II
C. III
D. IV
E. V

 

[문제15]

What are the values of var1 and var2 after the following code segment is executed and the while loop finishes?

1
2
3
4
5
6
7
int var1 = 0;
int var2 = 2;
 
while ((var2 != 0&& ((var1 / var2) >= 0)) {
   var1 = var1 + 1;
   var2 = var2 - 1;
}
cs

 

A. var1 = 0, var2 = 2
B. var1 = 1, var2 = 1
C. var1 = 3, var2 = -1
D. var1 = 2, var2 = 0
E. The loop won't finish executing because of a division by zero.

 

 

 

 


 

[문제 정답은 아래 "더보기" 클릭]

더보기

[문제1 정답]

B

(The digits in n = 35 are 3 and 5 and 3*5 = 15.)

 

[문제2 정답]

D

(This would stop the loop when count is 10.)

 

[문제3 정답]

B

(Note that I has an extra count++ at the beginning of the loop body that should be deleted.)

 

[문제4 정답]

C

(One will multiply 3*5 and the other 4*6.)

 

[문제5 정답]

D

(The value of i starts at 3 and this loop will execute until i equals 12. The last time through the loop the value of i is 12 at the begininng and then it will be incremented to 13 which stops the loop since 13 is not less than or equal to 12.)

 

[문제6 정답]

C

(Since i starts at 3 and the last time through the loop it is 8 the loop executes 8 - 3 + 1 times = 6 times.)

 

[문제7 정답]

C

(x is set to -5 to start but then incremented by 1 so it first prints -4.)

 

[문제8 정답]

B

(Note that it stops when i is 13 so 13 - 5 is 8.)

 

[문제9 정답]

E

(Since x is never changed in the loop this is an infinite loop.)

 

[문제10 정답]

D

(This loop starts with x = 0 and ends when it reaches 6.)

 

[문제11 정답]

C

(The first loop will execute 5 times, and for each time through, the second loop will execute 5 times. So the answer is the number of times through the first loop times the number of times through the second.)

 

[문제12 정답]

A

(This will loop with i changing from 1 to 5 and then for each i, j will loop from i to 0 printing the value of i and then a new line.)

 

[문제13 정답]

E

(This will loop with k having a value of 0 to 18 (it will stop when k = 20). It will print out the value of k followed by a space when the remainder of dividing k by 3 is 1.)

 

[문제14 정답]

A

(This will loop with j from 1 to 5 and k from 5 to j and print out the value of j and a space. So the first time through the loop it will print 1 five times and the next time it will print out 2 four times and so on.)

 

[문제15 정답]

D

(The loop starts with var1=0 and var2=2. The while checks that var2 isn't 0 and that var1/var2 is greater than or equal to zero (0/2=0) so this is equal to zero and the body of the while loop will execute. The variable var1 has 1 added to it for a new value of 1. The variable var2 has 1 subtracted from it for a value of 1. At this point var1=1 and var2=1. The while condition is checked again. Since var2 isn't 0 and var1/var2 (1/1=1) is >=0 so the body of the loop will execute a second time. The variable var1 has 1 added to it for a new value of 2. The variable var2 has 1 subtracted from it for a value of 0. At this point var1=2 and var2=0. The while condition is checked again. Since var2 is zero the while loop stops and the value of var1 is 2 and var2 is 0.)

 


 

728x90
반응형

댓글