CMPS 200 Exam 1 Sample, Fall 2012
American University of Beirut · Introduction to Programming · 1 page
Stuck on a question? A tutor can walk you through it.
More CMPS 200 files
Show the text of this exam
1866 Faculty of Arts & Sciences
Department of Computer Science
CMPS 200—Introduction to Programming
Sample Exam
Problem1 AlternatingSum
Write a program AlternatingSum. java that takes an integer N and a sequence of N doubles from the
command line. The program should compute and print the’ alternating sum of the ‘N doubles. The
alternating sum is obtained by alternately adding and subtracting the numbers in the sequence.
For example, the alternating sum of the following sequence of five numbers 4.2 3.8 7.8 3.5 1.9 is
5.8 because 4.2 - 3.8 + 7.0 - 3.5 +1.9 = 5.8.
Your program should output the alternating sum as a floating point number, followed by an integer
obtained by rounding the sum to the nearest whole number. For example,
> java AlternatingSum 5 4.2 3.8 7.0 3.5 1.9
Alternating sum is: 5.8 (Your output may contain extra decimal places)
Rounded alternating sum is: 6
Problem 2 SpecialStrings
Write a method with the following signature:
public static boolean sameChar (String str)
that returns true if its argument starts and ends with the same letter and false otherwise.
Use this method to write a program SpecialStrings. java that takes a positive integer N and N
strings as command line arguments and prints each string that starts and ends with the same letter.
> java SpecialStrings 3 test exam kayak
test kayak
Problem 3 TableRC
Write a program TableRC. java that takes three integers command line arguments (n,_r, c) and draws
an n-by-n table of x except for row with index r and column with index c that will contain o. For
example,
ava TableRC 6.2 3
x xX X
>J
xX X
x x
ne)
x x
x X
K
ooooo°o
x x KX O XK
x x xO x
aT KX K KO XK
produces a 6-by-6 table where the row with index 2 (third row) and column with index 3 (fourth column) contain o.
Submission Instructions
e Your exam submission must consist of single a zip archive that contains your properly commented three
source files (.java files) only. No other files will be accepted. We will compile and run your programs.
e The zipped file must adhere to the following naming convention s#_exam1_netid, where # and netid are
your section number and AUBnet id. Please make sure there are no folders or subfolders in your submitted zip
file. You will upload the single zip file to Moodle at the end of the exam.