rephactor weeks 3 - 5
What output is printed by the following code? String str = "Refactor Java"; int index = 0; int num = 0; while (index < str.length()) { if (str.charAt(index) == 'a') num++; index++; } System.out.println("Num: " + num); a) Num: 0 b) Num: 3 c) Num: 10 d) Num: 13
b) Num: 3
What does the following for loop do? for (int num = 3; num <= 30; num += 3) System.out.println(num); a) Prints the sum of the multiples of 3 between 3 and 30. b) Prints the multiples of 3 between 3 and 30 (3, 6, 9, etc.) c) Prints the numbers from 3 to 30 (3, 4, 5, etc.) d) Prints the sum of the numbers from 3 to 30.
b) Prints the multiples of 3 between 3 and 30 (3, 6, 9, etc.)
Which of the following statements is true? a) The body of an if statement must be enclosed in braces. b) The condition of an if statement must be a boolean expression. c) An if statement must contain an else clause. d) An if statement is an example of a repetition statement.
b) The condition of an if statement must be a boolean expression.
What is wrong with the following code? if (count > 5) if (sum < MAX) System.out.println("cattywampus"); else System.out.println("snickersnee"); a) One output statement can never be executed. b) The indentation does not properly reflect the processing. c) It contains at least one invalid condition. d) A second else clause is required.
b) The indentation does not properly reflect the processing.
If the value of weight is 7, what does the following statement print? System.out.println("The weight is " + ((weight % 2 == 0) ? "even" : "odd")); a) The weight is even b) The weight is odd c) The weight is void d) The weight is null
b) The weight is odd
What will happen if the variable total holds 5 when the following code is executed? if (total > 8) System.out.println("collywobbles"); a) The word collywobbles is printed and then processing continues. b) The word collywobbles is not printed and processing continues. c) The word collywobbles is not printed, but an error message is. d) The word collywobbles is not printed and an exception is thrown.
b) The word collywobbles is not printed and processing continues.
What package is the Scanner class a part of? a) java.input b) java.util c) java.lang d) java.io
b) java.util
What output is printed by the following code if it is executed when appetizer is 3 and entree is 12? if (appetizer > 1) if (entree < 7) System.out.println("ketchup"); else System.out.println("mustard"); else if (appetizer < 0) System.out.println("mayonnaise"); else System.out.println("relish"); a) ketchup b) mustard c) mayonnaise d) relish e) salsa f) None of the above
b) mustard
Which assignment statement is functionally equivalent to the following if statement? if (num1 > total) num1 = total; else num1 = 0; a) num1 = (num1 > total) ? 0 : total; b) num1 = (num1 > total) ? total : 0; c) num1 = (total > 0) ? 0 : total; d) num1 = (num1 > 0) ? total : 0;
b) num1 = (num1 > total) ? total : 0;
What is the text used to request information from a user called? a) trigger b) prompt c) prod d) cue
b) prompt
Given the following declaration, which expression would produce the substring "ledge"? String words = "Knowledge is power."; a) words.substring(5, 10); b) words.substring(4, 9); c) words.substring(5, 9); d) words.substring(4, 8);
b) words.substring(4, 9);
Which of the following literals is NOT valid? a) "\"\n" b) '\n' c) ""\n"" d) '"'
c) ""\n""
What operator is used to perform string concatenation in Java? a) & b) @ c) + d) *
c) +
If gen refers to a Random object, the following expression will produce a value in what range? gen.nextInt(12) - 2 a) 0 to 10 b) -2 to 10 c) -2 to 9 d) 1 to 10
c) -2 to 9
If a String variable called pioneer contains the characters "Grace Murray Hopper", what is the result of the following expression? pioneer.length() a) 17 b) 18 c) 19 d) 20
c) 19
If gen refers to a Random object, the following expression will produce a value in what range? gen.nextInt(10) + 5 a) 0 to 15 b) 10 to 15 c) 5 to 14 d) 5 to 10
c) 5 to 14
Which of the following expressions represents the color white? a) Color.rgb(0, 0, 0) b) Color.rgb(100, 100, 100) c) Color.rgb(255, 255, 255) d) Color.rgb(100, 0, 0)
c) Color.rgb(255, 255, 255)
What is the output of the following statement? System.out.println("He said \"Avada Kedavra\""); a) He said \\"Avada Kedavra\\" b) He said \"Avada Kedavra\" c) He said "Avada Kedavra" d) He said 'Avada Kedavra' e) No output. This statement contains a syntax error.
c) He said "Avada Kedavra"
What string is printed by the following code if it is executed when the value of finger is 3? switch (finger) { case 1: System.out.println("Thumb"); break; case 2: System.out.println("Index finger"); break; case 3: System.out.println("Middle finger"); break; case 4: System.out.println("Ring finger"); break; case 5: System.out.println("Pinky"); break; default: System.out.println("Not a finger!"); } a) Thumb b) Index finger c) Middle finger d) Ring finger e) Pinky f) Not a finger!
c) Middle finger
Which of the following ellipses is also a circle? a) new Ellipse(150, 150, 150, 100) b) new Ellipse(95, 120, 95, 120) c) new Ellipse(100, 200, 55, 55) d) new Ellipse(70, 70, 100, 70)
c) new Ellipse(100, 200, 55, 55)
Which of the following lines is horizontal? a) new Line(40, 20, 100, 70) b) new Line(30, 30, 50, 50) c) new Line(25, 70, 125, 70) d) new Line(75, 20, 75, 90)
c) new Line(25, 70, 125, 70)
What do the characters in a Unicode escape sequence represent? a) a range of Unicode characters b) the set of characters terminating in a particular Unicode character c) the hexadecimal code for a particular Unicode character d) the ASCII code for the equivalent Unicode character
c) the hexadecimal code for a particular Unicode character
What value is printed by the following code? int count = 0; for (int i = 0; i < 5; i++) count += 3; System.out.println(count); a) 0 b) 5 c) 12 d) 15 e) 18
d) 15
What number is printed by the following code? int num = 20; switch (num) { case 1: num = num + 1; break; case 12: num = num + 1; num = num * 2; case 20: num = num * 2; case 25: num = num + 1; break; case 999: num = 20; break; default: num = 999; } System.out.println(num); a) 20 b) 21 c) 40 d) 41 e) 88 f) 999
d) 41
What output would be produced when the following code is executed? String state = "Mississippi"; System.out.println(state.replace("is", "was")); a) Mwassissippi b) Mwasswasswaspi c) Mwaswasippi d) Mwasswassippi
d) Mwasswassippi
Which statement is true about the following two circles? new Circle(50, 120, 50) new Circle(70, 120, 20) a) They have the same size. b) They have the same center point. c) They partially overlap. d) One fully surrounds the other (but they don't have the same center point).
d) One fully surrounds the other (but they don't have the same center point).
If a String variable called greeting contains the characters "hello", what does the following expression do? greeting.toUpperCase() a) Converts the characters in the greeting object to "HELLO". b) Returns a new string containing the characters "Hello" c) Converts the characters in the greeting object to "Hello". d) Returns a new string containing the characters "HELLO"
d) Returns a new string containing the characters "HELLO"
Which statement would produce the following output? "Oom Pah Pah Oom Pah Pah," that's how it goes. a) System.out.println("\qOom Pah Pah Oom Pah Pah,\q that's how it goes."); b) System.out.println("\nOom Pah Pah Oom Pah Pah,\n that's how it goes."); c) System.out.println(""Oom Pah Pah Oom Pah Pah," that's how it goes."); d) System.out.println("\"Oom Pah Pah Oom Pah Pah,\" that's how it goes.");
d) System.out.println("\"Oom Pah Pah Oom Pah Pah,\" that's how it goes.");
What does the fourth parameter in this method call represent? Color.rgb(100, 100, 200, 0.65); a) The primary color ratio b) The color's hue c) The key color (black) contribution d) The alpha channel (transparency)
d) The alpha channel (transparency)
What is passed to the constructor when a new Scanner object is created? a) The text requesting that the user enter data. b) The type of the data the Scanner will read. c) The set of delimiter characters used to separate the input values. d) The source of the input.
d) The source of the input.
What does the following statement print? System.out.println((count < 0) ? "Invalid" : count); a) The value of count, or "Invalid" if count is positive. b) Zero, or "Invalid" if count is negative. c) Zero, or "Invalid" if count is positive. d) The value of count, or "Invalid" if count is negative.
d) The value of count, or "Invalid" if count is negative.
If gen refers to a Random object, which of the following expressions would produce a value in the range 1 to 10? a) gen.nextInt(11) + 1 b) gen.nextInt(1) + 11 c) gen.nextInt(1) + 10 d) gen.nextInt(10) + 1
d) gen.nextInt(10) + 1
Which of the following circles is largest? a) new Circle(30, 80, 25) b) new Circle(150, 175, 45) c) new Circle(20, 100, 30) d) new Circle(50, 50, 50)
d) new Circle(50, 50, 50)
Which of the following ellipses is thinner than it is tall? a) new Ellipse(150, 135, 80, 60) b) new Ellipse(270, 300, 180, 100) c) new Ellipse(90, 90, 60, 55) d) new Ellipse(140, 100, 20, 50)
d) new Ellipse(140, 100, 20, 50)
Which of the following rectangles extends furthest down? a) new Rectangle(150, 90, 70, 45) b) new Rectangle(80, 25, 100, 130) c) new Rectangle(100, 50, 50, 100) d) new Rectangle(50, 85, 70, 75)
d) new Rectangle(50, 85, 70, 75)
Going from left to right, what are the three sections of a for loop header? a) condition, initialization, increment b) condition, increment, initialization c) increment, initialization, condition d) initialization, increment, condition e) increment, condition, initialization f) initialization, condition, increment
f) initialization, condition, increment
If left unspecified, the font applied to a Text object is 12 point Lucinda Grande.
false
Java represents a color using the CMYK color model.
false
The control variable must be declared in the for loop header.
false
The increment section of the for loop header always adds 1 to the loop control variable.
false
The less than operator (<) should not be used to compare floating-point values.
false
The relational operators can be used to put String objects in alphabetical order.
false
There is no way to stop an infinite loop.
false
If A is false, then the B operand in the expression A || B is not evaluated.
false; Since A is false, the truth value of an || expression won't be known until B is evaluated.
When creating a Text object, you specify the position on which you want the text centered.
false; The coordinates specify where the leading (left) edge of the text begins.
The parameters to the Rectangle constructor represent the upper left corner and lower right corner of the rectangle.
false; The first two represent the upper left corner, but the last two represent the rectangle's width and length, respectively.
The expression in a switch statement can be an int or double value.
false; a switch expression can only be a integer, character, or enumeration constant (no floating-point)
The leftmost end point of a line segment must be specified as the start point of the line.
false; either point can be considered the start or end period
The body of a for loop is always executed at least once.
false; if the condition is false initially, the body is never executed
An import statement should be written inside a class but before any methods.
false; it should be written above the class
The String class is part of the java.util package.
false; its part of the java.lang package
The arithmetic operators have a lower precedence than the relational operators.
false; its the other way around
An object must be created when its object reference variable is declared.
false; no those can be separate activities
The classes of the java.util package are automatically imported into every Java program.
false; only java.lang is
A String object cannot be created with a new operator.
false; sure it can, but often a string literal is all you need
In a program, a class must always be referred to using its fully qualified name.
false; the import statement allows us to avoid always using the fully qualified name
The Java keyword new is a method that can be called to create objects.
false; the keyword new is an operator
The not operator (!) requires two operands.
false; the not operator only has one operand
The exclusive or operator (^) produces results that are opposite to the regular or operator (||).
false; the only difference is when both operands are true
Each value of a case can be a constant, variable, or expression.
false; the value of each case can only be a constant
The font applied to a Text object determines whether that text is underlined or not.
false; use setUnderline method of the text class
A String method can be called through a string literal.
true
A break statement is used at the end of each case of a switch statement to keep processing from continuing into the next case.
true
A constructor of a class has the same name as the class.
true
A for loop could always be written as an equivalent while loop.
true
A rectangle's corners can be rounded using calls to the setArcWidth and setArcHeight methods.
true
A static import let's you refer to a static method without using its class name.
true
A switch statement can always be rewritten as a series of nested if statements.
true
An alpha channel value of 1.0 represents a fully opaque color.
true
An entire package can be imported using one import statement.
true
An import statement tells the compiler which class you're referring to in a program.
true
Arial is an example of a font family.
true
By default, the values read by Scanner input methods are delimited by spaces, tabs, and newline characters.
true
Following a null reference will cause an exception to be thrown.
true
In some cases, a sentinel value can be used to signal the end of input.
true
Objects created from the Color class are immutable.
true
Switch statements can only test equality; they cannot make relational comparisons like greater than (>).
true
The Scanner class does not have a method to read a single character.
true
The Text class and the Font class are part of the same package.
true
The body of a while loop may never execute.
true
The human eye has three types of color receptors that correspond to wavelengths of red, green, and blue.
true
The next and nextLine methods both read and return character strings.
true
The nextLine method of the Scanner class consumes the newline character at the end of the line.
true
The result of a relational operator can be assigned to a boolean variable.
true
Two classes can have the same name if they are in different packages.
true
Using a while loop to check each input value for correctness is a form of input validation.
true
If A and B are both false, then the expression A && B is false.
true; both operands have to be true for A && B to be true
If A is true and B is false, then the expression A || B is true.
true; if either operand is true, then A || B is true
If the value of num is 7 and the value of max is 10, what is the value of num after the following statement is executed? num = (max < num) ? max : max - num; a) 3 b) 4 c) 7 d) 10
a) 3
What causes an infinite loop? a) A loop condition that is never false. b) A loop body that is never executed. c) A loop control variable that is repeatedly modified. d) A loop condition that is always false.
a) A loop condition that is never false.
What happens if an input value does not match the type expected by the Scanner method used to read it? a) An exception is thrown. b) The Scanner continues looking for a valid value. c) The input data is ignored. d) The input data is read as a character string.
a) An exception is thrown.
What object represents the standard input stream, which is input typed by the user at the keyboard? a) System.in b) Keyboard.iStream c) System.keyboard d) Input.standard
a) System.in
Which statement would produce the following output? Eeny Meeny Miny Moe a) System.out.println("Eeny\n\tMeeny\nMiny\n\tMoe"); b) System.out.println("Eeny\t\tMeeny\tMiny\t\tMoe"); c) System.out.println("Eeny\n\tMeeny\n\tMiny\n\tMoe"); d) System.out.println("Eeny\t\nMeeny\tMiny\t\nMoe");
a) System.out.println("Eeny\n\tMeeny\nMiny\n\tMoe");
Of the options given, what values of the variables height, size, and width would cause the following code to set the variable weight to 100? if (height < size) { weight = 50; if (width < 20) System.out.println("short"); } else { if (width > 100) weight = 100; System.out.println("tall"); } a) height = 15, size = 10, width = 110 b) height = 20, size = 12, width = 90 c) height = 15, size = 25, width = 50 d) height = 12, size = 20, width = 200
a) height = 15, size = 10, width = 110
Which of the following is NOT a repetition statement in Java? a) if statement b) while statement c) for statement d) for-each statement e) do-while statement f) They are all repetition statement
a) if statement
What output is produced by the following code? for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) System.out.print("*"); System.out.println(); } a) * ** *** **** ***** b) ***** ***** ***** ***** ***** c) ***** **** *** ** * d) ***** ***** ***** ***** *****
b) ***** ***** ***** ***** *****
After the following line of code is executed, the value stored in the variable num will be in what range? num = (int) (Math.random() * 25); a) 1 to 25 b) 0 to 24 c) 1 to 24 d) 0 to 2
b) 0 to 24
If gen refers to a Random object, the following expression will produce a value in what range? gen.nextInt(30) a) 1 to 30 b) 0 to 29 c) 1 to 29 d) 0 to 30
b) 0 to 29
If a String variable called address contains the characters "123 Main Street", what is the result of the following expression? address.indexOf('3') a) -1 b) 2 c) 3 d) That expression would cause an error.
b) 2
What is the role of the seed value of a random number generator? a) It's the maximum value that can be generated by that object. b) It's a number that is the basis of the calculated pseudorandom numbers. c) It's the minimum value of any range produced by the generator. d) It's the maximum number of values that can be produced by the generator.
b) It's a number that is the basis of the calculated pseudorandom numbers.