midterm-sample-2 - CSE 143 Sample Midterm Exam #2 1....

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 143 Sample Midterm Exam #2 1. ArrayList Mystery . Consider the following method: public static void mystery2(ArrayList<Integer> list) { for (int i = list.size() - 1; i > 0; i--) { if (list.get(i) < list.get(i - 1)) { int element = list.get(i); list.remove(i); list.add(0, element); } } System.out.println(list); } Write the output produced by the method when passed each of the following ArrayList s: List Output (a) [2, 6, 1, 8] ____________________________________ (b) [30, 20, 10, 60, 50, 40] ____________________________________ (c) [-4, 16, 9, 1, 64, 25, 36, 4, 49] ____________________________________
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2. ArrayList Programming . Write a method isConsecutive that accepts an ArrayList of integers as a parameter and returns true if the list contains a sequence of consecutive integers and false otherwise. Consecutive integers are integers that come one after the other in ascending order, as in 5, 6, 7, 8, 9, etc. For example, if a variable called list stores the values [3, 4, 5, 6, 7, 8, 9] , then the call of list.isConsecutive() should return true . If the list instead stored [3, 4, 5, 6, 7, 12, 13] then the call should return false because the numbers 7 and 12 are not consecutive. The list [3, 2, 1] might seem to be consecutive, but the elements appear in reverse order, so the method would return false in that case. Any list with fewer than two values should be considered to be consecutive. You may assume that the list passed is not null .
Background image of page 2
3. Stack and Queue Programming . Write a method interleave that accepts a queue of integers as a parameter and rearranges the elements by alternating the elements from the first half of the queue with those from the second half of the queue. For example, suppose a variable q stores the following sequence of values: front [1, 2, 3, 4, 5 , 6, 7, 8, 9, 10 ] back and we make the call of interleave(q); , the queue should store the following values after the call: front [1 , 6, 2 , 7, 3 , 8, 4 , 9, 5 , 10] back To understand the result, consider the two halves of this list. The first half is [1, 2, 3, 4, 5] and the second half is [6, 7, 8, 9, 10] . These are combined in an alternating fashion to form a sequence of interleave pairs: the first values from each half (1 and 6), then the second values from each half (2 and 7), then the third values from each half (3 and 8), and so on. In each pair, the value from the first half appears before the value from the second half. The previous example uses sequential integers to make the interleaving more obvious, but the same process can be applied to any sequence of even length. For example, if q had instead stored these values: front [2, 8, -5, 19 , 7, 3, 24, 42] back Then the method would have rearranged the list to become: front [2 , 7, 8 , 3, -5 , 24, 19 , 42] back Your method should throw an IllegalArgumentException if the queue does not have even size. You may use one stack as auxiliary storage to solve this problem. You may not use any other auxiliary data
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 12/17/2011.

Page1 / 11

midterm-sample-2 - CSE 143 Sample Midterm Exam #2 1....

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online