CSE 114 Exam 1 Review Answer Key

You are on page 1of 13

CSE 114: Computer Science I – Spring 2017 – Midterm Exam #1 Review Answer

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";

int value = Integer.parseInt(num);


double value2 = Double.parseDouble(num);
float value3 = Float.parseFloat(num);

System.out.print(value + ", " + value2 + ", " + value3);

A. 15, 15.6, 15.6

B. 15.6, 15.6, 15.6

C. 15, 15.6, 15

D. ERROR

E. 15.0, 15.6, 15.6


Question 2: Number Systems

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

Read remainders from bottom to top: 4B16

b. What is the sum of the binary number (00101100)2 and hexadecimal number (1B4)16 in decimal?
Show your work.

Answer: _____________

(1*22) + (1*23) + (1*25) = 4 + 8 + 32 = 44

(1*162) + (11*161) + (4*160) = 256 + 176 + 4 = 436

44 + 436 = 48010
Question 3: Number Systems

Perform the indicated mathematical operations. Write your final answers on the lines provided.

a. What is the decimal number (734)10 in binary? Show your work.

Answer: _____________

734/2 = 367 Remainder 0 367/2 = 183 Remainder 1

183/2 = 91 Remainder 1 91/2 = 45 Remainder 1

45/2 = 22 Remainder 1 22/2 = 11 Remainder 0

11/2 = 5 Remainder 1 5/2 = 2 Remainder 1

2/2 = 1 Remainder 0 1/2 = 0 Remainder 1

10110111102

b. What is the hexadecimal number (9A3)16 in base 8? Show your work.

Answer: _____________

9A3 in binary is 1001 1010 0011

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.

100 110 100 011 converts to 46438

c. What is the binary number (10010101100)10 in hexadecimal? Show your work.

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.

0100 1010 1100 converts to 4AC16


Question 4: Assignment Operators
What is the value of y after the following code is executed? Consider each case independently of the
others.
a. int x = 5;
int y = 7;
int z = (x++) + (++y);
System.out.println(z);

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: ___7 16 25______


e. int x = 1;
int y = (x++) - (--x);
System.out.print(y);

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________

c. What will be displayed by the following code?


int a = 5, b = 7, c = 7, d = 9;
System.out.println((a != b ? (d++) : (c + b)));

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

A. This loop produces an infinite loop.

B. 3 4 5

C. 6 8 10

D. This loop produces no output.

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

C. This loop produces no output.

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

E. This loop produces no output.


Question 6: Loops (cont.)

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

E. This loop produces an infinite loop.

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”.

a. What will be displayed by the following code?


int[] x = {2, 5, 6, 1, 9};
int[] y = {1, 4, 5};

y = x;

for (int i = 0; i < y.length(); i++) {


System.out.print(x[i] + " ");
}

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)”.

b. What will be displayed by the following code?


int[] x = {15, 8, 9, 0, 7, 12, 5};
int[] y = {8, 2, 4, 1, 9, 10, 5};

for (int i = 0; i < x.length; i++) {


if (i % 2 == 0) {
x[i] = y[i];
}
}

for (int j = 0; j < x.length; j++) {


System.out.print(x[j] + " ");
}

Answer: ___8 8 4 0 9 12 5____


Question 7: Arrays (cont.)
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”.

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);
}

public static void printArray(double[] list) {


for (int i = 0; i < list.length; i++) {
System.out.print(list[i] + " ");
}
}

What will be displayed by the following code?


(Hint: Write the output of the array for each time it runs through the loop.)

Loop 1: 6.0 4.4 1.9 2.9 3.4 2.9 3.5


Loop 2: 4.4 1.9 2.9 3.4 2.9 3.5 6.0
Loop 3: 1.9 2.9 3.4 2.9 3.5 4.4 6.0
Loop 4: 1.9 2.9 2.9 3.4 3.5 4.4 6.0

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.

public static int product(int start, int end) {


int p = 1;
for (int i = start; i <= end; i++) {
p *= i;
}
return p;
}
Question 9: Short Response - Palindromes
Palindromes are words, phrases, numbers or any series of characters which reads the same forward and
backward. For example, the word “racecar” spelled backward is still “racecar”. Same goes for the word
“eye”. However, “Norman” spelled backward is “Namron” and that’s not quite right :^).

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”.

public static String isPalindrome(String inString) {


String reverse = "";

for (int i = inString.length()-1; i >= 0; i--) {


reverse += inString.charAt(i);
}

if (inString.equals(reverse)) { // Make sure to use .equals() when


comparing Strings. == will not work.
return inString + " is a palindrome";
}

return inString + " is NOT a palindrome";


}
Question 10: Short Response – Happy Birthday
Assume you are given an array of integers of an unknown size. You may assume the integers inside
range from 1 to 365. Each number represents a specific day in the year correlating to a month and a day.
For example, 1 represents January 1st, 156 represents June 5th, and 365 represents December 31st. The
ranges are listed below:
January: 1 – 31 July: 182 – 212
February: 32 – 59 August: 213 – 243
March: 60 – 90 September: 244 – 273
April: 91 – 120 October: 274 – 304
May: 121 – 151 November: 305 – 334
June: 152 – 181 December: 335 – 365
Write a program that checks each value in an array, determines the corresponding month to that value,
increments each month’s respective counter by 1 and ultimately prints, “Month has NUMBER
birthdays” once all values have been checked. You may assume it is NOT a leap year.
(Hint: Declaring a String array with the names of the months will help when printing, as well as creating
a new array for the total number of days per month)
import java.util.*;
public class Birthday {

public static void main(String[] args) {


int[] days = { 333, 192, 228, 98, 222, 314, 319, 284, 293, 12, 56, 78,
99, 219, 304 };
int jan = 0, feb = 0, mar = 0, apr = 0, may = 0, jun = 0;
int jul = 0, aug = 0, sep = 0, oct = 0, nov = 0, dec = 0;

for (int i = 0; i < days.length; i++) {


if (days[i] >= 1 && days[i] <= 31) {
jan++;
}
else if (days[i] >= 32 && days[i] <= 59) {
feb++;
}
else if (days[i] >= 60 && days[i] <= 90) {
mar++;
}
else if (days[i] >= 91 && days[i] <= 120) {
apr++;
}
else if (days[i] >= 121 && days[i] <= 151) {
may++;
}
else if (days[i] >= 152 && days[i] <= 181) {
jun++;
}
else if (days[i] >= 182 && days[i] <= 212) {
jul++;
}
else if (days[i] >= 213 && days[i] <= 243) {
aug++;
}
else if (days[i] >= 244 && days[i] <= 273) {
sep++;
}
else if (days[i] >= 274 && days[i] <= 304) {
oct++;
}
else if (days[i] >= 305 && days[i] <= 334) {
nov++;
}
else if (days[i] >= 335 && days[i] <= 365) {
dec++;
}
}

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"};

for (int j = 0; j < totalDays.length; j++) {


System.out.println(monthNames[j] + " has " + totalDays[j] + "
birthdays");
}

You might also like