java final

String objects are immutable. This means they ________. A. must be initialized B. cannot be deleted C. cannot be changed D. None of the above

C. cannot be changed

The length of a string can be determined by ________. A. the String method length() B. the String instance variable length C. the String method strlen() D. All of the above.

A. the String method length()

The statement s1.equalsIgnoreCase(s4) is equivalent to which of the following? A. s1.regionMatches(true, 0, s4, 0, s4.length()); B. s1.regionMatches(0, s4, 0, s4.length()); C. s1.regionMatches(0, s4, 0, s4.length); D. s1.regionMatches(true, s4, 0, s4.length);

A. s1.regionMatches(true, 0, s4, 0, s4.length());

The statement s1.startsWith("art") has the same result as which of the following? A. s1.regionMatches(0, "art", 0, 3); B. s2 = s1.getChars(0, 3); s2.equals("art"); C. s1.regionMatches(true, 0, "art", 0, 3); D. All of the above

D. All of the above

5. For String c = "hello world"; The Java statements int i = c.indexOf('o'); int j = c.lastIndexOf('l'); will result in: A. i = 4 and j = 8. B. i = 5 and j = 8. C. i = 4 and j = 9. D. i = 5 and j = 9.

C. i = 4 and j = 9

Given the following declaration: StringBuilder buf = new StringBuilder(); What is the capacity of buf? A. 0 B. 10 C. 16 D. 20

C. 16

Given the following declarations: StringBuilder buffer = new StringBuilder("Testing Testing"); buffer.setLength(7); buffer.ensureCapacity(5); Which of the following is true? A. buffer has capacity 5. B. buffer has capacity 31. C. buffer has content "Testin". D. buffer has length 15.

B. buffer has capacity 31.

To find the character at a certain index position within a String, use the method ________. A. getChars, with the index as an argument B. getCharAt, with the index as an argument C. charAt, with the index as an argument D. charAt, with the character you are searching for as an argument

C. charAt, with the index as an argument

Consider the Java segment: String line1 = new String("c = 1 + 2 + 3") ; StringTokenizer tok = new StringTokenizer(line1, "+="); String foo = tok.nextToken(); String bar = tok.nextToken(); The values of foo and bar are: A. foo is "c ", bar is " = "B. foo is "c", bar is " "C. foo is " = ", bar is " + " D. foo is "c ", bar is " 1

D. foo is "c ", bar is " 1

Which of the following statements is false? A. Exception handling enables programmers to write robust and fault-tolerant programs. B. Exception handling can catch but not resolve exceptions. C. Exception handling can resolve exceptions. D. All of the above are true.

B. Exception handling can catch but not resolve exceptions

To catch an exception, the code that might throw the exception must be enclosed in a ________. A. throws block. B. catch block. C. try block. D. finally block.

C. try block.

In the catch block below, what is e?catch (ArithmeticException e) { System.err.printf(e); } a. The type of the exception being caught.b. The name of catch block's exception parameter.c. A finally block.d. An exception handler.

b. The name of catch block's exception parameter.

Which of the following statements about try blocks is true? A. The try block must be followed by at least one catch block. B. The try block must be followed by a finally block. C. The try block should contain statements that may process an exception. D. The try block should contain statements that may throw an exception.

D. The try block should contain statements that may throw an exception.

Which of the following statements is true? A. The code in a finally block is executed only if an exception occurs. B. The code in a finally block is executed only if an exception does not occur. C. The code in a finally block is executed only if there are no catch blocks. D. None of the above are true.

D. None of the above are true

Which of the following statements is true? A. The throw statement is used to throw an exception. B. The throw statement is used to specify that a method will throw an exception. C. The throw statement is used to access an exception parameter. D. All of the above.

A. The throw statement is used to throw an exception.

If the superclass contains only abstract method declarations, the superclass is used for ________. A. implementation inheritance. B. interface inheritance. C. Both. D. Neither.

B. interface inheritance.

Consider the abstract superclass below: public abstract class Foo { private int a; public int b; public Foo(int aVal, int bVal) { a = aVal; b = bVal; } public abstract int calculate(); } Any concrete subclass that extends class Foo: A. Must implement a method called calculate. B. Will not be able to access the instance variable a. C. Neither (a) nor (b). D. Both (a) and (b).

D. Both (a) and (b).

Consider classes A, B and C, where A is an abstract superclass, B is a concrete class that inherits from A and C is a concrete class that inherits from B. Class A declares abstract method originalMethod, implemented in class B. Which of the following statements is true of class C? A. Method originalMethod cannot be overridden in class C—once it has been implemented in concrete class B, it is implicitly final. B. Method originalMethod must be overridden in class C, or a compilation error will occur. C. If method originalMethod is not overridden in class C but is called by an object of class C, an error occurs. D. None of the above.

D. None of the above.

Interfaces can have ________ methods. A. 0 B. 1 C. 2 D. any number of

D. any number of

A class that implements an interface but does not declare all of the interface's methods must be declared ________. A. public. B. interface. C. abstract. D. final.

C. abstract

Which of the following statements is false? A. A subclass is often larger than its superclass. B. A superclass object is a subclass object. C. The class following the extends keyword in a class declaration is the direct superclass of the class being declared. D. Java uses interfaces to provide the benefits of multiple inheritance.

B. A superclass object is a subclass object.

Inheritance is also known as the ________________. A. knows-a relationship. B. has-a relationship. C. uses-a relationship. D. is-a relationship.

D. is-a relationship

Every class in Java, except ________, extends an existing class. A. Integer. B. Object. C. String. D. Class.

B. Object.

Overriding a method differs from overloading a method because: A. Overloaded methods have the same signature. B. Overridden methods have the same signature. C. Both of the above. D. Neither of the above

Overridden methods have the same signature.

Consider the classes below, declared in the same file: class A { int a; public A() { a = 7; } } class B extends A { int b; public B() { b = 8; } } Which of the statements below is false? A. Both variables a and b are instance variables. B. After the constructor for class B executes, the variable a will have the value 7. C. After the constructor for class B executes, the variable b will have the value 8. D. A reference of type A can be treated as a reference of type B.

D. A reference of type A can be treated as a reference of type B.

Which superclass members are inherited by all subclasses of that superclass? A. private instance variables and methods. B. protected instance variables and methods. C. private constructors. D. protected constructors.

B. protected instance variables and methods.

The static method ________ of class String returns a formatted String. A. printf. B. format. C. formatString. D. toFormatedString

B. format.

When must a program explicitly use the this reference? A. Accessing a private variable. B. Accessing a public variable. C. Accessing a local variable. D. Accessing an instance variable that is shadowed by a local variable

D. Accessing an instance variable that is shadowed by a local variable

Constructors: A. Initialize instance variables. B. When overloaded, can have identical argument lists. C. When overloaded, are selected by number, types and order of types of parameters. D. Both (a) and (c).

D. Both (a) and (c).

Set methods are also commonly called ________ methods and get methods are also commonly called ________ methods. A. query, mutator. B. accessor, mutator.C. mutator, accessor. D. query, accessor.

C. mutator, accessor.

Consider the code segment below. Which of the following statements is false? int[] g; g = new int[23]; A. The value of g[3] is -1. B. The first statement declares an array reference. C. The second statement creates the array. D. g is a reference to an array of integers.

A. The value of g[3] is -1.

Which of the following initializer lists would correctly set the elements of array n? A. int[] n = {1, 2, 3, 4, 5}; B. array n[int] = {1, 2, 3, 4, 5}; C. int n[5] = {1; 2; 3; 4; 5}; D. int n = new int(1, 2, 3, 4, 5);

A. int[] n = {1, 2, 3, 4, 5};

Which expression adds 1 to the element of array arrayName at index i? A. ++arrayName[i]B. arrayName++[i] C. arrayName[i++] D. None of the above.

A. ++arrayName[i]

Consider integer array values, which contains 5 elements. Which statements successfully swap the contents of the array at index 3 and index 4? A. values[3] = values[4]; values[4] = values[3]; B. values[4] = values[3]; values[3] = values[4]; C. int temp = values[3]; values[3] = values[4]; values[4] = temp;D. int temp = values[3]; values[3] = values[4]; values[4] = values[3];

C. int temp = values[3]; values[3] = values[4]; values[4] = temp;

Assume class Book has been declared. Which set of statements creates an array of Books? A. Book[] books; books = new Book[numberElements]; B. Book[] books]; books = new Book()[numberElements]; C. new Book() books[]; books = new Book[numberElements]; D. All of the above.

A. Book[] books; books = new Book[numberElements];

Which statement correctly passes the array items to method takeArray? Array items contains 10 elements. A. takeArray(items[]). B. takeArray(items). C. takeArray(items[9]). D. Arrays cannot be passed to methods—each item must be sent to the method separately.

B. takeArray(items)

Consider array items, which contains the values 0, 2, 4, 6 and 8. If method changeArray is called with the method call changeArray(items, items[2]), what values are stored in items after the method has finished executing? public static void changeArray(int[] passedArray, int value) {passedArray[value] = 12; value = 5; } A. 0, 2, 5, 6, 12. B. 0, 2, 12, 6, 8. C. 0, 2, 4, 6, 5. D. 0, 2, 4, 6, 12.

D. 0, 2, 4, 6, 12

Which of the following statements creates a multidimensional array with 3 rows, where the first row contains 1 element, the second row contains 4 elements and the final row contains 2 elements? A. int[][] items = {{1, null, null, null}, {2, 3, 4, 5}, {6, 7, null, null}}; B. int[][] items = {{1}, {2, 3, 4, 5}, {6, 7}}; C. int[][] items = {{1}, {2, 3, 4, 5}, {6, 7}, {}); D. int[][] items = {{1}, {4}, {2}};

B. int[][] items = {{1}, {2, 3, 4, 5}, {6, 7}};

To declare a method as static, place the keyword static before ________ in the method's declaration. A. the method modifier B. the return type C. the method name D. the argument list

B. the return type

Declaring main as static allows the JVM to invoke main ________. A. without knowing the name of the class in which main is declared. B. by creating an object of the class in which main is declared. C. without creating an instance of the class in which main is declared. D. None of the above.

C. without creating an instance of the class in which main is declared.

Consider the following Java statements: int x = 9; double y = 5.3; result = calculateValue(x, y); Which of the following statements is false? A. A method is called with its name and parentheses. B. x and y are parameters. C. Copies of x and y are passed to the method calculateValue. D. x and y are arguments.

B. x and y are parameters.

Which of the following promotions of primitive types is not allowed to occur? A. char to int. B. double to float. C. int to double. D. short to long

B. double to float

Which statement creates a random value from the sequence 2, 5, 8, 11 and 14. Suppose randomNumbers is a SecureRandom object. A. 2 + 5 * randomNumbers.nextInt(3); B. 3 + 2 * randomNumbers.nextInt(5); C. 5 + 3 * randomNumbers.nextInt(2); D. 2 + 3 * randomNumbers.nextInt(5);

D. 2 + 3 * randomNumbers.nextInt(5);

In a class containing methods with the same name, the methods are distinguished by ________. A. Number of arguments B. Types of arguments C. Return type D. (a) and (b) E. (b) and (c)

D. (a) and (b)

Which of the following will count down from 10 to 1 correctly? A. for (int j = 10; j <= 1; j++) B. for (int j = 1; j <= 10; j++) C. for (int j = 10; j > 1; j--) D. for (int j = 10; j >= 1; j--)

D. for (int j = 10; j >= 1; j--)

Which statement prints the floating-point value 123.456 right justified with a field width of 10? A. System.out.printf("%d10.3", 123.456);B. System.out.printf("%10.3d", 123.456); C. System.out.printf("%f10.3", 123.456); D. System.out.printf("%10.3f", 123.456);

D. System.out.printf("%10.3f", 123.456);

Which formatting flag indicates that the floating-point values should be output with a thousands separator? A. plus (+). B. minus (-). C. comma (,). D. period (.).

C. comma (,)

What does the following code segment print? public class MyClass { public static void main (String [] args) { for (int i=2; i <4; i++) { for (int j=4; j>1; j--) { System.out.print(i+j + ","); } } } } A. 6,5,4,7,6,5, B. 6,5,4,6,5,4, C. 2,4,2,4,6,5, D. 6,5,4,3,6,5,

A. 6,5,4,7,6,5,

Which of the following statements about a do...while iteration statement is true? A. The body of a do...while loop is executed only if the terminating condition is true. B. The body of a do...while loop is executed only once. C. The body of a do...while loop is always executed at least once. D. None of the above.

The body of a do...while loop is always executed at least once.

Consider the code segment below. if (gender == 1) { if (age >= 65) { ++seniorFemales; } } This segment is equivalent to which of the following? A. if (gender == 1 || age >= 65) { ++seniorFemales; } B. if (gender == 1 && age >= 65) { ++seniorFemales; } C. if (gender == 1 AND age >= 65) { ++seniorFemales; } D. if (gender == 1 OR age >= 65) { ++seniorFemales; }

B. if (gender == 1 && age >= 65) { ++seniorFemales; }

What does the following code segment print? public class Test { public static void main (String [] args) { int width=45; if(width>90) { System.out.println("not wider"); } else { switch(width/10) { case 5: System.out.println("it is okay"); break; case 4: System.out.println("it is not okay"); break; default: System.out.println("perfect"); } } } } A. it is okay B. it is not okay C. perfect D. not wider

B. it is not okay

What is output by the following Java code segment? int temp = 60; if (temp > 90) { System.out.println("This room is too hot."); // cool down temp = temp - (temp > 150 ? 100 : 20); } else { if (temp < 70) { System.out.println("This room is too cold."); // warm up temp = temp + (temp < 50 ? 30: 20); } } if (temp == 80) { System.out.println("This room is just right!"); } A. This room is too hot. B. This room is too cold. This room is just right! C. This room is just right! D. None of the above.

D. None of the above.

Which of the following code segments does not increment val by 3: A. val += 3; B. val = val + 1; val = val + 1; val = val + 1; C. c = 3; val = val + (c == 3 ? 2 : 3); D. All of the above increment val by 3.

C. c = 3; val = val + (c == 3 ? 2 : 3);

Which primitive type can hold the largest value? A. int B. long C. float D. double

D. double

Declaring instance variables ________ is known as data hiding or information hiding. A. secure B. private C. static D. masked

B. private

Types in Java are divided into two categories. The primitive types are boolean, byte, char, short, int, long, float and double. All other types are ________ types. A. static B. reference C. declared D. source

B. reference

Which of the following is a Scanner method for inputting an integer value? A. nextInteger B. integer C. nextInt D. int

C. nextInt