180E2F06
180E2F06
1. What happens when you try to assign a value larger than the maximum possible integer to an int
variable?
2. Which of the following patterns is produced when the following code snippet is executed?
(a) *....
**...
***..
****.
*****
(b) *....
**...
***..
****.
*****
(c) .....
....*
...**
..***
.****
(d) .****
..***
...**
....*
.....
1
3. What keyword is used to specify that a data member is a class data member (shared among all in-
stances of that class)?
(a) final
(b) shared
(c) public
(d) static
int x = 0;
for(int i = 0; i < 10; i++){
if(i == 5){
continue;
}
x++;
}
(a) 5
(b) 10
(c) 9
(d) 6
(a) 8
(b) 11
(c) 9
(d) 10
2
8. Which of the following is true about exceptions in Java?
(a) I only
(b) I and III
(c) II and III
(d) III only
9. Given the program below, what will be the values of asum and bsum?
3
10. Which of the following is true about try/catch blocks in Java?
11. What will the word “apple” become after being encrypted 101 times using the following encryption
table?
a -> e
e -> l
l -> p
p -> a
(a) eaapl
(b) pllea
(c) leeap
(d) apple
try {
int num = Integer.parseInt("four thousand two hundred and ninety-five");
System.out.println("Your number is: " + num + ".");
} catch (NumberFormatException n) {
System.out.println("You don’t have a number.");
} catch (Exception e) {
System.out.println("Something went terribly terribly wrong.");
} finally {
System.out.println("Number parsed successfully!");
}
4
13. Which subclass of Throwable is an exception checked at compile time?
(a) NullPointerException
(b) RuntimeException
(c) IOException
(d) ArrayIndexOutOfBoundsException
(a) 9876543210
(b) 0 1 4 9 16 25 36 49 64 81
(c) 9850
(d) an ArrayIndexOutOfBoundsException
15. Which layout manager places components in one of five regions: north, south, east, west, and center?
(a) AbsoluteLayout
(b) GridLayout
(c) BorderLayout
(d) FlowLayout
16. A frame’s __________ designates the area of the frame excluding the title, menu bar, and the
border.
(a) MouseButtonEvent
(b) ActionEvent
(c) MouseClickEvent
(d) MouseEvent
5
18. What is displayed by the following code?
(a) 0, 9
(b) 74, 0
(c) 9, 0
(d) 0, 74
(a) an InvalidArgumentException
(b) 210
(c) 90
(d) an ArrayIndexOutOfBoundsException
6
20. What is the value of arrr after the following?
(a) 0, 0, 1, 0, 1, 1, 0, 0, 0, 0
(b) 3, 2, 2, 1, 1, 2, 0, 1, 2, 3
(c) 0, 1, 1, 2, 2, 1, 3, 2, 1, 0
(d) 0, 0, 1, 0, 1, 2, 0, 1, 2, 3
The version of your test is A. Please FILL IN CIRCLE (A) for the TEST FORM field on the BUBBLE
SHEET directly under the DATE field and turn in your exam booklet and answer sheet to the stack labeled
(A) in the front of the classroom. Thank you.
7
This page is left blank intentionally.
8
Part II. Programming Questions (60 points total):
1. (20 points) You are creating a High-Low game. The first player will enter the “magic number”. Your
program must accept a “magic number” greater than zero and less than 100. You should continue
prompting for the “magic number” until a valid number is entered.
You should then prompt the second player for their guess. You should print out “Higher” if the magic
number is higher than the guess and you should print out “Lower” if the magic number is less than
the guess. Continue prompting the user until they have guessed the number. When the magic number
has been guessed you should print out a corresponding message and then end the program.
You should use the JOptionPane to prompt for input and display output. Remember it will return a
string you will have to parse. You can assume all input will be integers. You are not required to write
methods. All of your code can be in the main function.
9
Solution for programming question 1:
10
2. (20 points) You are given a partially complete SimpleGUI class which pops up a GUI with one text
field, two buttons (“ok” and “cancel”), and a feedback label. You must augment this class to make it
respond to button events.
Implement the following behavior:
• ok button pushed:
– Set the feedback label to the text in the textField.
– Clear the textField.
• cancel button pushed:
– Set the feedback label to “cancel pushed”.
– Clear the textField
You must add the necessary code to the SimpleGUI constructor in order for this class to receive action
events. You must add the necessary code to implement the described behavior. If you need to alter
the class declaration, you should be sure to do that as well.
SimpleGUI template is given on the following pages.
11
This page is left blank intentionally.
12
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public SimpleGUI() {
contentPane = getContentPane();
contentPane.setLayout( new GridLayout( 3, 1 ) );
// TODO Part1
// Insert code here to register this frame to receive action events
setDefaultCloseOperation( EXIT_ON_CLOSE );
}
// TODO Part2
// Insert code here to implement the described behavior
13
Solution for programming question 2: Please note you do not need to copy any of the given code. Just
label your code with TODO Part1 and TODO Part2. If you need to do any other code modification of
the given template, please label it TODO Part3.
14
This page is left blank intentionally.
15
This page is left blank intentionally.
16
3. (20 points) Write a program that asks the user to enter 300 integers between 0 and 2000 inclusive.
After the user enters these numbers, the statistical mode should be displayed. The statistical mode is
the most frequently occurring number. If there is a tie for the mode, ”no mode” should be displayed.
You need not perform error checking.
Example:
Mode: no mode
Hint: You should consider using an array of integers to keep a running total of the number of times
a particular number has been entered. e.g. count[180] can contain the total number of times the user
entered the number 180.
17
Solution for programming question 3:
18