CIS 260 Final Review
This review doesn't necessarily have all the topics covered
on the final exam, but it does cover most of what will be on the
exam. If you know this material, then you should be set.
Show all answers
Hide all answers
Objects
- When are objects created?
Answer
For most objects, they are created when
you use the keyword: new. Java has a special case
for String objects to make them easier to use. A quoted string
inside source code is automatically converted into a String
constructor.
- When are reference variables created?
Answer
Reference variables are created when they are
declared. If they are static variables, they are created when the
class they are in is first loaded into memory. If they are instance
variables, then they are created when an instance (object) of
their class is created.
- Describe inheritance.
Answer
Inheritance is when one class extends another.
The subclass (derived class) is the class that extends the superclass
(base class). The subclass inherits (and therefore has) the data
and methods of the superclass. Even though the subclass contains
the data and methods, it might not be able to access them since the
superclass may have made some of those members private access.
- Describe composition.
Answer
Composition is when an object has other objects as data
members. This is very common. You have probably written many objects that
contain either an instance or static String variable. That would be a simple
example of composition.
- What is a subclass?
Answer
A class which is based on (extends) another class.
- What is a superclass?
Answer
A class which another class is based on (extends).
Concatenation
- When is the toString() method called?
Answer
- when it is explicitly called: obj.toString()
- when an object is being concatenated to a string: "" + obj becomes "" + obj.toString()
- when a non-string is sent to a PrintWriter: System.out.println(obj) becomes System.out.println(obj.toString())
- When is addition performed instead of concatenation?
Answer
Addition is performed when both operands are internally numbers. (byte, short, int, long, float, double, char)
- What happens if an object does not have a toString() method?
Answer
That doesn't happen since the Object class has a toString method and all
other objects in Java (other than Object instances) have Object as their ultimate base class.
Threads
- How do you start a thread?
Answer
You call its start() method.
- When does a thread end?
Answer
A thread ends when its run() method ends or it is interrupted.
- How can you pause a thread?
Answer
You can pause execution of a thread using the Thread.sleep method.
- What exception(s) do you have to watch for when using a thread?
Answer
InterruptedException
String methods
- Create a new String in variable s3 which contains String s1 and
String s2 concatenated.
Answer
String s3 = s1 + s2; or
String s3 = s1.concat(s2);
- Create a lower case version of a string in variable s.
Answer
s.toLowerCase() [Note: this does not modify s]
- Create an upper case version of a string in variable s.
Answer
s.toUpperCase() [Note: this does not modify s]
- Compare the strings s1 and s2.
Answer
s1.compareTo(s2) [Note: this does not modify s1 or s2]
- Compare the strings s1 and s2 ignoring case.
Answer
s1.compareToIgnoreCase(s2) [Note: this does not modify s1 or s2]
- Test the strings s1 and s2 for equality.
Answer
s1.equals(s2) [Note: this does not modify s1 or s2]
- Test the strings s1 and s2 for equality ignoring case.
Answer
s1.equalsIgnoreCase(s2) [Note: this does not modify s1 or s2]
- Place the character in the fifth position in string s into a new
char variable named ch.
Answer
char ch = s.charAt(4);
Applets
- What three methods are guaranteed to be called when an applet starts up?
List them in order.
Answer
- public void init()
- public void start()
- public void paint(Graphics g)
Data structures
- Add 1 and 2 to an empty stack and then remove one item. What is left on the stack?
Answer
1
- Add 1 and 2 to an empty queue and then remove one item. What is left on the queue?
Answer
2
- Add 1 and 2 to an empty linked list. Draw a diagram of the resulting linked list?
Answer
head -> 1 -> 2 -> null or
head -> 2 -> 1 -> null
- List common operations on a binary tree in increasing order of difficulty.
Answer
- traverse (inorder, preorder, postorder)
- find minimum or maximum value in the tree
- find/search
- insert
- delete
- List common operations on a binary tree in increasing order of difficulty.
Answer
- find minimum or maximum value in the tree (about the same as traverse)
- traversal (about the same as finding minimum or maximum)
- find/search
- insert
- delete
- Add 5, 7, 1, 3, 2, 4, 0, 9 to an empty binary search tree. Show what the tree looks like.
Answer
5
/ \
1 7
/ \ \
0 3 9
/ \
2 4
- Add 5, 7, 1, 3, 2, 4, 0, 9 to an empty binary search tree. Show the inorder traversal of that tree.
Answer
0, 1, 2, 3, 4, 5, 7, 9
- Add 5, 7, 1, 3, 2, 4, 0, 9 to an empty binary search tree. Show the preorder traversal of that tree.
Answer
5, 1, 0, 3, 2, 4, 7, 9
- Add 5, 7, 1, 3, 2, 4, 0, 9 to an empty binary search tree. Show the postorder traversal of that tree.
Answer
0, 2, 4, 3, 1, 9, 7, 5
- Where is the minimum value located in a binary search tree?
Answer
As far to the left from the root as possible.
- Where is the maximum value located in a binary search tree?
Answer
As far to the right from the root as possible.
- If you deleted the 1 in the tree used above, which node would take its place?
Answer
2 (the successor), or 0 (the predecessor)
- If you deleted the 7 in the tree used above, which node would take its place?
Answer
9 (the only child)
- If you deleted the 2 in the tree used above, which node would take its place?
Answer
none
- Add 5, 7, 1, 3, 2, 4 to an empty min heap. Show what the resulting heap looks like.
Answer
1
/ \
/ \
2 4
/ \ /
7 3 5
- Add 5, 7, 1, 3, 2, 4 to an empty max heap. Show what the resulting heap looks like.
Answer
7
/ \
/ \
5 4
/ \ /
3 2 1
- Draw a weighted directed graph with the following edges:
A to B (one way, weight 5)
A to C (both ways, weight 4)
C to D (both ways, weight 3)
D to B (one way, weight 2)
Answer
5
A --> B
| ^
4 | | 2
| |
C --- D
3
- Given the weighted directed graph above, what are the neighbors of D?
Answer
B, C
- Given the weighted directed graph above, what are the neighbors of B?
Answer
There are none.
- Given the weighted directed graph above, what are the neighbors of A?
Answer
B, C
- Describe the mimimum spanning tree of a weighted undirected graph with the following edges:
A to B (weight 1)
A to C (weight 3)
B to C (weight 1)
B to D (weight 3)
C to D (weight 1)
Answer
It would consist of the edges A-B, B-C, C-D. A real test question
would include more edges and vertices.
- Describe the shortest path from A to D of a weighted undirected graph with the following edges:
A to B (weight 1)
A to C (weight 3)
B to C (weight 1)
B to D (weight 3)
C to D (weight 1)
Answer
It would be the path from A to B to C to D. A real test question
would include more edges and vertices.
- Write down the result of a breadth-first search of an undirected graph with these edges:
A-B, B-D, B-E, A-C, C-F, C-G
Answer
One possibility: A B C D E F G
- Write down the result of a depth-first search of an undirected graph with these edges:
A-B, B-D, B-E, A-C, C-F, C-G
Answer
One possibility: A C G F B D E
- What are some advantages of using a linked list versus using an array?
Answer
- A linked list can grow as needed, so you don't need to know ahead of time how many items will be in the list.
- It is easy and efficient to insert items into the middle of a linked list.
- It is easy and efficient to delete items from the middle of a linked list.
- What are some disadvantages of using a linked list versus using an array?
Answer
- Accessing a specific position in an array is much more efficient than finding an item in a linked list.
- Linked lists have more overhead than arrays.
- Linked lists don't have the built-in snytax support in Java that arrays do.
Programming
- What should you do with the programming example on the
exam that has an error?
Answer
Indicate where the error is and specify a fix.
- What should you do when the test requests that you write a recursive function
and code to test it?
Answer
Write it correctly.