home—info—archive—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
exam3-study-questions
exam3 study sheet
Exam3 will be given be during finals.
The exam is (inherently) cumulative, but will stress material since exam2.
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″.
(You'll turn this in with your exam.)
The material from Exam1 and Exam2 won't be especially tested,
but due to the cumulative nature of the material, you should
know all the topics from
the exam1 study questions.
and
exam2 study questions.
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)).
For the class java.util.Scanner, you will need to know
how to use its methods hasNext and next,
as well as the related hasNextInt, nextInt, etc..
You should know that its constructor takes one argument -- where the Scanner
reads its individual characters from
(either the keyboard, System.in, or
from a particular String.
You won't need to know that
java.io.Files
and
java.net.URLs
are also
perfectly fine source of characters.)
For the classes
String,
java.util.LinkedList<SomeType>,
and the wrapper classes
(Double,
Boolean,
etc.)
you will not need to memorize the names of any of the methods or fields
for those classes (beyond calling their constructor).
However, if you are provided with documentation (for any of these
classes or others), you should be able to create instances of that
class and call its methods appropriately.
You will not
need to know/use “import”,
“static” You will
need to know when to use static!,
or classes such as java.io.File.
For these practice questions, feel free to discuss answers on the
WebCT discussion board(s).
Problems tagged as “[Challenge]” or “Optional” aren't
necessarily difficult,
but might rely on remembering details or concepts which weren't stressed.
Such problems would be extra credit if they were on the test at all.
When giving a java expression or statement,
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.
New Topics for Final Exam
The final exam is cumulative.
A significant portion of the final exam will focus on the topics that are
new since the second exam, but most of the new material builds on previous
material.
-
Using class Scanner
-
System.out.println
-
for-each loops
-
while loops
-
for loops (in addition to for-each loops from exam2)
-
arrays
-
inheritance:
a class which extends another,
and overrides a method.
You won't have to write such code,
but if shown such code you'll have to understand it.
(You won't need to know super, or interfaces.)
Review Questions on new topics
-
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...”.
-
Review all topics, questions, and problems from the lab assignments, homework assignments, and lecture notes.
-
You can review Chapters 2-8 in the book (excepting Chpt. 6),
and do the end-of-chapter exercises (self-review, exercises,
and programming-project).
Powerpoint slides for the book are now posted on WebCT.
Although we introduced topics in a different order from the book
(and hence didn't use it extensively),
it is a very good source of material, and you should be able to
understand its content.
Note that we have emphasized things differently than the book
(more emphasis on returning values and less on
System.out.println;
more emphasis on breaking tasks into small functions and unit tests,
than one big public static void main( String[] args )).
Anything which we haven't mentioned at all in lecture-or-lab
can be ignored in the book
(e.g. the keyword “final”,
class DecimalFormat)
although they are certainly good topics to know it anyway.
-
Review the Exam 2 study guide.
-
When should a field or method be declared static?
-
Create linked lists and arrays that hold lists of:
-
Temperatures
-
GPAs
-
Pizza Toppings (Strings)
-
Dates
-
PhoneNumber
-
Rooms
-
For the lists above, write For Each, 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
-
Search for a specific object (reference == reference)
-
Search for similar objects (equals)
-
Search for objects with certain properties
-
Sum the temperatures in a given range
-
Compute the average of all GPAs 3.0 or higher
-
Count the phone numbers with area code 540
-
Count the total treasures in all the rooms of a list.
-
Create instances of class Scanner to:
-
Read input from the keyboard (System.in)
-
Read input from a String
-
For any for-each loop problem in the notes, write a total of three versions:
(a) using a for-each loop, (b) using a while loop,
and (c) using a for loop.
Note that (b) and (c) are similar, just re-arranging certain parts
(e.g. do you write the end-of-loop update at the end of the loop,
or between the for's parentheses?)
Note that the for-each loop does the same work as the other two,
but is simpler to write (since Java is doing stuff for you:
keeping track of where you are in the list,
and updating the loop variable for you).
-
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 ( Integer sc : gs ) {
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.)
LinkedList<Treasure> extract( LinkedList<Treasure> goods, double heavyThresh ) {
LinkedList<Treasure> flotsam = new LinkedList<Treasure>();
for ( Treasure t : goods ) {
if (t.getWeight() > heavyThresh) { flotsam.add(t); }
}
for ( Treasure h : flotsam ) {
goods.remove(h);
}
return flotsam;
}
|
-
Write a for-each loop which determines a list's size without
actually actually calling the size() method!.
(Suppose that you have a variable lst
which holds a LinkedList<Gewgaw>)
-
What is the difference between the soFar1 and soFar2 in the following?
String soFar1 = "";
String soFar2 = "";
for ( Treasure t : swag ) {
soFar1 = soFar1 + " " + d.getName();
soFar2 = d.getName() + " " + soFar2;
}
|
(Here, swag is a LinkedList<Treasure>.)
Don't look at
the solution key
until you've worked through two small examples by hand.
(You can also type this into BlueJ, and
use the debugger
to step through the loop.)
-
We'll write some 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]".
Use a for-each loop.
Don't look at
the solution key
until you've reviewed for-each loops from lecture.
-
Write shoddyListToString_v2 which
is like the previous, but it uses a
while or for loop.
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, "xx", "yy", "zz" )
= "xx6yy3yy5zz".
That is, "yy" is
and
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.
-
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.
-
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
home—info—archive—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs