Chapter 3 Using Classes and Objects Flashcards
What is a null reference?
A null reference is a reference that does not refer to any object. The reserved word null can be used to check for null references before following them.
What does the new operator accomplish?
The new operator creates a new instance (an object) of the specified class. The constructor of the class is then invoked to help set up the newly created object.
Write a declaration for a String variable called author, and initialize it to the string “Fred Brooks”. Draw a graphic representation of the variable and its value.
The following declaration creates a String variable called author and initializes it:
String author = new String(“Fred Brooks”);
For strings, this declaration could have been abbreviated as follows:
String author = “Fred Brooks”;
This object reference variable and its value can be depicted as follows:
author || –||—–>||”Fred Brooks”||
Write a code statement that sets the value of an integer variable called size to the length of a String object called name.
To set an integer variable size to the length of a String object called name, you code:
size = name.length();
What is an alias? How does it relate to garbage collection?
Two references are aliases of each other if they refer to the same object. Changing the state of the object through one reference changes it for the other because there is actually only one object. An object is marked for garbage collection only when there are no valid references to it.
Assume s1, s2, and s3 are String variables initialized to “Amanda”, “Bobby”, and “Chris”, respectively. Which String variable or variables are changed by each of the following statements?
a. System.out.println(s1);
b. s1 = s3.toLowerCase();
c. System.out.println(s2.replace(‘B’, ‘M’));
d. s3 = s2.concat(s1);
Strings are immutable. The only way to change the value of a String variable is to reassign it a new object. Therefore, the variables changed by the statements are: a. none, b. s1, c. none, d. s3
What output is produced by the following code fragment?
String s1 = "Foundations"; String s2; System.out.println(s1.charAt(1)); s2 = s1.substring(0, 5); System.out.println(s2); System.out.println(s1.length()); System.out.println(s2.length());
The output produced is:
o
Found
11
5
Write a statement that prints the value of a String object called title in all uppercase letters.
The following statement prints the value of a String object in all uppercase letters:
System.out.println(title.toUpperCase());
Write a declaration for a String variable called front, and initialize it to the first 10 characters of another String object called description.
The following declaration creates a String object and sets it equal to the first 10 characters of the String description;
String front = description.substring(0, 10);
What is a Java package?
A Java package is a collection of related classes. The Java standard class library is a group of packages that support common programming tasks.
What does the java.net package contain? The javax.swing package?
Each package contains a set of classes that support particular programming activities. The classes in the java.net package support network communication and the classes in the javax.swing class support the development of graphical user interfaces.
What package contains the Scanner class? The String class? The Random class? The Math class?
The Scanner class and the Random class are part of the java.util package. The String and Math classes are part of the java.lang package.
Using the online Java API documentation, describe the Point class.
The Point class, according to the online Java API documentation, represents a location with coordinates (x, y) in two-dimensional space.
What does an import statement accomplish?
An import statement establishes the fact that a program uses a particular class, specifying what package that class is a part of. This allows the programmer to use the class name (such as Random) without having to fully qualify the reference (such as java.util.Random) every time.
Why doesn’t the String class have to be specifically imported into our programs?
The String class is part of the java.lang package, which is automatically imported into any Java program. Therefore, no separate import declaration is needed.
Given a Random object called rand, what does the call rand.nextInt() return?
A call to the nextInt method of a Random object returns a random integer in the range of all possible int values, both positive and negative.
Given a Random object called rand, what does the call
rand.nextInt(20) return?
Passing a positive integer parameter x to the nextInt method of a Random object returns a random number in the range of 0 to x−1. So a call to nextInt(20) will return a random number in the range 0 to 19,
inclusive.
Assuming that a Random object has been created called generator, what is the range of the result of each of the following expressions?
a. generator.nextInt(50)
b. generator.nextInt(5) + 10
c. generator.nextInt(10) + 5
d. generator.nextInt(50) – 25
The ranges of the expressions are:
a. From 0 to 49
b. From 10 to 14
c. From 5 to 14
d. From -25 to 24