Java Software Solutions, 8e, Global Edition (Lewis/Loftus)
Chapter 2 Data and Expressions
2.1 Multiple-Choice Questions
Use the following class definition to answer the questions below.
public class Questions1_4
{
public static void main(String[ ] args)
{
System.out.print(“Here”);
System.out.println(“There ” + “Everywhere”);
System.out.println(“But not” + “in Texas”);
}
}
1) The program will print the word “Here” and then print
A) “There Everywhere” on the line after “Here
B) “There” on the line after “Here” and “Everywhere” on the line after “There”
C) “There Everywhere” on the same line as “Here”
D) “ThereEverywhere” on the same line as “Here
E) “ThereEverywhere” on the line after “Here
2) The final println command will output
A) “But not in Texas”
B) “But notin Texas”
C) “But not” on one line and “in Texas” on the next line
D) “But not+in Texas”
E) “But not + in Texas”
3) How many lines of output are provided by this program?
A) 1
B) 2
C) 3
D) 4
E) 5
4) A reasonable documentation comment for this program might be
A) // a program that demonstrates the differences between print, println and how + works
B) // a program that outputs a message about Texas
C) // a program that demonstrates nothing at all
D) // a program that outputs the message “Here There Everywhere But not in Texas”
E) // a program that contains three output statements
5) Consider the following statement:
System.out.println(“1 big bad wolf\t8 the 3 little pigs\n4 dinner\r2night”);
This statement will output ________ lines of text
A) 1
B) 2
C) 3
D) 4
E) 5
6) If you want to output the text “hi there”, including the quote marks, which of the following
could do that?
A) System.out.println(“hi there”);
B) System.out.println(“”hi there””);
C) System.out.println(“\”hi there”);
D) System.out.println(“\”hi there\””);
E) none, it is not possible to output a quote mark because it is used to mark the beginning and
ending of the String to be output
7) The word println is a(n)
A) method
B) reserved word
C) variable
D) class
E) String
8) A Java variable is the name of a
A) numeric data value stored in memory
B) data value stored in memory that can not change during the program’s execution
C) data value stored in memory that can change its value but cannot change its type during the
program’s execution
D) data value stored in memory that can change both its value and its type during the program’s
execution
E) data value or a class stored in memory that can change both its value and its type during the
program’s execution
9) Of the following types, which one cannot store a numeric value?
A) int
B) byte
C) float
D) char
E) all of these can store numeric values
10) What value will z have if we execute the following assignment statement?
float z = 5 / 10;
A) z will equal 0.0
B) z will equal 0.5
C) z will equal 5.0
D) z will equal 0.05
E) none of the above, a run-time error arises because z is a float and 5 / 10 is an int
11) A cast is required in which of the following situations?
A) using charAt to take an element of a String and store it in a char
B) storing an int in a float
C) storing a float in a double
D) storing a float in an int
E) all of the above require casts
12) If x is an int and y is a float, all of the following are legal except which assignment
statement?
A) y = x;
B) x = y;
C) y = (float) x;
D) x = (int) y;
E) all of the above are legal
13) Given the following assignment statement, which of the following answers is true regarding
the order that the operators will be applied based on operator precedence?
a = (b + c) * d / e – f;
A) *, /, +, –
B) *, +, /, –
C) +, *, /, –
D) +, /, *, –
E) +, -, *, /
14) What will be the result of the following assignment statement? Assume b = 5 and c = 10.
int a = b * (-c + 2) / 2;
A) 30
B) -30
C) 20
D) -20
E) -6
6
15) Which of the following is true regarding the mod operator, %?
A) It can only be performed on int values and its result is a double
B) It can only be performed on int values and its result is an int
C) It can only be performed on float or double values and its result is an int
D) It can only be performed on float or double values and its result is a double
E) It can be performed on any numeric values, and the result always is numeric
16) Assume that x, y, and z are all integers (int) equal to 50, 20, and 6 respectively. What is the
result of x / y / z?
A) 0
B) 12
C) 16
D) A syntax error as this is syntactically invalid
E) A run-time error because this is a division by 0
17) What is output with the statement System.out.println(x+y); if x and y are int values where
x=10 and y=5?
A) 15
B) 105
C) 10 5
D) x+y
E) An error since neither x nor y is a String
18) What is output with the statement System.out.println(“”+x+y); if x and y are int values where
x=10 and y=5?
A) 15
B) 105
C) 10 5
D) x+y
E) An error since neither x nor y is a String
19) If you want to store into the String name the value “George Bush”, you would do which
statement?
A) String name = “George Bush”;
B) String name = new String(“George Bush”);
C) String name = “George” + ” ” + “Bush”;
D) String name = new String(“George” + ” ” + “Bush”);
E) Any of the above would work
20) Consider having three String variables a, b, and c. The statement c = a + b; also can be
achieved by saying
A) c = a.length( ) + b.length( );
B) c = (int) a + (int) b;
C) c = a.concat(b);
D) c = b.concat(a);
E) c = a.plus(b);
21) If the String major = “Computer Science”, what is returned by major.charAt(1)?
A) ‘C’
B) ‘o’
C) ‘m’
D) “C”
E) “Computer”
22) Which of the following would return the last character of the String x?
A) x.charAt(0);
B) x.charAt(last);
C) x.charAt(length(x));
D) x.charAt(x.length( )-1);
E) x.charAt(x.length( ));
23) Suppose that String name = “Frank Zappa”. What will the instruction name.toUpperCase(
).replace(‘A’, ‘I’); return?
A) “FRANK ZAPPA ”
B) “FRINK ZIPPI”
C) “Frink Zippi”
D) “Frank Zappa”
E) “FrInk ZIppI”
24) Which library package would you import to use NumberFormat and DecimalFormat?
A) java.beans
B) java.io
C) java.lang
D) java.text
E) java.util
25) Which library package would you import to use the class Random?
A) java.beans
B) java.io
C) java.lang
D) java.text
E) java.util
26) The Random class has a method nextFloat( ) which returns a random float value between
A) -1 and +1
B) 0 and 1
C) 0 and 99
D) 1 and 100
E) -2,147,483,648 and +2,147,483,647
27) If you want to output a double so that at least 1 digit appears to the left side of the decimal
point and exactly 1 digit appears to the right side of the decimal point, which pattern would you
give a DecimalFormat variable when you instantiate it?
A) “0.0”
B) “0.#”
C) “0.0#”
D) “0.##”
E) “#.#”
28) Consider the double value likelihood = 0.013885. What would be output if DecimalFormat
dformatter = DecimalFormat(“0.00##”); and you execute
System.out.println(df.format(likelihood)); ?
A) 0.013885
B) 0.0139
C) 0.014
D) .0139
E) .014
29) Using getCurrencyInstance( ) formats a variable, automatically inserting
A) decimal point for cents
B) dollar sign
C) percent sign
D) all three
E) A and B but not C
30) What value will z have if we execute the following assignment statement?
int z = 50 / 10.00;
A) 5
B) 5.0
C) 50
D) 10
E) none of the above, a run-time error arises because z is an int and 50 / 10.00 is not
31) Since you cannot take the square root of a negative number, you might use which of the
following instructions to find the square root of the variable x?
A) Math.sqrt(x*x);
B) Math.sqrt((int) x);
C) Math.sqrt(Math.abs(x));
D) Math.abs(Math.sqrt(x));
E) Math.sqrt(-x);
32) Assume that x is a double that stores 0.362491. To output this value as 36%, you could use
the NumberFormat class with NumberFormat nf = NumberFormat.getPercentInstance( ); Which
of the following statements then would output x as 36%?
A) System.out.println(x);
B) System.out.println(nf);
C) System.out.println(nf.x);
D) System.out.println(nf.format(x));
E) System.out.println(format(x));
}
}
33) Questions33_34 computes
A) The correct average of x, y and z as a double
B) The correct average of x, y and z as an int
C) The average of x, y, and z as a double, but the result may not be accurate
D) the sum of x, y and z
E) the remainder of the sum of x, y and z divided by 3
34) What is output if x = 0, y = 1 and z = 1?
A) 0
B) 0.0
C) 0.6666666666666666
D) 0.6666666666666667
E) 0.67