Font size
Worksheets1510 Chapter 2
Total questions: 41
Worksheet time: 26mins
If x is a String, then x = new String("OH"); and x = "OH"; will accomplish the same thing.
True
False
If x is the String "Hi There", then x.toUpperCase().toLowerCase(); will return the original value of x.
True
False
If String name = "Bruce"; then the instruction name.length(); will return 5.
True
False
If String a = "ABCD" and String b = "abcd" then a.equals(b); returns false but a.equalsIgnoreCase(b); returns true.
True
False
Unlike the String class where you must pass a message to an object (instance) of the class, as in x.length(), in order to use either the Scanner or Math classes, you pass messages directly to the class name, as in Math.abs() or scan.nextInt().
True
False
In order to generate a random number, you must use Math.random().
True
False
A double is wider than a float and a float is wider than an int.
True
False
A variable of type boolean will store either a 0 or a 1.
True
False
In Java, 'a' and 'A' are considered to be different values.
True
False
You cannot cast a String to be a char and you cannot cast a String which stores a number to be an int, float, or double.
True
False
The values of (double) 5/2 and (double) (5/2) are identical.
True
False
In Java, there are three main ways that data conversion may occur: by assignment, by promotion, and by casting.
True
False
The following statement will output ______ lines of text. System.out.println("1 big bad wolf\t8 the 3 little pigs\n4 dinner\\2night");
1
2
3
4
5
If you want to output the text "hi there" including the quote marks, which of the following would you use?
println is a
method
reserved word
variable
class
String
A Java variable is the name of a
numeric typed data value stored in memory
immutable untyped data value stored in memory
mutable typed data value stored in memory
mutable untyped data value stored in memory
mutable data value or class stored in memory
Of the following types, which one cannot store a numeric value?
int
double
float
char
All of these can store numeric values
What is the value of z after the following assignment statement is executed? float z = 5/10;
0.0
0.5
5.0
0.05
None of these; a run-time error will occur because z is a float and 5/10 is an int.
Which of the following situations would require a cast?
using charAt to take an element of a String and store it in a char
storing an int in a float
storing a float in a double
storing a float in an int
All of these require casts
If i is an int and f is a float, which of the following statements is not a legal assignment statement?
f = i;
i = f;
f = (float)i;
i = (int)f;
All of these are legal assignment statements
Which of the following is true regarding the mod operator, %?
It can only be performed on int values and its result is a double.
It can only be performed on int values and its result is an int.
It can only be performed on float or double values and its result is an int.
It can only be performed on float or double values and its result is a double.
It can be performed on any numeric values and its result is always numeric.
int x = 20, int y = 10, and int z = 5. What is the result of: x / y / z?
0
12
0.4
this would cause a syntax error
this would cause a run-time error because it is a division by zero
Assume that x and y are ints equal to 10 and 5 respectively. What is the output of the following statement? System.out.println(x + y);
15
105
10 5
x + y
this would cause an error since neither x nor y is a String
Assume that x and y are ints equal to 10 and 5 respectively. What is the output of the following statement? System.out.println("" + x + y);
15
105
10 5
x + y
this would cause an error since neither x nor y is a String
If you want to store the value "Darth Vader" in the String variable name, which of the following statements could you use?
String name = "Darth Vader";
String name = new String("Darth Vader");
String name = "Darth" + " " + "Vader";
String name = new String("Darth" + " " + "Vader");
Any of these would work
Given three String variables, a, b, and c, which of the following statements could you use to achieve the same thing as: c = a + b;
c = a.length() + b.length();
c = (int)a + (int)b;
c = a.concat(b);
c = b.concat(a);
c = a.plus(b);
If the String major = "Computer Science", what is returned by major.charAt(1)?
'C'
'o'
'm'
"C"
"Computer"
Which of the following would return the last character of the String x?
x.charAt(x.length())
x.charAt(length(x));
x.charAt(length(x) - 1);
x.charAt(x.length() - 1)
Given String name = "Arleen Crabtree". What will the following instruction return? name.toUpperCase().replace('R', 'Z');
ARLEEN CRABTREE
AZLEEN CZABTREE
Azleen Czabtree
Arleen Czabtree
Ar2leen Cr2abtree
Which library package would you import to use NumberFormat and DecimalFormat?
java.beans
java.io
java.lang
java.text
java.util
Which library package would you import to use the class Random?
java.beans
java.io
java.lang
java.text
java.util
The Random class has a method, nextFloat(), which returns a random float value between
-1 and +1
0 and 1
0 and 99
1 and 100
If you want to output a double so that at least one digit appears to the left side of the decimal point and exactly one digit appears to the right side, which pattern would you give a DecimalFormat variable when you instantiate it?
0.0
#.0
0.#
#.#
#.##
Given the double likelihood = 0.013885 and given DecimalFormat df = new DecimalFormat("0.00##");, what would be the output if you execute the following: System.out.println(df.format(likelihood));
0.013885
0.0139
0.0145
0.0138
0.014
Using getCurrencyInstance() formats a variable, automatically inserting
a decimal point for cents
a dollar sign
a percent sign
all three of these
a decimal point for cents and a dollar sign but not a percent sign
What will be the value of z after the following statement is executed? int z = 50 / 10.00;
5
5.0
50
10
None of these; a run-time error will occur because z is an int and 50/10.00 is not an int.
Which of these will never return NaN?
Math.sqrt((int)x)
Math.sqrt(Math.abs(x))
Math.abs(Math.sqrt(x))
Math.sqrt(-x)
Given double x = 0.362, which of the following prints it as 36% using
NumberFormat nf = NumberFormat.getPercentInstance();
System.out.println(nf)
System.out.println(nf.format(x))
System.out.println(nf.x)
System.out.println(format(x))
In order to create a constant, which of the following Java reserved words is used?
private
static
int
final
class
Given int a = 5, b = 7, c = 12, what is the value of z?
int z = (a * b - c) / a;
0
4
5
-5
23
written answer question (not for marks)
What is a wrapper class? Why are wrapper classes useful?
