Midterm 1 Review

I. Data and Types

1) Are these valid assignments?

float f = 124.52;

long g = 53;

double d = 10;

2) Why are data members declared outside a method?

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.

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.

5) Which are primitive data types and which are reference data types?

byte

boolean

String

double

Integer

6) What's the problem with this code?

int x, y, z;

x = y + z;

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) {

}

2) Write an expression for (sin(2x))4 where x is a variable of type double.

3) Write a statement that makes c the remainder of a divided by b.

int a = 8, b = 4, c;

4) What is the value of y after each line?

int y = 3 - 1 + 4;

y = 4 / 2 * 5;

y = 1 + 2 * 3;

y = 9 / 3 - 4;

5) What is the value of z after each line?

float z = 1 / 2;

z = 2 + (float) 1 / 2;

z = (int) 9.6;

6) What is the value of x after each line?

short x = 9 / (double) 4;

x = (short)6.8 * 2 + 12 % 4 + 1;

x = (short) (1.5 + 1.5);

7) What gets printed by these lines?

System.out.println(1 + 2 + "test");

System.out.println("test" + 1 + 2);

8) Fill in the method so that it returns a circle's area (remember A = PI * r2).

public double areaOfCircle(int 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) {

}

2) Point out any problems in these three ways of using the JFrame class.

    a. javax.swing.JFrame f = new javax.swing.JFrame();

    b. import javax.swing.*;

      JFrame f = new JFrame();

    c. import javax.swing.JFrame;

      JFrame f = new JFrame();

3) Point out any problems in the following code.

int x = new int(5);

Integer x = new Integer(5);

4) Point out any problems with these method definitions, which are in the same class.

private void sayHi() {

         System.out.print("hi");

}

public int lotsOfHellos() {

          int x = sayHi(5);

}

5) Why can't a static method access an instance variable?

6) How can you tell the difference between overloaded methods?

7) When you assign a value to a parameter of a method, why does it have no affect once that method ends?

8) What does pass-by-value mean?