Java Quiz 2: Data and Expression

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

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

105

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

Of the following types, which one cannot store a numeric value?

char int and byte are used to store whole numbers (integers) and float 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.

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

2

Java is a strongly typed language. What is meant by "strongly typed"?

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

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.

Which of the following situations would require a cast?

storing a float in an int For A, charAt returns a char, so there is no problem. In B and C, the situations are widening operations 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. storing an int in a float storing a float in a double using charAt to take an element of a String and store it in a char storing a float in an int All of these require casts.

There are three ways that data conversion may occur: by assignment, by promotion, and by casting.

True Assignment conversion occurs when a value on the right side of the assignment operator is converted prior to being stored in the variable on the left. Promotion occurs within an expression when values of differing widths are combined. Casting is a programmer's explicit way to control the data conversion process.

What is the value of z after the following assignment statement is executed? float z = 5/10;

0.0 5 and 10 are both int values, so 5 / 10 is an integer division. The result is 0. Even though z is a float 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 float.

Which of the following is TRUE regarding the mod operator, %?

It can be performed on any numeric values and its result is always numeric. Mod, or modulo, returns the remainder that results from a division. The remainder is always is numeric. Although usually integer values are used, the % operator may be used on all kinds of numeric data.

A variable of type boolean will store either a 0 or a 1.

false A boolean variable can store only one of two values, but these values are the reserved words true and false. In C, 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 (and is much safer).

The values of (double)5/2 and (double)(5/2) are identical.

false In the first expression, the (double) cast applies to the int 5, changing it to the double value, 5.0. Then 5.0/2 is calculated, yielding the double value, 2.5. In the second expression the int division is performed first, yielding the value 2. 2 is then changed to a double, yielding the double value, 2.0.

The word println is a(n)

method The word println is passed as a message to the System.out object, and so println is a method.


Set pelajaran terkait

NUR2261 - Unit 5 - Development and Cognition

View Set

Georgia Laws, Rules, and Regulation (Life and Accident and Sickness Insurance)

View Set

MARK Test 3 ( Ch 12, 13, 14, 15, 16)

View Set

Square Numbers/ Square Roots (1-13)

View Set

Solving Linear Equations: Variable on One Side

View Set

Philippine Electrical Code - CHAPTER 6

View Set