Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Comp 110 Sample Final Exam

Putnam

NAME: ___________________________________________

Answer all questions on the test paper. This is a closed book exam. Show all of
your work. No calculators or any other electronic devices may be used during
the test. The test is 120 minutes long.

1. (20 pts) Write a complete java program that inputs four numbers x1, y1, x2, y2
and computes

The program should print the values of x1, y1, x2, y2 and the computed square
root.

2. (20 pts) Write a method


public static boolean isPrime(int k)
that returns true if k is a prime number and false if not.

Remark: An integer k is prime if k>=2 and k is divisible only by itself and 1

3. (15 pts) Determine the results of the evaluation of the following expressions.
Show how Java parenthesizes the expression, show the value of the
expression and show the resulting data type. Assume that x, y and z are type
double and that x = 2.0, y = 33.0 and z = 5.0. Assume that a, b and c are type
int and that a = 2, b = 2, c = 5.

Expression Fully Parenthesized Value Data


Expression Type
x+y/2*a

(int)(56.78*100)/100.0

( c == 5) || ( a > 2 ) && ( b
!=2)

45 + 43 % 5 * (23 * 3 % 2)
4. Given the following program:
boolean p, q, r;
A;
if (p)
{
B;
if (q) C;
else if (r) D;
if (p && q) E;
}
else if (q && r)
{
F;
if (!p) G;
if (!q) H;
}
if (r)
{
J;
if (p II q) K;
}

a. Which statements execute for p=true, q=true, r=true?

b. Which statements execute for p=false, q=true, r=true?

c. Which statements execute for p=false, q=false, r=true?

d. Which statements execute for p=false, q=false, r=false?

5. List the various ways that an abstract class can be created.


6. (25 pts) Write the constructor and the methods for the Time class defined
below. The Time object holds a 12 hour clock time. For example the Time
object represents 7:25 a.m. by setting hour =7, minute = 25, and am_pm =
"am" . The Time object represents 10:45 p.m. by setting hour = 10, minute = 45
and am_pm = "pm". The constraints are 1 <= hour <= 12 & 0<= minute < 60 &
am_pm = "am" or "pm". The constructor must check these constraints. If any
of them are not met, then set the data fields to correspond to midnight; i.e.,
hour = 12, minute = 0, am_pm = "am".

The public void addOneMinute() method adds one minute to the time while
maintaining the constraints. Note that adding one minute to 10:59 a.m. is not
10:60 a.m. it is 11:00 a.m. Adding one minute to 11:59 a.m. is 12:00 p.m, not
12:00 a.m.

Write your code in the framework provided below!

public class Time


{
private int hour; // 1 <= hour <= 12
private int minute; //0 <= min < 59
private String am_pm ; // Equals "am" or "pm"

public Time( int h, int m , String ampm)


{

public String toString()


{
// returns string like 12:45 am

}
public void addOneMinute()
{

public int getHour()


{

public int getMinute()


{

public String getAm_pm()


{

You might also like