CSIS 10A Practice Final Exam SOLUTIONS
Practice Questions
1) Define each of the following terms, as they relate to this course:
· machine language -- The low level instructions that only the computer's CPU can understand. The Java compiler translates your programs into Bytecode, which the java virtual machine runs by further translating them to machine language.
· algorithm -- a set of instructions for accomplishing a task.
· formal parameter -- the variable declared inside the parentheses of a method definition. Used to receive the actual parameter or argument of a method call. For example, status in the method below is a formal parameter:
public void setStatus(int status){
currentState = status;
}
· actual parameter -- sometimes called "argument". The variable or value we provide in a method call, which is "passed" to the formal parameter when we jump into a method. For example, both Pad.FROG and 2 are actual parameters.
lilyPad.setStatus( Pad.FROG);
lilyPad.setStatus(2);
· array -- a data type representing a collection of homogeneous (same data type) values arranged by location.
· garbage collection -- the removal of objects from memory that takes place when they are no longer referred to by any variables.
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
answer:
int sum = 0;
for (int k = 1; k<=10000; k++)
sum = sum + 1.0/k;
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 SummaryBook(Stringauthor, Stringtitle)
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 status of a book, true if checkedOut
public class Book {
private String title, author; // title and author
private boolean checkedOut; // whether the book is checked out
// constructor
public Book(String author, String title){
this.title = title;
this.author = author;
checkedOut = false;
}
// getAuthor
public String getAuthor(){
return author;
}
// checkOut
public void checkout(){
checkedOut = true;
}
// isCheckedOut
public boolean isCheckedOut(){
return checkedOut;
}
}
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
int numShake = 0, numCheckOut = 0;
for (int k = 0; k< books.length; k++){
if (books[k].getAuthor().equals("Shakespeare"))
numShake++;
if (books[k].isCheckedOut())
numCheckOut++;
}
System.out.println("Number of Shakespeare books = " + numShake);
System.out.println("Number of books checked out = " + numCheckOut);
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 / m / k / k<5
0 / 10 / 0 / T
10 / 12 / 1 / T
11 / 14 / 2 / T
12 / 16 / 3 / T
13 / 18 / 4 / T
7) What will be displayed on the terminal by the following nested loop?
for (int k = 3; k >= 1; k -- ){for (int m = k; m >=0 ; m --){
System.out.print( m );
}
System.out.println();
} / DISPLAY
3210
210
10
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:
0 / 1 / 2 / 3 / 4 / 5 / 6 / 77 / 7 / 7 / 7 / 7 / 7 / 7 / 7
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.
int num17= 0; // number of species 17 sightings
for (int k=0; k<100; k++) // loop over all cities
num17 = num17 + sightings[17][k]; // add the count of city k to num17
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().
for (int j=0; i100; j++){ // loop over all city numbers
int num = 0; // number of bird sightings in a city
for (int i = 0; i<20;i++) // loop over all bird species
num = num + sightings[i][j];
System.out.println("number of sightings for city " + i + " = " + num); }
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().
for (int i=0; i<20; i++){ // loop over all bird species
int num = 0; // number of cities where bird sighted
for (int j = 0; j<100;j++){ // loop over all cities
if ( sightings[i][j] > 0 ) // if we see bird i in city j …
num = num + 1; // add 1
}
// after loop
if ( num <=4)
System.out.println("species " + i + " is endangered ");
}
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 {
int messageNumber = 0;
public void init() { // (No need to change this.)
addMouseListener(this);
}
public void paint(Graphics g) {
if (messageNumber == 0)
g.drawString( 100, 100, "Hello World!");
else
g.drawString( 100, 100, "Goodbye World!");
};
public void mousePressed(MouseEvent evt) {
if (messageNumber == 0 ) // switch messages
messageNumber = 1;
else
messageNumber = 0;
repaint();
};
public void mouseReleased(MouseEvent evt) { } // Junk required by
public void mouseClicked(MouseEvent evt) { } // the MouseListener
public void mouseEntered(MouseEvent evt) { } // interface.
public void mouseExited(MouseEvent evt) { }
} // end class Greetings