home—info—labs—hws—exams
textbook—java.lang docs—java.util docs—archive
exam03-study-questions
exam03 study sheet
2008.Fall
The exam will be in-class, on-paper, closed-computer.
It will consist of questions like these questions,
the homeworks, and labs.
Some of the questions may be directly from those sources.
The single best way to study for the exam is to sit down and
re-work homework and lab questions from scratch.
For the class java.util.LinkedList<SomeType>,
you will need to know the methods
add(SomeType),
get(int),
and
size()
(but not remove(int) or remove(SomeType)).
You will need to know how to declare an array,
call an array constructor,
and fill an array with some value.
Here are some practice problems.
(This is far longer than an exam would be.)
You are encouraged to discuss these problems on
the Blackboard discussion boards.
-
Re-do programs from homework and lab.
Re-do questions from previous exams.
Some questions on the final might very similar
or even the same as such previous questions.
- What is the syntax for
calling a regular (non-static) method?
What about for a static method?
- What is the syntax for a method's signature?
What adjectives might come in front of the signature?
-
Make a class Treasure which has
a description (like "golden anvil" or "slightly-used gum")
and a weight (in kg).
Write a constructor, getters, and setters for this class.
Write a method boolean isHeavierThan( Treasure other ).
the solution key
-
The following statement doesn't make sense (and won't compile). Why not?
String m = System.out.println( "hi" );
-
What is the value of
new String("cat") + (newString("hay").equals( new String("cathay") ))?
(Pay attention to non-standard the parentheses.)
Confirm by typing this expression into Code Pad.
-
Most of the problems on
javabat.com
are good practice
(they tend to be a single method,
and test cases are provided for you!).
- Ignore the "recur1" and "recur2" sections.
-
Remember that
lists can do everything arrays can,
with different syntax.
- the Array2 section has some ungainly problems;
I would suggest skipping the questions of the form
“one number is adjacent to...”.
-
-
true or false?: In Java, Lists
can start empty, and have values added later.
-
true or false?: In Java, arrays can start empty, and have values added later.
-
true or false?: For any java type,
you can create a List of that type.
-
true or false?: For any java type,
you can create an array of that type.
-
Create linked lists and arrays to hold:
-
temperatures
-
GPAs
-
pizza toppings (Strings)
-
Names (as per hw10)
-
For the lists above, write while- and for-loops to:
-
Count the number of items
-
Sum the temperatures in a list
-
Compute the average GPA for a list of GPAs
-
Concatenate pizza toppings into a single string
-
Does a particular topping occur in the list?
-
Does the list contain a GPA above 3.9?
-
Count all the Names in a list
whose last-name is "Smith"
(using the class from hw10).
-
Skim the following classes,
then answer the following questions.
import java.util.LinkedList;
class Country {
private String name;
private LinkedList<State> states;
// public Getters omitted.
}
class State {
private private String name;
private private LinkedList<University> colleges;
// public Getters omitted.
}
class University {
private String name;
private LinkedList<Student> roster;
// public Getters omitted.
}
class Student {
private String name;
private LinkedList<String> grades;
// public Getters omitted.
}
|
Suppose you have a variable albania of type Country.
- Write a java expression for the 3rd state in albania.
- Write a java expression for the 4th university
in the 3rd state in albania.
- Write a java expression for
the 5th student
in the 4th university
in the 3rd state in albania.
- Write a java expression for
the 6th grade of
the 5th student
in the 4th university
in the 3rd state in albania.
(What is its type?)
- Write a java expression for
the length of
the 6th grade of
the 5th student
in the 4th university
in the 3rd state in albania.
(What is its type?)
the solution key
-
Read numbers from the keyboard
(until there isn't a number to read next).
-
Read words from the keyboard
until there isn't a word to read next
(that is, control-D is typed).
Return the number of words entered.
-
Suppose somebody gives you a
static method String wordToPigLatin(String),
which takes one word and converts it into pig latin.
Write a method which takes an entire sentence (w/o punctuation),
and converts it to pig latin.
Hint: call the
String method
split(" "),
and of course the method wordToPigLatin.
Don't look at
the solution key
until you've answered:
What task is being done once?
When you've looked at half the items of the list,
what accumulator (“so-far”) variable holds the result
of the work you've done up to that point?
-
What does the following loop do?
(The answer is 10 words or less, and is something you could
ask your non-programming friend to do to a list of numbers,
and they'd understand perfectly.)
void mystery( LinkedList<Integer> gs ) {
if (gs.size() == 0) {
System.err.println( "The list must contain at least one number." );
return;
}
int lw = gs.get(0);
for ( int i=0; i<gs.size(); ++i ) {
sc = gs.get(i);
if (sc < lw) {
lw = sc;
}
}
gs.remove(lw);
}
|
Don't look at
the solution key
until you've worked through this code by hand for two sample inputs,
each time keeping track of what each variable holds at each moment.
-
What does the following loop do?
(The answer is 10 words or less, and is something you could
ask your non-programming friend to do to a sorted list of numbers,
and they'd understand perfectly.)
/**
* @param a list of numbers *which is sorted* small-to-large.
*/
void mystery2( LinkedList<Integer> gs, int nw ) {
int i = 0;
while ((i < gs.size()) && (nw <= gs.get(i)) ) {
i = i+1;
}
// What is special about index i of the list, when we finish the loop?
gs.add(nw, i); // The "add" method adds nw to the list at index i,
// shoving existing numbers over by one to make room.
}
|
Don't look at
the solution key
until you've worked through this code by hand for two sample inputs,
each time keeping track of what each variable holds at each moment.
-
What does the following method return and do?
(The answer is a short, straightforward English sentence, with no Java.)
(For this example, class Treasure is some class
which has a method getWeight; that's all we need to know.)
LinkedList<Treasure> extract( LinkedList<Treasure> goods, double heavyThresh ) {
LinkedList<Treasure> flotsam = new LinkedList<Treasure>();
for ( int i=0; i<good.size(); ++i )
Treasure t = goods.get(i);
if (t.getWeight() > heavyThresh) { flotsam.add(t); }
}
for ( int i=0; i<flotsam.size(); ++i )
Treasure h = goods.get(i);
goods.remove(h);
}
return flotsam;
}
|
-
What does the following do?
class Doo {
int foo( int n ) { return 3*n; }
int goo( int n ) { return foo(n/2); }
int hoo( int n ) { return goo(n+1) + goo(n+3); }
}
Suppose Doo d = new Doo();.
-
What does d.hoo(5) return?
-
What does (new Doo()).hoo(6) return?
-
We'll write a series of three methods
very similar to a list's toString.
We give only an example, to explain the general method we're looking for.
Suppose data is a list of Integers containing
6,3,5.
-
shoddyListToString(data) = "[, 6, 3, 5]".
Don't look at
the solution key
until you've reviewed the while/for loops from lecture.
-
listToString(data) = "[6, 3, 5]".
This is how LinkedList's toString works
already; we are writing it ourselves without calling the built-in.
Note that this is like the previous problem, except that
the first element is handled specially.
Why is a for-each loop inappropriate for this problem?
(Of course -- make sure your method works for lists of length 0,
for lists of length 1, and for longer lists.)
Don't look at
the solution key
until you've worked through two small examples by hand.
-
customListToString( data, "Yo: {", "---", "}, dude." )
= "Yo: {6---3---5}, dude.",
and
customListToString( data, "xx", "yy", "zz" )
= "xx6yy3yy5zz".
That is, the user passes in what string to use
in place of the open-bracket,
what string to use in place of the comma,
and what string to use in place of the close-bracket.
Don't look at
the solution key
until after you've worked through the problem by hand for a list
of three numbers.
(What accumulater/so-far variable do you have?
How variable keeps track of where you are in the list?)
Rewrite listToString so that it is
a one-line function which calls customListToString.
Don't look at
the solution key
until after you've play-acted through:
you have a co-worker
(named “customListToString”)
who loves to take four inputs and give a String.
You have a boss you gives you just one input
(a list named “nums”),
and asks for the String back.
What exactly are the four pieces of info
you need to give to your co-worker, so
that you dupe them into doing most of your work for you.
-
Write method which takes in an array of objects,
and returns a new array
which has the same elements in reverse order.
Hint -- test cases:
Where should the object at index 0 of the input be, in your result?
Where should the object at index 1 of the input be, in your result?
Index 2? Index i?
Don't look at
the solution key
until after you've done this problem by hand for an array of four items,
and also for an array of five items.
Make sure your program works for an array of zero items.
-
[Harder than average:]
Write method which takes in an array of objects,
and reverses the elements of that array “in-place” (that
is, without using a second array.)
Don't look at
the solution key
until after you've done this problem by hand for an array of four items,
and also for an array of five items.
Make sure your program works for an array of zero items.
home—info—labs—hws—exams
textbook—java.lang docs—java.util docs—archive