CS 12300 Exam 1 Review The exam is on Thursday, February 18. The exam is over Chapters 1 - 5 from Think Java (from Chapter 5, Sections 5.1, 5.2, 5.3, 5.5, 5.7, 5.8), and the following sections from https://runestone.academy/runestone/books/published/csjava/index.html Sections 1.2, 1.3, 1.4. Sections 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.9. Sections 3.1, 3.2, 3.3, 3.4. The topics on the exam are: 1) variables a) name (identifier) b) type (int, double, boolean, String) c) value d) declaration e) initialization 2) expressions a) expressions contain variables, literals, and operators b) operator precedence (order of operations) c) integer division and mod operators d) concatenation operator 3) static methods a) method definition i) declaration ii) body b) method call c) method return d) method parameters and arguments e) the "flow of control" for methods f) the Math class 4) methods in objects a) String methods b) Scanner methods c) Turtle methods d) object attributes 5) memory and stack diagrams and the Java Visualizer 6) if-statement 7) boolean expressions 8) if-else-statement Here are a few review problems. Problem 1) For each of the following sentences, indicate whether it is a language rule that is enforced by the Java compiler or a convention (that is, not enforced by the Java compiler but most programmers follow it). (a) Variable names must begin with a lower-case letter. (b) The name of a class must match the name of the file containing it. (c) All variables must be declared. (d) There must be a space before and after each operator. (e) All lines after a left curly brace must be indented. (f) Class names must begin with an upper-case letter. (g) Every program must begin with comments describing the author of the program and what the program does. Problem 2) Find, and correct, three syntax errors in the following program. public class Incorrect public static void main(String[] args) { x = 3; System.out.println( "the value of x is " x ); System.out.println( "is that OK?" ) } } Problem 3) What does the following method print out when it is called? If there are any blank lines in the output, be sure to clearly state that. public static void myMethod() { System.out.print( "*" ); System.out.print( "*\n" ); System.out.println( "***" ); System.out.print( "**\n*" ); System.out.println( "*" ); } Problem 4) What does the following method print out when it is called? If there are any blank lines in the output, be sure to clearly state that. public static void mathod1() { System.out.print( "*" ); System.out.print( "*\n" + "**" ); System.out.println( "*\"*\"" ); System.out.print( "\n**\n*" ); System.out.println( "\\*\\" ); } Problem 5) What should be the data type for the variable x and the data type for the variable y in order that the following two lines of code compile correctly. Also, what will be the value of y after the two lines are executed? Briefly explain your answer. x = "2" + 2; y = x + 2; Problem 6) Assume that. int x = 1; int y = -3; int z = 101; What is the type and value for each of the following expressions? (a) 3 * z - y / x (b) (2.0 + x) / (1 - y) (c) x + 2 <= y (d) x + y + "w" (e) x + (y + "w") Problem 7) In the following java program, complete the definitions of the variables a, b, c so that the program prints the twelve lines of output given below it. public class Question1 { public static void main(String[] args) { String s1 = "oranges-limes-lemmons\n"; String s2 = "grapes-kumquats\n"; String s3 = "planes-trains-automobiles\n"; String s4 = "stampeding-horses\n"; String s5 = "Buffalo-buffalo-Buffalo\n"; String a = String b = String c = System.out.print( b + c + b + a + c ); } } oranges-limes-lemmons grapes-kumquats planes-trains-automobiles stampeding-horses Buffalo-buffalo-Buffalo oranges-limes-lemmons grapes-kumquats planes-trains-automobiles oranges-limes-lemmons grapes-kumquats stampeding-horses Buffalo-buffalo-Buffalo Problem 8) In the following program, complete the definition of the main method so that the program, when it is run, would produce the following output. * * * * * 12345-12345-12345-12345-12345- * * * * * public class Problem9 { public static void methodA() { System.out.println(" *"); System.out.println(" *"); System.out.println(" *"); System.out.println(" *"); System.out.println("*"); } public static void methodB() { System.out.println("*"); System.out.println(" *"); System.out.println(" *"); System.out.println(" *"); System.out.println(" *"); } public static void main(String[] args) { String s = "12345-"; } } Problem 9) Suppose that String s = "one two three four"; What are the values of the following expressions? (a) s.indexOf("e") (b) s.lastIndexOf("e") (c) s.substring( s.indexOf("e"), s.lastIndexOf("e") ) (d) s.indexOf("z") (e) s.substring(3,8).trim().length() (f) s.substring(0,3) + "2" + s.substring(8) (g) s.substring(4,5).toUpperCase() + s.substring(5,7) Problem 10) Suppose the String variable ssn holds a social security number in the form "xxx-xx-xxxx". Define a String variable ssn2 whose value is the last four digits of the social security number contained in ssn. Problem 11) Suppose the String variable pn is a phone number in the form "xxx-xxx-xxxx". Define a String variable pn2 whose value is the phone number contained in pn converted into the format "(xxx) xxx-xxxx". So, as an example, the string "219-989-2273" would be converted to "(219) 989-2273". You can assume that the phone number in pn is formatted correctly. Problem 12) Suppose the String variable pn is a phone number in the form "(xxx) xxx-xxxx". Define a String variable pn2 whose value is the phone number contained in pn converted into the international format "+1-xxx-xxx-xxxx". So, as an example, the string "(219) 989-2273" would be converted to "+1-219-989-2273". You can assume that the phone number in pn is formatted correctly. Problem 13) How many Turtle objects does the following code create? Another way to ask this questions is, how many turtles are drawn on the screen window that is represented by the World object? Explain your answer by drawing and using a Java memory diagram, similar to the way the Java Visualizer draws memory diagrams. World w = new World(400,400); Turtle bob = new Turtle(w); Turtle alice = bob; Turtle jane = new Turtle(w); Turtle joe = alice; Problem 14) When a Turtle is created it is always pointed north. When the following code is done, in what direction is the Turtle named alice heading? (The turnRight() method makes a Turtle turn 90 degrees clockwise.) Draw a Java memory diagram and use it to explain why your answer is correct. World w = new World(400,400); Turtle alice = new Turtle(w); alice.turnRight(); Turtle jane = alice; jane.turnRight(); Turtle joe = jane; joe.turnRight(); Problem 15) Draw a memory diagram for all the variables in these lines of code. String salutation = "Greetings"; salutation.toUppercase(); String s2 = salutation.substring(0, 5); String s3 = s2.toLowerCase(); String s4 = s2 + s3; Problem 16) Give the output of the following program segment if: (a) x=1 and y=1, (b) x=2 and y=2. if (x > 1) { if (y > 2) Sysytem.out.println(2*x-y); else Sysytem.out.println(2*x+y); } else { if (y > 2) Sysytem.out.println(x-y); else Sysytem.out.println(x+y); } Problem 17) Explain all the possible values that the variable w can have after the following code is executed. Assume that u and v are int's defined elsewhere in the program. int w = 0; if (u > 0) w++; if (v > 0) w++; Problem 18) Explain all the possible values that the variable x can have after the following code is executed. Assume that u and v are int's defined elsewhere in the program. int x = 0; if (u > 0) x++; else if (v > 0) x++; Problem 19) The following method contains a single error. Describe the error and show how to fix it. private static int findLastDigit(int n) { if (n > 0) return n % 10; else if (n < 0) return -n % 10; } Problem 20) Fill in the blanks so that the following method definition is correct. public static ________ max2( _______ a, int ______ ) { int result = a; if (b > a) { result = ________; } _________ result; } Problem 21) Write a complete definition for a public static method called closeEnough that takes a double and an integer and returns true if the double is within one unit of the integer. For example closeEnough(3.9, 5) -> false closeEnough(4.1, 5) -> true closeEnough(6.0, 5) -> true