This section will involve writing code on a computer, not necessarily
your own. Instructions about the computer
will be provided at the exam.
- Make sure that each program you write has the correct
name as specified in each question.
- You can ask for blank paper to sketch out solutions
but must return the paper with your NAME on it.
Exam questions
NOTE: the actual exam will have fewer questions. We are providing
additional questions to help you prepare by practicing.
Each programming problem below comes with code that performs
testing. To test whether your code is working, download
SampleExamTester.java. Then,
to test a particular problem, type the problem number after
SampleExamTester as in, for example:
java SampleExamTester 5
to test problem #5. You do not need to read or understand the code in
SampleExamTester.
Next, download
SampleExam.java
where you will see one method
per problem below. This is the file in which
where you implement your code. You are free to add additional
methods if you find them useful.
-
Array A is a list of revenues made on day 0, day 1 etc while
array B is a corresponding list of costs. Identify the day
on which maximum profit occured. Here is sample test code:
int[] A = {1,2,3,4};
int[] B = {1,2,1,3};
int a = SampleExam.problem1 (A,B);
// a == 2
int[] A2 = {4,2,3,9,8};
int[] B2 = {1,0,1,3,1};
int a2 = SampleExam.problem1 (A2,B2);
// a == 4
-
Given an array, find elements in the array that have larger elements
on either side. For example:
int[] A = {8,1,4,2,3,5,6};
int[] B = SampleExam.problem2 (A);
// Should return B = {1,2};
int[] A2 = {1,2,3,6,4,1,3};
int[] B2 = SampleExam.problem2 (A2);
// Should return B2 = {1};
-
Given a string, return true if the string has two consecutive
characters that are next to each other in the alphabet, like
"de" in "den" or "st" in "cast". For example:
String s = "den";
boolean a = SampleExam.problem3 (s);
// Should return true
String s2 = "cast";
boolean a2 = SampleExam.problem3 (s2);
// Should return true
String s3 = "base";
boolean a3 = SampleExam.problem3 (s3);
// Should return false
-
In this problem, you are given a char array and two integers.
You are to create and return a new char array with all the chars
from the original array in the range of positions represented
by those integers. For example:
char[] c = {'e','m','b','e','d','d','e','d'};
char[] d = SampleExam.problem4 (c, 2, 4);
// Should return {'b','e','d'};
char[] c2 = {'e','m','b','e','d'};
char[] d2 = SampleExam.problem4 (c2, 2, 4);
// Should return {'b','e','d'};
- In this problem, the input will be a number N. You
are to find the smallest number k (where k can be even or odd)
such that the sum of the first k odd numbers reaches or
crosses N. The test code for this is:
// Test 1
int s = SampleExam.problem5 (49);
System.out.println (s);
// Should print 7 (because 1+3+5+7+9+11+13 = 49)
// Test 2
s = SampleExam.problem5 (10000);
System.out.println (s);
// Should print 100
- In this problem you are given an array of integers.
Some may be positive while others may be negative. You are
to determine if the array has the property that: all the
negative numbers occur before any positive number. That is,
there should be no negative number that follows a positive number.
For example:
double[] A = {1,2,3};
boolean a = SampleExam.problem6 (A);
// Should return true
double[] A2 = {-1,-2,-3,-4};
boolean a2 = SampleExam.problem6 (A2);
// Should return true
double[] A3 = {-1,-2,-3,4};
boolean a3 = SampleExam.problem6 (A3);
// Should return true
double[] A4 = {-1,2,-3,4};
boolean a4 = SampleExam.problem6 (A4);
// Should return false
-
In this problem, the input consists of two char arrays. The goal
is to see whether the first char array is contained in the second,
as in these examples:
char[] A = {'c', 'a', 't'};
char[] B = {'s', 'c', 'a', 't'};
boolean c = SampleExam.problem7 (A, B);
// Should return true.
char[] A2 = {'b', 'a', 't'};
char[] B2 = {'a', 'b', 'a', 't', 'e'};
c = SampleExam.problem7 (A2, B2);
// Should return true.
char[] A3 = {'b','a', 't'};
char[] B3 = {'t', 'a', 'b'};
c = SampleExam.problem7 (A3, B3);
// Should return false
- In this problem, you are given two integer arrays.
Each array is assumed to have unique elements (no duplicates within
an array). However, the two arrays may have common elements. The
goal is to return a new array that contains all the unique elements
in A and B combined. Another way to think about it: the new array
will have all the elements of A followed by those elements of B that
are not in A. The elements in the final array need to occur
in the same order as they occur in A and B.
Also: your code must use only arrays (and no other
data structure).
Here's some sample test code:
// Test 1:
int[] A = {1,3,5,9,7};
int[] B = {2,3,4,5};
int[] C = SampleExam.problem8 (A, B);
// Should return {1,3,5,9,7,2,4};
// Test 2:
int[] A2 = {3,5,7};
int[] B2 = {1,2,3,5,6,7};
int[] C2 = SampleExam.problem8 (A2, B2);
// Should return {3,5,7,1,2,6};
-
The input to this problem is a char array. You are to return
a new array with the same chars as in the input array but
with all the vowels before all the consonants. The order
amongst the vowels needs to be the same as in the input array.
The same property needs to be true for the consonants.
Also: your code must use only arrays (and no other
data structure).
Here is sample test code:
char[] A = {'f','a','c','e'};
char[] B = SampleExam.problem9 (A);
// Should return {'a','e','f','c'};
char[] A2 = {'m','o','o','n'};
char[] B2 = SampleExam.problem9 (A2);
// Should return {'o','o','m','n'};
-
The input to this problem is an array of integers that may
have some negative numbers. The goal is to return a new array
without the negative numbers. Your code must use only arrays
(and no other data structure). Here is some test code:
// Test 1:
int[] A = {1, -1, -2, 3, -4};
int[] B = SampleExam.problem10 (A);
// Should return {1,3};
check ("Test 1: ", equalContents(B,C));
// Test 2:
int[] A2 = {0, 1, 2, -3};
int[] B2 = SampleExam.problem10 (A2);
// Should return {0,1,2};