C2 – True- False
1) If x is a string, then x = new String("OH"); and x = "OH"; will accomplish the same
thing. T/F ? - answerTrue. Explanation: In Java, to instantiate (assign a value to) an
object, you must use new and the class's constructor. However, since Strings are so
common in Java, they can be instantiated in a way similar to assigning primitive types
their values. So, both of the above assignment statements will accomplish the same
task.
2) If x is the String "Hi There", then x.toUpperCase().toLowerCase(); will return the
original version of x. T/F ? - answerFalse. Explanation: x.toUpperCase() returns x as all
capital letters, while x.toLowerCase() will return x as all lower case letters. So, this code
will first convert x to all upper case letters and then convert the new version to all lower
case characters.
3) If String name = "George W. Bush"; then the instruction name.length(); will return 14.
T/F ? - answerTrue. Explanation: There are 14 characters in the quote marks including
two blanks and a period,
4) If String a = "ABCD" and String b = "abcd" then a.equals(b); returns false but
a.equalsIgnoreCase(b); returns true. T/F ? - answerTrue. Explanation: Since "ABCD" is
not the same as "abcd", the equals method returns false, but by ignoring case in
equalsIgnoreCase, the two are considered true.
5) 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 the Math class, you pass messages directly to the
class name, as in Math.abs( ). T/F ? - answerTrue. Explanation: The Math class uses
methods known as static methods (or class methods) which are invoked by passing a
message directly to the class name itself rather than to an object of the class.
6) A double is wider than an int. T/F ? - answerTrue. Explanation: Wider types are larger
in size or can store a greater range of values. The double is 64 bits and because of the
way it is stored, can store a significantly larger range of values than the int.
7) A variable of type boolean will store either a 0 or a 1. T/F ? - answerFalse.
Explanation: A boolean variable can store only one of two values, but these values are
the reserved words true and false. In C and C++, booleans are implemented as int
variables that store only a 0 or a 1, but in Java, the authors of the language opted to use
the boolean literals true and false as this is considered to be semantically more
understandable.
1) If x is a string, then x = new String("OH"); and x = "OH"; will accomplish the same
thing. T/F ? - answerTrue. Explanation: In Java, to instantiate (assign a value to) an
object, you must use new and the class's constructor. However, since Strings are so
common in Java, they can be instantiated in a way similar to assigning primitive types
their values. So, both of the above assignment statements will accomplish the same
task.
2) If x is the String "Hi There", then x.toUpperCase().toLowerCase(); will return the
original version of x. T/F ? - answerFalse. Explanation: x.toUpperCase() returns x as all
capital letters, while x.toLowerCase() will return x as all lower case letters. So, this code
will first convert x to all upper case letters and then convert the new version to all lower
case characters.
3) If String name = "George W. Bush"; then the instruction name.length(); will return 14.
T/F ? - answerTrue. Explanation: There are 14 characters in the quote marks including
two blanks and a period,
4) If String a = "ABCD" and String b = "abcd" then a.equals(b); returns false but
a.equalsIgnoreCase(b); returns true. T/F ? - answerTrue. Explanation: Since "ABCD" is
not the same as "abcd", the equals method returns false, but by ignoring case in
equalsIgnoreCase, the two are considered true.
5) 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 the Math class, you pass messages directly to the
class name, as in Math.abs( ). T/F ? - answerTrue. Explanation: The Math class uses
methods known as static methods (or class methods) which are invoked by passing a
message directly to the class name itself rather than to an object of the class.
6) A double is wider than an int. T/F ? - answerTrue. Explanation: Wider types are larger
in size or can store a greater range of values. The double is 64 bits and because of the
way it is stored, can store a significantly larger range of values than the int.
7) A variable of type boolean will store either a 0 or a 1. T/F ? - answerFalse.
Explanation: A boolean variable can store only one of two values, but these values are
the reserved words true and false. In C and C++, booleans are implemented as int
variables that store only a 0 or a 1, but in Java, the authors of the language opted to use
the boolean literals true and false as this is considered to be semantically more
understandable.