Chapter 2 - Using Data Flashcards
When data cannot be changed after a class is compiled, the data is _____________.
a) variable
b) constant
c) volatile
d) mutable
constant
a data item is constant when its value cannot be changed while a program is running; ex) when you include the following statement in a Java class, the number 459 is a constant
System.out.println(459);
Which of the following is not a primitive data type in Java?
a) boolean
b) byte
c) sector
d) int
sector
boolean, byte and int are all primitive data types in Java; Java provides for 8 primitive types of data; a primitive data type is a simple data type;
1) byte - byte length integer (8-bits)
2) short - short integer
3) int - integer
4) long - long integer
5) float - single-precision floating point
6) double - double-precision floating point
7) char - a single character
8) boolean - a boolean value (true or false)
Which of the following elements is not required in a variable declaration?
a) a type b) an identifier c) an assigned value d) a semicolon
an assigned value
a data type that identifies the type of data that the variable will store; an identifier that is the variable’s name; an ending semicolon
an optional assignment operator and assigned value, if you want a variable to contain an initial value
The assignment operator in Java is _____________.
a) = b) == c) := d) ::
A
the equal sign is the assignment operator; any value to the right of the assignment operator is assigned to the memory location named on the left; an assignment made when you declare a variable is an initialization; an assignment made later is simple an assignment
Assuming you have declared shoeSize to be a variable of type int, which of the following is a valid assignment statement in Java?
a) shoeSize = 9;
b) shoeSize = 9.5;
c) shoeSize = “nine”;
d) shoeSize = 9.5F;
A
because the value is declared as int variable then the assigned value must be a whole integer therefore B would be incorrect as 9.5 would need to be initialized as a float; C is being initialized as a string value;
Which of the following data types can store the value 0 using the least amount of memory?
a) short
b) long
c) int
d) byte
byte
a byte has a minimum value of -128 and maximum value and occupies 1 byte or 8-bits
A boolean variable can hold _____________.
a) a character
b) a whole number
c) a decimal number
d) the value true or false
D
the value true or false
The value 137.68 can be held by a variable of type _____________.
a) int
b) short
c) double
d) byte
C
double - a double data type requires more memory than a float and can hold 14 or 15 significant digits of accuracy; the maximum value of a double data type is 1.7 * 10^308 and the minimum is -1.7 * 10^308
An escape sequence always begins with a(n) _____________.
a) e
b) forward slash
c) backslash
d) equal sign
C
a backslash
Which Java statement produces w on one line and xyz on the next line?
a) System.out.println(“wxyz”);
b) System.out.println(“w” + “xyz”);
c) System.out.println(“w\nxyz”);
d) System.out.println(“w\nx\ny\nz”);
C
the “\n” is an escape sequence for a newline or linefeed; moves the cursor to the beginning of the next line
The remainder operator _____________.
a) is represented by a forward slash
b) must follow a division operation
c) provides the quotient of integer division
d) is represented by a percent sign
D
The percent sign is the remainder operator . The remainder operator is most often used with two integers, and the result is an integer with the value of the remainder after division takes place. For example, the result of 45 % 2 is 1 because 2 goes into 45 twenty-two times with a remainder of 1. Other examples of remainder operations include the following:
39 % 5 is 4 because 5 goes into 39 seven times with a remainder of 4. 20 % 3 is 2 because when 20 is divided by 3, the remainder is 2. 36 % 4 is 0 because there is no remainder when 4 is divided into 36.
Which of the following operators has the lowest precedence?
a) multiplication
b) remainder
c) subtraction
d) division
C
- (mulitiplication) / (division) and % (remainder) have the highest precedence
+ (addition) - (subtraction) have the lowest precedence
The equal to relational operator is _____________.
a) =
b) ==
c) !=
d) !!’
B
the equal sign is a Java keyword used for variable assignments; in order to compare values being equal to one another you must use the double equal sign (==)
When you perform arithmetic with values of diverse types, Java _____________.
a) issues an error message
b) implicitly converts the values to a unifying type
c) requires you to explicitly convert the values to a unifying type
d) implicitly converts the values to the type of the first operand
B
Java automatically converts nonconforming operands to the unifying type; an implicit conversion is also called a promotion
the highest level of unifying type is double followed by float followed by long; int is the lowest unifying type
If you attempt to add a float, an int, and a byte, the result will be a(n) _____________.
a) float
b) int
c) byte
d) error message
A
float has the highest level unifying type in Java