0% found this document useful (0 votes)
69 views7 pages

Lang 2faced Bit - Ly Boolean Myvar Do4Others

This summary provides the high level information from the document: 1) The document contains a practice midterm for an intro to Java programming course. It has multiple choice, short answer, code tracing, and code writing questions. 2) For the code tracing section, code segments are provided and the student must write the output each segment would produce. Variables like intA, intB, etc. are predefined. 3) For the code writing section, the student must write Java code to solve problems described, like reading input from the keyboard and storing it in variables. There are errors to identify and fix in provided code snippets as well.

Uploaded by

Yasir Camarah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views7 pages

Lang 2faced Bit - Ly Boolean Myvar Do4Others

This summary provides the high level information from the document: 1) The document contains a practice midterm for an intro to Java programming course. It has multiple choice, short answer, code tracing, and code writing questions. 2) For the code tracing section, code segments are provided and the student must write the output each segment would produce. Variables like intA, intB, etc. are predefined. 3) For the code writing section, the student must write Java code to solve problems described, like reading input from the keyboard and storing it in variables. There are errors to identify and fix in provided code snippets as well.

Uploaded by

Yasir Camarah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

600.

107 Intro to Programming in Java

Practice Midterm

This test is closed book/notes.

=====================================================================
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

____ define & analyze the problem

____ write (JAVA) program code

____ maintain & upgrade

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]
=====================================================================




int intA, intB;

double db1, db2;

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

if ( 13 + 5 <= 15 || 'A' + 3 < 'F')



System.out.print(" one ");

else

System.out.print(" two ");

System.out.print(" three ");

// 6) ----------------------------------------------------------------------------------------------------------- 4 points

if ( "word".length() == 4 && ! (Math.ceil(2.222) == 2)) {



System.out.print(" $$ ");

if ( 'M' < 'a' )


System.out.print(" ## ");
}
else

System.out.print(" ++ ");

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

// 10) ----------------------------------------------------------------------------------------------------------- 7 points



String line = A;

for (ch = B; ch <= C; ch++)

line = ch + line + ch + \n;


System.out.println(line);

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

String s = "midterm time";



intA = 11;

while ( intA > 0 ) {


switch (s.charAt(intA)) {


case 'd': case ' ': System.out.print(" a "); break;


case 'e': System.out.print(" b ");


case 't': System.out.print(" c "); break;


case 'r': System.out.print(" d ");


case 'm': System.out.print(" e "); break;


case 'I': case 'y':


default: System.out.print(" + ");

}


intA = intA - 2;
}


Page 6 of 7

// 13) ----------------------------------------------------------------------------------------------------------- 12 points

for (intA = 1; 10 > intA ; intA *= 2) {




System.out.print(intA);


for (intB = 5; intB < 10 ; intB += intA)


System.out.print( o );


System.out.println();
}





=====================================================================
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) {




















}

You might also like