Chapter 3 Flashcards

1
Q

What is the primary task when writing a Java program?

A

Defining a set of classes that your program will use

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

What statement would you use if you wanted to create a variable called “name” which held the text string, “Joe”?

A

String name = new String(“Joe”);

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

What is the name of the class that is typically used to divide a string into shorter strings called tokens?

A

StringTokenizer

it’s in the java.util package

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

What do we call the special method that gets called when you use the “new” operator to create an object of a class?

A

a constructor

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

How does Java handle “garbage collection”?

A

Java automatically clears memory of any objects that are no longer in use.

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

Using dot notation, how would you refer to a variable called “driver” in an object called “car”?

A

car.driver

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

If the driver of a car was represented in dot notation as “car.driver”, how could you represent the age of that driver?

A

car.driver.age

this is called “chaining”

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

What statement would you use to declare a class variable called lastName and assign a value of “Jones” to it?

A

static String lastName = “Jones”;

static is the keyword that specifies the variable is a class variable

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

When retrieving or changing the value of a class variable, how do you refer to that variable using dot notation?

A

[object instance].[variable]
OR
[class].[variable]

(For readability, the second method is preferable since it is, after all, a class variable]

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

When reading code, what distinguishes methods from other program elements?

A

parentheses

Example: cancelOrder()

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

What method can be used to print a number in currency format?

A

System.out.format( );

In the parentheses, you use a percent sign followed by one or more letter flags, then a comma and the variable to be formatted.

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

What is an object reference?

A

An address indicating where the object’s variables and methods are stored

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

What special flexibility does Java demonstrate when processing a group of concatenated variables where at least one of them is a string?

A

Java treats the whole group as a String.

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

What is casting?

A

Converting a value from one data type to another

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

Which primitive data type can not be cast to any other data type?

A

Booleans

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

How do you cast a value or expression to another data type?

A

(typename) value

17
Q

What statement would you use to cast the result of expression “x / y” to an int data type, and store that result in a variable called “result”?

A

int result = (int)( x / y);

18
Q

What conditions must apply to be able to cast an object into an object of another class?

A

One class must be a subclass of the other

19
Q

When you cast a superclass object as an object of a subclass, what does the object gain?

A

All the methods and variables that the subclass defines

20
Q

Write a statement that will cast “emp”, an object of the Employee class, as an object of the VicePresident class, into the “veep” variable.

A

veep = (VicePresident) emp;

21
Q

Is it possible to cast an object to a primitive data type, and a primitive data type to an object?

A

No.

22
Q

What is the difference between a double and a Double?

A

A double is a primitive data type. Double is a “wrapper” class in the java.lang package that allows you to work with a double as an object.