C2 - True/False

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

22) Wrapper classes in Java allow you to create objects representing primitive data. T/F ?

True. Every primitive type has a corresponding wrapper class. For example, the Integer class wraps the int type.

16) If a, b and c are int variables with a = 5, b = 7, c = 12, then the statement int z = (a * b - c) / a; will result in z equal to 4. T/F ?

True. Explanation: (a * b - c) / a = (5 * 7 - 12) / 5 = (35 - 12) / 5 = 23 / 5, and since 23 and 5 are int values, the division is performed as an int division, or 23 / 5 = 4.

10) With an enumerated type, the programmer defines the possible values of the type. T/F ?

True. Explanation: An enumerated type, defined using the reserved word enum, lists out, or enumerates, all possible values of a variable of that type.

13) You would import the java.text library to use NumberFormat and DecimalFormat. T/F ?

True. Explanation: Both of these classes are used for "text processing", that is, to handle values like Strings. Such classes are found in java.text.

8) In Java, 'a' and 'A' are considered to be different values. T/F ?

True. Explanation: Characters (char) values are stored using Unicode, and 'a' and 'A' have different Unicode values.

21) A Java variable is the name of a data value stored in memory that can not change during the program's execution. T/F ?

False. A variable can change its value as long as it is within the same type, but the variable cannot change type.

7) A variable of type boolean will store either a 0 or a 1. T/F ?

False. 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.

19) The mod operator, %, can only be performed on int values and its result is a double. T/F ?

False. Explanation: Mod, or modulo, returns the remainder that results from an integer division. The remainder is also an integer.

15) In order to create a constant, you would use the static reserved word. T/F ?

False. Explanation: The reserved word final would be used. It indicates that this is the final value that will be stored in this variable, thus making it unchangeable, or constant.

18) Suppose that String name = "Frank Zappa". Then the instruction name.toUpperCase( ).replace('A', 'I'); will return "FrInk ZIppI". T/F ?

False. Explanation: The toUpperCase method returns the String as all upper case characters, or "FRANK ZAPPA". The replace method will replace each instance of 'A' with 'I', producing "FRINK ZIPPI".

20) The word println is a reserved word. T/F ?

False. Explanation: The word println is a method on the System.out object, it is not a reserved word.

2) If x is the String "Hi There", then x.toUpperCase().toLowerCase(); will return the original version of x. T/F ?

False. 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.

25) Since double to int is a narrowing conversion, there is no way to convert a double to an int. T/F ?

False. Going from double to int is a narrowing conversion, but it is possible to do using a cast.

24) The operators * and + have the same precedence. T/F ?

False. The * operator has higher precedence than +, so it is performed first in an expression (unless otherwise noted using parenthesis).

1) If x is a string, then x = new String("OH"); and x = "OH"; will accomplish the same thing. T/F ?

True. 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.

4) If String a = "ABCD" and String b = "abcd" then a.equals(b); returns false but a.equalsIgnoreCase(b); returns true. T/F ?

True. 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 ?

True. 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.

12) The NumberFormat class can be used to format percentages and currencies. T/F ?

True. Explanation: The NumberFormat class has static methods getCurrencyInstance and getPercentInstance that can be used to obtain NumberFormat objects to format currencies and percentages respectively.

14) The Random class has a method nextDouble( ) which returns a random double value between 0 and 1. T/F ?

True. Explanation: The method nextDouble( ) returns a double value between 0 and 1 so that it could be used as a probability.

11) An enumerated type defined by "enum Grade {A, A-, B+, B, B-, C, D, F}" is invalid. T/F ?

True. Explanation: The values of an enumerated type are identifiers and must follow the rules for identifiers. A valid identifier cannot use - or +.

3) If String name = "George W. Bush"; then the instruction name.length(); will return 14. T/F ?

True. Explanation: There are 14 characters in the quote marks including two blanks and a period,

9) You cannot cast a String to be a char and you cannot cast a String which stores a number to be an int or double. T/F ?

True. Explanation: There is no mechanism available to cast a String to one of the primitive types, but there are methods available to perform a similar action and return a character at a given location (charAt) or to return the int or double value equivalent to the number stored in the String.

6) A double is wider than an int. T/F ?

True. 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.

17) Java is a strongly typed language which means every variable has a single type associated with it throughout its existence in the program, and the variable can only store values of that type. T/F ?

True. Strong typing is a property of a programming language whereby the variable's type does not change during the variable's existence, and any value stored in that variable is of that type. The reason that strong typing is important is it guarantees that a program that was successfully compiled will not have run-time errors associated with the misuse of types for the variables declared.

23) If you want to use classes from a package other than java.lang, you must import them. T/F ?

True. The classes in java.lang are automatically imported, but classes in any other package must be imported by the programmer using an import statement.


Ensembles d'études connexes

Econ ch 9-13 launchpad questions

View Set

Chapter 25: Neurologic System PQ

View Set