You are on page 1of 6

NAME:____________________________ STUDENT ID# __________________________

(Write your name on THIS PAGE ONLY!!!) (Write your ID# on this and every page!!!)

COMMON FINAL EXAM,


CS-200, Programming I, Spring, 2016
Saturday, April 30th, 2016. 8:30 a.m. – 10:30 a.m.
Rooms LWH-1001, LWH-1002.

Instructions:

1. Do not turn this page until told to do so.

2. This exam is closed book and closed notes.

3. Write your STUDENT ID Number on every page. If you do not write your ID Number on a
certain page, you will not receive credit for the question on that page!

4. Do not write your name on any page (except this one). If you write your name on a certain page,
you will not receive credit for the question on that page!

5. There are five problems on the exam, one per page. Each problem will be graded as "pass" or "fail".
To pass this final exam, you must pass at least three out of the five questions. Once you start, verify
you have all 5 problems.

6. You must give your answer to a question on the same sheet of paper that the problem appears
on. If you run out of space on the front of the page, you may continue to the back of that page
only. If you put your answer on a different page, you will not receive credit for that problem!

7. For problems that ask you to write a method, you must use the given method header exactly as
shown, and you do not need to write the main( ) method.

8. For problems that ask you to write code, you should only write the method indicated in the
problem. You can assume the following import statement and keyboard declarations:

import java.util.Scanner;

Scanner keyboard = new Scanner (System.in);


or Scanner kbd = new Scanner (System.in);
or Scanner input = new Scanner (System.in);

9. You may use "SOP" as an abbreviation for "System.out.print" and "SOPln" for
"System.out.println".

10. You do not need to do any error checking of input values, unless the problem specifically asks
you to do so!

11. If you are caught looking at other papers or communicating with other students in any way, you will
receive an F for the course.
WRITE your STUDENT ID# here: ____________________________

Problem 1 Write the method public static int rangeProduct(int a, int b).
Assume that 𝑎 < 𝑏. Your method must compute and return the product of the integers in the range
from 𝑎 to 𝑏. For example, if 𝑎 = 3 and 𝑏 = 6, your method would compute and return the product
3 × 4 × 5 × 6 = 360.

public static int rangeProduct(int a, int b) {

CS-200, Common Final Exam, Spring, 2016, Page 2 of 6.


WRITE your STUDENT ID# here: ____________________________

Problem 2 Write a program that has the user enter three integers a, b, and c. The user may enter the same value
more than once. Print to the console the number of times the largest integer among a, b, and c was entered.
Here are some sample runs:

Enter a: 32 Enter a: 923 Enter a: 50


Enter b: 48 Enter b: 188 Enter b: 50
Enter c: 32 Enter c: 923 Enter c: 50

The largest integer 48 The largest integer 923 The largest integer 50
was entered once. was entered twice. was entered three times.

public static void main(String [] args) {

}
CS-200, Common Final Exam, Spring, 2016, Page 3 of 6.
WRITE your STUDENT ID# here: ____________________________

Problem 3 Write the method: public static String doubleVowels(String x). This method
takes a String x and returns a new String y which is identical to x except wherever there is a vowel in x, there will
be two of that same vowel in the returned String y. The original String x will contain only lower case letters.

For example, doubleVowels("easy") should return the String "eeaasy".


Another example: doubleVowels("abootstrap") should return the String "aabooootstraap".
Another example: doubleVowels("gggrrrhh") should return the String "gggrrrhh".

public static String doubleValue(String x) {

CS-200, Common Final Exam, Spring, 2016, Page 4 of 6.


WRITE your STUDENT ID# here: ____________________________

Problem 4 Write a program that has the user enter enter integers between 0 and 9999 until the user enters some
integer twice. Your program then prints to the console window how many distinct integers were entered. You
may assume all integers entered by the user are between 0 and 9999. Here are some sample runs:

Enter an int: 32 Enter an int: 32 Enter an int: 7


Enter an int: 32 Enter an int: 9285 Enter an int: 6
Enter an int: 575 Enter an int: 5
You entered 1 value. Enter an int: 7153 Enter an int: 1
Enter an int: 8 Enter an int: 4
Enter an int: 575 Enter an int: 3
Enter an int: 2
You entered 5 values. Enter an int: 1

You entered 7 values.

public static void main(String [] args) {

}
CS-200, Common Final Exam, Spring, 2016, Page 5 of 6.
WRITE your STUDENT ID# here: ____________________________

Problem 5 Write the method: public static int largestInCommon(int [] A, int [] B).

This method takes two arrays A and B, each containing positive integers only (no error checking necessary)
and returns the largest value that is common to both A and B. If there is no value that is contained in both
A and B, return the value –1. Here are some examples:

If A = {3, 8, 5, 2, 7, 9} and B = {5, 1, 22, 7, 2, 15, 3}, the return value would be 7.
If A = {35, 12, 19, 35, 45} and B = {55, 99, 12}, the return value would be 12.
If A = {33, 11, 77, 44, 55} and B = {99, 88, 222, 66, 1000}, the return value would be –1

public static int largestInCommon(int [] A, int [] B) {

}
CS-200, Common Final Exam, Spring, 2016, Page 6 of 6.

You might also like