Java Ch 8 – 11 – Flashcards
138 test answers
Unlock all answers in this set
Unlock answers 138question
A class's static methods do not operate on the fields that belong to any instance of the class
answer
True
Unlock the answer
question
If you have defined a class SavingsAccount with a public static method getNumberOfAccounts(), and created a SavingsAccount object referenced by the variable account20, which of the following will call the getNumberOfAccounts()method?
answer
SavingsAccount.getNumberOfAccounts();
Unlock the answer
question
The JVM periodically performs this process to remove unreferenced objects from memory.
answer
garbage collection
Unlock the answer
question
If the following is from the method section of a UML diagram, which of the following statements is true? + equals(object2:Stock) : boolean
answer
This is a public method that accepts a FeetInches object as its argument and returns a boolean value
Unlock the answer
question
You may use this to compare two enum data values.
answer
The equals and compareTo methods that automatically come with the enum data type
Unlock the answer
question
Look at the following declaration. enum Tree { OAK, MAPLE, PINE } What is the fully-qualified name of the PINE enum constant?
answer
Tree.PINE
Unlock the answer
question
If the this variable is used as a constructor call,
answer
A compiler error will result if it is not the first statement of the constructor
Unlock the answer
question
To copy an object to another object of the same class
answer
Write a copy method that will make a field by field copy of the two objects
Unlock the answer
question
Enumerated types have this method, which returns the position of an enum constant in the declaration list.
answer
ordinal
Unlock the answer
question
If you have defined a class named SavingsAccountwith a public static data member namednumberOfAccounts, and created a SavingsAccountobject referenced by the variable account20, which of the following will assign numberOfAccounts to numAccounts?
answer
numAccounts = SavingsAccount.numberOfAccounts;
Unlock the answer
question
When a method's return type is a class, what is actually returned to the calling program?
answer
A reference to an object of that class
Unlock the answer
question
The only limitation that static methods have is
answer
They cannot refer to non-static members of the class
Unlock the answer
question
When the this variable is used to call a constructor,
answer
. It must be the first statement in the constructor making the call
Unlock the answer
question
You can declare an enumerated data type inside of a method.
answer
False
Unlock the answer
question
The term for the relationship created by object aggregation is
answer
Has a
Unlock the answer
question
If you write a toString method to display the contents of an object, object1, for a class, Class1, then the following two statements are equivalent: System.out.println(object1); System.out.println(object1.toString());
answer
True
Unlock the answer
question
You cannot use the fully-qualifed name of an enum constant for this
answer
a case expression
Unlock the answer
question
To compare two objects
answer
Write an equals method that will make a field by field comparison of the two object
Unlock the answer
question
What will be returned from a method, if the following is the method header: public Rectangle getRectangle()
answer
The address of an object in the class Rectangle
Unlock the answer
question
An instance of a class does not have to exist in order for values to be stored in a class's static fields
answer
True
Unlock the answer
question
If you write a toString method for a class, Java will automatically call the method any time you concatenate an object of the class with a string.
answer
True
Unlock the answer
question
Which of the following is not true about static methods?
answer
They are called from an instance of the class.
Unlock the answer
question
Few programmers use wrapper classes because
answer
They are immutable, They are not easy to use
Unlock the answer
question
The Character wrapper provides numerous methods for
answer
Testing and converting char variables
Unlock the answer
question
What will be printed after the after the following code is executed? String str = "abc456"; int i = 0; while (1<6) { if(character.isLetter(str.charAt(i)) System.out.println(Character.toUpperCase(charAt(i))); i++; }
answer
ABC
Unlock the answer
question
If String str = "RSTUVWXYZ", what will be the value returned from str.charAt(5)?
answer
W
Unlock the answer
question
If a non-letter is passed to toLowerCase or toUpperCase method, it is returned unchanged
answer
True
Unlock the answer
question
What will be the value of matches after the following code is executed? boolean matches; String[] productCodes = {"456HI345", "3456hj"}; matches = productCodes[0].regionMatches(true, 1, productCode[1], 2, 3)
answer
True
Unlock the answer
question
What will be the value of loc after the following code is executed? int loc; String str = "The cow jumped over the moon." loc = str.indexOf("ov");
answer
15
Unlock the answer
question
In the following statement, what data type must recField be: str.getChars(5, 10, recField, 0);
answer
char[ ]
Unlock the answer
question
When you are writing with string objects that may have unwanted spaces at the beginning or end of the strings, use the ____ method to delete them.
answer
trim
Unlock the answer
question
The valueOf() method accepts a string representation as an argument and returns its equivalent integer value.
answer
False
Unlock the answer
question
StringBuffer objects are immutable.
answer
False
Unlock the answer
question
The following StringBuffer constructor will StringBuffer str = new String Buffer(25)
answer
Give the object, str, 25 bytes of storage and not store anything in them
Unlock the answer
question
What would be the results of executing the following code? StringBuffer str = new String Bufffer("Little Jack Horner") str.append("sat on the"); str.append("corner");
answer
str would equal " Little Jack Horner sat on the corner"
Unlock the answer
question
When using StringBuffer insert method, you cannot insert
answer
Another StringBuffer type
Unlock the answer
question
What will be the value of strbuff after the following statements are executed? StringBuffer strbuff = new StringBuffer('We have lived in Chicago, Trenton, and Atlanta." strbuff.replace(17,24,"Tampa");
answer
We have lived in Tampa, Trenton, and Atlanta.
Unlock the answer
question
In a string that contains a series of words or other items of data separated by spaces or other characters, the programming term for the spaces or other characters is
answer
Delimiter
Unlock the answer
question
To use the class StringTokenizer, you must have the following import statement.
answer
import java.util.StringTokenizer;
Unlock the answer
question
What will be the tokens in the following statement? StringTokenizer = strToken new StringTokenizer("123-456-7890", "-",true);
answer
123, 456 7890, and -
Unlock the answer
question
For the following code, how many times would the loop execute? StringTokenizer strToken = new StringTokenizer("Cars, trucks, and SUVs are all types of automobiles."); while(strToken.hasMoreTokens) { System.out.println(strToken.nextToken()); }
answer
9
Unlock the answer
question
If you are using characters other than whitespaces as delimeters, you will probably want to trim the string before tokenizing; otherwise, the leading and/or following whitespaces will become part of the first and/or last token
answer
True
Unlock the answer
question
What does the following statement do? Double number = new Double(8.8);
answer
It Creates a Double Object; It initializes that object to 8.8; It assigns the object's address to the number variable
Unlock the answer
question
To convert the string, str = "285.74" to a double and store it in the variable x, use the following statement.
answer
double x = Double.parseDouble(str);
Unlock the answer
question
To convert the integer vaiable, number = 56, to a string, use the following statement
answer
String str = Integer.toString(number);
Unlock the answer
question
which of the following statements will print the maximum value a double variable may have?
answer
System.out println(Double.MAX_VALUE);
Unlock the answer
question
You must call a method to get the value of a wrapper class object
answer
False
Unlock the answer
question
Although it is not normally useful to create objects from the wrapper classes, programmers might use wrapper classes because
answer
They provide static methods that are very useful
Unlock the answer
question
Use the following import statement when using the character wrapper class
answer
No import statement is needed
Unlock the answer
question
What will be printed after the after the following code is executed? String str = "abc456"; int i = 0; while (1<6) { if(!Character.isLetter(str.charAt(i)) System.out.println(Character.toUpperCase(charAt(i))); i++; }
answer
456
Unlock the answer
question
If String str = "ABCDEFGHI," what will be returned from Character.toLowerCase(str.charAt(5))?
answer
f
Unlock the answer
question
What eill be the value of matches after the following code has been executed> boolean matches; String str1 = "The cow jumped over the moon."; String str2 = "moon'; matches = str1.endsWith(str2);
answer
False
Unlock the answer
question
What will be the value of loc after the following code is executed? int loc; String str = "The cow jumped over the moon." loc = str.lastIndexOf("ov",14);
answer
-1
Unlock the answer
question
What is the value of str after the followuing code has been executed? String str; Strinf sourceStr = 'Hey diddle, diddle, the cat and the fiddle"; str = sourceStr.substring(12,17);
answer
diddl
Unlock the answer
question
Two ways of concatenating two strings are
answer
Use the concat() method or use the + between the two Strings
Unlock the answer
question
The valueOf() method accepts a value of any primitive data type as an argument and returns a string representation of the value.
answer
True
Unlock the answer
question
StringBuffer objects are not immutable.
answer
True
Unlock the answer
question
The parameterless constructor for a StringBuffer object gives the object enough storage space to hold ___
answer
16 characters
Unlock the answer
question
What would be the results of executing the following code? StringBuffer strbuff = new StringBuffer(12); strbuff.append("The cow"); strbuff.append("jumped over the"); strbuff.append("moon");
answer
strbuff would equal "The cow jumped over the moon"
Unlock the answer
question
Given the following statement, which of the following is not true? str.insert(8,32)
answer
The insert will start at position 32
Unlock the answer
question
What will be the value of strbuff after the following statements are executed? StringBuffer strbuff = new StringBuffer('We have lived in Chicago, Trenton, and Atlanta." strbuff.replace(26,33,"Tampa");
answer
We have lived in Chicago, Tampa, and Atlanta.
Unlock the answer
question
In a string that contains a series of words or other items of data separated by spaces or other characters, the programming term for the data items is
answer
Token
Unlock the answer
question
If you do not specify delimiters in the StringToken constructor, which of the following cannot be a delimiter?
answer
Semicolon
Unlock the answer
question
what will be the tokens in the following statements? StringTokenizer strToken = newStringTokenizer("January 1, 2003", ",",true);
answer
January, 1, 2004, space, comma
Unlock the answer
question
For the following code, how many times would the while loop execute? StringTokenizer strToken = new StringTokenizer('Ben and Jerry's ice cream is great.'); while (strToken.hasMoreTokens) { System.out.println(strToken.nextToken()); }
answer
7
Unlock the answer
question
If a string has more than one character used as a delimiter, we must write a loop to determine the tokens, one fore each delimiter character.
answer
False
Unlock the answer
question
What does the following statement do? Float number = new Float(8.8);
answer
It creates a Float Object; It initializes that object to 8.8; It assigns the object's address to the number variable
Unlock the answer
question
To convert the string, str = "285" to an integer and store it in the variable x, use the following statement
answer
integer x = Integer.parseInt(str);
Unlock the answer
question
To convert the double variable, d = 543.98, to a string, use the following statement,
answer
String str = Double.toString(d);
Unlock the answer
question
Which of the following Statements will print the maximum value an integer variable may have?
answer
System.out.println(Integer.MAX_VALUE);
Unlock the answer
question
You cannot assign a value to a wrapper class object.
answer
False
Unlock the answer
question
When an "is a" relationship exists between objects, it means that the specialized object has
answer
All the characteristics of the general object, plus additional chracteristics
Unlock the answer
question
Which of the following statements declares as a subclass of PayType?
answer
public class Salaried extends PayType
Unlock the answer
question
If ClassA is Derived from ClassB, then
answer
Public members in ClassB are public in ClassA, but private members in ClassB cannot be directly accessed in ClassA
Unlock the answer
question
In UML diagrams, inheritance is shown
answer
With a line that has an open arrowhead at one end that points to the superclass
Unlock the answer
question
It is not possible for a super class to call a subclass's method.
answer
True
Unlock the answer
question
If a subclass constructor does not explicitly call as superclass constructor,
answer
Java will automatically call the superclass's default constructor just before the code in the subclass's constructor executes
Unlock the answer
question
If a method in a subclass has the same signature as a method in the superclass, the subclass method overloads the superclass method.
answer
False
Unlock the answer
question
A protected member of a class may be directly accessed by
answer
Methods of the same class; Methods of a Subclass; Methods in the Same Package
Unlock the answer
question
When declaring class data members, it is best to declare them as
answer
Private Members
Unlock the answer
question
If you do not provide an access specifier for a class member, the class member is given ___ access by default
answer
Package
Unlock the answer
question
If ClassC is derived from ClassB, which is derived from ClassA, this would be an example of
answer
A chain of inheritance
Unlock the answer
question
Every class is either directly or indirectly derived from he Object class.
answer
True
Unlock the answer
question
If a class contains an abstract method
answer
You cannot create an instance of the class; The method will have only a header, but not a body, and end with a semicolon; Then Method must be overridden in subclasses
Unlock the answer
question
An abstract class is not instantiated, but serves as a superclass for other classes.
answer
True
Unlock the answer
question
Given the following statement which of the following is true? public class ClassB implements ClassA{ }
answer
ClassB must override each method in ClassA
Unlock the answer
question
All fields declared in an interface
answer
Are final and static
Unlock the answer
question
When on object is a specialized version of another object, there is a(n) ____ relationship between them.
answer
"is a"
Unlock the answer
question
A derived class can directly access
answer
Only Public and protected members of the base class
Unlock the answer
question
In the following statement which is the base class? public class ClassA extends ClassB implements ClassC
answer
ClassB
Unlock the answer
question
In UML diagrams, inheritance is shown
answer
With a line that has an open arrowhead at one end that point to the base class
Unlock the answer
question
In an inheritance relationship, the derived class constructor always executes before the base class constructor
answer
False
Unlock the answer
question
If a base class does not have a default constructor,
answer
Then a class that is derived from it, must call one of the constructors that the base class does have
Unlock the answer
question
If two methods in the same class have the same name but different signatures, the second overrides the first.
answer
False
Unlock the answer
question
Protected members are
answer
Not quite private; Not quite different
Unlock the answer
question
Protected class members are denoted in a UML diagram with the symbol
answer
#
Unlock the answer
question
The difference between protected access and package is
answer
Protected members may be accessed by methods in the same package or in a derived class, even when the derived class is in a different package
Unlock the answer
question
In a class hierachy
answer
The more general classes are toward the top of the tree and the more specialized are toward the bottom
Unlock the answer
question
Every class has a toString method and equal's method inherited from the Object class
answer
True
Unlock the answer
question
If a class contains an abstract method,
answer
The method will have only a header, but not a body, and end with a semicolon
Unlock the answer
question
All methods in an abstract class must also be declared abstract
answer
False
Unlock the answer
question
In an interface all methods have
answer
Public access
Unlock the answer
question
Which of the following statements correctly specifies three interfaces:
answer
public class ClassA implements Interface1, Interface2, Interface3
Unlock the answer
question
When an interface variable references an object you can use the interface variable to call all the methods in the class implementing the interface.
answer
False
Unlock the answer
question
What will be the value of x[1] after the following code is executed? int[] x = { 22, 33, 44 }; arrayProcess(x); ... public static void arrayProcess(int[] a) { for(int k = 0; k < 3; k++) { a[k] = a[k] + 5; } }
answer
38
Unlock the answer
question
A ragged array is
answer
A two-dimensional array when the rows are of different lengths
Unlock the answer
question
What will be the value of x[8] after the following code has been executed? final int SUB = 12; int[] x = new int[SUB]; int y = 20; for(int i = 0; i < SUB; i++) { x[i] = y; y += 5; }
answer
60
Unlock the answer
question
What would be the results of the following code? int[] array1 = new int[25]; ... // Code that will put values in array1 int value = array1[0]; for (int a = 1; a < array1.length; a++) { if (array1[a] < value) value = array1[a]; }
answer
Value contains the lowest value in array1
Unlock the answer
question
What would be the results after the following code was executed? int[] x = {23, 55, 83, 19}; int[] y = {36, 78, 12, 24}; for(int a = 0; a < x.length; a++) { x[a] = y[a]; y[a] = x[a]; }
answer
x[] = {36, 78, 12, 24} and y[] = {36, 78, 12, 24}
Unlock the answer
question
What will be the value of x[8] after the following code has been executed? final int SUB = 12; int[] x = new int[SUB]; int y = 100; for(int i = 0; i < SUB; i++) { x[i] = y; y += 10; }
answer
180
Unlock the answer
question
If numbers is a two-dimensional int array that has been initialized and total is an int that has been set to 0, which of the following will sum all the elements in the array?
answer
for (int row = 0; row < numbers.length; row++) { for (int col = 0; col < numbers[row].length; col++) total += numbers[row][col]; }
Unlock the answer
question
What would be the results after the following code was executed? int[] x = {23, 55, 83, 19}; int[] y = {36, 78, 12, 24}; x = y; y = x;
answer
x[] = {36, 78, 12, 24} and y[] = {36, 78, 12, 24}
Unlock the answer
question
What would be the results of the following code? int[] x = { 55, 33, 88, 22, 99, 11, 44, 66, 77 }; int a = 10; if(x[2] > x[5]) a = 5; else a = 8;
answer
a = 5
Unlock the answer
question
If a[] and b[] are two integer arrays, the expression a == b compares the array contents.
answer
False
Unlock the answer
question
Given the following two-dimensional array declaration, which statement is true? int [][] numbers = new int [6] [9];
answer
The array numbers has 6 rows and 9 columns
Unlock the answer
question
What does the following statement do? double[] array1 = new double[10];
answer
All of the above
Unlock the answer
question
The sequential search algorithm
answer
Uses a loop to sequentially step through an array, starting with the first element
Unlock the answer
question
Any items typed on the command-line, separated by space, after the name of the class are considered to be one or more arguments that are to be passed into the main method.
answer
True
Unlock the answer
question
Java does not limit the number of dimensions that an array may have.
answer
True
Unlock the answer
question
The binary search algorithm
answer
Will cut the portion of the array being searched in half each time the loop fails to locate the search value
Unlock the answer
question
int[] x = {22, 33, 44}; arrayProcess(x[1]); ... public static void arrayProcess(int a) { a = a + 5; }
answer
33
Unlock the answer
question
An ArrayList object automatically expands in size to accommodate the items stored in it.
answer
True
Unlock the answer
question
The following statement creates an ArrayListobject. What is the purpose of the notation? ArrayList arr = new ArrayList();
answer
**It specifies that only String objects may be stored in the ArrayList object.
Unlock the answer
question
What would be the results of the following code? final int SIZE = 25; int[] array1 = new int[SIZE]; ... // Code that will put values in array1 int value = 0; for (int a = 0; a < array1.length; a++) { value += array1[a]; }
answer
value contains the sum of all the values in array1
Unlock the answer
question
Given that String[] str has been initialized, to get a copy of str[0] with all characters converted to upper case, use the following statement
answer
str[0].toUpperCase();
Unlock the answer
question
In memory, an array of String objects
answer
Consists of an array of references to Stringobjects
Unlock the answer
question
For the following code, what would be the value of str[2]? String[] str = {"abc", "def", "ghi", "jkl"};
answer
A reference to the String "ghi"
Unlock the answer
question
What will be the result of executing the following code? int[] x = {0, 1, 2, 3, 4, 5};
answer
An array of 6 values ranging from 0-5 and referenced by the variable x will be created
Unlock the answer
question
You can use this ArrayList class method to insert an item at a specific location in an ArrayList.
answer
add
Unlock the answer
question
What will be the value of x[8] after the following code has been executed? final int SUB = 12; int[] x = new int[SUB]; int y = 100; for(int i = 0; i < SUB; i++) { x[i] = y; y += 10; }
answer
180
Unlock the answer
question
In order to do a binary search on an array,
answer
The array must first be sorted in ascending order
Unlock the answer
question
You can use this ArrayList class method to replace an item at a specific location in anArrayList.
answer
set
Unlock the answer
question
This ArrayList class method deletes an item from an ArrayList.
answer
remove
Unlock the answer
question
The String[] args parameter in the main method header allows the program to receive arguments from the operating system command-line.
answer
True
Unlock the answer