Please put your answer to each question on a separate page. The examination is designed to determine how well you know the material. Your goal is to get as many points as possible (out of a possible 100 points). Please keep this goal in mind as you budget your time. You have 75 minutes to complete this examination.
Read each question carefully. Make sure that you clearly understand a question before answering it. You may wish to work out an answer on scratch paper before writing it on your answer page; answers that are difficult to read may lose points for that reason.
Question | Value | Score |
1 | 10 | |
2 | 15 | |
3 | 10 | |
4 | 10 | |
5 | 10 | |
6 | 20 | |
7 | 25 | |
Total | 100 |
1. (10 points) Consider the following 2 lines of Java.
int y = 0, z = 5;
x = 17 * y++ - --z;
What is the value of x, y, and z after these statements are executed?
x = -4; y = 1; z = 4.
2. (15 points)
int x = 1, y = 2, z = 3;
x =+ 4;a) What is the value of the x after the above statements are executed?
int x = 1, y = 2, z = 3;
z %= y;b) What is the value of the z after the above statements are executed?
int x = 1, y = 2, z = 3;
y *= z + x;c) What is the value of the y after the above statements are executed?
Solution:
a) x = 4; b) z = 1; c) y = 8
3. (10 points) Translate the following if/else statement to an equivalent switch statement. Assume that x is declared to be an int.
if ( x % 3 == 0 )
x /= 5;
else if ( x % 3 == 1 )
x /= 7;
else x /= 11;
switch ( x % 3 )
{case 0:
x /= 5;
break;case 1:
x /= 7;
break;default:
x /= 11;
}
4. (10 points) Translate the following code segment to an equivalent segment that uses a for control structure instead of a while.
int i = 1, yPos = 85;
while ( i <= 1024 )
{j = (i - 32) * 5.0 / 8;
g.drawString("j = " + j, 25, yPos += 15);
i *= 2;}
int yPos = 85;
for (int i = 1; i <= 1024; i *= 2 )
{j = (i - 32) * 5.0 / 8;
g.drawString("j = " + j, 25, yPos += 15);}
5. (10 points) Define a boolean method implies that has 2 boolean parameters: x and y that returns true if and only if either x is false or y is true.
public boolean implies( boolean x, boolean y){
return !x || y ? true : false;
}
6. (20 points) Create an applet called gradeMaker that prompts the user to enter his/her name. When your action method is invoked, repaint the applet and return true. Your paint method should extract the text from your text field, produce a random number between 0 and 99, and report that to the user. For example, if the user enters:
Bob "Bill Dole" Clinton
and the random number that you produce is 53, then you reply:
Bob "Bill Dole" Clinton's grade is 53.
Use either showStatus or drawString to display your reply.
import java.awt.*;
import java.applet.*;
public class gradeMaker extends Applet{
Label prompt;
TextField name;
public void init(){
prompt = new Label("Please enter name and press Enter");
name = new TextField(20);
add(prompt);
add(name);}
public boolean action(Event e, Object o){
repaint();
return true;}
public void paint(Graphics g){showStatus(name.getText() + "'s grade is " + ((int) (100*Math.random())));
}
}
7. (25 points) Create an applet that generates a 10 X 10 board of squares, each of which is 20 X 20 pixels. The coloring of the squares is 2-dimensional, as follows:
import java.awt.*;
import java.applet.*;
public class redBlueBoard extends Applet {public void paint(Graphics g) {
float red, green = (float) 0.0, blue;
for (int r = 1; r <= 10; r++){
for (int c = 1; c <= 10; c++){
red = (float) 0.1*r;
blue = (float) 0.1*c;
g.setColor( new Color( red, green, blue ) );
g.fillRect( 20*c, 20*r, 20, 20 );}
}
}
}