Csci 102: Sample Exam
Duration: 65 minutes
Name:
NetID:
Student to your left: Student to your right:
DO NOT OPEN THIS EXAM UNTIL INSTRUCTED
Instructions:
• Write your full name and your NetID on the front of this exam.
• Make sure that your exam is not missing any sheets. There should be six (6) double sided pages in the exam.
• Write your answers in the space provided below each problem. If you make a mess, clearly indicate your final answer.
• Answer any five (5) problems from problems 1-6. Circle ”Do NOT Grade” next to ONE problem that should not be graded.
• Answer all multiple choice questions in problem 7.
• If you have any questions during the exam, raise your hand and we will try to get to you.
• At the end of the exam, there is one blank page. Use this as your scrap paper. If you need additional scrap paper, raise your hand and we will get it for you.
• This exam is closed books, closed notes, closed computers.
• You need to stay in your seat until the exam is finished. You should not leave the room even if you finish the exam. This distracts other students who are still
working.
Good luck!
CSCI-UA 102 Joanna Klukowska
Sample Exam 1 [email protected]
Problem 1 (14 points) . Do NOT Grade
A. Consider the following recursive method
1 public int recMethod ( int number ) {
2 if ( number <= 0 )
3 return 0;
4 if ( number % 2 == 0 )
5 return recMethod ( number - 1 );
6 else
7 return number + recMethod ( number - 1);
8 }
9
(a) How many times is this method called (including the initial call) when we run recMethod(10) ?
(b) How many times is this method called (including the initial call) when we run recMethod(-10) ?
(c) What does recMethod do (i.e. what does it compute)?
B. Write a recursive function that given a non-negative integer n, returns the count of the occurrences of 7 as a digit, so for example 717 yields 2.
Page 3
CSCI-UA 102 Joanna Klukowska
Sample Exam 1 [email protected]
Problem 2 (14 points) . Do NOT Grade
Write a recursive method that prints to the screen all binary sequences of a given length that do not start and do not end with a zero. Your answer should contain both a
wrapper public method and a private recursive method that performs the task. Your method should not be generating sequences that don’t need to be printed.
Page 4
CSCI-UA 102 Joanna Klukowska
Sample Exam 1 [email protected]
Problem 3 (14 points) . Do NOT Grade
Consider the following class definition
1 public class Foo implements Comparable<Foo>{
2
3 double x;
4 double y;
5
6 public Foo ( double x, double y ) {
7 this.x = x;
8 this.y = y;
9 }
10
11 public int compareTo ( Foo other ) {
12 double d1 = x*x + y*y;
13 double d2 = other.x * other.x + other.y * other.y;
14
15 return (d1 - d2);
16 }
17
18 public String toString ( ) {
19 return "( " + x + ", " + y + " )"; // r e t u r n s ( x, y )
20 }
21 }
A. Write a method that creates an ArrayList object and fills it with n objects of type Foo initialized with random values of x and y (you can pick the range of
values).
The value of n is specified as a parameter.
B. If you have the following array of Foo objects
0 1 2 3 4 5 6 7 8 9
x = 1.0 x = −2.0 x = 1.0 x = 1.0 x = 2.5 x = −1.0 x = 0.0 x = −1.0 x = 0.0 x = 0.0
y = 1.0 y = 2.0 y = 2.0 y = −1.0 y = 0.0 y = 0.0 y = 3.0 y = −4.0 y = 0.0 y = 1.5
show what the array will look like after it is passed as an argument to Arrays.sort(... ) method
Page 5
CSCI-UA 102 Joanna Klukowska
Sample Exam 1 [email protected]
Problem 4 (14 points) . Do NOT Grade
A. (8 points) Show the content of the, initially empty, stack of Character objects after each of the following operations.
Assume that the implementation is array based and follows the efficiency ideas that we discussed in class. Assume that the initial capacity of the array to store the
stack is equal to 5 and that its size is doubled whenever the array runs out of room. Initial non-existent locations are marked with X. In the consecutive rows you
should cross out all non-existent locations in the array.
operation 0 1 2 3 4 5 6 7 8 9
stack.push(’c’); c X X X X X
stack.push( ’s’ );
stack pop();
stack.push( ’A’ );
stack.push( ’w’ );
stack.push( ’1’ );
stack.push( ’p’ );
stack.peek();
stack.pop();
stack.push(’%’);
stack.pop();
stack.push(’x’);
stack.pop();
Page 6
CSCI-UA 102 Joanna Klukowska
Sample Exam 1 [email protected]
Problem 5 (14 points) . Do NOT Grade
A. Write a method that given a sorted ArrayList object of Java strings (objects of class String) removes all duplicates. Your method should modify the ArrayList
object passed to it. The method should return a boolean value indicating if the list was modified or not (true for ”has been modified”, false for ”has not been
modified”). For example, if the original list passed to your method contains the following strings:
Argentina, Chile, Chile, Czech Republic, France, Georgia, India, India, Poland, Romania, Romania
your method should remove one occurrence of Chile, India and Romania. The resulting list should contain:
Argentina, Chile, Czech Republic, France, Georgia, India, Poland, Romania
What is the performance of your method? Use Big-O notation.
B. Consider the MyArrayList class that extends the ArrayList class povided by Java API. Implement the missing find method. This method should return an
index of the first occurance of the string key in the list, or -1 if the key does not apprear in the list.
1 class MyArrayList extends ArrayList<String> {
2
3 public MyArrayList () {
4 super();
5 }
6
7 public int find (String key ) {
8 // TO DO : implement this method
9 }
10 }
Page 7
CSCI-UA 102 Joanna Klukowska
Sample Exam 1 [email protected]
Problem 6 (14 points) . Do NOT Grade
A. Write a method of a LinkedList class that computes and returns the sum of the values stored in the nodes. Assume that the node definition is as follows:
class Node {
int data;
Node next;
}
B. Consider an implementation of a reference based queue (not an array based queue). If the front of the queue is at the beginning of the list (that’s where the head
reference points to), describe an algorithm that should be used for adding an element to the back of the queue. You are working with a singly linked list and the
head reference is the only reference available. Be specific in your description.
What is the performance of this algorithm? Use Big-O notation.
Page 8
CSCI-UA 102 Joanna Klukowska
Sample Exam 1 [email protected]
Problem 7 ( 30 points) Multiple Choice Questions.
Answer the following multiple choice questions. Circle all applicable answers.
A. In an array based implementation of the Stack class, which operations require linear time for their worst-case behavior?
(a) peek
(b) pop
(c) push
(d) isEmpty
(e) none of the above
B. If the characters D, C, B, A are placed in a queue (in that order), and then removed one at a time, in what order will they be removed?
(a) ABCD
(b) DCBA
(c) DCAB
(d) ABDC
(e) none of the above
C. Consider an array-based implementation of a queue that we discussed in class (front is set to the index of first element, back is set to the index of the last
element). Suppose the current capacity is 10, front is 7 and back is 3. What is the value of the size variable?
(a) 4
(b) 5
(c) 6
(d) 7
(e) none of the above
(f) this cannot be determined from the given information
D. Fill in the code to complete the following method for checking whether a string is a palindrome (a palindrome is a string that reads the same forwards and
backwards).
1 public static boolean isPalindrome(String s) {
2 return isPalindrome(s, 0, s.length() - 1);
3 }
4
5 public static boolean isPalindrome(String s, int low, int high) {
6 if (high <= low)
7 // Base case
8 return true;
9 else if (s.charAt(low) != s.charAt(high))
10 // Base case
11 return false;
12 else
13 return _______________________________ ;
14 }
(a) isPalindrome(s)
(b) isPalindrome(s, low, high)
(c) isPalindrome(s, low + 1, high)
(d) isPalindrome(s, low + 1, high - 1)
(e) isPalindrome(s, low, high - 1)
Page 9
CSCI-UA 102 Joanna Klukowska
Sample Exam 1 [email protected]
E. A subclass inherits from its superclass.
(a) private methods
(b) protected methods
(c) public methods
(d) constructors
F. Suppose that Fruit}, Apple, Citrus, Lemon, Lime, Orange are classes defined in the following inheritance hierarchy.
Fruit
Apple Citrus
Lemon Lime Orange
Select all statements below that are valid.
(a) Fruit f = new Citrus();
(b) Fruit f = new Lime();
(c) Citrus c = new Fruit();
(d) Citrus c = new Orange();
(e) Apple a = new Citrus();
(f) Citrus c = new Citrus();
Page 10
CSCI-UA 102 Joanna Klukowska
Sample Exam 1 [email protected]
SCRAP PAPER NAME
Page 11
CSCI-UA 102 Joanna Klukowska
Sample Exam 1 [email protected]
SCRAP PAPER NAME
Page 12