home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
exam1-study-questions
exam1 study sheet
2007.Fall
Exam1 is scheduled for class on Feb.15 (Fri).
The exam will be in-class, on-paper, closed-computer.
Study Sheet --
you can bring one page of notes to the exam:
hand-written (no machine reproduction in any way),
single-sided,
8.5″x11″.
The exam will consist of questions very much like
the homeworks, and labs,
and
the questions here.
Some of the questions may be directly from these sources.
The single best way to study for the exam is to sit down and
re-work homework and lab questions from scratch.
You should know the syntax for
declaring classes,
writing methods (including constructors),
calling methods (including constructors),
creating instances (with new),
declaring local variables,
initializing local variables,
and
declaring+initializing named constants.
You will need to know the following types:
int, double,
boolean,
String;
the keywords
class,
return,
if-else,
if-else-if,
this,
and the javadoc keywords @return and @param.
(And, maybe others I forgot to list here.)
You will need to know about Java's arithmetic
(precedence of *,/
over +,-,
integer division (quotient) vs. floating-point division,
remainder (%),
and the potential of round-off error).
You will need to know
the logic operators
!, &&, ||.
When giving a java expression or definition,
you should be able to type it into BlueJ's Code Panel without any error;
this means knowing where you need parentheses, where you need squirly-braces,
and where you need semicolons (though I'll give some slack on these).
I particularly recommend the problems which require actual code
or definitions.
- Review the lecture notes, and write for yourself any
of the sample programs/exercises therein.
- Review the labs, and write (from scratch) any of those programs again.
- Review the homeworks, and write (from scratch) any of those programs again.
- What is the mantra for calling a function?
- What is the mantra for a function's signature?
- (2pts)
Give the signature of a function balanceOf
which takes in a String (a student's name),
and returns an int (their RU Express account balance, in cents).
- (3pts)
Give the signature of a function which takes in a String
and an int, and returns a double.
(Use whatever names you like.)
- (2pts)
The function upcase takes a String, and
returns that same text but in uppercase.
Write the signature for upcase.
- (2pts)
Suppose Widget were a Java type.
What would be the signature for
a function widgetRating which took in a Widget
and returned an int?
- (2pts)
In Java, what two functions are each named +?
(give the English word(s) for them.)
- (2pts)
In Java, what two functions are each named /?
(give the English word(s) for them.)
1
- (2pts)
Trick question:
In Java, what does the function2
named “+” do?
- (2pts)
Trick question:
In Java, what does the function named “/” do?
- (1pt)
Consider the Java expression 33 + " blind mice".
Which one of the following is true?
- This expression is illegal in Java, and won't compile (it will generate
a syntax error),
since you can't add an int
to an String.
- The expression will compile without syntax errors, but will generate
an error (“exception”) when the program is run
and the expression is evaluated,
since you can't add an int
to an String.
- Java first calls a function to convert 33 into a string,
and concatentates the result with " blind mice".
- Java first converts " blind mice" into a number,
and adds the result to 33.
-
(1pt)
True or False:
Java has two ways to include comments:
(A) From // to the end of a line, and
(B) from /* to the next */
(which can span multiple lines).
(You can write a small program to try this out, if you're not sure.)
- (1pt)
What is wrong with the following excerpt from a javadoc comment?
/** ...
* @param The diameter of the pizza, in inches.
* ...
*/
|
- (1pt)
Is the following a valid series of Java statements?
double ROOM_LENGTH = 10; // Height in feet.
double ROOM_WIDTH = 2 * ROOM_LENGTH; // In feet.
double ROOM_HEIGHT = ROOM_LENGTH * Math.sqrt(5) / 2.0; // In feet.
|
- (1pts)
What is the result of Java evaluating 25/10?
- (1pts)
What is the result of Java evaluating 25/10*10?
- (2pts)
Insert parentheses into the preceding expression, so that the result is 0.
- (1pts)
Why is it not obvious that
Java evaluates 25.0/10.0*10.0 to be 25.0?
- (2pts)
Rewrite the following expression for clarity (without changing the result):
2*3+16/2/2*3-1
(You might want to type in the before- and after- versions into Code Pad,
to make sure they agree.)
- (6pts)
Naming conventions.
Match each of the following with its naming convention (below):
-
function names
-
local variable names
-
parameter names
-
primitive types (that is, types built-in to Java, not a class)
-
class names (that is, non-primitive types :-)
-
named constants
with one of:
-
all capital letters (and underscores) (e.g. MINS_PER_GIG)
-
capitalize only the first letter of each (sub)word, including the first letter
(e.g. PizzaServer)
-
capitalize only the first letter of each (sub)word, not including the first letter
(e.g. numPetsInStock and diam)
- (1pt)
Show an integer-arithmetic expression where Java gives an incorrect answer.
- (1pt)
Show an floating-point (double) arithmetic expression
where Java gives an incorrect answer.
- (2pts)
[Challenge]
Which one of the following primitive types is archaic,
and should be rarely used?
- int
- long
- double
- float
- (2pts)
[Challenge:]
What is a circumstance in which you might use the numeric type
short, even though it can't represent numbers bigger
than 32,000 (approx)?
- (1pt each)
For each of the following, name the Java type best suited to represent it:
- The price of a gallon of gas, in dollars.
- The price of bananas per pound, in cents.
- A person's height.
- A student's name.
- Whether or not that student is on the President's honor roll.
- The number of classes a student is taking this semester.
- A person's zip-code.
- A number-of-stars entered by a user when rating a DVD (say, on amazon or Netflix or imdb.com or rottentomatoes.com).
- Whether or not a student has finished their Gen Ed requirement.
- (2pts each)
For each datum in the previous question,
declare a Java variable (or parameter)
to represent it and initialize it to a plausible value.
Use a descriptive name, and
(as necessary) include a short comment (to describe units, or further info)
For example, to represent
“the current speed of a horse-and-buggy”,
one answer
would be:
double buggySpeed = 0.0; // In leagues-per-fortnight.
|
- (1pt)
What is a(nother) real-world value which is best modeled in Java with an int?
- (1pt)
What is a(nother) real-world value which is best modeled in Java with a double?
- (1pt)
What is a(nother) real-world value which is best modeled in Java with a String?
- (2pts)
Assuming Widget were a java type,
how would you declare (and initialize) a variable of type Widget?
(Since we're not sure what an initial value for a Widget might be,
just write “...” instead
of an initial value.
Don't forget any semicolons!)
-
Suppose we are starting to think about writing a function which
takes two integers, and calculates their average.
- (1pt)
What is a good name for this function?
- (3pts)
What are a couple of test cases?
- (2pts)
Write the signature3 for this function.
- (2pts)
Write the entire function.
-
Write a function myAbs, which takes in a number and
returns its absolute value -- that is, it returns a number which is positive,
regardless of the sign of the input.
- (3pts)
Consider the legal Java expression
22*TAX_RATE-4*3/2+1
-
Rewrite the expression for clarity by adding spaces and parentheses only
(without changing its value, or doing any arithmetic yourself).
-
What does the expression evaluate to,
if we have declared the named-constant
double TAX_RATE = 0.50; /* That's a hefty tax rate! */
-
What does the expression evaluate to,
if we had instead declared
double TAX_RATE = 50/100; /* Is this tax rate really so bad? */
(Hint: Java doesn't give the same answer for (b) and (c)!)
- (2pt each)
You have landed a job operating the scoreboard for a football team.
For each of the following,
declare a variable of the appropriate type.
It should be appropriately named, and initialized to some reasonable value.
- The name of the team.
- The team's score during a game.
- The team's average yards-gained-per-play, this season.
-
Sometimes coaches need help in deciding what to shout at their team,
during a game.
Of course, what they should shout depends on many points ahead (or behind) their
team is.
- (2pts)
Write the signature (and only the signature)
of a function which
takes in one int (representing
how much the team is ahead by --
that is, the team's current score minus their opponent's score),
and returns a String (something the coach might
shout out, based on that score).
Use appropriate names.
-
(3pts)
Write (at least) three test cases for the function in the previous problem.
Be sure to indicate both what the input is, and an plausible output.
-
Write the body for the above function.
- (4pts)
The coaching-comments program for which you wrote the signature above has
been completed by other programmers; it works well, and your boss is happy
with it.
But it's admittedly a bit annoying,
to force whoever is calling that function to have to
compute the difference between the scores.
Write a second function which takes two inputs--the score
of the coach's team and the score of the opposing team--and returns a message
for the coach to shout.
(Write the entire function, although one test case will suffice.)
Hint: re-use existing code, rather than repeat any code!
-
How is Java's “=” pronounced?
What is java for “equals” (with respect to numbers and booleans,
at least)?
-
What type of statement is double myNumber; (according to
our syntax)?
What type of statement is myNumber = 17;?
-
To give a name to a sub-computation,
declare and initalize .
-
Unlike a local variable,
a named constant does not depend on
.
-
Write a function...
-
feetAndInchesToMeters:
If somebody is 6′4½″ tall, they are 1.9431m tall.
How many inputs to this function?
(Remember, there are 2.54cm/in.)
-
lbsToKg: If somebody weights 220lbs, they weigh 100kg.
-
bodyMassIndex:
Somebody who weighs 220lbs and is 6′4½″ tall has
a body mass index of 26.5 (approx).
Write a function which, given a person's weight (in pounds) and height
(how many feet and how many inches), returns their body mass index.
The general formula for BMI is simple:
The person's weight in kg, divided
by their height in meters squared.4
-
bodyMassIndexType:
Given the same info as the previous problem,
return the person's weight category.
BMI interval | category |
[0,18) | underweight |
[18,25) | normal |
[25,30) | overweight |
[30,∞) | obese |
Note that the BMI scale has
its limitations.
-
What is the syntax of an assignment statement?
(Assigning to a local variable, or to a field)
-
Recall that in the EBay Fee problem from lab, calculating the final-value-fee
involved three different tiers: the interval [$0,$25),
[$25,$1000), and [$1000,∞).
What are good test cases, which test all the boundary conditions,
as well as pertinent non-boundary conditions?
1We are not talking about comments.
Comments certainly aren't function, and besides they need more than
a single slash.
↩
2Well technically,
+ is an “operator”, since we don't use parentheses
to call the function ↩
3The first problem describes what a function's signature is. ↩
4
This formula takes a person's weight, adjusting for how tall they are.
The squaring stems from the fact that if
person A is (say) 10% taller than person B, then A is also allowed to be 10% wider,
but not 10% thicker.
So you square the person's height, rather than cube it.
↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs