Java Software Solutions, 8e, Global Edition (Lewis/Loftus)
Chapter 3 Using Classes and Objects
3.1 Multiple-Choice Questions
1) 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
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 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
3) 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
4) 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
5) 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
6) 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
7) 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.
8) 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
9) 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
10) 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
11) 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
12) 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
13) 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
14) 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
15) 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
16) 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
17) 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
18) 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
19) 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
20) 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
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
22) Layout managers are associated with
A) objects
B) interfaces
C) classes
D) containers
E) none of the above
23) 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
24) 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
25) 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
26) 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
27) 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
28) 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
29) Which of the following GUI components is used to accept input into a JFrame?
A) JLabel
B) JPanel
C) JInput
D) JTextField
E) JInputField
30) JOptionPane is a class that provides GUI
A) dialog boxes
B) buttons
C) output fields
D) panels and frames
E) all of the above
9
© Pearson Education Limited, 2015
3.2 True/False Questions
1) Only difficult programming problems require a pseudocode solution before the programmer
creates the implementation (program) itself.
2) You may apply the prefix and postfix increment and decrement operators to instances of the
Integer class.
3) In Java, the symbol “=” and the symbol “==” are used synonymously (interchangeably).
4) When comparing any primitive type of variable, == should always be used to test to see if two
values are equal.
5) These two ways of setting up a string yield identical results:
a) String string = new String(“string”);
b) String string = “string”;
6) These two ways of setting up a String yield identical results:
a) String string = “12345”
b) String string = 12345;
7) These two ways of setting up a String yield identical results:
a) String string = new String(“123.45”);
b) String string = “” + 123.45;
8) These two ways of setting up a String yield identical results:
a) String string = new String(12345);
b) String string = new String(“12345”);
9) System.out.println(“123” + 4) will display the value 127.
10) You may use the String replace( ) method to remove characters from a String.
11) 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.*.*;
…
12) The Random class’ setSeed( ) method allows you to restart the pseudo-random number
sequence.
13) All the methods in the Math class are declared to be static.
14) The printf method within System.out is designed to ease the conversion of legacy C code
into Java.
15) The names of the wrapper classes are just the names of the primitive data types, but with an
initial capital letter.
3.3 Free-Form Questions
1) 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;
2) 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).
3) 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?
4) 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?
5) Why does one set up a containment hierarchy?
6) What is the purpose of the ImageIcon class?
7) What is a wrapper class? Why are they useful?
8) What is the difference between a heavyweight and a lightweight container?
9) What is autoboxing?
10) For the program to get a name interactively a Scanner object must be instantiated. Write the
Java statement to do this.
11) Write a statement using a Scanner method to get the first name interactively.
12) Write a method to extract the initial from the first name.
13) Write a statement using a method to guarantee that the initial will be a capital letter.