CS161 – Winter 2015

FINAL EXAM

Name:______

1. (4 pts) Write the Java code for the following:(variable names are in boldface).

a.Boardis assignedBoard plus Header

b.Stuff is assignedStuff mod4

c.Abe is assigned Total divided byCount

d.Interestis assignedBalance multiplied byRate

2. (1 pt ea ) In Java, the computer would evaluate the following expressions to be:

a) 6 * 4 - 9 * 3 / 2 ______b) 5 % 4* 1 ______c) 2 / 5 ______

3. (4 pts) Which of the following are valid identifiers (circle the letter)?

a) no wayb) cross_countryc) 5i11id) int%

e) c3p0f) zeroWrngg) 1_2_3h) Money$$

4 (2 pts) A method that does not return a value has return type ______

5. (2 pts) ) What are two visibility modifiers in Java? ______

6. (3 pts) What are the three parts of a loop?

7. (1 pt ea) What are the following operators in Java?

a. assignment:______b. logical OR ______

c. logical AND______d. remainder______

e. increment______f. relational equal ______

g. greater than or equal ______h. not equal to ______

i. conditional ______j. string concatenation ______

k. decrement______l. integer division ______

8. (4 pts) Explain why the main() method is declared public and static?

9. (5 pts) Write a Java while loop toprint the integers from 1 to 150(inclusive), in readable format, with 8 integers on a line of output and a comma and space after each integer.

10 (5 pts) Write the code necessary to declare adouble variable calledDoorPrizeNum and assign it a random value between 25.0 and 97.5.

  1. (6 pts) Write a segment of code, including afor loop, to print a list of the multiples of 7: starting at 7 * 2 and ending with 7 * 85. Be sure that the output is readable.

12.. (3 pts) Write a line of code to declare an array of 12,000floats.

13. (4 pts) What is a dialog box in Java? List four kinds of dialog boxes.

14..(2 pts) Given an array called things, which is already declared and filled, write one line of code that will print the size of the array on the screen.

15. (3 pts) Some methods are declared to be static, How does that change the way the method is called? Give an example of calling a static method.

16. (8 pts) Write a segment of code, including asentinel loop, which does the following:

a)asks the user for a positive number, store it in a variable called num,

b) print the message "You have entered the value <num>!" on the screen, where <num> is the actual number entered by the user

c) prints the message again -- until the user enters the number -1.
(you may optionally create a verification of input loop that prints an error message for any negative value other than -1)

17. USE THE FOLLOWING CLASS DEFINITION FOR QUESTION #17:

class ReserveBook {

private String title;

private boolean OnShelf;

public ReserveBook( String newBook ) {

title = newBook;

OnShelf = true;

}

public void BookGoesOut() {

OnShelf = false;

}

public void BookComesIn() {

OnShelf = true;

}

public void ShowStatus() {

System.out.print("\n”+ title + " is”);

if( ! OnShelf )

System.out.print(" not”);

System.out.println(" On the Shelf\n”);

}

}

  1. (2 pts) What is the name of this class?
  1. (2 pts) List the names, if any, of the instance variables of this class.
  1. (2 pts) List the names, if any, of the constructors of this class
  1. (3 pts) List the names, if any, of the methods of this class.
  1. (3 pts) Write the code declaring myBook to be an object of this class, having the title War and Peace.
  1. (3 pts) Write the code to call BookGoesOut() for the object myBook.

18. (3 pts) Write the code to declare an array of 40 ReserveBook objects.

  1. (12 pts) Write a segment of Java code, which will:

a)get input from the user identifying the LETTER printed on the “fortune” in their fortune cookie

b) use a switch statement to print one of the following responses - take care of lower case input-

Your FortuneResponse

A:Ski season is here??

B:Don’t forget your sunscreen!….

C:Let’s go to Hawaii!

Anything else: Enjoy Winter Break!

20. (9 pts) In Java, three kinds of objects are needed to create a Graphical User Interface. List the three and give a definition and an example for each.

a)

b)

c)

21.(2 pts) In a Java applet, where on the screen is the coordinate <0,0> ?

22. (8 pts) You have been given an array called FinalScores, which has already been declared and filled with 25 test scores of type double. Write a segment of code which will do the following:

a)print each of the scores on the screen, in readable format

b)calculate the average of the scores

c)print the average

23 (4 pts) Radio Buttons and Check Boxes are both Button Components. How are they similar and how are they different?

24.(10 pts) Write a method called sumThingOddthat receives two integer parameters, First and Second. Return the sum of the odd integers between but not including First and Second. If First is bigger than Second, or if there are no odd integers between First and Second, return zero.

25. ( 10 pts) Write a method called minOfFour that receives fourfloat parameters, and returns thesmallest of the four floats.

page - 1