Chapter 2 - Using Data Flashcards

1
Q

When data cannot be changed after a class is compiled, the data is _____________.

a) variable

b) constant

c) volatile

d) mutable

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which of the following is not a primitive data type in Java?

a) boolean

b) byte

c) sector

d) int

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The assignment operator in Java is _____________.

a) =

b) ==

c) :=

d) ::
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A

byte

a byte has a minimum value of -128 and maximum value and occupies 1 byte or 8-bits

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

A boolean variable can hold _____________.

a) a character

b) a whole number

c) a decimal number

d) the value true or false

A

D

the value true or false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The value 137.68 can be held by a variable of type _____________.

a) int

b) short

c) double

d) byte

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

An escape sequence always begins with a(n) _____________.

a) e

b) forward slash

c) backslash

d) equal sign

A

C

a backslash

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

A

C

the “\n” is an escape sequence for a newline or linefeed; moves the cursor to the beginning of the next line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which of the following operators has the lowest precedence?

a) multiplication

b) remainder

c) subtraction

d) division

A

C

  • (mulitiplication) / (division) and % (remainder) have the highest precedence

+ (addition) - (subtraction) have the lowest precedence

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

The equal to relational operator is _____________.

a) =

b) ==

c) !=

d) !!’

A

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 (==)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

float has the highest level unifying type in Java

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

You use a _____________ to explicitly override an implicit type.

a) mistake

b) type cast

c) format

d) type set

A

B type cast

type casting forces a value of one data type to be used as a value of another type; to perform a type cast, you use a cast operator, which is created by placing the desired result type in parenthesis

float weeklyBudge = (float) (bankBalance / 4);

17
Q

In Java, what is the value of 3 + 7 * 4 + 2?

a) 21

b) 33

c) 42

d) 48

A

B

because of the order of operations precedence the multiplication would be calculated first (7*4) and then that product is added to 3 and finally 2 so 28+3+2 = 33

18
Q

Which assignment is correct in Java?

a) int value = (float) 4.5;

b) float value = 4 (double);

c) double value = 2.12;

d) char value = 5c;

A

C

19
Q

Which assignment is correct in Java?

a) double money = 12;

b) double money = 12.0;

c) double money = 12.0d;

d) All of these are correct.

A

B

double money = 12; <- this should be declared as an int

double money = 12.0d; <- this will result in an error

20
Q

Which assignment is correct in Java?

a) char aChar = 5.5;

b) char aChar = “W”;

c) char aChar = ‘*’;

d) char aChar = $;

A

C