Deck 3: Using Classes and Objects

Full screen (f)
exit full mode
Question
What is the function of the dot operator?

A) It serves to separate the integer portion from the fractional portion of a floating-point number.
B) It allows one to access the data within an object when given a reference to the object.
C) It allows one to invoke a method within an object when given a reference to the object.
D) It is used to terminate commands, similar to the way a period terminates a sentence in English.
E) Both B and C are correct.
Use Space or
up arrow
down arrow
to flip the card.
Question
If two variables contain aliases of the same object then

A) the object may be modified using either alias
B) the object cannot be modified unless there's a single reference to it
C) a third alias is created if/when the object is modified
D) the object will become an "orphan" if both variables are set to null
E) answers A and D are both correct
Question
These two ways to set up a String will yield identical results:
String my_string = new String("123.45");
String my_string = "" + 123.45;
Question
When comparing any primitive type of variable, the == operator should always be used to test if two values are equal.
Question
These two ways to set up a String will yield identical results:
String my_string = "12345";
String my_string = 12345;
Question
These two ways to set up a String will yield identical results:
String my_string = new String(12345);
String my_string = new String("12345");
Question
In Java a variable may contain

A) a value or a reference
B) a package
C) a method
D) a class
E) Any of these
Question
What happens if you attempt to use a variable before it has been initialized?

A) A syntax error may be generated by the compiler.
B) A run-time error may occur during execution.
C) A "garbage" or "uninitialized" value will be used in the computation.
D) A value of zero is used.
E) Only answers A and B are correct.
Question
In Java, instantiation means

A) noticing the first time something is used
B) creating a new object of the class
C) creating a new alias for an existing object
D) launching a method
E) none of these
Question
Only difficult programming problems require a pseudocode solution before the programmer creates the program itself.
Question
If you need to import not only the top-level of a package, but all its secondary levels as well, you should use the following:
import package.*.*;
Question
The printf method within System.out is designed to ease the conversion of legacy C code into Java.
Question
Which properties are true of String objects?

A) Their lengths never change.
B) The shortest String has zero length.
C) Individual characters within a String may be changed using the replace method.
D) The index of the first character in a String is one.
E) Only answers A and B are true.
Question
In Java, the symbol "=" and the symbol "==" are used interchangeably.
Question
The following statement will display the value 127.
System.out.println("123" + 4);
Question
These two ways to set up a String will yield identical results:
String my_string = new String("a bunch of words");
String my_string = "a bunch of words";
Question
You may apply the prefix and postfix increment and decrement operators to instances of the Integer class.
Question
You may use the String.replace() method to remove characters from a String.
Question
The names of the wrapper classes are just the names of the primitive data types, but with an initial uppercase letter.
Question
All the methods in the Math class are declared to be static.
Question
Java.text's NumberFormat class includes methods that

A) allow you to format currency
B) allow you to format percentages
C) round their display during the formatting process
D) truncate their display during the formatting process
E) A, B, C, but not D
Question
An alias is when

A) two different reference variables refer to the same physical object
B) two different numeric variables refer to the same physical object
C) two different numeric variables contain identical values
D) two variables have the same name
E) None of these
Question
Which is now the preferred approach for developing Java programs that use graphics and GUIs?

A) Swing
B) AWT
C) JavaFX
D) API
E) All of these are preferred approaches
Question
Consider the condition (x == y). How is this handled if x and y are primitive types? How is this handled if x and y are objects?
Question
Which of the following packages includes classes that represent shapes in JavaFX?

A) javafx.scene.shape
B) javafx.shape.scene
C) javafx.shapes
D) javafx.stage.shape
E) javafx.stage.scene
Question
Which of the following will yield a pseudorandom number in the range [-5, +5), given: Random gen = new Random();

A) gen.nextFloat()*5
B) gen.nextFloat()*10-5
C) gen.nextFloat()*5-10
D) gen.nextInt()*10-5
E) gen.nextInt(10)-5
Question
The String class's compareTo method

A) compares two strings in a case-independent manner
B) yields true or false
C) yields 0 if the two strings are identical
D) rerturns 1 if the first string comes lexically before the second string
E) None of these
Question
In the StringMutation program shown in Listing 3.1, if phrase is initialized to "Einstein", what will Mutation # 3 yield if mutation1 = phrase.concat(".")?

A) Einstein.
B) EINSTEIN.
C) XINSTXIN.
D) einstein.
E) xinstxin.
Question
Which statement is true, given the following code fragment? String strA = "aBcDeFg";
String strB = strA.toLowerCase();
StrB = strB.toUpperCase();
String strC = strA.toUpperCase();

A) strB.equals(strC) would be true
B) strB.compareTo(strC) would yield 0
C) strA.equals(strC) would be true
D) strA.compareTo(strC) would yield 0
E) None of these
Question
What does the following code fragment do? Circle circle = new Circle(100, 100, 50);
Circle.setFill(Color.GREEN);
Rectangle rectangle = new Rectangle(70, 70, 225, 75);
Rectangle.setStroke(Color.BLUE);
Rectangle.setStrokeWidth(2);
Rectangle.setFill(null);

A) It creates a green circle, centered at coordinates (100, 100).
B) It creates a rectangle which overlaps the circle since its upper-left corner is at coordinates (70, 70) and its dimensions are 225 X 75.
C) Since the rectangle's fill color is set to null, the circle will be visible through the rectangle.
D) The rectangle will have a blue border.
E) All of these are true.
Question
The advantages of the DecimalFormat class compared to the NumberFormat class include

A) precise control over the number of digits to be displayed
B) control over the presence of a leading zero
C) the ability to truncate values rather than round them
D) the ability to display a percent sign (%) automatically at the beginning of the display
E) only A and B
Question
Write a declaration for a Rectangle named squareShape that is 400 pixels wide, 400 pixels high, and its upper-left corner position is at point (50, 50).

A) Rectangle squareShape = new squareShape(50, 50, 400, 400);
B) squareShape = new Rectangle(50, 50, 400, 400);
C) Rectangle squareShape = new Rectangle(50, 400, 50, 400);
D) Rectangle squareShape = new Rectangle(50, 50, 400, 400);
E) Rectangle = new squareShape(400, 400, 50, 50);
Question
Given the following two lines of code, what can be said about s1 and s2? String s1 = "testing" + "123";
String s2 = new String("testing 123");

A) s1 and s2 are both references to the same String object.
B) the line declaring s1 is legal in Java; the line declaring s2 will produce a syntax error.
C) s1 and s2 are references to different String objects.
D) s1 and s2 will compare "equal."
E) None of the above
Question
In a development environment that fully supports JavaFX, which of the following is true?

A) The launch method of the scene class is called automatically.
B) The launch method of the Application class is called automatically.
C) Since the launch method is called automatically, you do not need to write the main method.
D) Since the launch method is called automatically, you do not need to call it in the main method.
E) Both B and C are true
Question
Assume you write a program that uses the Random class but you fail to include an import statement for java.util.Random or java.util.*. What will happen when you attempt to compile and run your program?

A) The program won't run but it will compile with a warning about missing the class.
B) The program won't compile and you'll receive a syntax error about the missing class.
C) The program will compile but you'll receive a warning about the missing class.
D) The program will encounter a run-time error when it attempts to access any member of the Random class.
E) none of these
Question
In addition to providing a mechanism to convert primitive data into objects, what else do the wrapper classes provide?

A) enumerations
B) static constants
C) arrays to contain the data
D) exceptions
E) None of these
Question
An API is

A) an Abstract Programming Interface
B) an Application Programmer's Interface
C) an Application Programming Interface
D) an Abstract Programmer's Interface
E) an Absolute Programming Interface
Question
The advantage(s) of the Random class's pseudorandom number generators, compared to the Math.random method, is(are) that

A) you may create several random number generators
B) the generators in Random are more efficient than the one in Math.random
C) you can generate random ints, floats, and ints within a range
D) you can initialize and reinitialize Random generators
E) all except B are true
Question
What will be displayed by the following command? System.out.println(Math.pow(3, 3-1));

A) 9
B) 8
C) 6
D) 4
E) 27
Question
Rewrite the following five assignment statements into a single statement using prefix and postfix increment and decrement operators as necessary. Assume all variables are int variables.
x = y + 1;
y = y + 1;
z = z - 1;
x = x - z;
x = x + 1;
Question
Refer to Problem Ch 03-1. Write a method to extract the initial from the first name.
Question
Refer to Problem Ch 03-1. For the program to get a name interactively a Scanner object must be instantiated. Write the Java statement to do this.
Question
Refer to Problem Ch 03-1. Write a statement to guarantee that the initial will be a capital letter.
Question
Why is it often a good idea to group particular elements together in a scene?
Question
Refer to Problem Ch 03-1. Write a statement using a Scanner method to get the first name interactively.
Question
What is a wrapper class? Why are wrapper classes useful?
Question
Given two String variables, s1 and s2, is it possible for (s1 != s2)to be true while (s1.equals(s2)) is also true? Why or why not?
Question
Write a statement to create a Color object equivalent to Color.ORANGE using the rgb method.
Question
What is autoboxing?
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/49
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 3: Using Classes and Objects
1
What is the function of the dot operator?

A) It serves to separate the integer portion from the fractional portion of a floating-point number.
B) It allows one to access the data within an object when given a reference to the object.
C) It allows one to invoke a method within an object when given a reference to the object.
D) It is used to terminate commands, similar to the way a period terminates a sentence in English.
E) Both B and C are correct.
E
2
If two variables contain aliases of the same object then

A) the object may be modified using either alias
B) the object cannot be modified unless there's a single reference to it
C) a third alias is created if/when the object is modified
D) the object will become an "orphan" if both variables are set to null
E) answers A and D are both correct
E
3
These two ways to set up a String will yield identical results:
String my_string = new String("123.45");
String my_string = "" + 123.45;
True
4
When comparing any primitive type of variable, the == operator should always be used to test if two values are equal.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
5
These two ways to set up a String will yield identical results:
String my_string = "12345";
String my_string = 12345;
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
6
These two ways to set up a String will yield identical results:
String my_string = new String(12345);
String my_string = new String("12345");
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
7
In Java a variable may contain

A) a value or a reference
B) a package
C) a method
D) a class
E) Any of these
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
8
What happens if you attempt to use a variable before it has been initialized?

A) A syntax error may be generated by the compiler.
B) A run-time error may occur during execution.
C) A "garbage" or "uninitialized" value will be used in the computation.
D) A value of zero is used.
E) Only answers A and B are correct.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
9
In Java, instantiation means

A) noticing the first time something is used
B) creating a new object of the class
C) creating a new alias for an existing object
D) launching a method
E) none of these
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
10
Only difficult programming problems require a pseudocode solution before the programmer creates the program itself.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
11
If you need to import not only the top-level of a package, but all its secondary levels as well, you should use the following:
import package.*.*;
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
12
The printf method within System.out is designed to ease the conversion of legacy C code into Java.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
13
Which properties are true of String objects?

A) Their lengths never change.
B) The shortest String has zero length.
C) Individual characters within a String may be changed using the replace method.
D) The index of the first character in a String is one.
E) Only answers A and B are true.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
14
In Java, the symbol "=" and the symbol "==" are used interchangeably.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
15
The following statement will display the value 127.
System.out.println("123" + 4);
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
16
These two ways to set up a String will yield identical results:
String my_string = new String("a bunch of words");
String my_string = "a bunch of words";
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
17
You may apply the prefix and postfix increment and decrement operators to instances of the Integer class.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
18
You may use the String.replace() method to remove characters from a String.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
19
The names of the wrapper classes are just the names of the primitive data types, but with an initial uppercase letter.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
20
All the methods in the Math class are declared to be static.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
21
Java.text's NumberFormat class includes methods that

A) allow you to format currency
B) allow you to format percentages
C) round their display during the formatting process
D) truncate their display during the formatting process
E) A, B, C, but not D
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
22
An alias is when

A) two different reference variables refer to the same physical object
B) two different numeric variables refer to the same physical object
C) two different numeric variables contain identical values
D) two variables have the same name
E) None of these
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
23
Which is now the preferred approach for developing Java programs that use graphics and GUIs?

A) Swing
B) AWT
C) JavaFX
D) API
E) All of these are preferred approaches
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
24
Consider the condition (x == y). How is this handled if x and y are primitive types? How is this handled if x and y are objects?
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
25
Which of the following packages includes classes that represent shapes in JavaFX?

A) javafx.scene.shape
B) javafx.shape.scene
C) javafx.shapes
D) javafx.stage.shape
E) javafx.stage.scene
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
26
Which of the following will yield a pseudorandom number in the range [-5, +5), given: Random gen = new Random();

A) gen.nextFloat()*5
B) gen.nextFloat()*10-5
C) gen.nextFloat()*5-10
D) gen.nextInt()*10-5
E) gen.nextInt(10)-5
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
27
The String class's compareTo method

A) compares two strings in a case-independent manner
B) yields true or false
C) yields 0 if the two strings are identical
D) rerturns 1 if the first string comes lexically before the second string
E) None of these
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
28
In the StringMutation program shown in Listing 3.1, if phrase is initialized to "Einstein", what will Mutation # 3 yield if mutation1 = phrase.concat(".")?

A) Einstein.
B) EINSTEIN.
C) XINSTXIN.
D) einstein.
E) xinstxin.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
29
Which statement is true, given the following code fragment? String strA = "aBcDeFg";
String strB = strA.toLowerCase();
StrB = strB.toUpperCase();
String strC = strA.toUpperCase();

A) strB.equals(strC) would be true
B) strB.compareTo(strC) would yield 0
C) strA.equals(strC) would be true
D) strA.compareTo(strC) would yield 0
E) None of these
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
30
What does the following code fragment do? Circle circle = new Circle(100, 100, 50);
Circle.setFill(Color.GREEN);
Rectangle rectangle = new Rectangle(70, 70, 225, 75);
Rectangle.setStroke(Color.BLUE);
Rectangle.setStrokeWidth(2);
Rectangle.setFill(null);

A) It creates a green circle, centered at coordinates (100, 100).
B) It creates a rectangle which overlaps the circle since its upper-left corner is at coordinates (70, 70) and its dimensions are 225 X 75.
C) Since the rectangle's fill color is set to null, the circle will be visible through the rectangle.
D) The rectangle will have a blue border.
E) All of these are true.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
31
The advantages of the DecimalFormat class compared to the NumberFormat class include

A) precise control over the number of digits to be displayed
B) control over the presence of a leading zero
C) the ability to truncate values rather than round them
D) the ability to display a percent sign (%) automatically at the beginning of the display
E) only A and B
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
32
Write a declaration for a Rectangle named squareShape that is 400 pixels wide, 400 pixels high, and its upper-left corner position is at point (50, 50).

A) Rectangle squareShape = new squareShape(50, 50, 400, 400);
B) squareShape = new Rectangle(50, 50, 400, 400);
C) Rectangle squareShape = new Rectangle(50, 400, 50, 400);
D) Rectangle squareShape = new Rectangle(50, 50, 400, 400);
E) Rectangle = new squareShape(400, 400, 50, 50);
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
33
Given the following two lines of code, what can be said about s1 and s2? String s1 = "testing" + "123";
String s2 = new String("testing 123");

A) s1 and s2 are both references to the same String object.
B) the line declaring s1 is legal in Java; the line declaring s2 will produce a syntax error.
C) s1 and s2 are references to different String objects.
D) s1 and s2 will compare "equal."
E) None of the above
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
34
In a development environment that fully supports JavaFX, which of the following is true?

A) The launch method of the scene class is called automatically.
B) The launch method of the Application class is called automatically.
C) Since the launch method is called automatically, you do not need to write the main method.
D) Since the launch method is called automatically, you do not need to call it in the main method.
E) Both B and C are true
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
35
Assume you write a program that uses the Random class but you fail to include an import statement for java.util.Random or java.util.*. What will happen when you attempt to compile and run your program?

A) The program won't run but it will compile with a warning about missing the class.
B) The program won't compile and you'll receive a syntax error about the missing class.
C) The program will compile but you'll receive a warning about the missing class.
D) The program will encounter a run-time error when it attempts to access any member of the Random class.
E) none of these
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
36
In addition to providing a mechanism to convert primitive data into objects, what else do the wrapper classes provide?

A) enumerations
B) static constants
C) arrays to contain the data
D) exceptions
E) None of these
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
37
An API is

A) an Abstract Programming Interface
B) an Application Programmer's Interface
C) an Application Programming Interface
D) an Abstract Programmer's Interface
E) an Absolute Programming Interface
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
38
The advantage(s) of the Random class's pseudorandom number generators, compared to the Math.random method, is(are) that

A) you may create several random number generators
B) the generators in Random are more efficient than the one in Math.random
C) you can generate random ints, floats, and ints within a range
D) you can initialize and reinitialize Random generators
E) all except B are true
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
39
What will be displayed by the following command? System.out.println(Math.pow(3, 3-1));

A) 9
B) 8
C) 6
D) 4
E) 27
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
40
Rewrite the following five assignment statements into a single statement using prefix and postfix increment and decrement operators as necessary. Assume all variables are int variables.
x = y + 1;
y = y + 1;
z = z - 1;
x = x - z;
x = x + 1;
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
41
Refer to Problem Ch 03-1. Write a method to extract the initial from the first name.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
42
Refer to Problem Ch 03-1. For the program to get a name interactively a Scanner object must be instantiated. Write the Java statement to do this.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
43
Refer to Problem Ch 03-1. Write a statement to guarantee that the initial will be a capital letter.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
44
Why is it often a good idea to group particular elements together in a scene?
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
45
Refer to Problem Ch 03-1. Write a statement using a Scanner method to get the first name interactively.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
46
What is a wrapper class? Why are wrapper classes useful?
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
47
Given two String variables, s1 and s2, is it possible for (s1 != s2)to be true while (s1.equals(s2)) is also true? Why or why not?
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
48
Write a statement to create a Color object equivalent to Color.ORANGE using the rgb method.
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
49
What is autoboxing?
Unlock Deck
Unlock for access to all 49 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 49 flashcards in this deck.