C2

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

8) What value will z have if we execute the following assignment statement? double 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 double and 5 / 10 is an int

a. Explanation: 5 and 10 are both int values, so 5 / 10 is an integer division. The result is 0. Even though z is a double and can store the real answer, 0.5, it only gets 0 because of the integer division. In order to get 0.5, we would have to first cast 5 or 10 as a double.

15) 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

a. Explanation: Java first computes x+y and then casts it as a String to be output. x + y = 10 + 5 = 15, so the statement outputs 15.

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"); } } 4) A reasonable 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 has three output statements in it

a. Explanation: Remember that comments should not state the obvious (ruling out d and e) but instead should explain what the program is doing or why. This program demonstrates print and println and +.

14) Assume that x, y and z are all ints 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

a. Explanation: This division is performed left to right, so first 50 / 20 is performed. Since 50 and 20 are ints, this results in 2. Next, 2 / 6 is performed which is 0. Notice that if the division were performed right to left, the evaluation would instead be 50 / (20 / 6) = 50 / 3 = 16.

10) 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 double c) storing a double in a double d) storing a double in an int e) all of the above require casts

d. Explanation: For a, charAt returns a char, so there is no problem. In b, the situation is a widening operation taking a narrower type and storing the value in a wider type. Only in d is there a situation where a wider type is being stored in a narrower type, so a cast is required.

20) 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( ));

d. Explanation: Since last is not defined, b is syntactically invalid. The 0th character is the first in the String, so a is true only if the String has a single character. The answer in c is syntactically invalid as length can only be called by passing the message to x. Finally, d and e are syntactically valid, but since length returns the size of the String, and since the first character starts at the 0th position, the last character is at x.length()-1, so e would result in a run-time error.

13) 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

d. Explanation: The unary minus is applied first giving -c + 2 = -8. Next, the * is performed giving 5 * -8 = -40, and finally the / is performed giving -40 / 2 = -20.

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.

d. Explanation: \" is an escape sequence used to place a quote mark in a String, so it is used here to output the quote marks with the rest of the String. 7) Of the following types, which one cannot store a numeric value? a) int b) double c) char d) all of these can store numeric values e) none of these can store numeric values Answer: c. Explanation: int is used to store whole numbers (integers) and double is used to store a real or floating point value (value with a decimal point). A char stores a single character including letters, punctuation marks and digits. However, storing the numeric digit '5' is not the same as storing the number 5.

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"); } } 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"

b. (Explanation: The "+" performs String concatenation, so that "But not" and "in Texas" are concatenated together. Notice that there is no blank space after "not" or before "in" so that when they are concatenated, they are placed together without a blank space.)

19) In the String major = "Computer Science", what is returned by major.charAt(1)? a) 'C' b) 'o' c) 'm' d) "C" e) "Computer"

b. Explanation: Neither d nor e would be correct because charAt returns a char (single character) whereas these answers are Strings. So, the question is, which character is returned? In Java, the first character of a String is numbered 0. So charAt(1) returns the second character of the String, or 'o'.

11) If x is an int and y is a double, all of the following are legal except which assignment statement? 1) y = x; 2) x = y; 3) y = (double) x; 4) x = (int) y; 5) all of the above are legal

b. Explanation: Since x is an int, it cannot accept a double unless the double is cast as an int. There is no explicit cast in the assignment statement in b. In a, a cast is not necessary because a double (y) can accept an int value (x), and in c and d, explicit casts are present making them legal.

16) 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

b. Explanation: The "" casts the rest of the expression as a String, and so the two + signs are used as String concatenation, and so x + y becomes x concatenated with y, or 105.

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

b. Explanation: The \t escape sequence inserts a tab, but leaves the cursor on the same line. The \n escape sequence causes a new line to be produced so that "4 dinner" is output on the next line. The escape sequence \r causes the carriage to return (that is, the cursor to be moved back to the left margin) but because it does not start a new line, "2night" is output over "4 dinn" resulting in a second line that looks like "2nighter".

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"); } } 3) How many lines of output are provided by this program? a) 1 b) 2 c) 3 d) 4 e) 5

b. Explanation: There will be one line of output for the first two statements combined because the print statement does not return the cursor to start a new line. And since the second statement is a println, it returns the cursor and the last println outputs its message on a separate line.

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"

c. (Explanation: System.out.print will output the word "Here" but will leave the cursor at that point rather than starting a new line. The next statement will output "There Everywhere" immediately after the word "Here". Since there is a blank space within the quote marks for "There", there is a blank space inserted between "There" and "Everywhere".)

22) 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);

c. Explanation: Math.abs returns the absolute value of x. If x is negative, Math.sqrt(x) causes a run-time error, but Math.sqrt(Math.abs(x)) does not since x is first converted to its positive equivalent before the square root is performed. Answer a returns x (square root of x2 is x). In answer b, casting x to an int will not resolve the problem if x is negative. In answer d, the two Math functions are performed in opposite order and so if x is negative, it still generates a run-time error. Answer e will only work if x is not positive and so if x is positive, it now generates a run-time error.

12) 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) +, -, *, /

c. Explanation: Order of precedence is any operator in ( ) first, followed by * and / in a left-to-right manner, followed by + and - in a left-to-right manner. So, + is first since it is in ( ), followed by * followed by / since * is to the left of /, followed finally by -.

18) Consider having three String variables a, b and c. The statement c = a + b; can also be achieved by doing 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);

c. The statement c = a + b uses the concatenation operator + (not to be confused with numeric addition). The same result can be achieved by passing a the concat message with b as the parameter. Answer d will set c to be b + a rather than a + b.

23) 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));

d. Explanation: nf is an object and so must be passed a message to use it. The method to format a double is called format and the value to be formatted is the parameter passed to format. Therefore, the proper way to do this is nf.format(x). The answer in a will merely output 0.362491 while the answers to b, c and e are syntactically invalid.

9) 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

e. Explanation: Because 10.00 is not an int, the division produces a double precision value which cannot be stored in the int z. For this to work, the result of the division must be cast as an int before being stored in z, or the value 10.00 would have to first be cast as an int before the division takes place.

17) If you want to store into the String name the value "George Bush", you would use 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

e. Explanation: There are two ways to store a character string into a String variable, by constructing a new String using "new String(string value); " or by using an assignment statement, so either a or b will work. In c and d, we have variations where the String concatenation operator + is used. So all four approaches will work.

21) 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

e. Explanation: This is a java utility, and so is found in the java.util package.


Set pelajaran terkait

IT- Anti- Money Laundering Laws and Practices

View Set

(155) Physical Assessment Techniques

View Set

INTERNATIONAL BUSINESS CHAPTER 1

View Set

Stats 1430 Chapter 4: Two-Way Tables

View Set

Chapter 07: Legal Dimensions of Nursing Practice (3)

View Set