Lang 2faced Bit - Ly Boolean Myvar Do4Others
Lang 2faced Bit - Ly Boolean Myvar Do4Others
Practice Midterm
=====================================================================
SHORT ANSWER SECTION [18 points total]
=====================================================================
1) TRUE/FALSE - Please circle your choice: Tr for true, Fa for false. [1 point each = 9 total]
Tr Fa a) In Java, to declare a primitive variable that could hold a real number such as 13.243
we can write "double number;".
Tr Fa b) Java programs are compiled into machine code level binary files.
Tr Fa c) Java bytecode files (*.class) are interpreted and executed in the same step.
Tr Fa d) We must import the Scanner class from the java.util package in order to use it in a
Java program.
Tr Fa e) We can put a float type value into an int type variable without explicit casting.
Tr Fa f) If a programming language doesnt have at least one way to specify loops, it is not a
general purpose programming language.
Tr Fa g) Java uses sequence as the default order of statement execution.
Tr Fa h) Counter controlled loops are best implemented with a do/while loop in Java.
Tr Fa i) Repeating until the user enters "no" is an example of a sentinel controlled loop.
2) Circle only the legal (will compile) identifiers below. [3 points]
lang
2faced
bit_ly
boolean
MyVar
do4others
Page 2 of 7
2) Write numbers to indicate the relative ordering of these programming phases. [4 points]
____ testing & debugging
4) What are two good reasons for creating sample runs during the first phase of programming?
[4 pts]
=====================================================================
Code Tracing: After each code segment below, write down the exact output it would produce in
the box provided. Show work for partial credit. Assume the following variables have been
defined for use in each segment. Also assume that each segment is independent of the others.
Warning: the indentation is purposely misleading in some cases. [58 points total]
=====================================================================
char ch;
// 4) ------------------------------------------------------------------------------------------------------------ 5 points
intA = 13;
intB = 2;
db1 = (intA + 4) / -intB;
db2 = intA / 2.0;
intA = 17 % 4;
System.out.println(intA + " " + intB + " " + db1 + " " + db2);
Page 3 of 7
// 5) ----------------------------------------------------------------------------------------------------------- 3 points
// 6) ----------------------------------------------------------------------------------------------------------- 4 points
// 7) ----------------------------------------------------------------------------------------------------------- 4 points
boolean a = true, b = true;
if (a || ! b)
System.out.println("ab");
if ( ! (a && b) == !a || !b ) {
System.out.println("cd");
if (! a && b) System.out.println("ef");
else System.out.println("gh");
}
Page 4 of 7
// 8) ----------------------------------------------------------------------------------------------------------- 5 points
numA = 5;
db1 = (double) -10 + Math.floor(3.995) * 2 / numA;
System.out.printf("result is %.4f", db1);
// 9) ----------------------------------------------------------------------------------------------------------- 5 points
intA = 15;
intB = 3;
db1 = 5;
do {
System.out.println( intA + " " + db1 );
db1 = db1 + intB;
intA = intA - 2;
} while ( intA > db1);
System.out.println(intB);
Page 5 of 7
// 11) --------------------------------------------------------------------------------------------------------- 4 points
String phrase = "Fall Break!";
String part = phrase.substring(2,7);
intA = phrase.length();
ch = phrase.charAt(1);
System.out.println(intA + " " + part + " " + ch);
// 12) ----------------------------------------------------------------------------------------------------------- 9 points
Page 6 of 7
// 13) ----------------------------------------------------------------------------------------------------------- 12 points
=====================================================================
Code Writing: write exact Java code to solve each problem as described. [24 points total]
=====================================================================
14) Assume that variable word has been declared as a String and initialized with user input. This
Java statement is supposed to output whether or not the string stored in variable word
contains the character 'p'. However, it contains three errors. What are they and how
would you fix them? [3]
if (word.substring('p') >= 0);
System.out.println("p is in ", word);
else
System.out.println("not there");
Page 7 of 7
15) This loop is supposed to display all the even numbers (only) between 13 and 20 exclusive, on
the screen, all on the same line. However, there are three errors. What are they and how
would you fix them? [3]
for ( int num = 13; num < 20; num++)
System.out.println(num);
16) Suppose you want to read input similar to this example from the keyboard:
X 20 $23.99 some sort of text
The first data value will be a single character item code, then an integer quantity, followed by
a price including the '$', then some more text, all separated by spaces. You must store this
data in four variables: a character called "type", an integer called quantity, a real number
called "price" (without the $), and for all the rest, a string called what. Write all the Java
variable declarations and statements necessary to make this happen, in the main method
below. Do not overly complicate this! [14]
public static void main(String[] args) {
}