Computing Science 114 Solutions to Midterm Examination Tuesday October 19, 2004 INSTRUCTOR: I E LEONARD TIME: 50 MINUTES In Questions 1 20, Circle EXACTLY ONE choice as the best answer 1 [2 pts] What company initially designed the Java programming language? (a) Microsoft (b) Sun (c) Intel (d) Oracle 2 [2 pts] What translates each line of source code into machine language and executes that line of code? (a) assembler (b) loader (c) compiler (d) interpreter 3 [2 pts] What set of rules determines whether or not a particular statement is correctly formulated? (a) semantic (b) syntactic (c) logical (d) precedence 4 [2 pts] What type of errors are detected by the Java compiler? (a) syntactic (b) semantic (c) logical (d) executable 5 [2 pts] What is a named memory location that can store a value of a particular type? (a) instance (b) object (c) class (d) variable
6 [2 pts] What is the extension used for the Java bytecode? (a) java (b) byte (c) class (d) app 7 [2 pts] What is a variable that is declared within a method called? (a) an instance variable (b) a class variable (c) a local variable (d) a static variable 8 [2 pts] What Java data type can have only two possible values? (a) button (b) boolean (c) byte (d) void 9 [2 pts] What type of access modifier is used to make a method accessible only within the class in which it is defined? (a) public (b) private (c) protected (d) prorated 10 [2 pts] In the following code fragment, what is displayed when issleeping is false and is Studying is true? if (issleeping) Systemoutprint(" Sleeping "); Systemoutprint(" Resting "); if (isstudying) Systemoutprint(" Studying "); (a) Sleeping (b) Resting (c) Studying (d) Resting Studying
11 [2 pts] In the following code fragment, what is displayed when iseating is true and isstudying is false? if (iseating) { if (isstudying) Systemoutprint(" Studying "); } else Systemoutprint(" Eating "); (a) Studying (b) Eating (c) Studying Eating (d) nothing is displayed 12 [2 pts] What keyword is used by an object to refer to itself? (a) abstract (b) extend (c) this (d) private 13 [2 pts] What data type is used to represent a real number? (a) short (b) double (c) byte (d) long 14 [2 pts] Which of the following declares a constant named RED? (a) public final RED = 0; (b) public static int RED; (c) public static final int RED = 0; (d) public final int RED = 0;
15 [2 pts] If a data type uses 4 bits in its representation, how many values can be represented? (a) 4 (b) 8 (c) 16 (d) 32 16 [2 pts] What operator has the highest precedence, that is, is evaluated first in an expression? (a) && (b) ( ) (c) (d)! 17 [2 pts] Which of the folowing for loop headers would be used to iterate from 10 down to 1? (a) for (int k = 1; k < 10; k++) (b) for (int k = 1; k < 10; k--) (c) for (int k = 10; k > 1; k--) (d) for (int k = 10; k > 0; k- -) 18 [2 pts] Which of the following loop headers would result in an infinite loop? (a) for (int k = 0; k > 10; k++) (b) for (int k = 0; k < 10; k- -) (c) for (int k = 0; k < 10; k++) (d) for (int k = 0; k > 0; k++) 19 [2 pts] Which loop structure does not contain syntax for the initializer and the updater? A (a) for (b) repeat (c) while (d) do while
20 [2 pts] In the code fragment below, what is the value of sum when the while loop terminates? int N = 12345; int n = N, sum = 0; while (n > 0) { sum = sum + n%10; n = n/10; } (a) 10 (b) 15 (c) 20 (d) 25 21 [2 pts] Perform the bitwise operation indicated below: 0 1 0 1 1 & 0 1 0 0 1 0 1 0 0 1 22 [2 pts] Perform the bitwise operation indicated below: 0 1 0 1 1 1 0 1 0 1 1 1 1 1 1
23 [2 pts] What is the output of the following code fragment? int limit = 1; Output: 1 for (int k = 1; k <= limit; k++) 2 { 3 Systemoutprintln(k); 4 limit++; 5 } 6 24 [2 pts] What is the output of the following code fragment? int limit = 0; Output: 0 for (int k = 0; k <= limit; k++) 0 { 0 Systemoutprintln(k); 0 k--; 0 } 25 [2 pts] Write a loop in JAVA which will display the cubes of the first 50 positive integers in decreasing order int limit = 50; for (int k = limit; k >= 1; k++) { Systemoutprintln(k*k*k); k--; }