Midterm 1 Review Solutions
I. Data and Types
1) Are these valid assignments?
float f = 124.52; No, the literal is a double by default. Correct: float f = 124.52F;
long g = 53; Yes
double d = 10; Yes
2) Why are data members declared outside a method?
So that their scope is the entire class. That way any method can use them, but they aren't local variables, so they still exist when the method ends.
3) Write the method header for an instance method average, which takes two int parameters, returns a double, and can be called only from inside its own class.
private double average(int x, int y)
4) With one line of code, declare a class constant called SIZE of the smallest numeric data type, make it accessible from outside the class, and initialize it with the value 1.
public static final byte SIZE = 1;
Note: it turns out we don't need any letters or casts to turn the literal constant into a short or byte type. We only need a letter or cast to turn a real-number literal constant into a float. So the following assignments are ok:
byte x = 100;
short y = 2048;
You'll only get an error if you try to assign too big a value. So the following assignments are not ok:
byte x = 130;
short y = 2000000000000000000000;
5) Which are primitive data types and which are reference data types?
byte primitive
boolean primitive
String reference
double primitive
Integer reference
6) What's the problem with this code?
int x, y, z;
x = y + z; <-- y and z are being used before they're initialized
II. Expressions and Math
1) Fill in the method so that it returns the value: (-b + sqrt(b^2 - 4ac) ) / 2a
public double quadratic (double a, double b, double c) {
return (-b + Math.sqrt(b*b - 4*a*c) ) / (2*a);
}
2) Write an expression for (sin(2x))4 where x is a variable of type double.
Math.pow(Math.sin(2*x), 4)
3) Write a statement that makes c the remainder of a divided by b.
int a = 8, b = 3, c;
c = a % b; (and in this case, the answer is 2)
4) What is the value of y after each line?
int y = 3 - 1 + 4; 6
y = 4 / 2 * 5; 10
y = 1 + 2 * 3; 7
y = 9 / 2 - 5; -1
5) What is the value of z after each line?
float z = 1 / 2; 0.0
z = 2 + 1 / (float) 2; 2.5
z = (int) 9.6; 9
6) What is the value of x after each line?
short x = 9 / (double) 4; none: type error, can't assign double into short
x = (short)6.8 * 2 + 12 % 4 + 1; 13
x = (short) (1.5 + 1.5); 3
7) What gets printed by these lines?
System.out.println(1 + 2 + "test"); 3test
System.out.println("test" + 1 + 2); test12
8) Fill in the method so that it returns a circle's area (remember A = r2).
public double areaOfCircle(int radius) {
return Math.PI * radius * radius;
}
III. Classes and Methods
1) Fill this in so that the value of the parameter gets assigned to an int data member age.
public Person(int age) {
this.age = age;
}
2) Point out any problems in these three ways of using the JFrame class.
a. javax.swing.JFrame f = new javax.swing.JFrame(); no problems
b. import javax.swing.*; no problems
JFrame f = new JFrame();
c. import javax.swing.JFrame; no problems
JFrame f = new JFrame();
3) Point out any problems in the following code.
int x = new int(5); can't use new or a constructor on a primitive type
Integer x = new Integer(5); no problems
4) Point out any problems with these method definitions, which are in the same class.
private void sayHi() { this is fine
System.out.print("hi");
}
public int lotsOfHellos() {
int x = sayHi(5); sayHi doesn't take a parameter or return a value
} lotsOfHellos is supposed to return an int
5) Why can't a static method access an instance variable?
The static method is called using the class name instead of an object name. Since this can be done without even making an instance of the class, the instance variable might not even exist at the time of the method call.
6) How can you tell the difference between overloaded methods?
They have different numbers and/or types of parameters.
7) When you assign a value to a parameter of a method, why does it have no affect once that method ends?
The parameter is a local variable, and goes away when the method finishes.
8) What does pass-by-value mean?
When you pass an argument to a method, the value of that argument gets assigned to the matching parameter in the method header.