CS 0401 Fall 2011 Quiz 1 Version A Solutions
1)
Fill in the
Blanks (10 points – 2 points
each). Provide the MOST appropriate
answers.
a) We build /
execute our Java programs in two steps.
In the first step we use the
____javac____ program to compile our .java files into
.class files. In the second step we use the ____java_______
program to run our .class files.
b)
Give
the values for M and N after the execution of the Java statements below:
int M = 6;
int N = --M; M ___5____ N ____5___
c)
Give
the value for X after the execution of the Java statements below:
double X, Y = 2.0, Z = 3.0;
X = 4 + Y * Z – 1; X ___9.0___
d) Java
_____method_____________ variables have a scope
from the point of declaration to the end of the block in which they are
declared.
e) Java methods provide us with functional (or
procedural) _______abstraction____________ meaning that we do not need to know the implementation
details in order to use them.
2) True / False (10 points – 2 points each). Correct
false statements for full credit.
a)
Java
byte code (.class files) can be
executed on any computer in which the compatible JRE is installed. TRUE
b)
The
output of the program below is "Yes":
double X = 0.6666666;
double Y = 2.0/3.0;
if (X == Y)
System.out.println("Yes");
FALSE – due to precision limits
the values are not exactly equal
c) The
multiplication operator in Java has a higher associativity than the addition operator. FALSE
– it has a higher precedence
d) The output of the program
segment below is "TwoÓ:
int
test = 2;
switch (test) {
case 1: System.out.println(ÒOneÓ);
case 2:
System.out.println(ÒTwoÓ);
case 3:
System.out.println(ÒThreeÓ);
}
FALSE – Three will
also be printed since switch cases are not exclusive
e) Because
Java parameters are passed by value, objects such as StringBuilder cannot
be changed (mutated) within a Java method. FALSE – the argument is a reference
to the object, which allows the object to be mutated via method calls.
3)
Short Answers (10 points – 2
points each) For each of the Java code
segments below, briefly answer one of the following: 1) there is a syntax (compiler) error,
2) there is a run-time error
(exception), 3) there is a logic
error or 4) the code is ok (no
errors). Assume everything up to
the code segment (ex: declarations, imports, etc) is
correct. Be very specific about what the error is for full credit.
a)
float
x = 5.5;
Answer: This will produce a compiler
error, since the literal 5.5 is a double and the variable x is a float. In Java more precise types cannot be
assigned to less precise variables without explicit
casting.
b)
int
val = inScan.nextInt(); // inScan is a valid Scanner,
// and user enters 10.0
Answer:
This will produce a InputMismatchException run-time
error because the value input is not the correct type for the variable.
c)
int
numCorrect, numTotal;
// numCorrect and numTotal are input, with numCorrect <=
numTotal
double
frac = numCorrect / numTotal;
System.out.println("Your
fraction correct is: " + frac);
Answer: This has a logic error, since it
is doing integer division when float division is intended. The value of frac will be 0 when
numCorrect < numTotal
d)
int
ctr = 1;
int max = 10;
System.out.println(ÒOdd
numbers from Ò + ctr + Ò to Ò + max);
while
(ctr != max)
{
System.out.print(ctr + Ò Ò);
ctr = ctr + 2;
}
System.out.println();
Answer: This is an infinite loop (logic
error) because ctr is never equal to 10.
e)
if
(num1 == guess1)
if
(num2 == guess2)
System.out.println("Both
guesses right");
else
System.out.println("First guess was wrong");
Answer: This is a logic error due to the
"dangling else" problem.
The else is actually associated with the second if, not the first.
4) Tracing (10 points) Give all output produced by the execution of the Java program below. Use the bottom of the page for your output and clearly mark it by drawing a box around it.
|
public class trace1 { public
static void wacky(int one, int
two) { for (int i
= one; i < two; i++) { for (int j = two; j >= i; j--) { System.out.print("[" + i + ","
+ j + "]"); } System.out.println(); } } public
static void main(String [] args) { int p = 3, q = 7, r = 9; boolean b1 = p < q; boolean b2 = q < r; boolean b3 = r < p; System.out.println("Starting trace"); if (!(b1 && b2)) System.out.println("First condition is true"); else System.out.println("First condition is false"); if (!b1 || !b2) System.out.println("Second condition is true"); else System.out.println("Second condition is false"); if (b1 && b2 || b3) System.out.println("Third condition is true"); else System.out.println("Third condition is false"); wacky(3,6); } } |
|
Starting trace First condition is false Second condition is false Third condition is true [3,6][3,5][3,4][3,3] [4,6][4,5][4,4] [5,6][5,5] |
5) Coding
(10 points) Write a complete
Java program that does ALL of the following:
a)
It
prompts the user to enter two integers, LOW and HIGH, with HIGH greater than or
equal to LOW.
b)
It reads
in the numbers using the Scanner class.
c)
If HIGH
is less LOW, it outputs an error message and ends the program. Otherwise, it uses a loop to
calculate the sum of the numbers from LOW to HIGH (inclusive), and prints out
the answer.
|
Answer: Answers may vary. One possible solution is below |
|
import
java.util.Scanner; public
class QuizCode { public static void main(String [] args) { Scanner
inScan = new Scanner(System.in); int LOW, HIGH; System.out.println("Please enter two integers"); System.out.println("Please make second >=
first"); LOW
= inScan.nextInt(); HIGH
= inScan.nextInt(); if (HIGH < LOW) System.out.println("Please follow directions!"); else { int sum = 0; for (int i = LOW; i <= HIGH; i++) sum += i; System.out.println("The sum is " + sum); } } } |