Deck 3: Using Classes and Objects
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/58
Play
Full screen (f)
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 (much as a period terminates a sentence in English)
E) Both B and C are correct
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 (much as a period terminates a sentence in English)
E) Both B and C are correct
E
Explanation: E) The dot operator is appended directly after the object reference, followed by the data or method to which access is desired. In the case of data, the access may be for reading or writing. In the case of a method, the access is to allow one to invoke the method. The dot within a floating point number is a decimal pointnot a dot operator.
Explanation: E) The dot operator is appended directly after the object reference, followed by the data or method to which access is desired. In the case of data, the access may be for reading or writing. In the case of a method, the access is to allow one to invoke the method. The dot within a floating point number is a decimal pointnot a dot operator.
2
In Java a variable may contain
A) a value or a reference
B) a package
C) a method
D) a class
E) any of the above
A) a value or a reference
B) a package
C) a method
D) a class
E) any of the above
A
Explanation: A) Java variables contain values or references to instances of classes (which contain values and/or additional references).
Explanation: A) Java variables contain values or references to instances of classes (which contain values and/or additional references).
3
Consider the following two lines of code. What can you say 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 s2 is legal Java; the line declaring s1 will produce a syntax error
C) s1 and s2 are both references to different String objects
D) s1 and s2 will compare "equal"
E) none of the above
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 s2 is legal Java; the line declaring s1 will produce a syntax error
C) s1 and s2 are both references to different String objects
D) s1 and s2 will compare "equal"
E) none of the above
C
Explanation: C) Both declarations are legal Java. s1 is a String reference, and it is initialized to the String "testing123". s2 is a String reference, and it is initialized to the String "testing 123". Note the space between the "testing" and the "123". So the two Strings are not equal.
Explanation: C) Both declarations are legal Java. s1 is a String reference, and it is initialized to the String "testing123". s2 is a String reference, and it is initialized to the String "testing 123". Note the space between the "testing" and the "123". So the two Strings are not equal.
4
Which of the following will yield a pseudorandom number in the range [ -5, +5 ) given the following: 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
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 58 flashcards in this deck.
Unlock Deck
k this deck
5
Say you write a program that makes use of 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 the missing class.
B) The program won't compile-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 runtime error when it attempts to access any member of the Random class
E) none of the above
A) The program won't run, but it will compile with a warning about the missing class.
B) The program won't compile-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 runtime error when it attempts to access any member of the Random class
E) none of the above
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
6
What is the advantage of putting an image in a JLabel instance?
A) It becomes part of the component and is laid out automatically
B) It becomes part of the container and is automatically scaled
C) Although it becomes part of the instance, it still must be drawn separately
D) The runtime efficiency is greatly improved
E) None of the above
A) It becomes part of the component and is laid out automatically
B) It becomes part of the container and is automatically scaled
C) Although it becomes part of the instance, it still must be drawn separately
D) The runtime efficiency is greatly improved
E) None of the above
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
7
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
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 58 flashcards in this deck.
Unlock Deck
k this deck
8
The String class' compareTo method
A) compares two string in a case-independent manner
B) yields True or false
C) yields 0 if the two strings are identical
D) returns 1 if the first string comes lexically before the second string
E) none of the above
A) compares two string in a case-independent manner
B) yields True or false
C) yields 0 if the two strings are identical
D) returns 1 if the first string comes lexically before the second string
E) none of the above
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
9
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 names
E) none of the above
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 names
E) none of the above
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
10
The advantage(s) of the Random class' pseudo-random number generators, compared to the Math.random method, is 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 but answer B
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 but answer B
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
11
In the StringMutation program shown in Listing 3.1, if phrase is initialized to "Einstein" what will Mutation #3 yield if Mutation #1: mutation1 = phrase.concat(".")?
A) Einstein.
B) EINSTEIN.
C) XINSTXIN.
D) einstein.
E) xinstxin.
A) Einstein.
B) EINSTEIN.
C) XINSTXIN.
D) einstein.
E) xinstxin.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
12
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.compareTo(strC) would yield 0
D) strA.equals(strC) would be True
E) none of the above
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.compareTo(strC) would yield 0
D) strA.equals(strC) would be True
E) none of the above
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
13
What will be displayed by this command: System.out.println(Math.pow(3, 3-1));
A) 9
B) 8
C) 6
D) 4
E) 27
A) 9
B) 8
C) 6
D) 4
E) 27
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
14
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 runtime error may occur during execution
C) A "garbage" or "uninitialized" value will be used in the computation
D) A value of zero is used if a variable has not been initialized
E) Answers A and B are correct
A) A syntax error may be generated by the compiler
B) A runtime error may occur during execution
C) A "garbage" or "uninitialized" value will be used in the computation
D) A value of zero is used if a variable has not been initialized
E) Answers A and B are correct
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
15
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 but 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 correct
A) the object may be modified using either alias
B) the object cannot be modified unless there's but 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 correct
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
16
Consider the following enumeration enum Speed { FAST, MEDIUM, SLOW };
A) The ordinal value of MEDIUM is 2
B) The ordinal value of SLOW is 1
C) The name of the Speed enumeration whose ordinal value is zero is FAST
D) The name of the Speed enumeration whose ordinal value is one is SLOW
E) None of the above
A) The ordinal value of MEDIUM is 2
B) The ordinal value of SLOW is 1
C) The name of the Speed enumeration whose ordinal value is zero is FAST
D) The name of the Speed enumeration whose ordinal value is one is SLOW
E) None of the above
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
17
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
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 58 flashcards in this deck.
Unlock Deck
k this deck
18
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 to an existing object
D) launching a method
E) none of the above
A) noticing the first time something is used
B) creating a new object of the class
C) creating a new alias to an existing object
D) launching a method
E) none of the above
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
19
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 A and B are True
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 A and B are True
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
20
The advantages of the DecimalFormat class compared with 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 to round them
D) the ability to display a % automatically at the beginning of the display
E) only A and B
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 to round them
D) the ability to display a % automatically at the beginning of the display
E) only A and B
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
21
What is the function of a frame's pack method?
A) It deletes unused storage
B) It forces orphan objects to be reclaimed by the garbage collector
C) It sets the size appropriately for display
D) It forces the frame contents to be up-left justified
E) None of the above
A) It deletes unused storage
B) It forces orphan objects to be reclaimed by the garbage collector
C) It sets the size appropriately for display
D) It forces the frame contents to be up-left justified
E) None of the above
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
22
These two ways of setting up a string yield identical results:
a) String string = new String("string");
b) String string = "string";
a) String string = new String("string");
b) String string = "string";
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
23
Autoboxing is
A) the automatic conversion of a wrapper object to/from its corresponding primitive type
B) the automatic enclosure of a graphics object within a bounding box
C) the automatic widening of ints into floats or doubles, as required
D) the automatic conversion of an enumeration into its numeric representation
E) none of the above
A) the automatic conversion of a wrapper object to/from its corresponding primitive type
B) the automatic enclosure of a graphics object within a bounding box
C) the automatic widening of ints into floats or doubles, as required
D) the automatic conversion of an enumeration into its numeric representation
E) none of the above
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
24
JOptionPane is a class that provides GUI
A) dialog boxes
B) buttons
C) output fields
D) panels and frames
E) all of the above
A) dialog boxes
B) buttons
C) output fields
D) panels and frames
E) all of the above
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
25
The main difference between a frame and a panel is
A) a panel can have a title bar; a frame cannot
B) neither frames nor panels can be nested, but frames can be contained within panels
C) frames can have a title bar; panels cannot
D) frames can be nested; panels cannot
E) none of the above
A) a panel can have a title bar; a frame cannot
B) neither frames nor panels can be nested, but frames can be contained within panels
C) frames can have a title bar; panels cannot
D) frames can be nested; panels cannot
E) none of the above
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
26
Which of the following GUI components is used to accept input into a JFrame?
A) JLabel
B) JPanel
C) JInput
D) JTextField
E) JInputField
A) JLabel
B) JPanel
C) JInput
D) JTextField
E) JInputField
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
27
You may apply the prefix and postfix increment and decrement operators to instances of the Integer class.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
28
These two ways of setting up a String yield identical results:
a) String string = new String("123.45");
b) String string = "" + 123.45;
a) String string = new String("123.45");
b) String string = "" + 123.45;
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
29
In Java, the symbol "=" and the symbol "==" are used synonymously (interchangeably).
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
30
System.out.println("123" + 4) will display the value 127.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
31
Only difficult programming problems require a pseudocode solution before the programmer creates the implementation (program) itself.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
32
You may use the String replace( ) method to remove characters from a String.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
33
In addition to their usage providing a mechanism to convert (to box) 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 the above
A) enumerations
B) static constants
C) arrays to contain the data
D) exceptions
E) none of the above
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
34
These two ways of setting up a String yield identical results:
a) String string = "12345"
b) String string = 12345;
a) String string = "12345"
b) String string = 12345;
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
35
These two ways of setting up a String yield identical results:
a) String string = new String(12345);
b) String string = new String("12345");
a) String string = new String(12345);
b) String string = new String("12345");
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
36
A containment hierarchy is
A) a collection of relationships among panels, all at the same level
B) a nested collection of relationships among containers
C) a nested collection of relationships among applets
D) a collection of relationships among frames, usually at different levels
E) none of the above
A) a collection of relationships among panels, all at the same level
B) a nested collection of relationships among containers
C) a nested collection of relationships among applets
D) a collection of relationships among frames, usually at different levels
E) none of the above
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
37
A JPanel can be added to a JFrame to form a GUI. In order to place other GUI components such as JLabels and Buttons into the JFrame, which of the following messages is passed to the JPanel?
A) insert
B) include
C) get
D) getContentPane
E) add
A) insert
B) include
C) get
D) getContentPane
E) add
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
38
The Swing package
A) completely replaces the AWT
B) partially replaces the AWT
C) is complementary to the AWT
D) has no relationship to the AWT
E) none of the above
A) completely replaces the AWT
B) partially replaces the AWT
C) is complementary to the AWT
D) has no relationship to the AWT
E) none of the above
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
39
When comparing any primitive type of variable, == should always be used to test to see if two values are equal.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
40
Layout managers are associated with
A) objects
B) interfaces
C) classes
D) containers
E) none of the above
A) objects
B) interfaces
C) classes
D) containers
E) none of the above
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
41
Write a method to extract the initial from the first name.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
42
Write a statement using a method to guarantee that the initial will be a capital letter.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
43
The names of the wrapper classes are just the names of the primitive data types, but with an initial capital letter.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
44
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?
Why or why not?
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
45
Why does one set up a containment hierarchy?
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
46
Write a statement using a Scanner method to get the first name interactively.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
47
All the methods in the Math class are declared to be static.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
48
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 58 flashcards in this deck.
Unlock Deck
k this deck
49
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 58 flashcards in this deck.
Unlock Deck
k this deck
50
What is a wrapper class?
Why are they useful?
Why are they useful?
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
51
The Random class' setSeed( ) method allows you to restart the pseudo-random number sequence.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
52
Write the code to define the JFrame which will have the components placed into the JPanel first where the JPanel will have the inputLabel and ageField side by side and the outputLabel and messageLabel beneath them. Do not worry about making the JFrame do anything (i.e., don't implement an ActionListener).
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
53
What is the purpose of the ImageIcon class?
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
54
If you need to import not only the top-level of a package, but all its secondary levels as well, you should write: import package.*.*;
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
55
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;
x = y + 1;
y = y + 1;
z = z - 1;
x = x - z;
x = x + 1;
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
56
What is the difference between a heavyweight and a lightweight container?
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
57
What is autoboxing?
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
58
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?
How is this handled if x and y are objects?
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck