In-Class Quiz 1 Topics
The quiz will cover Sections 3.1, 3.2, 4.2, 4.4, 4.5, 4.6 and 4.9 of the book,
the readings for the first two weeks, and whatever was covered in lecture so
far this semester.
You will not have to write any code.
It will be closed book and calculators cannot be used.
Sample quiz questions (and answers):
-
What is a class?
-
What is a method?
-
Describe in English what the following object defined by the following class
does when used by the Animator.
import animator.*;
import java.awt.*;
public class Quiz extends Animated {
private int yy = 0;
public void draw(int x, int y) {
screen.setColor(Color.black);
screen.fillOval(x,yy,50,50);
yy = yy + 1;
if (yy == 350) {
yy = 0;
}
} // end of draw method
} // end of Quiz class
-
What is the difference between an int and a double?
-
Assume j and k are integer (int) variables
and that the value of j is 2 and k is 20.
Assume s and t are floating point (double) variables
and that the value of s is 6.4 and t is 0.5.
What is the result of evaluating the following expressions (write integer
results with no decimal point, floating point results with a decimal
point)?
j + 3 * k
(j - 1) * ( 1 + k )
k / 7
k % 7
s / 4
1.5 + j / 4 - t
1.5 + j / 4.0 - t
Math.sqrt(4.0)
Math.sqrt(Math.sqrt(20.0/5)+7)
-
What values are assigned to the variables x and y in the following commands?
int x = (int) 5.7;
double y = (int) 5.7 + 1.4
double z = (int) (5.7 + 1.4)
-
Suppose x is a double variable.
Write an expression that will evaluate to the "rounded" integer value of x.
For example, if x = 4.3, then your expression should evaluate to 4, but if
x = 4.7, then your expression should evaluate to 5.
Solutions:
-
A class is a description of one or more objects.
It specifies how the corresponding object(s) will work when the
program they are part of executes.
-
A method is the name given to the code in a Java program that implements
a behavior.
It has a name, may have parameters, and contains commands.
-
It draws a blue circle with diameter 50 that starts at the top and moves down
until it reaches 350 pixels from the top, then jumps up to the top and moves
down again (and again and again).
The user can also make the circle move left/right while it is moving down via
the arrow keys (the up/down arrow keys have no effect).
-
An int is a whole number (negative, positive or 0) - e.g., -5, 0, 29.
A double is an approximation to a real number, and can have a fractional part,
e.g., 5.2, -7.99, 1.5e-5.
-
62 (precedence of * over +)
21 (use of parentheses)
2 (integer division)
6 (remainder operation)
1.6 (mixed mode, floating point division)
1.0 (mixed mode, integer division, precedence)
1.5 (mixed mode, precedence)
2.0
3.0
-
5 (casting to an int and discarding fractional part)
6.4 (casting to int, then converting back to double, then adding)
7.0 (sum is 7.1, then cast to an int, then converted to a double)
-
Two ways:
(int)(x + 0.5)
or
(int)Math.rint(x)