home—info—lects—labs—exams—hws
D2L—tutor/PIs—zoom (snow day)
Object120 + its docs—java.lang docs—java.util docs
exam01-practice
This is a sample exam, based on a previous semester.
Although it represents most of the major topics we've covered,
it doesn't necessarily cover everything
that will be on this semester's exam.
Instructions:
- This exam is closed-book, 50-minutes.
- You may use any of the functions on the provided
documentation for Object120;
-
You may presume any code you write on this exam
is inside
class Exam01 extends Object120 { … }.
- (5pts)
What Java type is most appropriate,
to represent each of the following pieces of information —
boolean,
int,
double,
char,
or
String?
-
The price of some medicine, in dollars.
-
The price of some medicine, in cents.
- Instructions on how frequently to take some medicine (e.g. 3 times per day, once a month, before every meal, at age 5 only (like the Mumps vaccine), etc.).
-
A person's weight, in kg.
-
The weight of some medicine, in mg.
- (3pts)
How many implicit type-conversions are happening in the following code?
System.out.println( "Hi-ho " + Math.sqrt(16) )
Re-write the statement, making the conversions explicit
(using functions in Object120):
System.out.println( )
- (1pt) In Java, + can mean one of two different functions,
depending on context. What are they?
- (1pt) In Java, / can mean one of two different functions,
depending on context. What are they?
- (3pts)
What is the result of the following expressions, in Java?
(No calculator needed.)
Include a decimal point in your answer only if the result is a double.
Ignore any round-off error.
-
2+7/3*6+1
-
2.0+7/3*6.0+1
-
2.0+7.0/3.0*6.0+1
- (6pts)
What is the result of the following expressions, in Java?
Include quotation-marks in your answer only if the result is a String.
-
toUpperCase( "hee" + "haw" )
-
toUpperCase( "hee" ) + "haw"
-
toUpperCase( substring( "howdy", 1, 3) ) + "ch"
-
length(toUpperCase( substring("howdy", 1, 3) )) + "ch"
-
substring( "howdy", 1, length("folk")+1 )
-
substring( "howdy", 1, length("folk") ) + 1
- (3pts)
Browsing through the on-line Java documentation,
you find a class TimeZone,
you see the following:
static boolean |
inDaylightTime( String country, int year, int month, int day )
Determine whether the given date is in daylight savings time,
for the given country.
|
Call this function,
to find whether Peru is observing daylight savings time today.
-
Suppose there were a
function
isFrabjous
which
takes in an double and a String
(in that order),
and returns a boolean.
- (2pts)
Declare a variable which could hold the result of calling isFrabjous.
- (2pts)
Call isFrabjous, passing it
12.3 and
aloha
,
and store the result in the variable you declared in (a):
- (2pts)
Write the signature for isFrabjous.
-
- (1pt) To see if two Strings are the same,
use the function
.
- (1pt) To see if two chars are the same, use
.
- (1pt) When initializing a variable, use
between the variable-name and the expression.
- (1pt)
Write an expression determining whether
a (already-initialized) variable s
has the value "Wumpus".
-
We will write a function hasPrefix
which
is given two Strings,
and determines whether the first starts with the second.
For example,
catheter
has cat
as a prefix,
and also
catheter
has ca
as a prefix,
but catheter
does not have cathy
as a prefix.
- (4pts)
Complete the following with three plausible test cases
which are different from any of the ones suggested above:
static void testHasPrefix() {
System.out.println( "Actual : " + );
System.out.println( "Desired: " + );
System.out.println( "Actual : " + );
System.out.println( "Desired: " + );
System.out.println( "Actual : " + );
System.out.println( "Desired: " + );
}
|
- (3pts)
Complete the signature for hasPrefix in the space provided below.
- (4pts)
Complete the body for hasPrefix in the space provided below.
hint:
When calling substring,
be sure that the indices you provide aren't bigger than the length
of the string you're extracting the substring from!
// (b) Signature here
static ( ,
) {
// (c) Body here
}
|
-
You want to advertise your company,
and have purchased a list of a million names and addresses.
However, when you start to browse the list you see that some
names include a title (either
Mr.
or Dr.
,
nothing else),
and some don't.
You need to write a function which takes in the entire name
as one String,
and adds the title
Mr. if the name didn't already include
either Dr.
or Mr.
.
For example, here are some test cases your code must pass:
static void testAddTitleIfNeeded() {
System.out.println( "Actual : " + addTitleIfNeeded( "Mr. Oscar Grouch" ) );
System.out.println( "Desired: " + "Mr. Oscar Grouch" ); // no need to add title
System.out.println( "Actual : " + addTitleIfNeeded( "Dr. B. Burner" ) );
System.out.println( "Desired: " + "Dr. B. Burner" ); // no need to add title
System.out.println( "Actual : " + addTitleIfNeeded( "Big Bird" ) );
System.out.println( "Desired: " + "Mr. Big Bird" ); // NEED to add title
System.out.println( "Actual : " + addTitleIfNeeded( "T" ) );
System.out.println( "Desired: " + "Mr. T" ); // NEED to add title
}
|
Below, write the signature (2pts) and body (5pts) for addTitleIfNeeded.
Even if you didn't finish the previous problem,
you may assume that hasPrefix works correctly.
home—info—lects—labs—exams—hws
D2L—tutor/PIs—zoom (snow day)
Object120 + its docs—java.lang docs—java.util docs
 This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland radford.edu |
 |