So Im in an AP computer Science A class. (AP Computer Science A is the official name from the college board) and my teacher handed us a coding packet, on papper. The idea was that we had to trace the code and apply our knowledge of java to explain what the hell happend. It took myself, who is a novice, a friend of mine, who is supper smart, and someone else who has coded for a long time to work this problem. Eventually we got it, but it took us a long time to figure it out. So now Im curious to see if you guys to figure it out. The rule is that you you can not use a program to figure out the output. You can only use your brain and something to keep track of the values, like paper. Code: public void mysteryMethod (int [] first, int [] second) { int smaller = 0; if (first.length < second.length) smaller = first.length; else smaller = second.length; int temp = 0; int pos = 0; for (int i = 0; i < smaller; i++) { if (first [i] < second [i] ) { temp = first [pos]; first [pos] = second [pos]; second [pos] = temp; pos++; } } } Given the Following declarations: int [] arrA = {5, 3, 6, 1, 2, 3, 12, 9, 7}; int [] arrB = {7, 9, 2, 11, 0, 4}; What do arrA and arrB hold after the following call: mysteryMethod (arrA, arrB)? So ya, have fun it took us a long time to figure it out.
First logic statement will be false So smaller = 6 Loop will run 6 times 0 true swap 5, 7 1 true swap 3, 9 2 false no swap 3 true swap 6, 2 4 false no swap 5 true swap 1, 11 Final answer: int [] arrA = {7, 9, 2, 11, 2, 3, 12, 9, 7}; int [] arrB = {5, 3, 6, 1, 0, 4}; It's only logic :p