600.107 Intro to Programming in Java PRACTICE TEST 1 Classify each of the following as hardware or software (circle choice): hard soft 1) appletviewer hard soft 2) Windows98 hard soft 3) printer hard soft 4) operating system hard soft 5) my_program.java hard soft 6) CPU True/False - circle your choice true false 9) Data stored in a memory location overwrites and destroys any data previously stored there. true false 10) A bit can have the value 0 or 1 only. true false 11) The compiler requires that each statement in a Java program appears on its own line. true false 12) Compiler errors result from mistakes in figuring out how to solve a problem. true false 13) The java command in JDK interprets and runs Java bytecode. true false 14) Java is interpreted, but not compiled. true false 15) Java bytecode is machine/system independent and therefore universal. true false 16) Square brackets [] are used to group multiple Java statements into one block. 17) What three words must be used at the start of every 'main' method definition? 18) Classify each of the following as either valid or invalid identifiers: valid invalid hep_cat valid invalid a valid invalid Dr. H valid invalid book-mark valid invalid class valid invalid 21st_century valid invalid funny$ 19) What are two different ways in which we use identifiers in a Java program? 21) What is the exact output of each of the following code segments? Write the word "nothing" if no output is created. Assume the following variables have been declared: int n1, n2; double d1, d2; // start n1 = 17; n2 = n1/5; d1 = n1/4; d2 = n1/2.0; n1 = 22 % 5; System.out.println(n1 + " " + n2 + " " + d1 + " " + d2); // stop // start n1 = 3 - 14 / 2 % 5 + -3; System.out.println(n1); // stop // start d1 = 3 * 5 - 1.5 * 5 / 3; System.out.println(d1); // stop // start n1 = 3; n2 = 8; if (n1 <= n2) System.out.print(n1 + " "); System.out.print(n2 + " "); System.out.println("end"); // stop // start d1 = 13.4; d2 = 9.801; if (d1 == d2) System.out.println("equal"); else if (d1 > d2) System.out.println("greater"); else { System.out.println("less"); System.out.println("end");} // stop // start n1 = 16; d1 = 3.2; n2 = 0; while (d1 < n1) { n2 = n2 + 1; d1 = d1 + 5; } System.out.println(n2 + " " + d1); // stop // start if ( 23 <= 5) if (4 >= 2) System.out.println("middle"); else System.out.println("not"); System.out.println("end"); // stop // start if ( 3 <= 15) if (4 >= 2) System.out.println("happy"); else System.out.println("sad"); System.out.println("end"); // stop // start if ( 13 <= 5) if (4 <= 2) System.out.println("tiny"); else { System.out.println("huge"); System.out.println("end"); } // stop // start if ( 13 <= 5) { if (4 <= 2) System.out.println("tiny"); else System.out.println("huge"); System.out.println("end"); } // stop // start n1 = 3; while (n1 < 8) { if (n1 % 2 == 0) System.out.println(n1); n1 = n1 + 3; if (n1 % 3 == 0) System.out.println(n1); n2 = n1; while (n2 < 6) { System.out.println(n2); n2 = n2 + 1; } } // stop // start n2 = 0; for (n1 = 24; n1 <= 30; n1 = n1 + 2) n2 = n2 + n1; System.out.println(n2); // stop // start char ch; String s1 = new String("this is it"); n1 = 0; n2 = 0; do { if (s1.charAt(n1) == 'i') n2 = n2 + 1; n1 = n1 + 2; } while (n1 < s1.length()); System.out.println(n2); // stop // start int num = 6; while (num < 16) { switch (num % 4) { case 1: System.out.println("one"); break; case 2: System.out.println("two"); case 3: System.out.println("three"); break; case 0: System.out.println("multiple"); } num += 3; } // stop 22) Write a program to read exactly 5 integers from the user and output their average. You must use standard input and output. Assume the values are all on one line of input. Be sure to inlude any necessary Java libraries. 23) Write a sequence of statements that will read the first line of a plain text file named "input.txt" and write it to a text file called "output.txt".