1 Exceptions (Spring 2016 MT2 Q3)
Consider the code below. Recall that x / 2 rounds down to the nearest integer.
1 | public static void checkIfZero(int x) throws Exception { |
What will be the output when main is run?
mystery of 1 is 0
3
counter is 1
1
counter is 2
mystery of 6 is 2
2 AltList (Summer 2016 MT2 Q2)
A normal generic linked list contains objects of only one type. But we can imagine
a generic linked list where entries alternate between two types. AltList is an
implementation of such a data structure:
1 | public class AltList<X, Y> { |
Let’s construct an AltList instance:
1 | AltList<Integer, String> list = |
This list represents [5 cat 10 dog]. In this list, assuming indexing begins at 0, all even-index items are Integers and all odd-index items are Strings.
Write an instance method called pairsSwapped() for the AltList class that returns a copy of the original list, but with adjacent pairs swapped. Each item should only be swapped once. This method should be non-destructive: it should not modify the original AltList instance.
For example, calling list.pairsSwapped() should yield the list [cat 5 dog 10]. There were two swaps: ”cat” and 5 were swapped, then ”dog” and 10 were swapped. You may assume that the list on which pairsSwapped() is called has an even nonzero length. Your code should maintain this invariant.
1 | public class AltList<X, Y> { |
3 Every Kth Element (Fall 2014 MT1 Q5)
Fill in the next() method in the following class. Do not modify anything outside of next.
1 | import java.util.Iterator; |