midterm-sample-3 - CSE 143 Sample Midterm Exam#3(based on...

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 #3 (based on Winter 2009's midterm) 1. ArrayList Mystery . Consider the following method: public static void mystery3(ArrayList<Integer> list) { for (int i = list.size() - 1; i >= 0; i--) { if (i % 2 == 0) { list.add(list.get(i)); } else { list.add(0, list.get(i)); } } System.out.println(list); } Write the output produced by the method when passed each of the following ArrayList s: List Output (a) [10, 20, 30] ____________________________________ (b) [8, 2, 9, 7, 4] ____________________________________ (c) [-1, 3, 28, 17, 9, 33] ____________________________________
Background image of page 1

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

View Full Document Right Arrow Icon
2. ArrayList Programming . Write a method removeBadPairs that accepts an ArrayList of integers and removes any adjacent pair of integers in the list if the left element of the pair is larger than the right element of the pair. Every pair's left element is an even-numbered index in the list, and every pair's right element is an odd index in the list. For example, suppose a variable called list stores the following element values: [3, 7, 9, 2, 5, 5, 8, 5, 6, 3, 4, 7, 3, 1] . We can think of this list as a sequence of pairs: [3, 7 , 9, 2 , 5, 5 , 8, 5 , 6, 3 , 4, 7 , 3, 1 ] . The pairs 9-2, 8-5, 6-3, and 3- 1 are "bad" because the left element is larger than the right one, so these pairs should be removed. So the call of removeBadPairs(list); would change the list to store [3, 7, 5, 5, 4, 7] . If the list has an odd length, the last element is not part of a pair and is also considered "bad;" it should therefore be removed by your method. If an empty list is passed in, the list should still be empty at the end of the call. You may assume that the list passed is not null . You may not use any other arrays, lists, or other data structures to help you solve this problem, though you can create as many simple variables as you like.
Background image of page 2
3. Stack and Queue Programming . Write a method mirrorHalves that accepts a queue of integers as a parameter and replaces each half of that queue with itself plus a mirrored version of itself (the same elements in the opposite order). For example, suppose a variable q stores the following elements (each half is underlined for emphasis): front [10, 50, 19 , 54, 30, 67 ] back After a call of mirrorHalves(q); , the queue would store the following elements (new elements in bold): front [10, 50, 19, 19, 50, 10 , 54, 30, 67, 67, 30, 54 ] back If your method is passed an empty queue, the result should be an empty queue. If your method is passed a null queue or one whose size is not even, your method should throw an IllegalArgumentException . You may use one stack or one queue (but not both) as auxiliary storage to solve this problem. You may not use any other auxiliary data structures to solve this problem, although you can have as many simple variables as you like. You may not use recursion to solve this problem. For full credit your code must run in O( n ) time where n is the number of elements of the original queue. Use the Queue interface and
Background image of page 3

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

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

{[ snackBarMessage ]}

Page1 / 15

midterm-sample-3 - CSE 143 Sample Midterm Exam#3(based on...

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