CSE 114 Exam 1 Review Answer Key
CSE 114 Exam 1 Review Answer Key
CSE 114 Exam 1 Review Answer Key
Key (JLee)
Disclaimer: This is not 100% like the real exam. This is just review set modeled as such.
Question 1: Basic Java Syntax
_____ Which of the following would print the double variable result with four digits after the decimal
point?
A. System.out.println(“%.4d”, result);
B. System.out.printd(“%4f”, result);
C. System.out.printf(“%4f”, result);
D. System.out.println(“%.4f, result);
E. System.out.printf(“%.4f”, result);
_____ When executing the following code, what value is stored in variable x?
double x = (int) (19 / 6 – 9.8 + 1);
A. 5.0
B. ERROR
C. -5.0
D. -5.6333333333
E. -6.0
_____ Which of the following is NOT a method in the Scanner class?
A. nextString()
B. nextDouble()
C. next()
D. nextFloat()
E. nextLine()
_____ When executing the following code, which is the correct output?
String num = "15.6";
C. 15, 15.6, 15
D. ERROR
Perform the indicated mathematical operations. Write your final answers on the lines provided.
a. What is the sum of decimal numbers (27)10 + (48)10 in hexadecimal. Show your work.
Answer: _____________
27 + 48 = 75
75 / 16 = 4 Remainder 11
4 / 16 = 0 Remainder 4
b. What is the sum of the binary number (00101100)2 and hexadecimal number (1B4)16 in decimal?
Show your work.
Answer: _____________
44 + 436 = 48010
Question 3: Number Systems
Perform the indicated mathematical operations. Write your final answers on the lines provided.
Answer: _____________
10110111102
Answer: _____________
From binary to hexadecimal (base 16), you cluster bits in groups of 4 starting from right to left. When
there aren’t enough bits to make groups of 4, you pad the front with extra 0’s.
To go from binary to octal (base 8), you do the same thing except with groups of 3.
Answer: _____________
From binary to hexadecimal (base 16), you cluster bits in groups of 4 starting from right to left. When
there aren’t enough bits to make groups of 4, you pad the front with extra 0’s.
Answer: ______13______
b. int y = -3;
y -= (y++) + y;
System.out.print(y);
Answer: ______2_______
c. int y = -10;
y -= (++y) + 1 / y;
System.out.print(y);
Answer: _____-1________
d. int x = 7;
System.out.print(x + " ");
int y = (x++) + (++x);
System.out.print(y + " ");
int z = (y--) + x;
System.out.print(z);
Answer: ______0_______
Question 5: Selections
a. What will be displayed by the following code?
char ch = 'b';
switch(ch) {
case 'a':
case 'A': System.out.print(ch);
case 'b':
case 'B': System.out.print(ch);
case 'c':
case 'C': System.out.print(ch);
case 'd':
case 'D': System.out.print(ch);
}
Answer: ______bbb_______
b. Given the following main method, what will be displayed by the following code?
public static void main(String [] args) {
boolean a = false;
boolean b = !a;
boolean c = (25 % 3 == 0 || 2 != 3);
if (a && !a){
if(c)
System.out.println("32");
else System.out.println("31");
}
else if(c || b && a){
if(c)
System.out.println("30");
else System.out.println("29");
}
else System.out.println("28");
}
Answer: _____30________
Answer: _____9________
Question 6: Loops
Choose the ONE option which best answers the question. Write the letter for your choice on the blank
line provided.
What is the output for the following code? Please consider each completely separately.
Code 1
A. 7 6 5 4 3 2 1
B. 6 5 4 3 2 1 0
C. 7 6 5 4 3 2 1 0
D. 5 4 3 2 1 0
E. 6 5 4 3 2 1
Code 2
B. 3 4 5
C. 6 8 10
E. 3 3 4 4 5 5
Code 3
A. 3 9 15
B. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
D. 1 3 5 7 9 11 13 15 17
E. 3 6 9 12 15
Code 4
A. 0 1 2 3 0 1 2 3 0 1 2
B. 0 1 2 3 0 1 2 3 0 1 2 3
C. 1 2 3 0 1 2 3 0 1 2 3 0
D. 1 2 3 0 1 2 3 0 1 2 3
Choose the ONE option which best answers the question. Write the letter for your choice on the blank
line provided.
What is the output for the following code? Please consider each completely separately.
Code 5
A. 1 1
B. 0 1
C. 0 0
D. 1 1
Code 6
A. A B C D E
B. A B C D E F
C. Compiler Error
D. 65 66 67 68 69 70
E. 65 66 67 68 69
Question 7: Arrays
Please consider the following codes separately. If there is an infinite loop, please write “INFINITE”. If
there is no output, please write “NONE”. If there is any other error, please write “ERROR”.
y = x;
Answer: ___ERROR_____
Note: Before you question the answer to this problem, take note of the syntax. Since y is an array,
you’d get the length by using “y.length” instead of “y.length()”. The latter would break your code, as
emphasized in Professor Tashbook's slides “Introduction to Arrays (Chapter 7)”.
c. The following code is for a main method, bubbleSort method and a printArray method.
public static void main(String[] args) {
double[] list = new double[] { 6.0, 4.4, 1.9, 2.9, 3.4, 2.9, 3.5 };
bubbleSort(list);
}
public static void bubbleSort(double[] list) {
boolean changed = true;
do {
printArray(list);
System.out.println("");
changed = false;
for (int j = 0; j < list.length - 1; j++) {
if (list[j] > list[j + 1]) {
double temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
changed = true;
}
}
} while (changed);
}
Answer: _______________________________________________________________
Disclaimer: The short responses can be done multiple ways. This is merely one way of doing
it.
Question 8: Short Response – Product Calculator
Write a method that takes two integer values. One will serve as a starting value while the other serves
as the ending value. The method then calculates the product of every integer within the range,
inclusively, and returns the product.
Write a method isPalindrome that takes a user-specified String input and determines whether or not the
String is a palindrome. Assume the user-specified String consists of at least 3 characters. If it is a
palindrome, return, “word is a palindrome”. Otherwise, return, “word is NOT a palindrome”.
int[] totalDays = {jan, feb, mar, apr, may, jun, jul, aug, sep, oct,
nov, dec};
String[] monthNames = {"January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December"};