Gaddis: Starting Out with Java: Early Objects, 4/e Test Bank 1 Chapter 2 Exam Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) Which of the following is not a valid comment statement? 1) A) /** comment 4 B) /* comment 2 */ C) */ comment 3 /* D) // comment 1 2) When saving a Java source file, save it with an extension of 2) A).java B).javac C).src D).class 3) What will be displayed as a result of executing the following code? int x = 8; String msg = "I am enjoying java."; String msg1 = msg.touppercase(); String msg2 = msg.tolowercase(); char ltr = msg.charat(x); int strsize = msg.length(); System.out.println(msg); System.out.println(msg1); System.out.println(msg2); System.out.println("Character at index x = " + ltr); System.out.println("msg has " + strsize + " characters."); A) I am enjoying java. I AM ENJOYING JAVA. i am enjoying java. Character at index x = j msg has 20 characters. C) I am enjoying java.i AM ENJOYING JAVA.i am enjoying java.character at index x = omsg has 19 characters. B) I am enjoying java. I AM ENJOYING JAVA. i am enjoying java. Character at index x = o msg has 20 characters. D) I am enjoying java. I AM ENJOYING JAVA. i am enjoying java. Character at index x = y msg has 19 characters. 3) 4) This is a value that is written into the code of a program. 4) A) literal B) operator C) assignment statement D) variable 5) Which of the following is not a rule that must be followed when naming identifiers? 5) A) After the first character, you may use the letters a-z, A-Z, the underscore, a dollar sign, or digits 0-9. B) Identifiers can contain spaces. C) Uppercase and lowercase characters are distinct. D) The first character must be one of the letters a-z, A-Z, and underscore or a dollar sign. 6) Character literals are enclosed in ; string literals are enclosed in. 6) A) single quotes; double quotes B) double quotes; single quotes
Gaddis: Starting Out with Java: Early Objects, 4/e Test Bank 2 C) single quotes; single quotes D) double quotes; double quotes 7) What is the result of the following statement? 17 % 3 * 2-12 + 15 A) 105 B) 12 C) 8 D) 7 7) 8) What will be the value of z after the following statements have been executed? 8) int x = 4, y = 33; double z; z = (double) (y / x); A) 8.0 B) 4 C) 0 D) 8.25 9) What is the result of the following expression? 10 + 5 * 3-20 A) -5 B) -50 C) 5 D) 25 9) 10) Variables are classified according to their. 10) A) names B) location in the program C) value D) data type 11) Which of the following will correctly convert the data type, if x is a float and y is a double? 11) A) x = float y; B) x = <float>y; C) x = y; D) x = (float)y; 12) The boolean data type may contain values in the following range of values 12) A) -128 to +127 B) true or false C) -32,768 to +32,767 D) -2,147,483,648 to +2,147,483,647 13) What will be displayed as a result of executing the following code? int x = 5, y = 20; x += 32; y /= 4; System.out.println("x = " + x + ", y = " + y); 13) A) x = 160, y = 80 B) x = 32, y = 4 C) x = 37, y = 5 D) x = 9, y = 52 14) This is a named storage location in the computer's memory. 14) A) operator B) constant C) variable D) literal 15) Given the declaration double r;, which of the following statements is invalid? 15) A) r = 9.4632E15; B) r = 326.75; C) r = 9.4632e15; D) r = 2.9X106;
Gaddis: Starting Out with Java: Early Objects, 4/e Test Bank 3 16) To print "Hello, world" on the monitor, use the following Java statement 16) A) SystemOutPrintln("Hello, world"); B) Print("Hello, world"); C) System.out.println{"Hello, world"} D) System.out.println("Hello, world"); 17) Variables of the boolean data type are useful for 17) A) evaluating true/false conditions B) working with very large integers C) working with small integers D) evaluating scientific notation 18) What would be displayed as a result of the following code? 18) int x = 578; System.out.print("There are " + x + 5 + "\n" + "hens in the hen house."); A) There are 583 hens in the hen house. B) There are 5785 hens in the hen house. C) There are x5\nhens in the hen house. D) There are 5785 hens in the hen house. 19) In the following Java statement what value is stored in the variable name? String name = "John Doe"; A) "name" B) The memory address where "John Doe" is located C) The memory address where name is located D) "John Doe" 19) 20) What will be the displayed when the following code is executed? final int x = 22, y = 4; y += x; System.out.println("x = " + x + ", y = " + y); A) x = 22, y = 26 B) x = 22, y = 4 C) x = 22, y = 88 D) Nothing, this is an error 20) 21) A Java program must have at least one 21) A) Comment B) System.out.println(); statement C) Class definition D) Variable 22) What will be displayed after the following statements have been executed? int x = 15, y = 20, z = 32; x += 12; y /= 6; z - = 14;
Gaddis: Starting Out with Java: Early Objects, 4/e Test Bank 4 System.out.p rintln ("x = " + x + ", y = " + y + ", z = " + z); 22) A) x = 27, y = 3, z = 18 B) x = 37, y = -14, z = 4 C) x = 27, y = 3.333, z = 18 D) x = 27, y = 2, z = 18 23) Which of the following statements correctly creates a Scanner object for keyboard input? 23) A) Scanner kbd = new Scanner(System.keyboard); B) Scanner keyboard = new Scanner(System.in); C) Scanner keyboard(system.in); D) Keyboard scanner = new Keyboard(System.in); 24) What will be the value of z as a result of executing the following code? 24) int x = 5, y = 28; float z; z = (float) (y / x); A) 5.60 B) 5.0 C) 5.6 D) 3.0 25) The primitive data types only allow a(n) to hold a single value. 25) A) variable B) class C) literal D) object 26) Which Scanner class method reads a String? 26) A) nextline B) getstring() C) readstring() D) nextstring() 27) What will be displayed as a result of executing the following code? public class test { public static void main(string[] args) { int value1 = 9; System.out.println(value1); int value2 = 45; System.out.println(value2); System.out.println(value3); 27) val ue = 16; } }
Gaddis: Starting Out with Java: Early Objects, 4/e Test Bank 5 A) 94516 B) 9 45 16 C) 9 45 16 D) Nothing, this is an error 28) This is normally considered the standard output and standard input devices, and usually refer to the monitor and keyboard. A) console B) CRT C) secondary storage devices D) CPU 28) 29) What would be printed out as a result of the following code? 29) System.out.println("The quick brown fox" + "jumped over the \n" "slow moving hen."); A) The quick brown fox jumped over the \nslow moving hen. B) The quick brown fox jumped over the slow moving hen. C) The quick brown fox jumped over the slow moving hen. D) Nothing. This is an error. 30) Which of the following is not a primitive data type? 30) A) short B) long C) String D) float 31) Which of the following does not describe a valid comment in Java? 31) A) Documentation comments, any comments starting with /** B) Multi-line comments, start with */ and end with /* C) Single line comments, two forward slashes - // D) Multi-line comments, start with /* and end with */ 32) To display the output on the next line, you can use the println method or use this escape sequence in the print method. A) \r B) \n C) \t D) \b 32) 33) If the following Java statements are executed, what will be displayed? 33) System.out.println("The top three winners are\n"); System.out.print("Jody, the Giant\n"); System.out.print("Buffy, the Barbarian"); System.out.println("Adelle, the Alligator");
Gaddis: Starting Out with Java: Early Objects, 4/e Test Bank 6 A) The top three winners are Jody, the Giant\nBuffy, the BarbarianAdelle, the Alligator B) The top three winners are Jody, the Giant Buffy, the Barbarian Adelle, the Alligator C) The top three winners are Jody, the Giant Buffy, the BarbarianAdelle, the Alligator D) The top three winners are Jody, the Giant\nBuffy, the BarbarianAdelle, and the Alligator 34) If x has been declared an int, which of the following is invalid? 34) A) x = 0; B) x = 592; C) x = -58932; D) x = 1,000; 35) Which of the following is a valid Java statement? 35) A) string str = "John Doe"; B) String str = 'John Doe'; C) string str = 'John Doe'; D) String str = "John Doe"; 36) What will be displayed as a result of executing the following code? int x = 6; String msg = "I am enjoying this class."; String msg1 = msg.touppercase(); String msg2 = msg.tolowercase(); char ltr = msg.charat(x); int strsize = msg.length(); System.out.println(msg); System.out.println(msg1); System.out.println(msg2); System.out.println("Character at index x = " + ltr); System.out.println("msg has " + strsize + "characters."); A) I am enjoying this class. I AM ENJOYING THIS CLASS. i am enjoying this class. Character at index x = n msg has 24 characters. C) I am enjoying this class. I AM ENJOYING THIS CLASS. i am enjoying this class. Character at index x = e msg has 25 characters. B) I am enjoying this class. I AM ENJOYING THIS CLASS. i am enjoying this class. Character at index x = n msg has 25 characters. D) I am enjoying this class. I AM ENJOYING THIS CLASS. i am enjoying this class. Character at index x = e msg has 24 characters. 36) 37) Which of the following is valid? 37) A) float y; z 934.21; B) float w; C) double v; double z; = y = z; w = 1.0f; v = 1.0f;
Gaddis: Starting Out with Java: Early Objects, 4/e Test Bank 7 D) float y; y = 54.9; 38) Every Java application program must have 38) A) a class named MAIN B) integer variables C) comments D) a method named main 39) To compile a program named, First, use the following command 39) A) javac First.java B) java First.java C) javac First D) compile First.javac 40) What is the result of the following statement? 25/4 + 4 * 10 % 3 A) 19 B) 3 C) 5.25 D) 7 40) 41) Which Scanner class method reads an int? 41) A) readint() B) getint() C) read Int() D) nextint() 42) What will be displayed after the following statements have been executed? final double x; x = 54.3; System.out.println("x = " + x ); A) x B) x = 54.3 C) x = 108.6 D) Nothing, this is an error. 42) 43) This is a variable whose content is read only and cannot be changed during the program's execution of the program. A) operator B) reserved word C) named D) literal constant 43) 44) What is the result of the following expression? 25-7 * 3 + 12/3 A) 12 B) 8 C) 10 D) 6 44) TRUE/FALSE. Write 'T' if the statement is true and 'F' if the statement is false. 45) Programming style includes techniques for consistently putting spaces and indentation in a program so visual cues are created. 45) 46) Assuming that pay has been declared a double, the following statement is valid. pay =
Gaddis: Starting Out with Java: Early Objects, 4/e Test Bank 8 2,583.44; 46) 47) Both character literals and string literals can be assigned to a char variable. 47) 48) All Java lines of code end with semicolons. 48) 49) A variable's scope is the part of the program that has access to the variable. 49) 50) In Java the variable named One is the same as the variable named ONE. 50) 51) Identifiers and class names are examples of variables. 51) 52) If the compiler encounters a statement that uses a variable before the variable is declared, an error will result. 52) 53) Named constants are initialized with a value, that value cannot be changed during the execution of the program. 53) 54) A variable's scope is the part of the program that has access to the variable. 54)
Gaddis: Starting Out with Java: Early Objects, 4/e Test Bank 9 1) C 2) A 3) C 4) A 5) B 6) A 7) D 8) A 9) C 10) D 11) D 12) B 13) C 14) C 15) D 16) D 17) A 18) B 19) B 20) D 21) C 22) A 23) B 24) B 25) A
Gaddis: Starting Out with Java: Early Objects, 4/e Test Bank 10 26) A 27) D 28) A 29) D 30) C 31) B 32) B 33) C 34) D 35) D 36) B 37) B 38) D 39) A 40) D 41) D 42) D 43) C 44) B 45) TRUE 46) FALSE 47) FALSE 48) FALSE 49) TRUE 50) FALSE
Gaddis: Starting Out with Java: Early Objects, 4/e Test Bank 11 51) FALSE 52) TRUE 53) TRUE 54) TRUE