Chapter 2: practice questions

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

If the String major = "Computer Science", what is returned by major.charAt(1)?

'o'

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?

0

What is the output with the statement System.out.println(""+x+y); if x and y are int values where x=10 and y=5?

15

The following statement will output ________ lines of text. System.out.println("1 big bad wolf\t8 the 3 little pigs\n4dinner\r2night");

2

Which Java statement produces the following output? w xyz 1. System.out.println("waxy"); 2. System.out.println("w" + "xyz"); 3. System.out.println("w\nxyz"); 4. System.out.println("w\nx\ny\nz");

3.

The expression (int)(760252175 * 100)/100 evaluates to __.

76

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) 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 widertype is being stored in a narrower type, so a cast is required.

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

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

Which assignment is correct in Java? 1. int value = (float) 4.5; 2. float value = 4 (double); 3. double value = 2.12; 4. char value = 5c;

double value = 2.12;

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

To create a constant, you would use what term?

final

Suppose a Scanner object is created as follows:Scanner input = new Scanner(System.in);What method do you use to read an int value?

input.nextInt();

A cast is required in which situation?

storing a double in an int

Which of the following situations would require a cast? 1. storing an int in a float 2. storing a float in a double 3. using charAt to take an element of a String and store it in a char 4. storing a float in an int 5. All of these require casts.

storing a float in an int

1. Given the following code fragmentString strA = "aBcDeFg"; String strB = strA.toLowerCase( ); strB = strB.toUpperCase( ); String strC = strA.toUpperCase( );

strB.compareTo(strC) would yield 0

If x is an int and y is a double, all of the following are legal except which assignment statement?

x=y

If you attempt to add a float, an int, and a byte, the result will be a(n) __________. 1. Float 2. Int 3. Error message 4. Byte

Float

When you perform arithmetic with values of diverse types, Java __________. 1. Issues An Error Message 2. Implicitly Converts The Values To A Unifying Type 3. Requires You To Explicitly Convert The Values To A Unifying Type 4. Implicitly Converts The Values To The Type Of The First Operand

Implicitly Converts The Values To A Unifying Type

______ conversions should be avoided because they can ___ information.

Narrowing, lose

What values will z have if we execute the following assignment statement? int z = 50 / 10.00

None of the above, a run time error arises

Write an output statement which will output the following characters exactly as shown: /'\"/'\

System.out.println("/'\\\"/'\\");

True or False? If x is a string, then x = new String("OH"); and x = "OH"; will accomplish the same thing.

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

True or false? If String a = "ABCD" and String b = "abcd" then a.equals(b); returns false but a.equalsIgnoreCase(b); returns true.

True. Since "ABCD" is not the same as "abcd", the equals method returns false, but by ignoring case in equalsIgnoreCase, the two are considered true.

True or False? If String name = "George W. Bush"; then the instruction name.length(); will return 14.

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

True or 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. 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, float, or double value equivalent to the number stored in the String.

You use a __________ to explicitly override an implicit type.

Type Cast

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) 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 get0.5, we would have to first cast 5 or 10 as a double.

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

a) 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'.

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

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

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

b) The division is performed as an int division since x, y, z and 3 are all ints. Therefore, average gets the value 0.0. It is output as 0.0 instead of 0 because average is a double, which outputs at least one decimal digit unless specified otherwise using the DecimalFormat class.

How many lines of output are provided by this program? System.out.print("Here"); System.out.println("There " + "Everywhere"); System.out.println("But not" + "in Texas"); a) 1 b) 2 c) 3 d) 4 e) 5

b) 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.

Given three String variables, a, b, and c, which statement could you use to achieve the same thing as:c = a + b;

c = a.concat(b); 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.

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

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

c) 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.

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.

Which assignment is correct in Java? 1. char aChar = 5.5; 2. char aChar = "W"; 3. char aChar = '*'; 4. char aChar = "W"; & char aChar = '*'; Are Correct

char aChar = '*';


Set pelajaran terkait

Anatomy & Physiology - Chapter 10

View Set

Midterm Exam 1 - 7 Food and Culture

View Set

Passpoint Quality Improvement Block 3 MS ML6

View Set

Anatomy and Physiology - Lesson 1

View Set

Combo with "Genetics Ch 5 Genetic Linkage and Mapping in Eukaryotes" and 14 others

View Set

Things Fall Apart characters from chapter 1-7

View Set

1 Commercial Excess and Umbrella Liability Insurance Quiz

View Set