CS 12300 Exam 1 Review The exam is on Thursday, September 26. The exam is over the following sections from the textbook, Sections 1.2, 1.3, 1.4, 1.5 Sections 2.1, 2.2, 2.3, 2.4, 2.5 Here is a list of the Self-Check Problems that have been assigned from Chapters 1 and 2. Chapter 1: 10, 11, 12, 18, 19, 21, 22, 23, 26, 29, 30, 32. Chapter 2: 5, 9, 10, 11, 17, 19, 20, 21, 22, 23, 28, 29, 30, 31, 32, 34, 35, 36. The topics on the exam are: 1) variables a) name (identifier) b) type (int, double, String, boolean) c) value d) declaration e) initialization 2) expressions a) made up of variables, literals, and operators b) operator precedence (order of operations) c) mod operator d) concatenation operator e) increment, decrement operators 4) static methods a) method definition i) declaration ii) body b) method call c) method return d) the "flow of control" for methods e) procedural decomposition 5) for-loops a) the four parts of a for-loop (the "syntax" of a for-loop) initialization, test, update, body b) the "flow of control" for for-loops c) nested for-loops 6) variable scope 7) class constants Here are a few more 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) Each of the following two program segments prints out the first five positive odd integers. (Notice that each for-loop has an empty update.) int i; int i; for (i = 1; i < 10; ) for (i = 0; i < 10; ) { System.out.println( i++ ); { i = i + 1; i = i + 1; System.out.println( i++ ); } } What would be the output of each program segment if we had forgotten to include the pair of braces from each of them? Explain your answer. Problem 8) What do each of the following for-loops print? (Notice that each for-loop has an empty body.) (a) for (int i = 5; i > 0; System.out.print(i-- + " ")); (b) for (int i = 5; i > 0; System.out.print(--i + " ")); (c) for (int i = 5; i-- > 0; System.out.print( i + " ")); (d) for (int i = 5; --i > 0; System.out.print( i + " ")); Problem 9) What is the output from the following program segment? for (int i = 2; i <= 5; i++) for (int j = 5; j > i; j = j-1) System.out.println(i + " and " + j); Problem 10) What does the following program print? Justify your answer by showing the values of all of the program variables for each iteration of the for-loop. public class Mystery { public static void main(String args[]) { int y = 0, total = 0; for (int x = 1; x <= 10; x++) { y = 2 * x; System.out.println( y ); total += y; } System.out.println("Total is = " + total); } } Problem 11) Write nested for-loops to produce the following output. Your for-loops can use "magic numbers". Your for-loops only need to produce exactly these seven lines (there is no SIZE variable). 6 55 444 3333 22222 111111 0000000 Problem 12) Write a code fragment, using nested for-loops, whose output in the console will look like the following. Your code fragment does not need to be a complete program, it just needs to output these eight lines. (Hint: Don't try to compute the output numbers from the loop indices. To get the output numbers, just have a counter variable that counts up from 10, and print out the counter.) 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 Problem 13) Suppose you have to write a Java program that produces the following twelve lines of output. Give complete definitions for three methods that you would use to decompose your program so that there would be no repeated blocks of code in your program. (Each line of output can be produced by a single System.out.println statement.) 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 14) Write a code fragment that uses a constant called SIZE to print a line with 1+SIZE number of characters on the line and with the first character being an asterisk, the last character being an equal sign, and the rest of the characters being ampersands. Problem 15) 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-12345-12345-12345-12345-12345-12345-12345- * * * * * public class Problem15 { public static void methodA() { System.out.print("12345"); } public static void methodB() { System.out.println(" *"); System.out.println(" *"); System.out.println(" *"); System.out.println(" *"); System.out.println("*"); } public static void methodC() { System.out.println("*"); System.out.println(" *"); System.out.println(" *"); System.out.println(" *"); System.out.println(" *"); } public static void main(String[] args) { } }