Reference Solution for MCQ of 2025 Asia Set

19 阅读11分钟

Reference Solution for MCQ of 2025 Asia Set

Answers

  1. B
  2. D
  3. E
  4. D
  5. D
  6. A
  7. E
  8. C
  9. IGNORED
  10. A
  11. D
  12. B
  13. C
  14. A
  15. C
  16. D
  17. C
  18. E
  19. C
  20. C
  21. IGNORED
  22. B
  23. B
  24. C
  25. E
  26. B
  27. C
  28. B
  29. E
  30. B
  31. B
  32. IGNORED
  33. A
  34. C
  35. IGNORED
  36. B
  37. E
  38. E
  39. IGNORED
  40. D

Explanation

  1. Constructors

    • based on the code given:
      • public Room(String description) will be called if you pass one String value as the argument, such as Room r1 = new Room("kitchen");
      • public Room(String description, int numDoors) will be called if you pass one String value and one int value as arguments, such as Room r2 = new Room("bedroom", 2);
    • so the answer is B.
  2. loops (for and while loops)

    • As to the code given: for (int j = 1; j <= 100; j++) will be executed 100 times, so count will be 100 finally.
    • As to code segment I: while (j <= 100) { j++; }, j is 0 in the beginning, and is 100 when entering the loop for the last round, so count will be 101 finally.
    • As to code segment II: while (j <= 200) { j+=2; }, j is 2 in the beginning, j is incremented by 2 each time, and j is 200 when entering the loop for the last round, so count is 100 finally.
      • j is: 2, 2 + 1 * 2, 2 + 2 * 2, ... 2 + 99 * 2
      • so j has 100 values, count++ is executed 100 times, and will be 100 finally.
    • As to code segment III: j is 100, 99, ..., 1, so count will be 100 finally.
    • So the answer is D.
  3. if statement and boolean expressions

    • if (newCustomer || serviceYears > 10) is false based on the values given.
    • if (requestCount >= 5) is true based on the value given for requestCount, so cost += 2.0, cost will be 27.0.
    • So the answer is E.
  4. Methods and Parameters

    • to call the public void run(Program p) method of the Computer class, you need to create a Computer object and pass a Program object as the argument into the run method.
    • to create a Program object, you need to use one of the constructors:
      • Program p = new Program();
      • Program p = new Program("blocks");
    • and then pass object p into the run method: run(p)
    • so the answer is D.
  5. Relational Operators

    • relational operators: >, <, >=, <=, ==, !=
    • you can use == or != to check if two primitive values (int, double, boolean) are equal or not.
    • so I and II are correct.
    • III is not correct since we can not call methods on variables of primitive types.
    • so the answer is D.
  6. String Manipulation

    • Let's use the example given, to move the 'comfort'(stored in word) in 'uncomfortable'(stored in phrase) to the end, you need to
      • find the location(stored in index) of 'comfort' in 'uncomfortable', and the length of 'comfort' (stored in len)
      • then get the part before 'comfort' using phrase.substring(0,index), which is 'un'
      • then get the part after 'comfort' using phrase.substring(index+word.length), which is 'able'
      • then concatenate 'un', 'able', and 'comfort'.
    • so the answer is A.
  7. if statements

    • to return 0 in the method, num should be 0.
    • so:
      • if (value % 2 == 1) should be false, value must be an even number.
      • else if (value >= 25) should be false, value must be <25.
      • else if (value % 2 == 0) should be false, value must be odd number.
      • since there is no value of both even and odd, value will never be 0.
    • so the answer is E.
  8. Type casting and arithmetic operations

    • The answer is (double) (a + b) / 2, in which , (a+b) is evaluated first, and then convert the result to a double value, and then divide the sum by 2.
    • so the answer is C.
  9. Question Missing.

  10. if statements

    • The question tries to return the median(middle value) among three values.
    • the first if...else statement set variable 'largestSoFar' to the greater value of 'secVal' and 'firstVal'.
    • the second if...else statement returns the lesser value of 'largestSoFar' and 'thirdVal'.
    • In option A:
      • 'largestSoFar' is set to 3 when comparing 3 and 2.
      • then 1 is returned when comparing 'largestSoFar'(3) and 'thirdVal'(1).
      • so A does not work as intended.
    • For the tree values given, it will not work when the third value is the smallest.
    • so the answer is A.
  11. Arithmetic Operations:

    • to get each digit in a number, for example 1234, we need to use a loop to:
      • 1st round: get 4 using 1234 % 10, and get the remaining digits 123 using 1234/10.
      • 2nd round: get 3 using 123 % 10, and get the remaining digits 12 using 123/10.
      • 3rd round: get 2 using 12 % 10, and get the remaining digits 1 using 12/10.
      • 4th round: get 1 using 1 % 10, and get 0 using 1/10.
      • Since there are no more digits, so the loop is over.
    • the correct code should be:
        public static int reverse(int num)
        {
            int retVal = 0; // Line 7
            int digit = 0;
            while (num > 0) // Line 10
            {
                digit = num % 10; // Line 12
                retVal = (retVal * 10) + digit; // Line 13
                num = num / 10; // Line 14
            }
            return retVal;
        }
      
      • in the code:
        • the variable 'digit' stores the digit retrieved each time, and would be: 4, 3, 2, 1 respectively after each iteration.

        • the variable 'retVal' stores the result to be returned, for 1234, 4321 should be returned.

          roundnumdigitretValnum(updated)
          112344(0 * 10) + 4 is 4123
          21233(4 * 10) + 3 is 4312
          3122(43 * 10) + 2 is 4321
          411(432 * 10) + 1 is 43210
    • so the answer is D: change += to =.
  12. String Manipulation

    • in enigma("abcde", "WXYZ"):

      • "abcde" is passed to 'first', a nd 'WXYZ' to 'second'
      • len stores 4.
      • in for (int i = 0; i < len; i++), i is 0, 1, 2, 3
      • in result += first.substring(i, i + 1) + second.substring(i, i + 1):
        • first.substring(i, i+1) retrieves one character at position i.
        • second.substring(i, i+1) retrieves one character at position i.
      • Use a trace table to help analysis.
      ifirst.substring(i, i+1)second.substring(i, i+1)result
      0aWaW
      1bXaWbX
      2cYaWbXcY
      3dZaWbXcYdZ
    • so the answer is B.

  13. Nested loops

    • before modification:
      • outer loop: for (int first = 10; first < 25; first++), first are 10, 11, ..., 24, that is 15 values, so the outer loop executes 15 times.
      • inner loop: for (int second = 25; second > 10; second--), second are 25, 24, ..., 11, that is 15 values, so the inner loop executes 15 times.
      • as a result, count++ executes 15 * 15 (225) times.
    • after modification:
      • inner loop: for (int second = 25; second > first; second--), since first are 10, 11, ..., 24, so second are 25 to 11, 25 to 12, 25 to 13, ..., 25, which are 15 values, 14 values, ..., 1 value.
      • as a result, count++ executes 15 + 14 + ... + 1 = 120 times.
    • so the answer is C.
  14. if statement and boolean expressions

    • when you call checkIt(10, 0)
      • in if (count != 0 && sum / count >= 10), the condition evaluates to false.
      • in else if (count == 0 || sum < 5), the condition evaluates to true, 'Maybe' is printed.
    • so the answer is A.
  15. Object References

    • in the code given:
    Road a = new Road();
    a.setName("Pine");
    Road b = new Road("Pine");
    Road c = a;
    Road d = c;
    d.setName("Maple");
    
    • Road a = new Road(), creates a new object, and assigns it to a.
    • Road b = new Road("Pine"), creates another new object, and assigns it to b.
    • Road c = a, no new object created, a is assigned to c, so c and a refer to the same object.
    • Road d = c, no new object created, c is assigned to d, so d and c refer to the same object.
    • As a result, d, c, a refers to the same object hiwle b refers to a different object.
    • so the answer is C since a and b refer to two different objects.
  16. Recursion

    • locate where recursion is: in else, return mystery(n - 1) + mystery(n - 2).
    • how n changes: n - 1 and n - 2
    method callnn-1n-2recursion
    mystery(4)432mystery(3) + mystery(2)
    mystery(3)321mystery(2) + mystery(1)
    mystery(2)210mystery(1) + mystery(0)
    • Analysis:
      • return value of mystery(0): 2 * 0 = 0
      • return value of mystery(1): 2 * 1 = 2
      • return value of mystery(2): mystery(1) + mystery(0) = 2
      • return value of mystery(3): mystery(2) + mystery(1) = 2 + 2 = 4
      • return value of mystery(4): mystery(3) + mystery(2) = 4 + 2 = 6
    • so the answer is D.
  17. Constructors

    • a constructor is normally used to initialize instance variables or do other initialization work.
    • in the constructor: public City(int numPeople), numPeople is passed as a parameter to set the initial value of the instance variable population, so the code to add should be: population = numPeople.
    • So the answer is C.
  18. Traverse 1D arrays

    • int[] nums = {4, 0, 9, 8, 2, 6}
    • for (int i = nums.length - 1; i >= 0; i--) means traverse the elements in 'nums' reversely, from the last element to the first element,
      • so the order of elements is: 6, 2, 8, 9, 0, 4.
      • so the order of indexes is: 5, 4, 3, 2, 1, 0.
    • print the index if it is > 2, so the output is : 5, 3, 2, 0.
    • So the answer is E.
  19. Implement 1D array algorithms.

    • The code segment given computes the sum of indexes of elements with a value of less than 10.
    • Code segment I: compute the sum of elements with a value of less than 10.
    • Code segment II: compute the sum of indexes of elements with a value of less than 10. j is index, x is element.
    • Code segment III: compute the sum of indexes of elements with a value of less than 10. j is index, arr[j] is element.
    • so the answer is C.
  20. Update instance variables

    • Arithmetic alpha = new Arithmetic(3), creates a new object, assigns it to 'alpha', and set instance variable 'value' of alpha to 3.
    • Arithmetic beta = new Arithmetic(2), creates a new object, assigns it to 'beta', and set instance variable 'value' of beta to 2.
    • alpha.update(1), updates 'value' of alpha to 4(3+1).
    • alpha.update(2,3), updates 'value' of alpha to 11(4 * 2 + 3).
    • beta.update(), updates the 'value' of beta to 4(4 * 4)
    • so the output of alpha.getValue() + beta.getValue() is 11 + 4 = 15.
    • so the answer is C.
  21. Ignored

  22. 2D Arrays

    • You may use a trace table if necessary:
      • outer loop: for (int j = 0; j < 3; j++)
      • inner loop: for (int k = 0; k < j; k++)

      jarr[j][j] = jkarr[j][k] = j-karr[k][j] = k - j
      0arr[0][0] = 0---
      1arr[1][1] = 10arr[1][0] = 1 - 0arr[0][1] = 0 - 1
      2arr[2][2] = 20
      1
      arr[2][0] = 2 - 0
      arr[0][2] = 0 - 2
      arr[2][1] = 2 - 1
      arr[1][2] = 1 - 2
    • As a result:
      • arr[0][0] = 0, arr[0][1] = -1, arr[0][2] = -2
      • arr[1][0] = 1, arr[1][1] = 1, arr[1][2] = -1
      • arr[2][0] = 2, arr[2][1] = 1, arr[2][2] = 2
    • so the array will be:
      0  -1  -2
      1    1   -1
      2    1    2
    • so the answer is B.
  23. Implement ArrayList Algorithms

    • The logic of the reorder method is:
      • if the current element is an even number, then finds the first odd number after it, and swaps the even number with the odd number.
      • As a result, when the loop is over, all the odd numbers will be swapped and placed before even numbers.
      • For example, as to the example data given: 2, 3, 5, 8, 4, 1
        • when i is 0, element is 2, swap 2 with 3, then you get: 3, 2, 5, 8, 4, 1
        • when i is 1, element is 2, swap 2 with 5, then you get: 3, 5, 2, 8, 4, 1
        • when i is 2, element is 2, swap 2 with 1, then you get: 3, 5, 1, 8, 4, 2
        • when i is 3, element is 8, data.get(i) % 2 == 0 is false, no swapping.
        • when i is 4, element is 4, data.get(i) % 2 == 0 is false, no swapping.
        • when i is 5, element is 2, data.get(i) % 2 == 0 is false, no swapping.
    • as to the data given: 2, 6, 7, 4, 1, 8
      • i = 0, element = 2, swap 2 with 7, you get: 7, 6, 2, 4, 1, 8
      • i = 1, element = 6, swap 6 with 1, you get: 7, 1, 2, 4, 6, 8
      • i = 2, element = 2, no swapping
      • i = 3, element = 4, no swapping
      • i = 4, element = 6, no swapping
      • i = 5, element = 8, no swapping
    • so the answer is B.
  24. Implement ArrayList Algorithms

    • data.get(i) % 2 == 0 is changed to data.get(i) / 2 == 0
      • data.get(i) % 2 == 0 means the element at i is even
      • data.get(i) / 2 == 0 means the element at i either 0 or 1.
    • data.get(index) % 2 == 0 is changed to data.get(index) / 2 ==0
      • data.get(index) % 2 == 0 means the element at index is even
      • data.get(index) / 2 == 0 means the element at index either 0 or 1.
    • As specified in the question, the old code reorder the elements so that elements with odd values appear before all other elements.
    • Similaly, the new code reorder the elements so that other elements appear before elements with a value equal to 0 or 1.
    • so the answer is C.
  25. static

    • Based on the scenario given, "the variable count is intended to store the total number of social media posts made by all SocialMediaAccount objects", meaning that count is shared by all SocialMediaAccountobjects, so it must be declared as static.
    • so the answer is E.
  26. Recursion

    • A 'base case' in recursion is the case to terminate recursion.
    • In this example, the base case is "n == 1".
    • However, in this question, n is not changed, leading to infinite recursion.
    • The correct logic is that n progress toward the base case 'n==1', so return n * factorial(n) should be changed to return n * factorial(n-1).
    • so the answer is B.
  27. Compound Boolean Expressions: (&&, ||, !)

    • a = true, b = false, c = false
    • boolean b1 = a && !(b || c)
      • a is true
      • b || c is false, !(b || c) is true
      • so b1 is true
    • boolean b2 = !a || b && c
      • !a is false
      • b && c is false
      • so b2 is false
    • boolean b3 = a && !b || !c
      • a && !b is true
      • !c is true
      • so b3 is true
    • so the answer is C.
  28. Nested loops

    • outer loop: for (int j = 1; j < 7; j += 2)
    • inner loop: for (int k = j; k > 0; k--)
    • how k changes with j
      • j    k
      • 1    1
      • 3    3 2 1
      • 5    5 4 3 2 1
    • so the line inside the inner loop will be executed 1 + 3 + 5 times, which is 9 times.
    • so the answer is 9 times.
  29. 1D array

    • The given code in the question is used to reverse all the elements in one 1D array.
    • To achieve this:
      • the loop condition should be k < length/2
      • reverse the element at index i with the element at index length-1-i. For example, if length is 7, swap 0 with 6, 1 with 5, 2 with 4
    • so the answer is E.
  30. Implement ArrayList Alorithms

    • for (int item : theList) traverses all the elements in 'theList', including the first element.
    • However, double sum = theList.get(0) sets the initial value of sum to the value of the first element in the list.
    • As a result, the first element is added twice, which is not correct.
    • So sum = theList.get(0) should be changed to sum = 0.
    • so the answer is B.
  31. Recursion:

    • locate where recursion is: in else, return 1 + aMystery(str.substring(n + 1))
    • how n changes: str.substring(n + 1)
    • What the recursion does: add 1 to the result to return.
    • The recursion add 1 if 'a' is found in str, and then set str to the remaining content after the 'a' found. In summary, it counts the number of 'a's in the String value passed into the method in the beginning.
    • You may use a trace table if necessary:
    method callstrn = str.indexOf("a")str.substring(n+1)recursion
    aMystery("aardvark")aardvark0ardvarkaMystery("ardvark")
    aMystery("ardvark")ardvark0rdvarkaMystery("rdvark")
    aMystery("rdvark")rdvark3rkaMystery("rk")
    aMystery("rk")rk-1NANA
    • so the answer is B.
  32. Ignored

  33. 2D Array

    • option A: The code segments compute the sum of all the elements in the 2D array, and then subtract those values along the main diagonal from sum. It works as intended.
    • So the answer is A.
  34. 1D Array

    • in for (int i = 1; i < arr.length - 1; i += 2): i starts from 1, increment by 2 each time, and is less than length - 1. In summary, the loop traverses the second, fourth, sixth elements in the array.
    • Note: i < arr.length - 1 indicates that it will not access the last element.
    • so the answer is C.
  35. Ignored

  36. String Manipulation

    • As to the method call countSpecial("24341243"):
      • str.length() is 8
      • for (i = 0; i < str.length() - 1; i++): i is 0 to length - 2
      • String temp = str.substring(i, i + 2): temp stores the two characters starting at index i.
      • If the sum of the two characters in temp is 7, count += 7.
      • The value of temp: 24, 43, 34, 41, 12, 24, 43
      • To assign count the value 14, count +=7 is executed 2 times (43 and 34).
    • when temp is 34, the value of i is 2.
    • so the answer is B.
  37. 2D arrays

    • we mainly use two ways to traverse a 2D array:
      • in row-major order: outer loop traverse rows
      • in column-major order: outer loop traverse columns
    • in this question, we usse column-major order, so in the outer loop, x is the column index.
    • y is used in the inner loop, so it represents the row index.
    • therefore, when you access the element in the array, you should use letters[y][x].
    • so the answer is E.
  38. String Manipulation

    • in word.substring(k, k + 1), k is used to represent an index.
    • so num += k computes the sum of indexes.
    • if (target.indexOf(letter) != -1), the condition returns true if target does not contain letter.
    • so the answer is E.
  39. Ignored

  40. 1D array

    • int[] nums = {2, 5, 1, 8, 0, 6}
    • int a = nums[nums.length / 2]: a = nums[3], which is 8
    • int b = nums[nums[0]]: b = nums[2], which is 1
    • int c = nums[nums[2] + 2]: c = nums[1+2], which is 8
    • so the answer is D.