CS2: Final Exam Review
Inheritance
-
What is an abstract class and what is an interface?
-
If class MyClass extends MyClass2, which of the statements is valid:
MyClass x = new MyClass2();
MyClass2 x = new MyClass();
Is the answer different if MyClass2 is abstract? And what if MyClass is abstract?
-
Generally, do we want to have instance variables private or public?
Exceptions
-
How to throw an exception?
-
How to catch an exception? How do we deal with a try-block
that might cause different types of exceptions?
Streams and File IO
-
What is the difference between a text file and a binary file?
-
How to work with BufferedReader and PrintWriter? Useful methods:
read(), readLine(), close(), and
print(...), append(...), close(), flush()
-
How do we check for the end of file (EOF)?
-
How to work with FileInputStream and FileOutputStream? Useful methods:
int read()
- Reads a byte of data from this input stream.
void write(int b)
- Writes the specified byte to this file output stream.
Java Collection Framework
-
-
Go through items in a collection using the iterator.
-
-
What is the difference between LinkedList and ArrayList?
-
What is the difference between Set, HashSet, and TreeSet?
-
What is the difference between Map, HashMap, and TreeMap?
-
A map maps keys to values. What happens if we add the following key-value pairs to the same map?
(A,1), (B,2), (C,1), (C,4), (B,1)
-
Specific methods to review:
- boolean add(E o)
Ensures that this collection contains the specified element (optional operation). Returns true if this collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.)
-
Set< K> keySet()
Returns a set view of the keys contained in this map.
-
Collection< V> values()
Returns a collection view of the values contained in this map.
Threading
-
How to create a thread? (2 options: implement Runnable, extend Thread)
-
How to start a thread? (start(), not run() !)
-
Keyword: synchronized - used on methods, or a block of code - the critical section
-
Review methods join, sleep, and yield. If you create a class MyThread which
extends Thread, and you create an object x from MyThread, what happens if you call x.sleep(1000)?
-
Exercise: Write a program that starts as many threads as there are command line arguments.
Each thread will have a private String equal to the corresponding command line argument (i.e.
the thread created first will take the first argument, etc). Then use wait()
and notifyAll() methods to ensure that the threads will print the arguments in
alphabetical order.
Graphical User Interface (GUI)
-
Which layout managers do we have and what is the difference between them?
Event-driven Programming
-
Suppose that you write a class that extends the action listener. What is the thing you need to
do to have the actionPerformed method execute each time a JButton is pushed? (Add the listener
onto the JButton.)
-
Can we create graphical objects inside our listener class? (Sure, why not?)
-
There are pre-implemented Adapter classes - all methods are empty so that we do not have to
implement empty methods ourselves (particularly useful: MouseAdapter which contains
five methods - inherited from the interface MouseListener).
Networking
-
What is the difference between the protocols IP, UDP, and TCP/IP?
-
How to initiate a connection between computers in Java? (Socket, ServerSocket)
Intellectual Property
-
Trade Secret, Trademark, Copyright, Patent - what are the advantages and disadvantages?
Miscellaneous
-
Work with command line arguments (e.g. "java MyClass oh no command line").
-
Read compact statements. E.g. what happens if we write:
System.out.println( new HashSet().add( "hello" );
or:
LinkedList s = new LinkedList();
while ( ( x = s.add( "ouch" ) == true );
-
Trace a program: read a program from the book. Try also more complicated programs like
those that use listeners and initiate several threads.