Mid Term Exam 1                                                                         Name: ________

Multiple Choices

1.   Programmer-named computer memory locations are called__B_ (1-A-5)

      b. variables

      c. addresses

      d. appellation

  1. An object’s attributes also are known as its __A__ (1-A-8)
    1. states
    2. rientations
    3. Methods
    4. Class
  1.  All Java Programs must have a method named   B ( 1-A-15)
    1. method()
    2. main()
    3. java()
    4. Hello()
  2. Which of the following elements is not required in a variable declaration?

C  (1-B-3)

    1. type
    2. an identifier
    3. an assigned value
    4. a semicolon
  1. The “equal to” comparison operator is __B  (1-B-10)
    1. =
    2. = =
    3. !=
    4. !!
  2. A method named printStatistics( ) is void and takes 1 integer arguments.  Which of the following is a correct call to printStatistics( ) ?  C  ( 2-A-6)
    1. void printStatistics(50);
    2. printStatistics( );
    3. printStatistics( 30);
    4. void printStatistics();
  3. A public method named findSum( ) is locate in classOne. To call the method from classTwo, use the statement.   B (2-A-7)
    1. classOne(findSum( ));
    2. classOne.findSum( );
    3. classTwo.classOne.findSum( );
    4. classTwo.findSum( );
  4. Which of the following is a correct call to a method declared as

‘ int add( int x, int y);    C ( 2-A-10)

    1. int add ( x, y);
    2. public int add( int x, int y);
    3. add( 3, 4);
    4. int add ( 3, 5 );
  1. Which of the following is a correct call to a method declared as

char getCode( char code)?   C ( 2-A-10)

    1. char getCode( char code);
    2. getCode(char code);
    3. getCode(‘x’);
    4. getCode( char ‘y’);
  1.  If a class is named program1( ), then the class constructor name is  D ( 2-B-21)
    1. program1.constructor
    2. constructor( )
    3. program1(constructor( ));
    4. program1( );
  2. Methods with the same name that have identical  argument lists but different return types are ___ B (3-A-14)
    1. legal
    2. overloaded
    3. constructor
    4. unstructured

 

  1. What is the value of  ans  you will get  for the statement  ans = Math.pow(2,3);

A ( 3-B-16)

a.       8

b.      6

c.       2

d.      3

 

      13.   Which  of the following statements determines the square root of the number and

               assigns it to the variable x?  C ( 3-B-16)

a.      number = squareRoot(x);

b.      x = sqrt(number);

c.      x = Math.sqrt(number);

d.      number = Math.sqrtRt(x);

 

     14.     The date constructed with Data oneDay = new Date(2,3,4); is    D ( 3-B-19)

a.       Febuary  3, 2004

b.      March 4, 2002

c.       March 4, 1902

d.      April 4, 1902

15.    The date stored in a Date object is stored in __B ( 3-B-20)

a.      seconds

b.      milliseconds

c.      minutes

d.      years

 

 

 

 

 

 

Short Questions.

 

  1. Write a Java program that print out your name and ID number. ( 3 pts)

 

public class student1

{

public static void main(String[] args)

{

            System.out.println("My name is Shin Liu");

            System.out.println(" ID = 1234");

}

}

 

 

  1. Write a class named “area”.  You declare the length as 10 and the width as 5 and print the area of the room. ( 3 pts)

 

public class area

{

public static void main(String[] args)

{

             int length = 10, width =5;

             System.out.println("The area =  " + length * width);

}

}

 

  1. Create a class name Hamburger.  Data fields includes a string for size ( large, medium, small),  and a double for price.  Include methods to get and set values for each of these fields.  ( 3 pts)

public class hamburger

{

             private String size;

             private double price;

 

            void setSize(String s)

            {

             size = s;

            }

            String getSize( )

            {

             return size;

}

void setPrice(double p)

{

             price = p;

}

double getPrice( )

{

            return price;

}

}

 

  1. Create a class named “TestHamburger” that instantiates one Hamburger object , assign the size to large, and the price to 1.99 , print the size and the price for the Hamburger.  ( 4 pts)

public class  testHam

{

             public static void main(String[] args)

{

                hamburger h1 = new hamburger();

 

                h1.setSize("Large");

             h1.setPrice(1.99);

                System.out.println("The size " + h1.getSize());

                 System.out.println("The size " + h1.getPrice());

}

}

  1. Create a class named InterstRate that includes four variables: Principle, Rate ( ex. 0.08), Rate1 ( ex. 8 %) and interest. Create two overload methods named compute();  The first method takes two double arguments, and the second method takes one double , one integer, and return the Interest. ( 3 pts)

 public class Interest

{

           

            public static void main (String[] args)

            {

                        double principle = 1000;

                        double rate = .05;

                        int rate1 = 5;

                        double money;

                                               

                        money = compute(principle,rate);

                        System.out.println("You get " + money );

                        money = compute(principle, rate1);

                        System.out.println("You get " + money);

            }

            public static double compute(double p, double r)

            {

                        return (p * r);

            }

            public static double compute(double p, int r)

            {

                          return ( p * r/100.0);

            }

}