Reference Solution for MCQ of 2024 Asia Set
Answers
- E
- E
- C
- D
- B
- D
- E
- B
- IGNORED
- B
- D
- E
- A
- D
- C
- C
- A
- A
- E
- D
- B
- E
- C
- C
- E
- B
- C
- E
- IGNORED
- C
- E
- E
- C
- C
- E
- B
- E
- C
- A
- A
Explanation
-
To print platypus
- x > y && x % 2 == 0 should evaluate to false, that means, x <= y or x is odd.
- x + y < 30 should evaluate to false, that means, x + y >=30.
- x % 2 == 0 should evaluate to true, that means, x is even.
- Combine all the conditions above:
- x <= y && x + y >= 30 && x % 2 == 0, so the answer is
E.
- x <= y && x + y >= 30 && x % 2 == 0, so the answer is
-
Method overloading(方法重载)
- A method in a class can be overloaded, which means you are allowed to declare multiple methods with the same name in a class, but the parameters must be different (data type or number of parameters).
- Two methods with only different return types are viewed as the same method, and will lead to compilation error.
- So the answer is
E.
-
Work on 1D array
-
You may use a trace table to track how the loop is executed.
-
int[] arr = (4, 2, 2, 3, 1};
knumarr[num]num00 arr[0] is 4 4 14 arr[4] is 1 1 21 arr[1] is 2 2 32 arr[2] is 2 2 42 arr[2] is 2 2 -
So the answer is
C.
-
-
To set z as 20, then x >= y, so the answer is
D. -
Type casting and arithmetic operations
- int x = (int) valOne + (int) valTwo
- x = 5 + 2
- int y = (int) (valOne + valTwo);
- y = (int)(8.5)
- So the answer is
B.
- int x = (int) valOne + (int) valTwo
-
while loop
- how n changes: 0 2 4 6 8
- increment n by 2 first, and then print n, so 2 4 6 8 10 is printed.
- So the anwser is
D.
-
if...else
-
Nested loops
-
the outer loop controls rows: j is 0 to 3, a total of 4 rows.
-
the inner loop controls columns in each row: k is 0 to 2
j/k 01200 0 0 10 1 2 20 2 4 30 3 6 -
So the answer is
B.
-
-
Ignored
-
boolean expression
- (x && y) || y
- when y is true, the expression evaluates to true
- when y is false the expression evaluates to false
- So the expression has the same result as y.
- So the answer is
B.
-
Traverse an ArrayList
- valid index of elements in an ArrayList: 0 to size-1
- to traverse the elements reversely:
- int k = names.size() - 1; k >= 0; k--
- so the answer is
D.
-
Recursion
- locate where recursion
- in else,
return calc(n - 1) + n;
- in else,
- how n changes: n, n-1, n-2, ...1, 0
- what the recursion does
- call cal(n-1) and add the current n to the result to return.
- in summary, the recursion implements 0 + 1 + 2 + ... + n-1 + n
- so calc(4) returns 0+1+2+3+4, which is 10, so the answer is
E.
- locate where recursion
-
2D Array
- For a 2D array like int[][] rectangle = new int[4] [5];
- use two indexes to access one element: rectangle[row][col]
- use a row indes to access one row: rectangle[row]
- so x = rectangle[0] assigns the first row to x, x is of type int[].
- so the answer is
A.
- For a 2D array like int[][] rectangle = new int[4] [5];
-
for loop: you may try each option
- option A: v = v * 0, v is 0, leading to infinite loop
- option B: v = v * 1, v is always 1, leading to infinite loop
- option C: v = v * 2, t = 1 + 2 + 4 + 8 + 16, which is 31
- option D: v = v * 3, t = 1 + 3 + 9, which is 13
- option E: v = v * 4, t = 1 + 4 + 16, which is 21
- so the answer is
D.
-
1D array
- III checkes whether the lengths of the two arrays are the same or not, and also whether the elements in the same position are the same or not.
- in I: arr1 == arr2 checkes if the two arrays represent the same array, but they do not refer to the same array.
- in II: it returns false only when the lengths of the two arrays are different, but would not return false if the elements at the same position are not equal.
- so the answer is
C.
-
while loops
- c++ when x - y >=0
- in the while loop body, x is decremented by y each time.
- How x changes: 102,
102 - 1*5, 102 - 2*5, ..., 102 - 20*5. - so x - y in the loop body is executed 20 times, the answer is
C.
-
static and non-static
- a non-static variable in a class is an instance variable, and each object of the class has its own value for an instance variable.
- a static variable in a class is a class variable, and all the objects of the class share the same value for a class variable.
- Analyze how the values of variables change:
- initial value of maxInitialOrder is 0
- initial value of numOfItems for each object is 0 by default
- Container c1 = new Container(10); set numOfItems of c1 object to 10, and update maxInitialOrder to 10.
- Container c2 = new Container(6); set numOfItems of c2 object to 6, since 6 is less than maxInitialOrder(10), so maxInitialOrder remains unchanged.
- c1.addItems(2) adds 2 to numOfItems of c1, therefore, c1.numOfItems = 10 + 2, which is 12.
- c2.addItems(4) adds 4 to numOfItems of c2, therefore, c2.numOfItems = 6 + 4, which is 10.
- c2.calcShippingCost() checkes if c2.numOfItems == maxInitialOrder, which is true, so 0.0 is returned.
- so the answer is
A.
-
Compound Boolean Expression(复合布尔表达式): which uses &&, || or !
- in code if (alpha(w) > 0 || beta(w) < 0)
- alpha(w), that is alpha(1)
prints 2(x = x * 2 = 1 * 2). - alpha(2) returns 3, 3 > 0 is true, so beta(w) < 0 is not executed.
- in code System.out.print(alpha(w)), alpha(w)
prints 2, and then alpha(2) returns 3, then System.out.print(3), so3 is printed. - so 223 is printed.
- alpha(w), that is alpha(1)
- Note:
- in a && b, if a is false, b is not evaluated.
- in a || b, if a is true, b is not evaluated.
- So the answer is
A.
- in code if (alpha(w) > 0 || beta(w) < 0)
-
String Manipulation
- Note: Precondition: a.length() == 1; b.length() == 1, a and b contain one character only.
- int position = orig.indexOf(a), position is the first occurrence of a.
- orig = orig.substring(0, position) + b + orig.substring(position + 1), this code retrieves the part before a(part1), the part after a(part2), and then concatenate part1, b, part2.
- position = orig.indexOf(a); position is the first occurrence of a in the updated orig.
- option E does not work as intended since it replaces C with C, which leads to infinite loop.
- For the first round, in CCD, position is 0, the first C is replaced with C, the result is again CCD.
- For the second round, position is still 0, it again replaces the first C with C, as a result, the loop never terminates.
- so the answer is
E.
-
Compound Boolean Expression(复合布尔表达式): which uses &&, || or !
- number is positive, number > 0
- number is between -10 and -5, -10 <= number && number <= -5
- number is positive
oris between -10 and -5, use || to combine the conditions. - So the answer is
D.
-
Recursion
- locate where recursion is: in else, return doSomething(current - 2) + current;
- how current changes: 10, 8, 6, 4, 2, 0(recursion ends)
- what the recursion does: append the current 'current' to the
endof the value to return. - so the return value is "2" + "4" + "6" + "8" + "10", which is 246810
- Note: when current is 0, else will not be executed, so 0 is not included in the final result.
- so the answer is
B.
-
Traverse 2D arrays
- outer loop traverse rows: for (int a = 0; a < table.length; a++)
- inner loop traverse columns in each row: for (int b = 0; b < table[0].length; b++)
- 'a' is the row index, while 'b' is the column index
- System.out.print(table[b][a] + " ") uses b as the row index, and a as the column index, which is wrong.
- As a result, if the array has more columns than rows, it throws an ArrayIndexOutOfBoundsException.
- So the answer is
E.
-
1D arrays
-
You may use a trace table to help analysis.
-
int[] items = {10, 20, 30, 40, 50, 60};
kitems[k-1]sumitems6items[5] is 60 60 10 20 30 40 50 50 5items[4] is 50 110 10 20 30 40 40 50 4items[3] is 40 150 10 20 30 30 40 50
-
-
for loops
- when start and end are both odd
- old code prints all the odd numbers between start and end.
- new code prints one less odd number than the old code.
- when start and end are both even
- old code prints all the even numbers between start and end.
- new code prints one less even number than the old code.
- when start is odd, and end is even
- old code prints all the odd numbers between start and end-1.
- new code prints all the odd numbers between start and end-1.
- when start is even, and end is odd
- old code prints all the even numbers between start and end-1.
- new code prints all the even numbers between start and end-1.
- so the answer is
C.
- when start and end are both odd
-
Nested loops
- the outer loop executes lim times.
- the inner loop executes lim times.
- so the entir nested loop executes lim * lim times.
- so, when lim is k, it executes k * k times.
- so the answer is
E.
-
Math.random
- code segment I: generates a new random integer value of 0, 1, 2, with each used to represent one case (red, blue or green).
- code segment II: uses the double value generated, which is >=0 and <1, with randNum < 1/3.0 for one case, 1/3.0 <= randNum < 2/3.0 for another, and 2/3.0 <= randNum < 1 for the third case.
- code segment III: the first if is of no use, since the second if...else will update result to either blue or green, covering only two cases.
- so the answer is
B.
-
Selection sort
- the outer loop for(int j = 0; j < elements.size() - 1; j++) traverses the first to the second last elements in the list.
- the inner loop for(int k = j + 1; k < elements.size(); k++) traverses all the elements after j to find the minimum element after j.
- then it swaps the elements at j and index (the mininum after j).
- In-step analysis: [2, 3, 1, 5, 6, 1]
- 1st round: swap the element at 0 (value:2) with the element at 2 (value:1), you get [1, 3, 2, 5, 6, 1].
- 2nd round: swap the element at 1 (value:3) with the element at 5 (value:1), you get [1, 1, 2, 5, 6, 3].
- 3rd round: since there is no value less than the element at 2 (value:2), so no swapping.
- As a result, after 3 passes, you get [1, 1, 2, 5, 6, 3].
- So the answer is
C.
-
Selection sort
- in the code: if (elements.get(k) < elements.get(index)) tries to find a lessser element.
- so change the < to >, you get if (elements.get(k) > elements.get(index)), which tries to find a greater element.
- so the answer is
E.
-
Ignored.
-
Arithmetic operations
- double a = r % t;
- r % t is 23 % 10, which is 3.
- assign 3 to a, int 3 is converted to double 3 automatically, so a is 3.0.
- doubleb = r / t;
- r / t is 23 / 10, which is 2.
- assign 2 to b, int 2 is converted to double 2 automatically, so b is 2.0.
- so the answer is
C.
- double a = r % t;
-
Nested loops
- how the outer j changes: 0 1 2 3
- what is printed
(0,1) (0,2) (0,3)
(1,2) (1,3)
(2,3) - j,k is printed each time
- Analysis on how k changes with j
- when j is 0, k are 1,2,3
- when j is 1, k are 2,3
- when j is 2, k is 3
- How k changes: k = j+1, and k <4
- so the answer is
E.
-
1D arrays
- valid index of an 1D array: 0 to length-1.
- to check if an index is out of range, check the largest index.
- As to the loop given: for (int k = 0; k < arr.length; k++), the largest value of k is length-1, when k is length-1, the k + 1 used in arr[k + 1]) is invalid, leading to ArrayIndexOutOfBoundsException.
- so the answer is E.
-
String manipulation.
- for a question like this, you may try each code segment given using a specific example, such as the example given in the question.
- suppose str is "wagon", len is 5, we want to get "nagow".
- As to code segment I:
- result = str.substring(len - 1), substring(4) gets the last character "n" in "wagon", result is "n".
- result += str.substring(1, len - 1), substring(1, 4) gets the 3 characters "ago" in "wagon" , result = result + "ago", result is "nago".
- result += str.substring(0, 1), substring(0,1) gets the first character "w" in "wagon", result = result + "w", result is "nagow".
- so code segment I works as intended.
- As to code segment II:
- result = str.substring(len - 1), result is substring(4), which is "n";
- result += str.substring(1), substring(1) is "agon", result = result + "agon", so result is "nagon".
- result += str.substring(0, 1), substring(0,1) is "w", result = result + "w", which is "nagonw".
- so code segment II does not work as intended.
- As to code segment III:
- result = str.substring(0, len - 1), substring(0, 4) gets "wago", so result is "wago".
- result += str.substring(0, 1), substring(0,1) is "w", so result is "wagow".
- result =
str.substring(len - 1) +result.substring(1), str.substring(len-1) is "n", result.substring(1) is "agow", result = "n" + "agow", which is "nagow". - so code segment III works as intended.
- so the answer is
C.
-
Implement 1D array algorithms: reverse elements
- to reverse the elements in one 1D array, you need to swap the first element with the last, and the second with the second last,....
- valid index of elements in one 1D array: 0 to length-1
- swap 0 with length-1
- swap 1 with length-2
- swap 2 with length-3
- ...
- so the sum of the indexes of two elements to swap is length - 1
so for index i, the index of the element to swap is length - 1 - i.
- to reverse the elements, you only need to access the first half of the elements, and find the corresponding elements to swap in the second half, so the loop condition should be
k < values.length/2. - so the answer is
C.
-
Traverse 2D arrays
- as to the code for (int[] d: data), data is the name of a 2D array, d represents each row in the 2D array.
- result1 = true when isSpecial(d) is true, meaning that if any one row is special, then result1 is set to true.
- as to the code for (int n : d), d represents each row, n represents an element in the current row.
- result2 = result2 && isIncredible(n), here && is used, so result2 is true when result2 is true, and each call to isIncredible(n) is true.
- so result1 is true if any one row is special.
- and result2 is true only when all the elements are incredible.
- so the answer is
E.
-
getters: both setters and getters are
public- a getter method for an instance variable has the following features:
- with a name of getXxx
- has no parameters
- has a return type same as its instance variable
- returns the instance variable as the return value
- a setter method for an instance variable has the following features:
- with a name of setXxx
- has a parameter with the same type as its instance variable
- has a return type of void
- sets the instance variable to a new value using the parameter.
- so the answer is
B.
- a getter method for an instance variable has the following features:
-
2D arrays
- when row is even, col starts at 1
- when row is odd, col starts at 0
- In summary, col will be either 1 or 0
- only option E (row + 1) % 2 will achieve the purpose.
- As to option D, (row % 2) + 1:
- (row % 2) + 1 is 1 when row is even
- (row % 2) + 1 is 2 when row is odd
- so the answer is
E.
-
Recursion
- locate where the recursion is: in else, result =
processWords(words, index + 1) + words[index] - words.length is 5
- how index changes: 2 -> 3 -> 4 -> 5 (end recursion)
- what the recursion does: append the current element to the end of result.
- so, result = "" + words[4] + words[3] + words[2], which is "CarHourseGorilla".
- Note: the words[4] is before words[3], and words[3] is before words[2].
- so the answer is
C.
- locate where the recursion is: in else, result =
-
setters: both setters and getters are
public- a setter method for an instance variable has the following features:
- with a name of setXxx
- has a parameter with the same type as its instance variable
- has a return type of void
- sets the instance variable to a new value using the parameter.
- in this example, p is a parameter used to assign a new value to instance variable price.
- so the answer is
A.
- a setter method for an instance variable has the following features:
-
Methods
- the code segments given are used to update the value of variable word.
- Analysis:
- WordPlay wp = new WordPlay("bandana") sets the value of 'word' to "bandana".
- wp.update("d", "tie") updates 'word' using code word = word.indexOf(first) + second, in which, word.indexOf("d") is 3, second is "tie", so word is set to "3tie".
- wp.update("scarf") updates 'word' using code word = first + word, in which first is "scarf" and word is "3tie".
- so finally word is "scarf3tie".
- so the answer is
A.