Oop Java Codes Summary
Oop Java Codes Summary
Final Exam
Spring 2014
Answer Key
I, the undersigned, do hereby affirm that the work contained in this exam is solely my own, and that
none of the results were achieved by copying or otherwise cheating. This includes using Internet
access or another communication device to get help or find answers. Please do the work yourself.
Name ____________________________________________________________________
(printed legibly)
Signature____________________________________________________________________
a) Procedural
b) Object-Oriented
c) Functional
a) 64
b) 128
c) 256
d) 512
e) 1024
3) (2 pts.) Is it a compile-time or run-time error when a Java program throws an exception because
it cannot open a file?
a) Run-time
b) Compile-time
4) (2 pts.) A _____________ is a program that executes compiled Java code on a specific platform.
5) (2 pts.) Circle the letter of the Java statement that declares and allocates a 2-dimensional array
of integers with four rows and five columns:
a) int array[4][5];
b) int array[5][4];
c) int array[ ] [ ] = new int [4][5];
d) int array[ ] [ ] = new int [5][4];
e) None of the above
a) Yes
b) No
8) (2 pts.) Name 3 primitive data types and one Java class, identifying which is which.
Primitive types: char, byte, short, int, long, float, double, boolean
Classes: String, Scanner, PrintWriter, P5, R5, etc.
9) (1 pt.) Show the Java code to declare a variable of type String and initialize it to “Whatever”.
String myString = “Whatever”; or
String myString = new String(“Whatever”);
10) (1 pt.) Show the Java code to declare and allocate an array with 497 elements of type double.
double dArray[] = new double[497];
11) (2 pts.) Show one line of Java code that declares, allocates, and initializes an array of type
integer with exactly 4 elements whose values are 97, 33, 44, and 12, in that order.
12) (1 pt. each) Evaluate the following Java expressions, assuming x and y are both declared
as integer variables and x = 17 and y = 6:
a) x / y + 3 = 5
b) x % y * 4 = 20
c) x – y / 3 = 15
d) x / 5 – y * 2 = -9
14) (4 pts.) Write a Java switch statement for the table above that is identical to the statement in the
previous problem.
switch
(grade)
{
case
1:
case
2:
case
3:
case
4:
case
5:
school
=
“Elementary
School”;
break;
case
6:
case
7:
case
8:
school
=
“Junior
High”;
break;
case
9:
case
10:
case
11:
case
12:
school
=
“High
School”;
break;
default:
school
=
“College
School”;
}
15) (2 pts.) Using a Scanner object called input that has been instantiated and initialized, write code
to read a string (without spaces), double, and integer from a file into variables you declare.
String
s
=
input.next();
double
d=
input.nextDouble();
int
i
=
input.nextInt();
17) (2 pts.) Write a Java statement to assign the sixth character of the string variable s to a
previously declared character variable named c.
c = s.charAt(5);
18) (2 pts.) Given a variable x declared as a double and initialized, show the code to cast and store
the value of x into a variable y which is defined as a short. Truncation will occur, which is fine.
y = (short) x;
19) (2 pts.) Show the code to create an object named myObject by instantiating a class called
MyClass and passing the constructor a String literal “hello” as the argument.
21) (5 pts.) Show the declaration for a method called myMethod that 1) is visible outside the
class, 2) can only access class (static) variables, 3) returns an array of integers, and 4)
accepts two parameters which are a String and double, in that order.
// Initialize array
for (int index = 0; index < array.length; index++)
array[index] = (index * 3.0) + 0.5;
p q p ⊕ q p → q p ∧ q p ↔ q p ∨ q
F F F T F T F
F T T T F F T
T F T F F F T
T T F T T T T
25) Truth Tables (8 points): Fill in the truth table shown below with T for true and F for false.
p q r p ∨ q p → r q → r (p→ r) ∧ ( q→ r) (p ∨ q) ∨ ¬ q
F F F F T T T T
F F T F T T T T
F T F T T F F T
F T T T T T T T
T F F T F T F T
T F T T T T T T
T T F T F F F T
T T T T T T T T
26) Definitions (2 points): Is the compound proposition (p ∨ q) ∨ ¬ q shown above a contradiction, tautology, or contingency?
A. Tautology
B. Contradiction
C. Contingency
D. None of the above
27) Logical Equivalence (2 points): Are the compound propositions (p→ r) ∧ ( q→ r) and (p ∨ q) ∨ ¬ q shown above logically
equivalent?
A. No
B. Yes
a) Propositions:
p: sitting next to Brad and Angelina
q: have a very good table
r: be a movie star
s: handed someone a tip
b) Axioms:
p → q ! (r " s) – 2 points
p – 1 point
¬ s – 1 point
c) Conclusion:
q ! r – 2 points
1) p → q ! (r " s) Axiom
2) p Axiom
3) ¬s Axiom
6)
7)
8)
9)