Java Ch 8 – 11 – Flashcards

Unlock all answers in this set

Unlock answers
question
When an Object is passed to a method, the method may change the values in the object
answer
True
question
You cannot use the fully-qualified name of an enum constant for this
answer
A case experssion
question
Look at the Following declaration: enum Tree { OAK, MAPLE, PINE } What is the ordinal value of the MAPLE enum constant?
answer
1
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
question
A class's static methods do not operate on the fields that belong to any instance of the class
answer
True
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();
question
The JVM periodically performs this process to remove unreferenced objects from memory.
answer
garbage collection
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
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
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
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
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
question
Enumerated types have this method, which returns the position of an enum constant in the declaration list.
answer
ordinal
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;
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
question
The only limitation that static methods have is
answer
They cannot refer to non-static members of the class
question
When the this variable is used to call a constructor,
answer
. It must be the first statement in the constructor making the call
question
You can declare an enumerated data type inside of a method.
answer
False
question
The term for the relationship created by object aggregation is
answer
Has a
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
question
You cannot use the fully-qualifed name of an enum constant for this
answer
a case expression
question
To compare two objects
answer
Write an equals method that will make a field by field comparison of the two object
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
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
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
question
Which of the following is not true about static methods?
answer
They are called from an instance of the class.
question
Few programmers use wrapper classes because
answer
They are immutable, They are not easy to use
question
The Character wrapper provides numerous methods for
answer
Testing and converting char variables
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
question
If String str = "RSTUVWXYZ", what will be the value returned from str.charAt(5)?
answer
W
question
If a non-letter is passed to toLowerCase or toUpperCase method, it is returned unchanged
answer
True
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
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
question
In the following statement, what data type must recField be: str.getChars(5, 10, recField, 0);
answer
char[ ]
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
question
The valueOf() method accepts a string representation as an argument and returns its equivalent integer value.
answer
False
question
StringBuffer objects are immutable.
answer
False
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
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"
question
When using StringBuffer insert method, you cannot insert
answer
Another StringBuffer type
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.
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
question
To use the class StringTokenizer, you must have the following import statement.
answer
import java.util.StringTokenizer;
question
What will be the tokens in the following statement? StringTokenizer = strToken new StringTokenizer("123-456-7890", "-",true);
answer
123, 456 7890, and -
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
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
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
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);
question
To convert the integer vaiable, number = 56, to a string, use the following statement
answer
String str = Integer.toString(number);
question
which of the following statements will print the maximum value a double variable may have?
answer
System.out println(Double.MAX_VALUE);
question
You must call a method to get the value of a wrapper class object
answer
False
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
question
Use the following import statement when using the character wrapper class
answer
No import statement is needed
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
question
If String str = "ABCDEFGHI," what will be returned from Character.toLowerCase(str.charAt(5))?
answer
f
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
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
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
question
Two ways of concatenating two strings are
answer
Use the concat() method or use the + between the two Strings
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
question
StringBuffer objects are not immutable.
answer
True
question
The parameterless constructor for a StringBuffer object gives the object enough storage space to hold ___
answer
16 characters
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"
question
Given the following statement, which of the following is not true? str.insert(8,32)
answer
The insert will start at position 32
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.
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
question
If you do not specify delimiters in the StringToken constructor, which of the following cannot be a delimiter?
answer
Semicolon
question
what will be the tokens in the following statements? StringTokenizer strToken = newStringTokenizer("January 1, 2003", ",",true);
answer
January, 1, 2004, space, comma
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
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
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
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);
question
To convert the double variable, d = 543.98, to a string, use the following statement,
answer
String str = Double.toString(d);
question
Which of the following Statements will print the maximum value an integer variable may have?
answer
System.out.println(Integer.MAX_VALUE);
question
You cannot assign a value to a wrapper class object.
answer
False
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
question
Which of the following statements declares as a subclass of PayType?
answer
public class Salaried extends PayType
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
question
In UML diagrams, inheritance is shown
answer
With a line that has an open arrowhead at one end that points to the superclass
question
It is not possible for a super class to call a subclass's method.
answer
True
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
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
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
question
When declaring class data members, it is best to declare them as
answer
Private Members
question
If you do not provide an access specifier for a class member, the class member is given ___ access by default
answer
Package
question
If ClassC is derived from ClassB, which is derived from ClassA, this would be an example of
answer
A chain of inheritance
question
Every class is either directly or indirectly derived from he Object class.
answer
True
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
question
An abstract class is not instantiated, but serves as a superclass for other classes.
answer
True
question
Given the following statement which of the following is true? public class ClassB implements ClassA{ }
answer
ClassB must override each method in ClassA
question
All fields declared in an interface
answer
Are final and static
question
When on object is a specialized version of another object, there is a(n) ____ relationship between them.
answer
"is a"
question
A derived class can directly access
answer
Only Public and protected members of the base class
question
In the following statement which is the base class? public class ClassA extends ClassB implements ClassC
answer
ClassB
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
question
In an inheritance relationship, the derived class constructor always executes before the base class constructor
answer
False
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
question
If two methods in the same class have the same name but different signatures, the second overrides the first.
answer
False
question
Protected members are
answer
Not quite private; Not quite different
question
Protected class members are denoted in a UML diagram with the symbol
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
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
question
Every class has a toString method and equal's method inherited from the Object class
answer
True
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
question
All methods in an abstract class must also be declared abstract
answer
False
question
In an interface all methods have
answer
Public access
question
Which of the following statements correctly specifies three interfaces:
answer
public class ClassA implements Interface1, Interface2, Interface3
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
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
question
A ragged array is
answer
A two-dimensional array when the rows are of different lengths
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
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
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}
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
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]; }
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}
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
question
If a[] and b[] are two integer arrays, the expression a == b compares the array contents.
answer
False
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
question
What does the following statement do? double[] array1 = new double[10];
answer
All of the above
question
The sequential search algorithm
answer
Uses a loop to sequentially step through an array, starting with the first element
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
question
Java does not limit the number of dimensions that an array may have.
answer
True
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
question
int[] x = {22, 33, 44}; arrayProcess(x[1]); ... public static void arrayProcess(int a) { a = a + 5; }
answer
33
question
An ArrayList object automatically expands in size to accommodate the items stored in it.
answer
True
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.
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
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();
question
In memory, an array of String objects
answer
Consists of an array of references to Stringobjects
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"
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
question
You can use this ArrayList class method to insert an item at a specific location in an ArrayList.
answer
add
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
question
In order to do a binary search on an array,
answer
The array must first be sorted in ascending order
question
You can use this ArrayList class method to replace an item at a specific location in anArrayList.
answer
set
question
This ArrayList class method deletes an item from an ArrayList.
answer
remove
question
The String[] args parameter in the main method header allows the program to receive arguments from the operating system command-line.
answer
True
Get an explanation on any task
Get unstuck with the help of our AI assistant in seconds
New