Chapter 5

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

With the enhanced for, the ____ ____contains the value of the element for the given iteration.

With an enhanced for,the block variable contains the value of the element for the given iteration.

❑With an enhanced for,the ____ is the block variable, whose ____ is compatible with the ____ of the array or collection

With an enhanced for,the declaration is the block variable, whose type is compatible with the elements of the array or collection,

With break and continue, the ____ loop will be exited by default (i.e., when the statement is unnamed)

With break and continue, the innermost loop will be exited by default (i.e., when the statement is unnamed)

❑What happens to uncaught exceptions? What's the worse that could happen and how does it happen?

Uncaught exceptions propagate back through the call stack, starting from the method where the exception is thrown and ending with either the first method that has a corresponding catch for that exception type or a JVM shutdown (which happens if the exception gets to main(), and main()is "ducking" the exception by declaring it).

What happens to the finally clause if an exception is thrown at runtime?

-If there was an exception thrown, the finally block executes immediately after the proper catch block completes.

Using a try statement by itself will result in ....

A try clause by itself will result in a COMPILER error.

❑An enhanced for statement (new as of Java 6), has ____ parts

An enhanced for statement (new as of Java 6), has two parts, the declaration and the expression. It is used only to loop through arrays or collections.

❑An ____ ____ statement will cause the current iteration of the ____-____ looping construct to stop and the line of code ____ the loop to run.

An unlabeled break statement will cause the current iteration of the inner-most looping construct to stop and the line of code following the loop to run.

What does the "finalize" clause do? Where is it used? ____ is an exception that is thrown when attempting to access an array with an ____ index value (either ____ or ____ the length of the array). It is thrown by the ____, which means that it produces a ____ error

ArrayIndexOutOFBoundsException is an exception that is thrown when attempting to access an array with an invalid index value (either negative or beyond the length of the array). It is thrown by the JVM which means that it produces a RUNTIME error.

____ is an exception that is thrown when a statement's ____ test returns ____. It is thrown by the ____, which means that it produces a ____ error

AssertionError is an exception that is thrown when a statement's boolean test returns false. It is thrown by the programmer which means that it produces a COMPILE-TIME error.

❑Checked exceptions are ____ subject to the handle or declare rule;

Checked exceptions are always subject to the handle or declare rule;

❑____ exceptions include all subtypes of Exception, excluding classes that extend RuntimeException.

Checked exceptions include all subtypes of Exception, excluding classes that extend RuntimeException.

____ is an exception that is thrown when attempting to cast a ____ ____ to a type that fails the ____-____ test.

ClassCastException is an exception that is thrown when attempting to cast a reference variable to a type that fails the IS-A test. It is thrown by the JVM which means that it produces a RUNTIME error

Combining enabling and disabling switches would ....

Combining enabling and disabling switches would selectively enable/disable assertions for certain classes and/or packages

What is the format of a do loop?

Do { System.out.println("at least one loop"); } while (false); // remember the semicolons

____ is an exception that is thrown when attempting to initilize a ____ variable or ____ block. It is thrown by the ____, which means that it produces a ____ error.

ExceptionInInitializerError is an exception that is thrown when attempting to initilize a static variable or initialization block. It is thrown by the JVM which means that it produces a RUN-TIME error. REMEMBER THAT IT ONLY WORKS ON STATIC INIT BLOCKS Example: 1. package demo; 2. public class Demo { 3. 4. 5. static Demo showError=null; 6. 7. static{ 8. showError.go(); 9. } 10. 11. 12. public void go(){ 13. System.out.println("Hi"); 14. } 15. 16. public static void main(String[] args) { 17. 18. } 19. 20. } public class Demo { static{ int i=1/0; } public static void main(String[] args) { } }

TRUE or FALSE:A try statement is independent of the catch and finally statements and can be used by itself

False. It is illegal to use a try clause without either a catch clause or a finally clause. Any catch clauses must immediately follow the try block. The following ILLEGAL code demonstrates a try without a catch or finally: try { // do stuff } // need a catch or finally here System.out.println("out of try block");

TRUE or FALSE: Each method that declares that it may throws an exceptions, throws an exception

False. Not every method that says that it may throw an exception will throw one

What happens when the default is entered?

If no case matches, the default block will be entered, and if the default does not contain a break, then code will continue to execute (fall-through) to the end of the switch or until the break statement is encountered.

If the condition in a switch statement matches a case constant, execution will....

If the condition in a switch statement matches a case constant, execution will run through all code in the switch following the matching case statement until a break statement or the end of the switch statement is encountered.

❑If you enable or disable assertions using the flag without any arguments, you're enabling or disabling assertions in general.

If you enable or disable assertions using the flag without any arguments, you're enabling or disabling assertions in general.

____ is an exception that is thrown when a method receives an ____ formatted ____ than the method ____. It is thrown by the ____, which means that it produces a ____ error.

IllegalArgumentException is an exception that is thrown when a method receives an argument formatted differently than the method expects. It is thrown by the PROGRAMMER which means that it produces a COMPILE-TIME error.

____ is an exception that is thrown when the state of the environment doesn't match the ____ being attempted (for example, using a scanner that has been ____). It is thrown by the ____, which means that it produces a ____ error.

IllegalStateException is an exception that is thrown when the state of the environment doesn't match the operation being attempted (for example, using a scanner that has been closed). It is thrown by the PROGRAMMER which means that it produces a COMPILE-TIME error. Examples in Chapter 6

The two parts of the enhanced for statement are: the ____ and the ____. It is used only to loop through ____ or ____.

Instead of having three components, the enhanced for has two. Let's loop through an array the basic (old) way, and then using the enhanced for: int [] a = {1,2,3,4}; for(int x = 0; x < a.length; x++) // basic for loop System.out.print(a[x]); for(int n : a) // enhanced for loop - int n loops thru an array of ints named a System.out.print(n)

Do case variables receive default values?

No. Case variables must be explicitly given a value in order to be initialized correctly, otherwise you get a compiler error.

NoClassDefFoundError is an exception that is thrown when the JVM can't find a class it needs, because of a ____-____ error, a ____ issue, or a ____ .class file. It is thrown by the ____, which means that it produces a ____ error.

NoClassDefFoundError is an exception that is thrown when the JVM can't find a class it needs, because of a command-line error, a classpath issue, or a missing .class file. It is thrown by the JVM which means that it produces a RUNTIME error.

____ is an exception that is thrown when attempting to access an ____ with a reference variable whose current value is ____. It is thrown by the ____ which means that it produces a ____ error.

NullPointerException is an exception that is thrown when attempting to access an object with a reference variable whose current value is null. It is thrown by the JVM which means that it produces a RUNTIME error.

____ is an exception that is thrown when a method that converts a ____ to a number receivs a ____ that it cannot convert. It is thrown by the ____, which means that it produces a ____ error.

NumberFormat Exception is an exception that is thrown when a method that converts a String to a number receivs a Sthring that it cannot convert. It is thrown by the programmer which means that it produces a COMPILE-TIME error.

StackOverflowError is an exception that is thrown when a method ____ too deeply (Each invocation is added to the stack). It is thrown by the ____, which means that it produces a ____ error.

StackOverflowError is an exception that is thrown when a method recurses too deeply (Each invocation is added to the stack). It is thrown by the JVM which means that it produces a RUNTIME error.

Switch statements CANNOT EVALUATE to ____, ____, or ____ primitive data types.

Switch statements CANNOT EVALUATE to long, float or double

TRUE or FALSE: You can't have code between try and catch blocks

TRUE. The following ILLEGAL code demonstrates a misplaced catchblock: try { // do stuff } // can't have code between try/catch System.out.println("out of try block"); catch(Exception ex) { }

❑The case constant must be a ____ or ____ variable, or a ____ expression, including an enum.

The case constant must be a literal or final variable, or a constant expression, including an enum.

If the catch blocks are not ordered according to the rules......

The compiler will stop you from defining catch clauses that can never be reached.

What is the handle or declare rule?

The handle or declare rule: any method that might throw a checked exception (including methods that invoke methods that can throw a checked exception) must either declare the exception using throws, or handle the exception with an appropriate try/catch.

The only legal expression in an if statement is a ____

The only legal expression in an if statement is a Boolean expression, in other words an expression that resolves to a boolean or a Boolean variable.

You ____ combine enabling and disabling switches

You can combine enabling and disabling switches to have assertions enabled for some classes and/or packages, but not others.

❑You can enable and disable assertions on a class-by-class basis, using what syntax?

You can enable and disable assertions on a class-by-class basis, using the following syntax: java -ea -da:MyClass TestClass

You can use assert as a keyword (as of version 1.4) or an identifier, but not both together. To compile older code that uses assert as an identifier (for example, a method name), use the -source 1.3 command-line flag to javac.

You can use assert as a keyword (as of version 1.4) or an identifier, but not both together. To compile older code that uses assert as an identifier (for example, a method name), use the -source 1.3 command-line flag to javac.

You can use assert ____; for code that should never be reached, so that an ____ ____ is thrown immediately if the assert statement is executed.

You can use assert false; for code that should never be reached, so that an assertion error is thrown immediately if the assert statement is executed.

You cannot have a case that includes a ____ variable, or a ____ of values.

You cannot have a case that includes a non-final variable, or a range of values.

What happens if you handle or declare an unchecked exception?

You're free to handle unchecked exceptions, or to declare them, but the compiler doesn't care one way or the other.

Your exception will then be considered a ____ exception

Your exception will then be considered a checked exception, and the compiler will enforce the handle or declare rule for that exception.

Methods that " might" throw a checked exception include....

methods that call on other methods that throw checked exceptions

While statements in do loops end with a....

semicolon

switch statements can evaluate only to ____ or the ____, ____, ____, and ____ data types.

switch statements can evaluate only to enums or the byte, short, int, and char data types. You can't say, long s = 30; switch(s) { } Example: int x = 3; switch (x) { case 1: System.out.println("x is equal to 1"); break; case 2: System.out.println("x is equal to 2"); break; case 3: System.out.println("x is equal to 3"); break; default: System.out.println("Still no idea what x is");

❑The default block ____ be located first/last/anywhere in the switch block

the default block can be located anywhere

The matching case is considered an ____ ____

the matching case is just the entry point into the case block, but unless there's a break statement, the matching case is not the only case code that runs.

❑A basic for statement has ____ parts: ____ and/or ____ , ____ evaluation, and the ____ expression.

❑A basic for statement has three parts: declaration and/or initialization, Boolean evaluation, and the iteration expression.

❑A variable declared (not just initialized) within the basic for loop declaration ____ be accessed outside the for loop

❑A variable declared (not just initialized) within the basic for loop declaration cannot be accessed outside the for loop (in other words, code below the for loop won't be able to use the variable).

❑All ____ blocks must be ordered from most ____ to most ____.

❑All catch blocks must be ordered from most specific to most general. If you have a catch clause for both IO Exception and Exception, you must put the catch for IO Exception first in your code. Otherwise, the IO Exception would be caught by catch(Exception e), because a catch argument can catch the specified exception or any of its subtypes!

❑An ____ ____ statement will cause: the ____ iteration of the innermost loop to stop, the ____ of that loop to be checked, and if the ____ is met, the loop to ____ ____.

❑An unlabeled continue statement will cause: the current iteration of the innermost loop to stop, the condition of that loop to be checked, and if the condition is met, the loop to run again.

Assertions are ____ at run-time by default. To ____ them, use a command-line flag -____ or -____.

❑Assertions are disabled at run-time by default. To enable them, use a command-line flag -ea or -enableassertions.

❑Assertions are typically enabled during ____ but disabled during ____.

❑Assertions are typically enabled during testing but disabled during deployment.

❑Assertions give you a way to ____ your assumptions during ____ and ____.

❑Assertions give you a way to test your assumptions during development and debugging.

Curly braces are optional for if blocks when they....

❑Curly braces are optional for if blocks that have only one conditional statement. But watch out for misleading indentations.

❑Do not use assert expressions that cause ____ ____.

❑Do not use assert expressions that cause side effects. Assertions aren't guaranteed to always run, and you don't want behavior that changes depending on whether assertions are enabled.

❑Do not use assertions to validate ____ to public ____.

❑Do not use assertions to validate arguments to public methods.

Don't/Do use assertions—even in ____ methods—to validate that a particular code block will never be reached.

❑Do use assertions—even in public methods—to validate that a particular code block will never be reached.

❑Exceptions come in two flavors: ____ and ____.

❑Exceptions come in two flavors: checked and unchecked.

❑If a variable is incremented or evaluated within a basic for loop, it must be declared ____ the loop, or ____ the for loop ____.

❑If a variable is incremented or evaluated within a basic for loop, it must be declared before the loop, or within the for loop declaration.

❑If the break statement or the continue statement is ____, it will cause similar ____ to occur on the labeled loop, not the ____ loop.

❑If the break statement or the continue statement is labeled, it will cause similar action to occur on the labeled loop, not the innermost loop.

❑If you use an optional finally block, under which conditions will it be invoked?

❑If you use an optional finally block, it will always be invoked, regardless of whether an exception in the corresponding try is thrown or not, and regard-less of whether a thrown exception is caught or not.

❑TRUE or FALSE: Code in the finally block will always be invoked.

❑Just because finally is invoked does not mean it will complete. Code in the finally block could itself raise an exception or issue a System.exit().

❑Selectively ____ assertions by using the -____ or -____ flag.

❑Selectively disable assertions by using the -da or -disableassertions flag.

❑Some exceptions are created by ____, some by the ____. Those created by the former are always ____ ____

❑Some exceptions are created by programmers, some by the JVM.Those created by programmers area always checked exceptions.

Subtypes of Error or RuntimeException are unchecked, so the ____ doesn't enforce the handle or declare rule.

❑Subtypes of Error or RuntimeException are unchecked, so the compiler doesn't enforce the handle or declare rule.

❑The default keyword should be used in a switch statement if ....

❑The default keyword should be used in a switch statement if you want to run some code when none of the case values match the conditional value.

❑The do loop will enter the body of the loop.....

❑The do loop will enter the body of the loop at least once, even if the test condition is not met. The expression is not evaluated until after the loop's code is executed do { System.out.println("Inside loop"); } while(false); // note the semicolon after the while expressions

What is the only exception to the finally-will-always-be-called rule ? How could this happen?

❑The only exception to the finally-will-always-be-called rule is that a finally will not be invoked if the JVM shuts down. That could happen if code from the try or catch blocks calls System.exit().

What will this code produce? boolean y = false; if (y = true) { return 16}

❑Watch out for boolean assignments (=) that can be mistaken for boolean equality (==) tests: boolean x = false; if (x = true) { } // an assignment, so x will always be true!

With an enhanced for, the ____ is the array or collection through which you want to loop.

❑With an enhanced for, the expression is the array or collection through which you want to loop.

❑You can create your own exceptions, normally by ____ ____ or one of its ____.

❑You can create your own exceptions, normally by extending Exception or one of its subtypes.

You can enable and disable assertions on a package-by-package basis, and any package you specify also includes any subpackages (packages further down the directory hierarchy).

❑You can enable and disable assertions on a package-by-package basis, and any package you specify also includes any subpackages (packages further down the directory hierarchy).

❑You can initialize more than one variable of the same ____ in the first part of the basic for loop declaration; each initialization must be separated by a ____.

❑You can initialize more than one variable of the same type in the first part of the basic for loop declaration; each initialization must be separated by a comma.

❑You ____ use a number condition for a looping construct.

❑You cannot use a number (old C-style language construct) or anything that does not evaluate to a Boolean value as a condition for a looping construct.

You ____ use a number condition for an if statement

❑You cannot use a number (old C-style language construct) or anything that does not evaluate to a Boolean value as a condition for an if statement


Kaugnay na mga set ng pag-aaral

Chapter 15: Succession Planning and Strategies for Harvesting and Ending the Venture

View Set

BUSINESS FOR LAW CHAPTER 11 QUESTIONS: WRITTEN CONTRACTS

View Set