Sei sulla pagina 1di 5

CS-404, 6/3/15. Exam 1. Name_______________________________________________________ p. 1 of 5.

. (–1 point for name missing on this page)

Problem 1 (20 pts) Write a Java program that has the user enter integers k and m. The program then outputs
all integers from k to m. You may use abbreviations “SOP” and “SOPln” and may also call
“kbd.nextInt( )” without defining it. Do not do any error checking. Here are three sample runs:

Enter k: 9
Enter m: 21

9 10 11 12 13 14 15 16 17 18 19 20 21
Enter k: 15
Enter m: -2

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 -1 -2
Enter k: 293
Enter m: 293

293

public static void main(String[] args) {

}
CS-404, 6/3/15. Exam 1. Name_______________________________________________________ p. 2 of 5.
. (–1 point for name missing on this page)

Problem 2 (20 pts) Write an algorithm that determines if two dimensional, n by n, integer array B has a row
in which all the values are the same. If there is such a row, return true; otherwise return false. For
example, if n = 4 and B were the array below on the left, the algorithm would return false, but if B were the
array on the right, the algorithm would return true.

7 7 9 7 8 9 10 13
15 2 15 15 0 4 0 9
108 44 44 44 27 27 27 27
63 63 63 64 -5 981 0 3

boolean hasRowWithAllValuesTheSame(int [][] B, int n) {

}
CS-404, 6/3/15. Exam 1. Name_______________________________________________________ p. 3 of 5.
. (–1 point for name missing on this page)

Problem 3 (15 pts) Write an algorithm that returns the integer in array A that appears the most times in a row,
i.e. consecutively, where n is the length of A. For example, if A were the array below, the
algorithm would return the integer 17 because it appears three times consecutively and no other
integer in A appears more than three times consecutively.

A 81 81 39 17 39 17 17 17 81 81 39 5 39 39

int integerThatAppearsMostTimesInARow(int [] A, int n) {

}
CS-404, 6/3/15. Exam 1. Name_______________________________________________________ p. 4 of 5.
. (–1 point for name missing on this page)

Problem 4 (4 pts) Expand (write an equivalent expression without parentheses) the following: (𝑥𝑥 − 3)3.

Problem 5 (4 pts) Evaluate: log 2 256.

Problem 6 (4 pts) Which of the following choices is equal to 2𝑥𝑥 + 2𝑥𝑥 + 2𝑥𝑥 + 2𝑥𝑥 ?
4
(A) 4𝑥𝑥 (B) 8𝑥𝑥 (C) 2𝑥𝑥+2 (D) 2𝑥𝑥+4 (E) 42𝑥𝑥 (F) 24𝑥𝑥 (G) 2𝑥𝑥 (H) 22𝑥𝑥 (I) NOP

𝑛𝑛(𝑛𝑛+1)
Problem 7 (4 pts) 𝑓𝑓(𝑥𝑥) = + 3 log 𝑛𝑛 − 𝑛𝑛 + 10000 belongs in which categories (circle all that apply):
2

(a) θ (lg n) (
(b) θ n 2 log n ) (c) θ (n) (d) θ (n lg n) (e) θ (n 2 )
(f) O(lg n) (
(g) O n 2 log n ) (h) O(n) (i) O(n lg n) (j) O(n 2 )
(k) Ω(lg n) (
(l) Ω n 2 log n ) (m) Ω(n) (n) Ω(n lg n) (o) Ω(n 2 )

Problem 8 (4 pts) Exam 1 counts 10%, Exam 2 counts 15%, Exam 3 counts 20%, Exam 4 counts 25%, and the
Final Exam counts 30%. If you score 50 on Exam 1, 80 on Exam 2, 70 on Exam 3, and 50 on Exam 4, what
score do you need on the Final Exam in order for your weighted average to be exactly 60?
CS-404, 6/3/15. Exam 1. Name_______________________________________________________ p. 5 of 5.
. (–1 point for name missing on this page)

void mysteryAlgorithm(int n) { Problem 9 Output Window:


int i, j, k, m = 0;

for (i = 2; i <= n; i = i * 2) {
System.out.println(i);
m++;
}

for (i = 1; i <= m-4; i++)


for (j = 1; j < n/8; j++)
for (k = 1; k <= j; k++)
System.out.println(i + ", " + j + ", " + k);
}

Problem 9 (10 pts) Show the output of mysteryAlgorithm(32) in the


output window on the right.

Problem 10 (10 pts) Exactly how many lines of output would mysteryAlgorithm(128) produce?
Do NOT trace through this, but DO show your work.

Problem 11 (5 pts) The running time of mysteryAlgorithm belongs to which of the following categories (circle
all that apply):

(A) θ (1) (B) θ (lg n) (C) θ ((lg n) 2 ) (D) θ (n) (E) θ (n lg n) (F) θ (n (lg n) 2 )
(G) θ (n 2 ) (H) θ (n 2 lg n) (I) θ (n 2 (lg n) 2 ) (J) θ (n 3 ) (K) θ (n 3 lg n) (L) NOP

Potrebbero piacerti anche