PracticeFinal

advertisement
CSIS 10A
Practice Final Exam
Things you should know
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)
12)
13)
14)
15)
16)
17)
Primitives vs Object Reference variables, memory maps
If statements, Logical operators (AND, OR, NOT)
Loops – while, do-while and for
Writing instantiable classes and methods, constructors
Using classes that represent the state of an object
Drawing a memory map of an object
Apps that use classes we have created
Applets and drawing graphics in paint method
Mouse and key handlers
2D graphical applet that works with a given class
Methods that draw objects such as cows and barns
Creating and printing arrays of strings, ints, double
Selecting random cell index of array
for loops to print items, plot data
for loops (nested) to draw grid of squares
draw an array given code
algorithm to process an array
Practice Questions
1) Define each of the following terms, as they relate to this course:

machine language

algorithm

formal parameter

actual parameter

array

garbage collection
2) Write a Java code segment that will compute the sum of the reciprocals of the first 10000 positive integers.
Use a for statement. The sum you should compute is:
1/1 + 1/2 + 1/3 + 1/4 + . . . + 1/10000
3) Here is a class for representing books owned by the library. Complete the three methods indicated according
to the javadoc comments provided. You don't have to write the javadocs.
Constructor Summary
Book(String author, String title)
creates a new Book object with a given title, and author. Initially a book is not checked out.
Method Summary
void checkOut()
checkOut - checks a book out by changing checkedOut to true
String getAuthor()
returns the author of the book
boolean isCheckedOut()
returns the checkOut of a book, true for checkedOut and false for not checkedOut
public class Book {
private String title, author; // title and author
private boolean checkedOut; // whether the book is checked out
//
constructor
// getAuthor
// checkOut
// isCheckedOut
}
4) Draw a map of memory after the following statements execute:
Book b = new Book("Gladwell","Blink");
Book c = new Book("Ballard","Crash");
Book d = b;
d.checkOut();
5) Suppose that a library owns 27,532 books and that information about all the books has already been stored
in an array
Book[] books = new Book[27532];
Write a code segment that will (do both in the same loop)
a) count the number of books by "Shakespeare" owned by the library.
b) count the number of books that are currently checked out
6) Complete the next 4 lines of the trace table for the following code:
int sum = 0, m = 10;
for (int k=0; k<5; k++){
sum = m - k;
m = m + 2;
}
sum
0
m
10
k
0
k<5
T
7)
What will be displayed on the terminal by the following nested loop?
DISPLAY
for (int k = 3; k >= 1; k -- ){
for (int m = k; m >=0 ; m --){
System.out.print( m );
}
System.out.println();
}
8) Suppose we have an array of int declared with the following values:
int [] data = {7, 5, 1, 2, 4, 5, 3, 6};
and, we execute the statements:
for (int k = 0; k<7 ; k++) {
data[k+1] = data[k];
}
Please draw the data array after the statements have completed:
9) Suppose that a bird watching club has collected data on the number of sightings of 20 different bird species
in each of 100 different cities, for the year 1995. The data has already been stored in an array, sightings,
which was created by the statement
int[][] sightings = new int[20][100];
The value stored in sightings[i][j] is the number of times that a bird belonging to species number i was
sighted in city number j.
a) Write a code segment that will count the total number of sightings of species number 17 in all 100 cities.
b)
Write a code segment that will compute and print the total number of bird sightings for city number 0,
then for city number 1, then for city number 2, and so forth, up to city number 99. You can print the
answers to standard output using System.out.println().
c)
Let's say that a species is endangered if it has been sighted in only 4 cities or fewer. (Species number i
was sighted in city number j if sightings[i][j] is greater than zero.) Write a code segment that will
print a list of all the species that are endangered. You can print the answers to standard output using
System.out.println().
10) a) Complete the following little applet. When it starts up, the applet should display the message "Hello World."
When the user clicks on the applet, the message should change to "Goodbye World." If the user clicks again, it should
change back to "Hello World," and so forth. Use the paint() method to display the strings. You will need to add an
instance variable to keep track of the message that is currently displayed.
class Greetings extends Applet implements MouseListener {
public void init() { // (No need to change this.)
addMouseListener(this);
}
public void paint(Graphics g) {
};
public void mousePressed(MouseEvent evt) {
};
public void mouseReleased(MouseEvent evt) { } // Junk required by
public void mouseClicked(MouseEvent evt) { }
//
public void mouseEntered(MouseEvent evt) { }
//
public void mouseExited(MouseEvent evt) { }
} // end class Greetings
the MouseListener
interface.
Download