Final Practice Solution
Final Practice Solution
a) Procedural
b) ObjectOriented
c) Functional
2) (2 pts.) How many distinct values can be represented by a 7bit value?
a) 64
b) 128
c) 256
d) 512
e) 1024
3) (2 pts.) Is it a compiletime or runtime error when a Java program throws an exception because
it cannot open a file?
a) Runtime
b) Compiletime
4) (2 pts.) A _____________ is a program that executes compiled Java code on a specific platform.
b) Java Compiler
d) Eclipse Editor
5) (2 pts.) Circle the letter of the Java statement that declares and allocates a 2dimensional array
of integers with four rows and five columns:
a) int array[4][5];
b) int array[5][4];
a) Yes
b) No
b) Any class
c) 1dimensional array
d) 2dimensional array
8) (2 pts.) Name 8 primitive data types and one Java class, identifying which is which.
Primitive types: char, byte, short, int, long, float, double, boolean
10)(1 pt.) Show the Java code to declare and allocate an array with 497 elements of type double.
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
try {
} catch (IOException e) {
System.out.println(“Cannot open file: “ + filename);
}
14) (2 pts.) Show a line of code that shifts the binary literal 0b00000101 left by 2 bits into a variable
named byteValue of type byte that has previously been declared.
15) (2 pts.) What is the value of byteValue in the previous question after the shift operation has
been performed? You can show the result as a binary literal or a decimal value.
0b00010100 or 20
if (grade <= 5)
school = “Elementary School”;
else if (grade <= 8)
school = “Junior High”;
else if (grade <= 12)
school = “High School”;
else
school = “College”;
17) (4 pts.) Write a Java switch statement for the table above that is identical to the statement in the
previous problem.
if (grade <= 5)
school = “Elementary School”;
else if (grade <= 8)
school = “Junior High”;
else if (grade <= 12)
school = “High School”;
else
school = “College”;
18) ( 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();
CS160, Sample Final Exam, Page 5
19)(2 pts.) Convert the following into a valid Java expression: 15.2 ≤ limit ≤ 62.9:
20) (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);
21) (1 pt. each) Write the output for the following Java code:
22) (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;
23) (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.
25) (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
System.out.println(tripleNumber(10));
System.out.println(tripleNumber(variable));
System.out.println(tripleNumber(values[2]));
System.out.println(values[2]);
doubleArray (values);
System.out.println(values[2]);
array[i] *= 2.0;
}
Line 0: 57.31 22
Line 1: Computer Science
Line 2: 332211 1234.5
Line 3: Have a nice summer!
Line 4: double: 468.531