Exam 1 Solutions
Exam 1 Solutions
MIDTERM EXAM 1
(Score weight: 10% of total score)
Date: 05/27/2015
NAME: SOLUTION
UID:
Set A
15 x 4 = 60
1) When executing a program, the processor reads each program instruction from
A) secondary memory (storage)
B) the Internet
C) registers stored in the processor
D) main memory
E) could be any of these
Answer: D
Explanation: D) The program is first loaded from secondary memory into main memory before
it is executed so that the processor is not slowed down by reading each instruction.
8) An error in a program that results in the program outputting $100 instead of the correct
answer, $250 is
A) a programmer error
B) a syntax error
C) a run-time error
D) a logical error
E) a snafu
Answer: D
9) Which of the following is a legal Java identifier?
A) 1ForAll
B) oneForAll
C) one/4/all
D) 1_4_all
E) 1forall
Answer: B
10) Mistyping "println" as "printn" will result in
A) a syntax error
B) a run-time error
C) a logical error
D) no error at all
E) converting the statement into a comment
Answer: A
11) Consider the following statement:
System.out.println("1 big bad wolf\t8 the 3 little pigs\n4 dinner\r2night");
This statement will output ________ lines of text
A) 1
B) 2
C) 3
D) 4
E) 5
Answer: B
Explanation: B) The \t escape sequence inserts a tab, but leaves the cursor on the same line. The
\n escape sequence causes a new line to be produced so that "4 dinner" is output on the next line.
The escape sequence \r causes the carriage to return (that is, the cursor to be moved back to the
left margin) but because it does not start a new line, "2night" is output over "4 dinn" resulting in
a second line that looks like "2nighter".
12) What value will z have if we execute the following assignment statement?
float z = 5 / 10;
A) z will equal 0.0
B) z will equal 0.5
C) z will equal 5.0
D) z will equal 0.05
E) none of the above, a run-time error arises because z is a float and 5 / 10 is an int
Answer: A
Explanation: A) 5 and 10 are both int values, so 5 / 10 is an integer division. The result is 0.
Even though z is a float and can store the real answer, 0.5, it only gets 0 because of the integer
division. In order to get 0.5, we would have to first cast 5 or 10 as a float.
13) Given the following assignment statement, which of the following answers is true regarding
the order that the operators will be applied based on operator precedence?
a = (b + c) * d / e - f;
A) *, /, +, B) *, +, /, C) +, *, /, D) +, /, *, E) +, -, *, /
Answer: C
Explanation: C) Order of precedence is any operator in ( ) first, followed by * and / in a left-toright manner, followed by + and - in a left-to-right manner. So, + is first since it is in ( ),
followed by * followed by / since * is to the left of /, followed finally by -. Both * and / are at
the same level of precedence but evaluated left-to-right. The same is true of + and -.
14) Assume that x, y, and z are all integers (int) equal to 50, 20, and 6 respectively. What is the
result of x / y / z?
A) 0
B) 12
C) 16
D) A syntax error as this is syntactically invalid
E) A run-time error because this is a division by 0
Answer: A
15) What is output with the statement System.out.println(""+x+y); if x and y are int values where
x=10 and y=5?
A) 15
B) 105
C) 10 5
D) x+y
E) An error since neither x nor y is a String
Answer: B
Set B
20 x 1
1. Write a program that will input some number of cents (less than 100) and output the
number of quarters, dimes, nickels and pennies needed to add up to that amount.
Example input and output are shown below:
Input : 99
Output: 3 quarter(s), 2 dime(s), 0 nickel (s), 4 pennie(s)
Input: 34
Output: 1 quarter(s), 0 dime(s), 1 nickel (s), 4 pennie(s)
import java.util.Scanner;
public class Change
{
public static void main(String[ ] args)
{
Scanner scan = Scanner.create(System.in);
System.out.println("Enter the amount of change");
int amount = scan.nextInt( );
System.out.println("The change for " + amount + " cents is: ");
int quarters = amount / 25;
System.out.println(" " + quarters + " quarters");
amount = amount - quarters * 25;
int dimes = amount / 10;
System.out.println(" " + dimes + " dimes");
amount = amount - dimes * 10;
int nickels = amount / 5;
System.out.println(" " + nickels + " nickels");
amount = amount - nickels * 5;
int pennies = amount;
System.out.println(" " + pennies + " pennies");
}
}
Set C
5 x 2 = 10
1. Write an output statement which will output the following characters exactly as shown:
/ ' \" / ' \
System.out.println("/ \ ' \ \ \" / \ ' \ \ ");
2. Write four different program statements that increment the value of an integer variable
total.
total = total + 1;
total++
++total
total += 1
3. Identify and explain two syntax errors from the following program
public class Enigma
{
public static void main(String[ ] args)
{
System.out.println("Input a String");
int x = scan.nextString( );
int x = 5;
}
}
Variable x is redefined
Variable scan is undefined
10