Each Question Is Worth 4 Points Unless Otherwise Noted

Total Page:16

File Type:pdf, Size:1020Kb

Each Question Is Worth 4 Points Unless Otherwise Noted

Name______December 18, 2013 Final Computer Science 119

Each question is worth 4 points unless otherwise noted. Circle the correct answer in the multiple choice.

______1. The primary task of the compiler is to A) translate a Java program to bytecode. B) translate a Java program to unicode. C) provide meaningful error messages. D) provide an algorithmic solution to a problem.

______2. Assigning an int value to a double variable in Java: A) is a throw. B) does an automatic type conversion. C) is bad syntax. D) is a runtime error.

______3. A constructor is called by: A) calling an accessor. B) the new operator. C) a wrapper. D) the build statement.

______4. Which of the following expressions is true?

A) 10 != 10 B) 5 >= 6 C) 7 <= 0 D) 17 != 20

______5. (Skip)You can think of a ______as a canvas on which we paint and draw in Java.

A) JFrame. B) Graphics. C) Container. D) JPanel.

1 ______6. Which of the follow is an example of a compound statement in Java? A) if (a > b) System.out.println("Bigger"); else System.out.println("Smaller");

B) (a > b) && (c < d)

C) System.out.println("The score for the game was " + home + " to " + visitors);

D) { x = 15; System.out.println("x is " + x); }

7. What does it mean when I say that c1 is a Counter object?

8. Circle the names in this list that follow the convention used by Java programmers to name classes: (8 points)

Greeter sayHello BlueTooth MONTH

9. Consider the Java expression: 4.4 – 2 / 5 A. What is the type of the expression? (2 points)

B. What is the expression's value? (2 points)

10. For the Java expression: “May “+ 1 + 2 A. What is the type of the expression? (2 points)

2 B. What is the expression's value? (2 points)

11. Evaluate the following boolean expressions and indicate their values.(true or false) Assume that variable x = 2 and y = 4. (2 points each.)

A)y != 7

B)y > 4

12. How can breakpoints be used to debug a Java program in eclipse? (Skip)

13. What is an infinite loop?

14. When comparing two objects, what is the difference between using the equals method and the == operator?

3 15. What output is produced by the following code fragment? int num = 0, max = 20; while (num < max) { System.out.println(num); num = num + 4; }

16. Show the output of this Pay class, when the user inputs 37.

import java.util.Scanner; public class Pay { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Hours?"); double hours = scan.nextDouble(); double pay = 0.0; if(hours > 35) pay = 35 * 10.00 + (hours - 35)*15.00; else pay = hours * 10.00; System.out.println("$" + pay); scan.close(); } }

Execution: (input is underlined) Hours?37

4 17. Given this Point class and PointDriver, show the output when the PointDriver class executes. public class Point { private double coord; public Point(){ coord = 0.0; } public Point(double c){ coord = c; } public void setCoord(double c){ coord = c; } public double getCoord(){ return coord; } } public class PointDriver { public static void main(String[] args) { Point p = new Point(5.4); System.out.println(“p is at “ + p.getCoord()); } }

18. Imagine that you have created a new Point object from #17 by writing:

Point pt = new Point();

and later you write:

pt = new Point(4.0); What happens to the Point that was created by the new operator in the first line? Draw a picture if needed.

19. (Skip)What is the error in this program? The indicated line generates an error. Notice the note below about it. public class EmptyWindow { //Creates an empty graphics window public static void main(String[] args) { JFrame aWindow = new JFrame(); //ERROR, SEE NOTE*

5 aWindow.setSize(300, 200); aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); aWindow.setTitle("Empty Window"); aWindow.setVisible(true); } }

*eclipse says “JFrame type cannot be resolved into a type” about this line.

20. Here is a Square class and a driver, SquareDriver. When the driver executes, a programmer expects it to output “side = 3.0”, instead it outputs “side = 0.0”. Fix the error that is causing the problem. public class SquareDriver { //Tests the Square Class public static void main(String[] args) { Square sq = new Square(3.0); System.out.println(sq.toString()); } } public class Square { private double side; public Square(){ double side = 0.0; } public Square(double s){ double side = s; } public String toString(){ return "Side = " + side; } }

6 21. Here is a Sphere class with a default constructor. A. Write an accessor method for the class.(8 points)

B. Write a constructor that takes a double argument. If the value of the argument is positive or zero it should set myRadius to that value. If it is negative it should set myRadius to zero and output a message that says the radius can't be negative.(8 points) public class Sphere { //instance variable private double myRadius; //Constructor public Sphere(){ myRadius = 0.0; } //Write another constructor

//Write an accessor

//volume of Sphere public double volume(){ return 4.0/3.0 * Math.PI * Math.pow(myRadius, 3.0); }

7

Recommended publications