Exam
Name___________________________________
MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. | ||
1) | 6 bits can be used to represent ________ distinct items or values. | 1) ______ |
A) 6 | |
B) 24 | |
C) 32 | |
D) 20 | |
E) 64 |
2) | Once we have implemented the solution, we are not done with the problem because | 2) ______ |
A) the solution may, at a later date, need revising because of new programming language features | |
B) the solution may not be the best (most efficient) | |
C) the solution may have errors and need testing and fixing before we are done | |
D) the solution may, at a later date, need revising to handle new specifications | |
E) all of the above |
3) | Which character below is not allowed in an identifier? | 3) ______ |
A) ^ | |
B) 0 (zero) | |
C) _ | |
D) q | |
E) $ |
4) | Which of the following would be a legal Java identifier? | 4) ______ |
A) i | |
B) class | |
C) ilikeclass! | |
D) idon'tlikeclass | |
E) i-like-class |
5) | An error in a program that results in the program outputting $100 instead of the correct answer, $250 is | 5) ______ |
A) a programmer error | |
B) a logical error | |
C) a syntax error | |
D) a snafu | |
E) a run-time error |
6) | Which of the following would be a good variable name for the current value of a stock? | 6) ______ |
A) curstoval | |
B) theCurrentValueOfThisStockIs | |
C) currentStockVal | |
D) csv | |
E) current |
7) | If you want to output the text "hi there", including the quote marks, which of the following could do that? | 7) ______ |
A) System.out.println("\"hi there"); | |
B) System.out.println("\"hi there\""); | |
C) System.out.println("hi there"); | |
D) System.out.println(""hi there""); | |
E) none, it is not possible to output a quote mark because it is used to mark the beginning and ending of the String to be output. |
8) |
What value will z have if we execute the following assignment statement? double z = 5 / 10; | 8) ______ |
A) z will equal 5.0 | |
B) z will equal 0.5 | |
C) z will equal 0.05 | |
D) z will equal 0.0 | |
E) none of the above, a run-time error arises because z is a double and 5 / 10 is an int |
9) | A cast (ie saying (int)x/y is required in which of the following situations? | 9) ______ |
A) using charAt to take an element of a String and store it in a char | |
B) storing an int in a double | |
C) storing a double in a double | |
D) storing a double in an int | |
E) all of the above require casts |
10) |
What will be the result of the following assignment statement? Assume b = 5 and c = 10. int a = b * (-c + 2) / 2; | 10) ______ |
A) 30 | |
B) -30 | |
C) -6 | |
D) 20 | |
E) -20 |
11) | Assume that x, y and z are all ints equal to 50, 20 and 6 respectively. What is the result of x / y / z? | 11) ______ |
A) A syntax error as this is syntactically invalid | |
B) 16 | |
C) A run-time error because this is a division by 0 | |
D) 12 | |
E) 0 |
12) | What is output with the statement System.out.println(""+x+y); if x and y are int values where x=10 and y=5? | 12) ______ |
A) x+y | |
B) 15 | |
C) 10 5 | |
D) 105 | |
E) An error since neither x nor y is a String |
13) | In the String major = "Computer Science", what is returned by major.charAt(1)? | 13) ______ |
A) 'm' | |
B) 'C' | |
C) 'o' | |
D) "C" | |
E) "Computer" |
14) | Which of the sets of statements below will add 1 to x if x is positive and subtract 1 from x if x is negative but leave x alone if x is 0? | 14) ______ |
A)
if (x > 0) x++; else x--; |
|
B)
if (x > 0) x++; else if (x < 0) x--; |
|
C)
if (x > 0) x++; if (x < 0) x--; else x = 0; |
|
D)
if (x == 0) x = 0; else x++; x--; |
|
E)
x++; x--; |
Given the nested if-else structure below, answer the question(s) below. if (a > 0) if (b < 0) x = x + 5; else if (a > 5) x = x + 4; else x = x + 3; else x = x + 2; | ||
15) | If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed? | 15) ______ |
A) 0 | |
B) 2 | |
C) 3 | |
D) 4 | |
E) 5 |
16) |
Consider the following code that will assign a letter grade of 'A', 'B', 'C', 'D', or 'F' depending on a student's test score. if(score >= 90) grade = 'A'; if(score >= 80) grade = 'B'; if(score >= 70) grade = 'C'; if(score >= 60) grade = 'D'; else grade = 'F'; | 16) ______ |
A) This code will work correctly only if grade >= 60 | |
B) This code will not work correctly under any circumstances | |
C) This code will work correctly only if grade < 60 | |
D) This code will work correctly only if grade < 70 | |
E) This code will work correctly in all cases |
17) |
Assume that count is 0, total is 20 and max is 1. The following statement will do which of the following? if (count != 0 && total / count > max) max = total / count; | 17) ______ |
A) The condition short circuits and the assignment statement is executed without problem | |
B) The condition will not compile because it uses improper syntax | |
C) The condition does not short circuit causing a division by zero error | |
D) The condition short circuits so that there is no division by zero error when evaluating the condition, but the assignment statement causes a division by zero error | |
E) The condition short circuits and the assignment statement is not executed |
18) |
If x is an int where x = 1, what will x be after the following loop terminates? while (x < 100) x *= 2; | 18) ______ |
A) 2 | |
B) 100 | |
C) 128 | |
D) 64 | |
E) none of the above, this is an infinite loop |
19) |
Given the following code, where x = 0, what is the resulting value of x after the for-loop terminates? for(int i=0;i<5;i++) x += i; | 19) ______ |
A) 10 | |
B) 15 | |
C) 4 | |
D) 0 | |
E) 5 |
20) |
The following nested loop structure will execute the inner most statement (x++) how many times? for(int j = 0; j < 100; j++) for(int k = 100; k > 0; k--) x++; | 20) ______ |
A) 20,000 | |
B) 10,000 | |
C) 100 | |
D) 200 | |
E) 1,000,000 |
21) |
Given that s is a String, what does the following loop do? for(int j = s.length( ); j > 0; j--) System.out.print(s.charAt(j-1)); | 21) ______ |
A) it prints s out backwards but does not print the 0th character | |
B) it prints s out backwards | |
C) it prints s out forwards | |
D) it yields a run-time error because there is no character at s.charAt(j-1) for j = 0 | |
E) it prints s out backwards after skipping the last character |