midterm-sample-1

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

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

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

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

View Full Document
2. ArrayList Programming . Write a method stretch that accepts an ArrayList of strings and an integer "stretch factor" k as parameters and that replaces each element with k copies of that element. For example, if a variable called list stores the elements ["hi", "how are", "you?"] , then the call of stretch(list, 3); would change list to store ["hi", "hi", "hi", "how are", "how are", "how are", "you?", "you?", "you?"] . If a stretch factor of 0 or less is passed, the list is made empty. If an empty list is passed in, regardless of the stretch factor, 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.
3. Stack and Queue Programming . Write a method compressDuplicates that accepts a stack of integers as a parameter and that replaces each sequence of duplicates with a pair of values: a count of the number of duplicates, followed by the actual duplicated number. For example, suppose a variable called s stores the following sequence of values (duplicates underlined): bottom [2, 2, 2, 2, 2 , -5, -5 , 3, 3, 3, 3 , 4, 4 , 1, 0, 17, 17 ] top and we make the call of compressDuplicates(s); , after the call s should store the following values: bottom [5, 2, 2, -5, 4, 3, 2, 4, 1, 1, 1, 0, 2, 17] top This new stack indicates that the original had 5 occurrences of 2 at the bottom of the stack followed by 2 occurrences of -5 followed by 4 occurrences of 3, and so on. This process works best when there are many duplicates in a row. For example, if the stack instead had stored: bottom [10, 20, 10, 20, 20 , 10] top Then the resulting stack after the call ends up being longer than the original: bottom [1, 10, 1, 20, 1, 10, 2, 20, 1, 10] top If the stack is empty, your method should not change it. You may use one queue 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 stack. Use the Queue interface and Stack / LinkedList classes discussed in lecture. You have access to the following two methods and may call them as needed to help you solve the problem:

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

View Full Document
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-1 - CSE 143 Sample Midterm Exam #1 1....

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

View Full Document
Ask a homework question - tutors are online