CS 310-01

Test No. 1 (Max: 86)

10/2/09 Name______

Directions: Answer all questions on a separate answer sheet. When finished, turn in both the question and the answer sheets.

Part I. Multiple Choice. (2 x 28 = 56) For each question choose the best answer and enter its letter.

  1. Which of the following is a legal Java identifier?
  1. i
  2. class
  3. ilikeclass!
  4. idon’tlikeclass
  5. i-like-class
  1. A unique aspect of Java that allows code compiled on one machine to be executed on a machine of a different platform (e.g., Windows, Mac, Linux) is Java’s
  1. syntax
  2. use of objects
  3. bytecode
  4. use of exception handling
  5. all of the above
  1. Java is similar in syntax to what other high level language?
  1. Pascal
  2. Ada
  3. C++
  4. FORTRAN
  5. BASIC
  1. An error in a program that results in the program outputting $100 instead of the correct answer, $250 is
  1. a programmer error
  2. a syntax error
  3. a run-time error
  4. a logical error
  5. a snafu
  1. Following Java’s naming convention, which of the following would be the best name for a class about store customers?
  1. StoreCustomer
  2. Store Customer
  3. storeCustomer
  4. STORE_CUSTOMER
  5. Store-Customer
  1. Which of the following would be a good variable name for the current value of a stock?
  1. curstoval
  2. thisIsTheCurrentValueOfTheStock
  3. currentStockValue
  4. csv
  5. current
  1. Mistyping “println” as “printn” will result in
  1. a syntax error
  2. a run-time error
  3. a logical error
  4. no error at all
  1. If you want to output the text “hi there”, including the quote marks, which of the following could do that?
  1. System.out.println("hi there");
  2. System.out.println(""hi there"");
  3. System.out.println("\"hi there");
  4. System.out.println("\"hi there\"");
  5. none, it is not possible to output a quote mark because it is used to mark the beginning and ending of the String to be output.

Use the following class definition to answer the next question.

public class Demo {

public static void main(String[ ] args){

System.out.print("Here");

System.out.print("There " + "Everywhere\n");

System.out.println("in Texas");

}

}

  1. The output is
  1. Here There Everywhere in Texas
  2. Here
    There Everywhere in Texas
  3. Here There Everywhere
    in Texas
  4. Here
    There Everywhere
    in Texas
  1. What value will z have if we execute the following assignment statement?

float z = 5 / 10;

  1. 0.0
  2. 0.5
  3. 5.0
  4. 0.05
  5. none of the above, a run-time error arises because z is a float and 5 / 10 is an int
  1. If x is an int and y is a float, all of the following are legal except which assignment statement?
  1. y = x;
  2. x = y;
  3. y = (float) x;
  4. x = (int) y;
  5. all of the above are legal
  1. Given the following assignment statement, which of the following answers is true regarding the order that the operators will be applied in the right-hand expression?

a = (b + c) * d / e – f;

  1. *, /, +, -
  2. *, +, /, -
  3. +, *, /, -
  4. +, /, *, -
  5. +, -, *, /
  1. What will be the result of the following assignment statement?
    Assume:int a = 4;
    int b = -5;
    What is the cotent of c after the following statement?

int c = a + b * 1 / 2;

  1. 4
  2. 2
  3. 0
  4. 1
  5. none of the above
  1. If the String major = "Computer Science", what is returned by major.charAt(1)?
  1. ‘C’
  2. ‘o’
  3. ‘m’
  4. “C”
  5. “Computer”
  1. Which of the following would return the last character of the String x?
  1. x.charAt(0);
  2. x.charAt(last);
  3. x.charAt(length(x));
  4. x.charAt(x.length( )-1);
  5. x.charAt(x.length( ));
  1. The Random class has a method nextInt( max)—where max is a positive integer. The method returns a random float value between
  1. 1 and max
  2. 0 and max
  3. 1 and max - 1
  4. 0 and max - 1
  5. -2,147,483,648 and +2,147,483,647
  1. Assume that x is a double that stores 0.362491. To output this value as 36%, you could use the NumberFormat class with NumberFormat nf = NumberFormat.getPercentInstance( ); Which of the following statements then would output x as 36%?
  1. System.out.println(x);
  2. System.out.println(nf);
  3. System.out.println(nf.x);
  4. System.out.println(nf.format(x));
  5. System.out.println(format(x));

For the next 2 questions, refer to the class defined below:

import java.util.Scanner;

public class Demo {

public static void main(String[ ] args) {

int x, y, z;

double average;

Scanner scan = new Scanner (System.in);

System.out.println("Enter 3 integer values");

x = scan.nextInt( );

y = scan.nextInt( );

z = scan.nextInt( );

average = (x + y + z) / 3;

System.out.println("The result of my calculation is " + average);

}

}

  1. The Demo class computes
  1. The correct average of x, y and z as a double
  2. The correct average of x, y and z as an int
  3. The average of x, y, and z as a double, but the result may not be accurate
  4. the sum of x, y and z
  5. the remainder of the sum of x, y and z divided by 3
  1. What is output if x = 0, y = 1 and z = 1?
  1. 0
  2. 0.0
  3. 0.6666666666666666
  4. 0.6666666666666667
  5. 0.67
  1. In Java, “instantiation” means
  1. Noticing the first time something is used
  2. Creating a new object
  3. Creating a new alias to an existing object
  4. Launching a method
  5. None of the above
  1. Say you write a program that makes use of the Random class, but you fail to include an import statement for java.util.Random (or java.util.*). What will happen when you attempt to compile and run your program.
  1. The program won’t run, but it will compile with a warning about the missing class.
  2. The program won’t compile – you’ll receive a syntax error about the missing class.
  3. The program will compile, but you’ll receive a warning about the missing class.
  4. The program will encounter a runtime error when it attempts to access any member of the Random class
  1. The behavior of an object is defined by the object’s
  1. instance data
  2. constructor
  3. visibility modifiers
  4. methods
  5. all of the above
  1. The relationship between a class and an object is best described as follows:
  1. Classes are instances of objects
  2. Objects are instances of classes
  3. Objects and classes are the same thing
  4. Classes are programs while objects are variables
  5. Objects are the instance data of classes
  1. A variable whose scope is restricted to the method where it was declared is known as a
  1. parameter
  2. global variable
  3. public instance data
  4. private instance data
  5. local variable
  1. A class constructor usually defines
  1. how an object is initialized
  2. how an object is interfaced
  3. the number of instance data in the class
  4. the number of methods in the class
  5. if the instance data are accessible outside of the object directly
  1. Having multiple class methods of the same name where each method has a different number of or type of parameters is known as
  1. encapsulation
  2. information hiding
  3. tokenizing
  4. importing
  5. method overloading
  1. In order to preserve encapsulation of an object, we would do all of the following EXCEPT for which one?
  1. Make the instance data private
  2. Define the methods in the class to access and manipulate the instance data
  3. Make the methods of the class public
  4. Make the class final
  5. All of the above preserve encapsulation
  1. If a method does not have a return statement, then
  1. it will produce a syntax error when compiled
  2. it must be a void method
  3. it can not be called from outside the class that defined the method
  4. it must be defined to be a public method
  5. it must be an int, double, float or String method

Part II (30). Write a Java code for each of the following.

  1. Write a complete Java program which reads a time duration as number of seconds and prints it as number of minutes and seconds.

For the next two problems, assume that class BaseballPlayer contains the following instance variables.

private String name; // player’s name

private String position; // player’s position

private int numAtBats; // number at bat

private int numHits; // number of all hits

Write the complete code for the following methods to be included in the class.

  1. a constructor which is passed the player’s name and position.
  1. method battingAverage(), which returns the player’s batting average. Hint: Batting average = (Number of hits) / (Number at bats.).

CS 310-01

Test No. 1 (Max: 86)

10/2/09 Name______

Part I (56)

1____ 2____ 3____ 4____ 5____ 6____ 7____ 8____ 9____ 10____
11____ 12____ 13____ 14____ 15____ 16____ 17____ 18____ 19____ 20____

21____ 22____ 23____ 24____ 25____ 26____ 27____ 28____

Part II (30)