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

APCSA(JAVA) Midterm Test(Unit.01~05)

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

APCSA(JAVA) Midterm Test(Unit.01~05)

 

-Test link : Click this link

 

5.15. Midterm Test — AP CSAwesome

5.15. Midterm Test The following 20 questions are similar to what you might see on the AP CSA exam for Units 1 - 5. You may only take this test once while logged in. There are no time limits, but it will keep track of how much time you take. Click on the f

runestone.academy

 

 


 

[문제1]

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
42
43
44
45
46
47
48
49
50
51
52
53
54
/* Output:
1
22
333
4444
55555
*/
 
//Loop I
for (int i = 1; i <= 5; i++)
{
   for (int j = i; j > 0; j--)
   {
      System.out.print(i);
   }
   System.out.println();
}
 
//Loop II
for (int i = 0; i < 5; i++)
{
   for (int j = 0; j < i; j++)
   {
      System.out.print(i);
   }
   System.out.println();
}
 
//Loop III
for (int i = 1; i < 5; i++)
{
   for (int j = i; j > 0; j--)
   {
      System.out.print(i);
   }
   System.out.println();
}
 
//Loop IV
for (int i = 1; i < 6; i++)
{
   for (int j = 0; j < i; j++)
   {
      System.out.println(i);
   }
}
 
//Loop 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 only
B. I and II only
C. III only
D. IV and V only
E. V only

 

[문제2]

Consider the following method. What is the output from conditionTest(3,-2);?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public static void conditionTest(int num1, int num2)
{
   if ((num1 > 0&& (num2 > 0))
   {
      if (num1 > num2)
         System.out.println("A");
      else
         System.out.println("B");
   }
   else if ((num2 < 0|| (num1 < 0))
   {
      System.out.println("C");
   }
   else if (num2 < 0)
   {
      System.out.println("D");
   }
   else
   {
      System.out.println("E");
   }
}
cs

 

A. A
B. AC
C. C
D. BD
E. E

 

[문제3]

Which of these loops will output 01234?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int max = 5;
 
//Loop I
for (int i = 0; i < max; i++)
{
   System.out.print(i);
}
 
//Loop II
int j = 0;
while (j < max)
{
   System.out.print(j);
   j++;
}
 
//Loop III
int k = 0;
for (int i = max; i > 0; i--)
{
   System.out.print(i);
}
cs

 

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

 

[문제4]

Consider the following block of code. What value is returned from solution(5)?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public int solution(int limit)
{
   int s = 0;
   for (int outside = 1; outside <= limit; outside++)
   {
      for (int middle = 1; middle <= limit; middle++)
      {
         for (int inside = 1; inside <= limit; inside++)
         {
            s++;
         }
      }
   }
  return s;
}
cs

 

A. 25
B. 15
C. 125
D. 64
E. 625

 

[문제5]

Which of the following is equivalent to !((x > 10) && (x <= 5)) ?

A. (x < 10) && (x > 5)
B. (x > 10) && (x <=5)
C. (x <= 10) && (x > 5)
D. (x <= 10) || (x > 5)
E. (x > 10) || (x <= 5)

 

[문제6]

Consider the following class with the method test. What is the output after the main method is executed calling test(s,b)?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Test1
{
    public static void test(String str, int y)
    {
        str = str + "bow";
        y = y * 2;
    }
 
    public static void main(String[] args)
    {
        String s = "rain";
        int b = 4;
        test(s, b);
        System.out.println("s=" + s + "; b=" + b);
    }
}
cs

 

A. s="rainbow"; b=8;
B. s="rain"; b=8;
C. s="rainbow"; b=4;
D. s="rain"; b=4;
E. s="bow"; b=4;

 

[문제7]

Consider the following Cat class that has an age attribute of type int. The getAge method is intended to allow methods in other classes to access a Cat object’s age value; however, it does not work as intended. Which of the following best explains why the getAge method does NOT work as intended?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Cat
{
    private int age;
 
    public Cat(int a)
    {
        age = a;
    }
 
    public int getAge()
    {
        return a;
    }
}
cs

 

A. The ``getAge()`` method should be declared as private.
B. The return type of the ``getAge()`` method should be void.
C. The ``getAge()`` method should have at least one parameter.
D. The variable ``age`` is not declared inside the ``getAge()`` method.
E. The instance variable ``age`` should be returned instead of a, which is local to the constructor.

 

 

[문제8]

Which of the following statements are TRUE about local variables?

I. Local variables can be declared in the body of constructors and methods.
II. Local variables may only be used within the constructor or method and cannot be declared to be public or private.
III. When there is a local variable with the same name as an instance variable, the variable name will refer to the local variable instead of the instance variable.

 

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

 

[문제9]

Which of the following statements are TRUE about static methods?

I. Static methods and variables include the keyword static before their name in the header or declaration and can be public or private.
II. Static methods can access or change the values of instance variables.
III. Static methods are associated with the class, not objects of the class.

 

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

 

[문제10]

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.

 

 

[문제11]

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
D. -5 -4 -3 -2 -1 0
E. -4 -3 -2 -1

 

[문제12]

What will be printed after this code is executed?

1
2
3
4
5
6
7
for (int i = 0; i <= 15; i++)
{
   if (i % 3 == 0)
   {
      System.out.print(i + " ");
   }
}
cs

 

A. 0 3 6 9 12
B. 0 1 2 3 4 5
C. 1 4 7 10 13
D. 0 3 6 9 12 15
E. This code will not print anything.

 

[문제13]

Consider the following declaration for a class that will be used to represent points in time. Which of these options correctly implement addMinutes()?

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
42
43
44
45
46
47
48
49
50
51
52
public class Timer
{
   private int hours; // number of hours
   private int minutes; // 0 <= minutes < 60
 
   void addHours(int addition)
   {
      hours = hours + addition;
   }
 
   /** addMinutes adds the given argument to the time stored in hours and minutes.
   The argument additionMinutes is between 0 and 119.  **/
   void addMinutes(int additionMinutes)
   {
      // implementation not shown
   }
 
   // ... other methods not shown
}
 
 
//Proposed Implementations:
I.   public void addMinutes(int additionMinutes)
     {
         minutes = minutes + additionMinutes;
     }
II.  public void addMinutes(int additionMinutes)
     {
         minutes += additionMinutes;
         if (minutes >= 60)
         {
            hours += minutes / 60;
            minutes = minutes % 60;
         }
     }
III. public void addMinutes(int additionMinutes)
     {
         minutes += additionMinutes;
         while (minutes >= 60)
         {
            hours++;
            minutes -= 60;
         }
     }
IV.  public void addMinutes(int additionMinutes)
     {
         if (additionMinutes + minutes >= 60)
         {
            minutes = additionMinutes + minutes - 60;
            hours += 1;
         }
     }
cs

 

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

 

[문제14]

Which option will evaluate to true, if and only if both a and b are false?

 

A. !(a && b)
B. !a && b
C. !a && !b
D. a && b
E. a || !b

 

[문제15]

What does the function mystery do?

1
2
3
4
5
6
7
8
public void mystery(String tester)
{
   for (int i = tester.length() - 1; i >= 0; i--)
   {
      System.out.print(tester.substring(i,i+1));
   }
   System.out.println("");
}
cs

 

A. Prints the string in reverse order
B. Deletes the second half of the string
C. Prints string normally
D. Compile-time error occurs
E. Prints alternating characters from beginning and end of the string.

 

[문제16]

After the following code is executed, what does the variable mystery hold?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Mysterious
{
    public static void main(String[] args)
    {
        String mystery = "";
        String starter = "Hello World!";
        for (int i = 0; i < starter.length(); i++)
        {
            if (i % 2 == 0)
            {
                mystery += starter.substring(i, i + 1);
            }
        }
    }
}
cs

 

A. "Hello World!"
B. "Hello "
C. "He"
D. "HloWrd"
E. "el ol!"

 

[문제17]

Which of the following code segments correctly creates an instance of a new Party object

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Party
{
 
   private int numInvited;
   private boolean partyCancelled;
 
   public Party()
   {
      numInvited = 1;
      partyCancelled = false;
   }
 
   public Party(int invites)
   {
      numInvited = invites;
      partyCancelled = false;
   }
}
 
I.   Party myParty;
II.  int classSize = 20;
     Party ourParty = new Party(classSize);
III. int numOfFriends = 6;
     Party yourParty = new Party(numOfFriends + 3.0);
cs

 

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

 

[문제18]

What are the values of a and b after the for loop finishes?

1
2
3
4
5
6
7
int a = 10, b = 3, t = 0;
for (int i = 1; i <= 6; i++)
{
   t = a;
   a = i + b;
   b = t - i;
}
cs

 

A. a = 6 and b = 7
B. a = 6 and b = 13
C. a = 13 and b = 0
D. a = 6 and b = 0
E. a = 0 and b = 13

 

[문제19]

Consider the following code. What string is referenced by s1 after the code executes?

1
2
3
4
5
6
7
String s1 = "Hi There";
String s2 = s1;
String s3 = s2;
String s4 = s1;
s2 = s2.toLowerCase();
s3 = s3.toUpperCase();
s4 = null;
cs

 

A. hi there
B. HI THERE
C. Hi There
D. null
E. hI tHERE

 

[문제20]

Given following code, which of the following statements is a valid assignment statement using these variables?

1
2
3
int a = 5;
int b = 3;
int c = 4;
cs

 

A. a = 6.7
B. b = 87.7
C. 12 = c * b
D. c = a - b


 

[Midterm Test 정답지는 아래 "더보기" 클릭]

더보기

[문제1 정답]

A

 

[문제2 정답]

C

 

[문제3 정답]

D

 

[문제4 정답]

C

 

[문제5 정답]

D

 

[문제6 정답]

D

 

[문제7 정답]

E

 

[문제8 정답]

D

(지역변수 (메소드 내에서 선언되는 변수)에는 접근 지정자(public, private 등)를 붙일 수 없다. 지역변수는 클래스의 멤버가 아니기 때문에)

 

[문제9 정답]

C

(B=> Static methods cannot acccess instance variables. They can only access static variables.)

 

[문제10 정답]

D

 

[문제11 정답]

C

 

[문제12 정답]

D

 

[문제13 정답]

D

 

[문제14 정답]

C

 

[문제15 정답]

A

 

[문제16 정답]

D

 

[문제17 정답]

C

 

[문제18 정답]

C

 

[문제19 정답]

C

(Strings are immutable and so any change to a string returns a new string.)

(Java의 대표적인 불변 객체로는 String이 있다. 그렇기 때문에 참조를 통해 값을 수정하면 내부의 상태가 변하기 때문에 내부를 복사하여 전달하고 있는데, 이를 방어적 복사(defensive-copy) 라고 한다.)

=> String immutable에 대한 자세한 콘텐츠 링크: https://vo.la/WbqHO

 

[문제20 정답]

D

 

 

728x90
반응형

댓글