The test is open-everything with the sole limitation that you
neither solicit nor give help while the exam is in progress.
Since this is a timed, in-class, exam, you do not have to
write comments in your code. Other aspects of good style, such
as nice formatting and naming are still required, though.
| Problem | You got | Out of |
1 | | 15 |
2 | | 15 |
3 | | 15 |
4 | | 15 |
5 | | 15 |
6 | | 15 |
7 | | 10 |
TOTAL | | 100 |
- Match the terms with their definitions, by writing
the letter of the definition next to the term.
- Expression _______
- Variable _______
- Statement _______
- Application _______
- Subroutine _______
- Loop _______
- Exception _______
- Argument _______
- % _______
- Applet _______
- Enum _______
- Block _______
- boolean _______
- Graphics _______
- Computing _______
|
- An object that you throw to signal that something is wrong
- The primitive type containing only the values true and false.
- The science of information processes
- A named storage location
- A sequence of statements
- A user-defined type containing a fixed number of elements
- In Java, an object used for drawing
- The mathematical remainder operator
- A parameterized chunk of code that can be run on demand
- A program designed to run inside of another (often a web browser)
- A chunk of code that is evaluated
- Code that produces an action
- Code that is executed repeatedly
- A value passed to a subroutine
- A standalone program
|
- Here is an attempt to write a program that computes the sum
of the numbers from 1 to 10000, inclusive. However, there are three
major mistakes. Identify the mistakes and explain how to correct them.
(Note: find actual programming mistakes, not mistakes of style; do not
pick on the names, the indentation, the formatting, or the lack of
comments.)
public class Summer {
public static void main(String[] args) {
int sum;
for (int i = 1; i < 10000; i++) {
sum += i;
System.out.println("The sum from 1 to 10000 is " + sum);
}
}
}
- Write a complete Java program that displays a string made
up of the first characters of each of the program's arguments.
For example, if you ran the program with the four arguments
Rats are very intelligent your program should display
Ravi.
- Write a subroutine — just a subroutine, not a complete
Java program — to determine whether a given year is a leap
year or not. Throw an IllegalArgumentException if the year is
earlier than 1582. Note: A year is a leap year if it divisible
by 4, but not 100, unless also by 400. Got that? English can
be confusing.
To help you get started, here is the first line of the
subroutine. (Please note that you are being asked to do the leap
year computation on your own, and not via the GregorianCalendar
class in the Java library.)
public static boolean isLeapYear(int year) {
- Write a complete Java program that asks the user for a string
via showInputDialog, then displays the Fuddified version of the
string with showMessageDialog.
- Write the paint method for a Java applet that
draws 20 randomly placed squares, each 30 pixels wide. The
overall canvas size for the applet should be 400 x 300. Make
sure each square fits completely in the canvas. The squares can
be any color you choose. I've started the applet for you; again, just
write the paint method.
import java.applet.Applet;
import java.awt.Graphics;
import java.util.Random;
public class RandomSquareApplet extends Applet {
private static final int WIDTH = 400;
private static final int HEIGHT = 300;
private static final int SIDE_LENGTH = 30;
private static final int NUMBER_OF_SQUARES = 20;
private static Random randy = new Random();
public void paint(Graphics g) {
- If you've been reading the textbook for this class, you will have
come across the "3n+1" sequence. This is a sequence of
non-negative numbers determined by the
rule:
Call the current value n
If n == 1, the sequence is done
Else if n is odd, the next value is 3*n + 1
Else the next value is n/2
Write a subroutine — just a subroutine, not a complete
program — that accepts an integer and returns the length of
the 3n+1 sequence for that initial value. Some examples:
lengthOf3nPlus1(1) == 1
lengthOf3nPlus1(3) == 8
lengthOf3nPlus1(4) == 3
lengthOf3nPlus1(16) == 5
Psst: Why do you think the word "non-negative" appeared
in bold in the problem description?