Java Final Review Solutions 1. Give a short definition of each of the following words. (all are on the final!) a. Exception-a problem that can keep an application or applet from running. b. return value-the number (or other data) returned by a method. c. Cast-temporarily treating data like another data type. (e.g. (int)dDouble) d. continue-"jumps" to top of loop. e. break-"jumps" to end of block of code. f. Syntax error-a spelling error or typo in code. g. out-of-bounds index-an integer in square brackets that is too large or too small. h. case-part of a switch statement. i. GUI-graphical user interface, an applet for example. j. Object-a collection of data and behavior. k. member methods-methods that belong to the same class. l. member variables-variables that belong to the same class. m. Argument-data that is passed to a method. n. Parameter-same as an argument. o. public-available to any class. p. private-available only inside the class it is a member of. q. import-a way to include code from other programmers in your program. r. void-"nothing" s. keyword-a word that has special meaning and cannot be used as the name of a variable or method. t. Constructor-a special method used to initialize member data. u. boolean-a primitive data type that can only hold true or false. v. primitive data type-simple data that only has one part-e.g. int, double, float, char, boolean w. Hungarian notation-a system for naming variables and methods. x. long-a bigger mailbox than int for holding integers. y. Literal-NOT a variable (e.g. 3, 'c' or "Hello" NOT nNum, cChar or sString) z. Relational operator- > < >= <= == != aa. Logical operator- && || ! bb. Choice--a gui component for pull down choice menus 2. Complete the following code fragment so that it displays the sum of all the multiples of 3 from 1 to nNum. (Assume that nNum has already been declared and initialized) int nInt, nSum; _ nSum = 0 _; for (_ nInt = 1; nInt <= nNum; nInt++_){ if (_ nInt % 3 == 0 __) nSum = nSum + nInt; } System.out.println("" + nSum); 3. Explain why each of the following assignment statements is invalid. a. int 2Age = 45; can't use a number for first character in variable name. b. char cLetter = "C"; chars use single quotes, not double quotes c. float fWeight = "43.0"; no quote marks should be used with floats (or any other numeric data type) d. int Joe'sKids = 4; can't use ' as part of variable name e. int nPrice = 243.67; can't store decimals in an int f. nPrice = !nPrice; ! can only be used with true and false. 4. Assume that variable exp1 is equal to true, exp2 is equal to false, and exp3 is equal to true. Evaluate the following expressions. a. exp1 || exp2 && exp3 true b. exp1 && exp2 false c. exp1 && exp2 || exp3 true d. exp1 || exp3 true e. (exp1 || exp2) && exp3 true f. !exp1 false 5. Given the following nested Java if statement: if(nA == nB) nX = 10; else if(nC == nD) nX = 55; else nX = 100; what is the value of nX under the following conditions? a. nA = 5, nB = 1, nC = 100, nD = 50 b. nA = 5, nB = 10, nD = 0, nD = 4 c. nA = 0, nB = 100, nC = 5, nD = 0 nX is 100 in all three 6. Given the following logical expression: (nA == nB) && (nA != nC) evaluate the expression under the following conditions: a. nA = 0, nB = 0, nC = 5 true b. nA = 1, nB = 50, nC = 100 false 7. Given the following nested loop, how many times will the System.out.println() be executed? int nX = 1; int nI = 1; while(nI < 25){ int nJ = 100; while nJ > 50){ nX = nX + (nI * nJ); nJ -= 10; System.out.prinln("hello"); } nI = nI + 5; } hello will be displayed 25 times, the outer loop will run 5 times (nI = 1, 6, 11, 16, 21) and the inner loop will run 5 times (nJ = 100, 90 , 80, 70, 60)