CRC CISP 402 Java Quiz 1

Ace your homework & exams now with Quizwiz!

Which of the following statements is true? 1. A finally block is always executed. 2. A finally block is only executed if an exception is thrown. 3. A finally block is not executed if the try block exits via a break, continue, or return statement. 4. A finally block only executes if none of the catch statements respond to the exception that was thrown in the try block.

1. A finally block is always executed.

The code in a finally block: 1. Is always executed if the corresponding try block is entered. 2. Is executed only if an exception occurs. 3. Is executed only if an exception does not occur. 4. Is executed only if there are no catch blocks.

1. Is always executed if the corresponding try block is entered.

Which of the following statements regarding the throw point of an exception is false? 1. It specifies the point at which the exception must be handled. 2. It is the initial point at which the exception occurs. 3. It is specified as the top row of the method-call stack at the time the exception occurred. 4. All of the above statements are true.

1. It specifies the point at which the exception must be handled.

Consider the Java segment: String line1 = new String( "c = 1 + 2 + 3" ) ; StringTokenizer tok = new StringTokenizer( line1, delimArg ); For the String line1 to have 4 tokens, delimArg should be: 1. String delimArg = "+="; 2. String delimArg = "123" 3. String delimArg = "c+"; 4. String delimArg = " ";

1. String delimArg = "+=";

The length of a string can be determined by: 1. The String method length(). 2. The String instance variable length. 3. The String method strlen(). 4. All of the above.

1. The String method length().

Which of the following statements is true? 1. The throw statement is used to throw an exception. 2. The throw statement is used to specify that a method will throw an exception. 3. The throw statement is used to access an exception parameter. 4. All of the above.

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

In Java, after an exception is handled, control resumes ______________. This is known as the ___________________ model of exception handling. 1. after the last catch block (or the finally block, if there is one), termination 2. after the last catch block (or the finally block, if there is one), resumption 3. just after the throw point, termination 4. just after the throw point, resumption

1. after the last catch block (or the finally block, if there is one), termination

After a finally block has finished executing (and there are no exceptions to be handled): 1. control proceeds to the first statement after the finally block. 2. control returns to the throw point. 3. the application exits. 4. control proceeds to the first statement after the last catch block.

1. control proceeds to the first statement after the finally block.

String c = "Hello. She sells sea shells"; The Java statements int i = c.indexOf( "ll" ); int j = c.lastIndexOf( "ll" ); will result in: 1. i = 2 and j = 24. 2. i = 3 and j = 24. 3. i = 2 and j = 25. 4. i = 3 and j = 23.

1. i = 2 and j = 24.

The statement s1.equalsIgnoreCase( s4 ) is equivalent to which of the following? 1. s1.regionMatches( true, 0, s4, 0, s4.length() ); 2. s1.regionMatches( 0, s4, 0, s4.length() ); 3. s1.regionMatches( 0, s4, 0, s4.length ); 4. s1.regionMatches( true, s4, 0, s4.length );

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

Which of the Java strings represent the regular expression ,\s*? 1. "\,\s\*". 2. ",\\s*". 3. ",\\s\\*". 4. ".\\s\*".

2. ",\\s*".

The String method substring returns: 1. A char. 2. A String. 3. void. 4. A char[].

2. A String

Which of the following are static Character methods? 1. Character.hashcode( char c ); 2. Character.isDigit( char c ); 3. Character.equals( char c ); 4. All of the above.

2. Character.isDigit( char c );

Which of the following statements is false? 1. Exception handling enables programmers to write robust and fault-tolerant programs. 2. Exception handling can catch but not resolve exceptions. 3. Exception handling can resolve exceptions. 4. All of the above are true.

2. Exception handling can catch but not resolve exceptions.

Which of the following exceptions is a checked exception? 1. ArithmeticException. 2. IOException. 3. RuntimeException. 4. InputMismatchException.

2. IOException.

Resources allocated in a try block: 1. Should be freed in the try block. 2. Should be freed in the associated finally block. 3. Should be freed in the next associated catch block. 4. Should be freed at the end of the method containing the try block.

2. Should be freed in the associated finally block.

Which of the following statements is false? 1. All exceptions must derive from the class Throwable. 2. The class Throwable provides the method getStackTrace that outputs the stack trace to the standard error stream. 3. The class Throwable provides the method getMessage that returns the descriptive string stored in an exception. 4. The string returned from class Throwable's getMessage method contains the name of the exception's class.

2. The class Throwable provides the method getStackTrace that outputs the stack trace to the standard error stream.

In the catch block below, what is arithmeticException? catch ( ArithmeticException arithmeticException ) { System.err.printf( arithmeticException ); } // end catch 1. The type of the exception being caught. 2. The name of catch block's exception parameter. 3. A finally block. 4. An exception handler.

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

Consider the statements below: StringBuilder sb = new StringBuilder( "a toyota" ); sb.insert( 2, "landrover" ); sb.delete( 11, 16 ); sb.insert( 11, " " ); The StringBuilder contents at the end of this segment will be: 1. a landrovertoyota. 2. a landrover a. 3. a landrov a. 4. a landrover toy a.

2. a landrover a

Given the following declarations: StringBuilder buf; StringBuilder buf2 = new StringBuilder(); String c = new String( "test" ); Which of the following is not a valid StringBuilder constructor? 1. buf = new StringBuilder(); 2. buf = new StringBuilder( buf2, 32 ); 3. buf = new StringBuilder( 32 ); 4. buf = new StringBuilder( c );

2. buf = new StringBuilder( buf2, 32 );

Given the following declarations: StringBuilder buffer = new StringBuilder( "Testing Testing" ); buffer.setLength( 7 ); buffer.ensureCapacity( 5 ); Which of the following is true? 1. buffer has capacity 5. 2. buffer has capacity 31. 3. buffer has content "Testin". 4. buffer has length 15.

2. buffer has capacity 31.

A String constructor cannot be passed variables of type: 1. char arrays. 2. int arrays. 3. byte arrays. 4. Strings.

2. int arrays.

An anonymous String: 1. has no value. 2. is a string literal. 3. can be changed. 4. none of the above.

2. is a string literal.

Consider the statement below: StringBuilder sb1 = new StringBuilder( "a toyota" ); Which of the following creates a String object with the value "toy"? 1. String res = sb1.subString( 2, 5 ); char dest[] = new char[ sb1.length() ]; 2. sb1.getChars( 2, 5, dest, 0 ); String res = new String( dest ); char dest[] = new char[ sb1.length ]; 3. dest = sb1.getChars( 2, 5 ); String res = new String( dest ); char dest[] = new char[ sb1.length() ]; 4. dest = sb1.getChars( 0, 3 ); String res = new String( dest );

2. sb1.getChars( 2, 5, dest, 0 ); String res = new String( dest ); char dest[] = new char[ sb1.length ];

If the catch-or-declare requirement for a checked exception is not satisfied: 1. the compiler will issue an error message indicating that the exception must be caught. 2. the compiler will issue an error message indicating that the exception must be caught or declared. 3. a stack trace will be displayed indicating the exception that has occurred and where it occurred. 4. a stack trace will be displayed, along with a message indicating that the exception must be caught.

2. the compiler will issue an error message indicating that the exception must be caught or declared.

When an exception occurs it is said to have been: 1. caught. 2. thrown. 3. declared. 4. handled.

2. thrown.

Which of the following statements is true after a catch block is entered to handle an exception? A.A catch block can rethrow an exception that it has not handled B.A catch block can rethrow an exception that it has partially handled. C.A rethrown exception is detected by the next enclosing try block. D.A rethrown exception may be handled by an exception handler list after that enclosing try block. E.A catch block cannot rethrow an exception that is has completely handled. 1 .All of the above. 2 .B, C, D, E. 3 .A, B, C, D. 4. A, B, D, E.

3 .A, B, C, D.

Which of the following statements is true? 1. Ranges of characters can be represented by placing a ~ between two characters. 2. [^Z] is the same as [A~Y]. 3. Both "A*" and "A+" will match "AAA", but only "A*" will match an empty string. 4. All of above.

3. Both "A*" and "A+" will match "AAA", but only "A*" will match an empty string.

Which of the following is true? 1. A precondition must be true when a method is invoked. 2. A postcondition must be true when a method successfully returns to its caller. 3. Both of the above. 4. Neither of the above.

3. Both of the above.

String objects are immutable. This means they: 1. Must be initialized. 2. Cannot be deleted. 3. Cannot be changed. 4. None of the above

3. Cannot be changed.

Which of the following exceptions is a checked exception? 1. ArithmeticException. 2. RuntimeException. 3. IOException. 4. NumberFormatException.

3. IOException.

Which of the following statements is true? 1. Using existing exceptions makes the program less robust. 2. Always create your own exception classes. 3. Like any other class, an exception class can contain fields and methods. 3. The new exception class should extend RuntimeException if the program should be required to handle the exception.

3. Like any other class, an exception class can contain fields and methods.

Consider the String below: String r = "a toyota"; Which of the following will create the String r1 = "a TOYOTa"? 1. String r1 = r.replace( "toyot", TOYOT" ); String r1 = r.replace( 't','T' ); 2. r1 = r.replace( 'o','0' ); r1 = r.replace( 'y','Y' ); 3. String r1 = r.replace( 't','T' ).replace( 'o', '0').replace( 'y', 'Y' ); 4. d. String r1 = r.substring( 2, 4 ).toUpperCase();

3. String r1 = r.replace( 't','T' ).replace( 'o', '0').replace( 'y', 'Y' );

Which of the following statements is false? 1. A finally block is placed after the last catch block. 2. A finally block typically releases resources acquired in the corresponding try block. 3. The finally block and try block can appear in any order. 4. A finally block is optional.

3. The finally block and try block can appear in any order.

Which of the following statements is true? 1. The capacity of a StringBuilder is equal to its length. 2. The capacity of a StringBuilder cannot exceed its length. 3. The length of a StringBuilder cannot exceed its capacity. 4. Both a and b are true.

3. The length of a StringBuilder cannot exceed its capacity.

What is the difference between a try block and a try statement? 1. There is no difference; the terms can be used interchangeably. 2. A try statement refers to the block of code following the keyword try, while the try block refers to the try keyword and the block of code following this keyword. 3. The try block refers to the keyword try followed by a block of code. The try block and its corresponding catch and/or finally clauses together form a try statement. 4. The try statement refers to the keyword try followed by a block of code. The try statement and its corresponding catch and/or finally clauses together form a try block.

3. The try block refers to the keyword try followed by a block of code. The try block and its corresponding catch and/or finally clauses together form a try statement.

What is the difference between a try block and a try statement? 1. There is no difference; the terms can be used interchangeably. 2. A try statement refers to the block of code following the keyword try, while the try block refers to the try keyword and the block of code following this keyword. 3. The try block refers to the keyword try followed by a block of code. The try block and its corresponding catch and/or finally clauses together form a try statement. 4. The try statement refers to the keyword try followed by a block of code. The try statement and its corresponding catch and/or finally clauses together form a try block.

3. The try block refers to the keyword try followed by a block of code. The try block and its corresponding catch and/or finally clauses together form a try statement.

Which of the following statements about try blocks is true? 1. The try block must be followed by at least one catch block. 2. The try block must be followed by a finally block. 3. The try block should contain statements that may throw an exception. 4. The try block should contain statements to process an exception.

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

Which of the following statements about the try-with-resources statement is false? 1. The try-with-resources statement simplifies writing code in which you obtain a resource, use it in a try block and release the resource in a corresponding finally block. 2. You allocate the resource in the parentheses following the try keyword and use the resource in the try block; then the statement implicitly calls the resource's close method at the end of the try block. 3. You allocate the resource in the parentheses following the try keyword and use the resource in the try block; then you explicitly call the resource's close method at the end of the try block. 4. Each resource must be an object of a class that implements the AutoCloseable interface—such a class has a close method.

3. You allocate the resource in the parentheses following the try keyword and use the resource in the try block; then you explicitly call the resource's close method at the end of the try block.

Chained exceptions are useful for finding out about: 1. exceptions thrown using the chained keyword. 2. checked exceptions only. 3. an original exception that was caught before the current exception was thrown. 4. the current exception's chain of superclasses.

3. an original exception that was caught before the current exception was thrown.

Java SE 7's multi-catch enables you to catch multiple exception types in a single catch handler and perform the same task for each type of exception. The syntax for a multi-catch is: 1. catch ( Type1 or Type2 or Type3 e ) 2. catch ( Type1 e1 | Type2 e2 | Type3 e3 ) 3. catch ( Type1 | Type2 | Type3 e ) 4. catch ( Type1 e1 or Type2 e2 or Type3 e )

3. catch ( Type1 | Type2 | Type3 e )

To find the character at a certain index position within a String, use the method: 1. getChars, with the index as an argument. 2. getCharAt, with the index as an argument. 3. charAt, with the index as an argument. 4. charAt, with the character you are searching for as an argument.

3. charAt, with the index as an argument.

All exception classes inherit, either directly or indirectly, from: 1. class Error. 2. class RuntimeException. 3. class Throwable. 4. None of the above.

3. class Throwable.

Which of the following creates the string of the numbers from 1 to 1000 most efficiently? 1. String s; for ( int i = 1; i <= 1000; i++ ) s += i; StringBuilder sb = new StringBuilder( 10 ); 2. for ( int i = 1; i <= 1000; i++ ) sb.append( i ); String s = new String( sb ); StringBuilder sb = new StringBuilder( 3000 ); 3. for ( int i = 1; i <= 1000; i++ ) sb.append( i ); String s = new String( sb ); 4. All are equivalently efficient.

3. for ( int i = 1; i <= 1000; i++ ) sb.append( i ); String s = new String( sb );

For String c = "Now is the time for all"; The Java statements String i = c.substring( 7 ); String j = c.substring( 4, 15 ); will result in: 1. i = "he time for all" and j = "is the time". 2. i = "the time for all" and j = "s the time". 3. i = "the time for all" and j = "is the time ". 4. i = "he time for all" and j = "s the time".

3. i = "the time for all" and j = "is the time ".

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

3. i = 4 and j = 9.

An uncaught exception: 1. is a possible exception that never actually occurs during the execution of the program. 2. is an exception that occurs for which the matching catch clause is empty. 3. is an exception that occurs for which there are no matching catch clauses. 4. is another term for a thrown exception.

3. is an exception that occurs for which there are no matching catch clauses.

The throws clause of a method: 1. specifies the exceptions a method catches. 2. specifies the exceptions thrown by the calling method. 3. specifies the exceptions a method throws. 4. specifies the exceptions a method throws and catches.

3. specifies the exceptions a method throws.

Which of the following is not a method of class String? 1. toUpperCase. 2. trim. 3. toCharacterArray. 4. All of the above are methods of class String.

3. toCharacterArray.

To catch an exception, the code that might throw the exception must be enclosed in a: 1. throws block. 2. catch block. 3. try block. 4. finally block.

3. try block.

Which of the following is not a word character? 1. w 2. 0 3. _ 4. &

4. &

Which of the following statements is true? 1. Class Matcher provides methods find, lookingAt, replaceFirst and replaceAll. 2. Method matches (from class String, Pattern or Matcher) will return true only if the entire search object matches the regular expression. 3. Methods find and lookingAt (from class Matcher) will return true if a portion of the search object matches the regular expression. 4. All of above.

4. All of above.

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

4. All of the above

Exceptions can be thrown by: 1. the Java Virtual Machine. 2. code in a try block. 3. calls from a try block to other methods. 4. All of the above.

4. All of the above.

StringBuilder objects can be used in place of String objects if: 1. The string data is not constant. 2. The string data size may grow. 3. The programs frequently perform string concatenation. 4. All of the above.

4. All of the above.

When an unchecked exception occurs in a method but is not caught: 1. the method-call stack is "unwound." 2. the method terminates. 3. all local variables in that method go out of scope. 4. All of the above.

4. All of the above.

Which of the following errors is synchronous? 1. Divide by zero. 2. Arithmetic overflow. 3. Unsuccessful memory allocation. 4. All of the above.

4. All of the above.

Consider the examples below: A. a string. B. 'a string'. C. "a string". D. "1234". E. integer. Which could be the value of a Java variable of type String? 1. A and B. 2. B and E. 3. B and C. 4. C and D.

4. C and D.

Which of the following is not included in an exception's stack trace? 1. A descriptive message for the exception. 2. The method-call stack at the time the exception occurred. 3. The name of the exception. 4. Instructions on handling the exception.

4. Instructions on handling the exception.

Which of the following statements is true? 1. The code in a finally block is executed only if an exception occurs. 2. The code in a finally block is executed only if an exception does not occur. 3. The code in a finally block is executed only if there are no catch blocks. 4. None of the above are true.

4. None of the above are true.

Which of the following statements is not true? 1. J2SE 1.4 introduces chained exception. 2. J2SE 1.4 adds a new constructor to Exception: Exception ( String message, Throwable cause ). 3. J2SE 1.4 adds a new method getStackTrace to Throwable. 4. None of the above is true.

4. None of the above is true.

Which of the following are types of assertions? 1. Preconditions. 2. Postconditions. 3. Conditions in control statements. 4. Preconditions, Postconditions, and Conditions in control statements. 4. Preconditions and Postconditions.

4. Preconditions and Postconditions.

Which of the following statements is false? 1. A finally clause is placed after the last catch clause. 2. A finally clause should release all resources acquired in the corresponding try block. 3. A finally clause is optional. 4. The finally clause and try block can appear in any order.

4. The finally clause and try block can appear in any order.

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

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

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: 1. foo is "c ", bar is " = ". 2. foo is "c", bar is " ". 3. foo is " = ", bar is " + ". 4. fvvoo is "c ", bar is " 1 ".

4. fvvoo is "c ", bar is " 1 ".

Consider the Java segment: String line1 = new String( "c = 1 + 2 + 3" ) ; StringTokenizer tok = new StringTokenizer( line1 ); int count = tok.countTokens(); The value of count is: A. 8. B. 7. C. 13. D. 4.

B. 7.

Which of the following will create a String different from the other three? A. String r = "123456" int i = 123; B. int j = 456; String r = String.valueOf(j) + String.valueOf(i); int i = 123; C. int j = 456; String r = String.valueOf(i) + String.valueOf(j); int i = 123; D. int j = 456; String r = i + j;

B. int j = 456; String r = String.valueOf(j) + String.valueOf(i); int i = 123;

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

C. 16

How many String objects are instantiated by the following code segment (not including the literals)? String s1, output; s1 = "hello"; output = "\nThe string reversed is: " ; for ( int i = s1.length() - 1; i >= 0; i-- ) output += s1.charAt( i ) + " " ; A. 1. B. 4. C. 5. D. 7.

D. 7.


Related study sets

Global regents questions Test 1 casey

View Set

Legal term- chap 40- the law of bankruptcy

View Set

Verbs: Level 2: Distinguishing Action and Linking Verbs

View Set

Chapter 43: loss, grief, and dying

View Set