• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/107

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

107 Cards in this Set

  • Front
  • Back
What 2 types of statements are commonly referred to as decision statements?
"if" and "switch"
What type of expression does an "if" test against?
only an expression that resolves down to a boolean
Do you need to enclose the "if" block in curly braces?
You don't if there is only one statement to execute
With nested "fi-else" statements, an "else" clause belongs to the ______ "if" statement to which it might possibly belong.
innermost, i.e the closest preceding "if" statement to which it might possibly belong.
What is the only type that a "switch" can evaluate?
the primitive "int", which means only variables and values that can be automatically promoted to an "int" - byte, short, char, int
Can you "switch" on a "long"?
Can you "switch" on a "float"?
Can you "switch" on a "double"?
No
No
No
The case argument has to be resolved at _________. So what are the only 2 things a case argument can be?
compile time
a constant or "final" variable that is assigned a literal value
A "switch" can only check for _____.
equality
If a method invocation is used in a "switch" expression, what must be true?
the method being invoked on the object reference must return a value compatible with an "int".
If you switch on a variable smaller than an "int", say a "byte", and one of the case arguments is too large for a "byte", what happens?
a compile error, "possible loss of precision"
Can you have more than one "case" label in a "switch" statement with the same value?
No, it won't compile
Can you "switch" on an Integer object?
No, only an "int" primitive
What statement do you use so that the execution will immediately move out of the "switch" block to the next statement after the "switch"?
break;
If you don't use the "break" in a "case" block, what happens?
execution just keeps dropping down to the next "case". This is called "fall through"
Does the "default" case have to come at the end of the "switch"?
No
In all loops, the expression (test) must evaluate to a ______.
boolean result
What must be true of any variables used in the expression of a "while" loop?
they must be declared before the expression is evaluated
What kind of loop is good to use when you don't know how many times a block or statement should repeat?
a "while" loop
Will a "while" loop always run?
No, if the test expression is "false" the first time the "while" expression is checked, the loop body will be skipped and the program will begin executing at the first statement after the "while" loop.
When will the body of a "while" loop be executed?
when the condition results in a "true" value
The _____ loop will always run the code in the loop at least once.
"do-while"
What type of loop is useful when you already know how many times you need to execute the statements in the loop's block?
"for"
What are the 3 main parts of a "for" loop?
1. Declaration and initialization of variables
2. The boolean expression (conditional test)
3. The iteration expression
What separates each of the 3 main parts of a "for" loop?
a semicolon
If you declare more than one variable of the same type in a "for" loop, how do you separate them?
with commas
Where does the scope of the variables declared in a "for" loop end?
with the "for" loop
How many logical expressions can you have as the conditional in a "for" loop?
only one, although it can be very complex
When is the iteration expression in a "for" loop executed?
after the loop body runs, it is the last thing that happens in an interation
What 4 things can cause a loop to terminate abruptly?
"break", "return", exception, or "System.exit()" and then the iteration expression will not run
If all parts of the declaration of a "for" loop are left out, what happens?
it is legal and will act like an endless loop

for( ; ; ){
// loop body
}
With the absence of the initialization and increment sections, a "for" loop will act like a _____.
"while" loop
What must be true of the variables declared in a "for" loop?
They must be of the same type.
When can a variable in the declaration part of a "for" loop be used outside the loop?
when it is only initialized in the "for" loop but declared earlier
Does the iterator expression in a "for" loop have to increment or set anything?
No, it can be virtually any arbitrary code statement that you want to happen with each iteration of the loop.
"continue" statements must be inside ______.
a loop, otherwise you'll get a compiler error
"break" statements must be used inside either ______ or _______.
loop
switch statement
What does the "break" statement do?
causes the program to stop execution of the innermost looping and start processing the next line after the block
What does the "continue" statement do?
causes only the current iteration of the innermost loop to cease and the next iteration of the same loop to start if the condition of the loop is met.
When the "continue" statement is hit in a "for" loop, will the iteration expression still run?
Yes
Labeled "break" and "continue" are needed only when you have a _____ loop.
nested
What does a labeled "break" do?
exit out of the "labeled" loop
What does a labeled "continue" do?
indicates which of the nested loops you want to "continue" with the next iteration
What happens if a labeled "continue" or "break" statment is not inside the loop that has the same label name?
the code will not compile
What are the benefits of Java's exception handling?
It allows developers to detect errors easily without writing special code to test return values. It lets us keep exception-handling code cleanly separted from the exception-generating code. It lets us use the same exception-handling code to deal with a range of possible exceptions.
What does the term "exception" mean?
"exception condition" and is an occurrence that alters the normal program flow.
When an exceptional event occurs in Java, an exception is said to be ______.
thrown
The code that's responsible for doing something about the exception is called an _________, and it _____ the thrown exception.
exception handler
catches
The ___ is used to define a block of code in which exceptions may occur. This block of code is called a ______ region.
try
guarded
One or more ____ clauses match a specific exception to a block of code that handles it.
catch
If you have one or more "catch" blocks, they must immediatley follow the _______.
"try" block
Can the "catch" blocks have other statements or blocks in between them?
No
If control jumps to a "catch" block can it return to complete the balance of the "try" block?
No
What is a "finally" block?
it encloses code that is always executed at some point after the "try" block, whether an exception was thrown or not.
Will a finally block execute if there is a "return" statement in the "try" block?
Yes, it executes right after the "return" statement.
What does one normally do in a "finally" block?
close your files, release your network sockets, and perform any other cleanup your code requires.
When does the "finally" block run if the "try" block executes with no exceptions?
it is executed immediately after the "try" block completes
When does the "finally" block run if there was an exception thrown?
it executes immediately after the proper "catch" block completes
Does "finally" always run?
Yes, although there are a few scenarios in which "finally" might to run or complete
Are "finally" clauses required?
No
Are "catch" clauses required?
No, useful not to have if the exception is going to be passed back to the calling method
Can you use a "try" clause without either a "catch" clause or a "finally" clause?
No - you will get a compiler error
What is the "call stack"?
the chain of methods that your program executes to get to the current method. When visualized as growing upward, the last method called is at the top of the stack while the first calling method is at the bottom
What are you producing when you print out the state of the stack at any given time?
a "stack trace"
When an exception is thrown down through the methods on the stack, what is this called?
exception propagation
What happens when you throw an exception out of "main()"?
the JVM halts, and the stack trace is printed to the output
Every exception is an instance of a class that has the class ______ in its inheritance hierarchy.
java.lang.Exception
What happens when an exception is thrown?
an object of a particular Exception subtype is instantiated and handed to the exception handler as an argument to the "catch" clause
All exception classes are subtypes of class _______.
Exception
What are the two subclasses that derive from the class Throwable?
Exception and Error
What do classes that derive from the Error class represent?
unusual situations that are not caused by program errors or by anything that would normally happen during program execution.
Are you required to handle Errors?
No - your application won't generally be able to recover from them
Are Errors exceptions?
Technically no, because they do not derive from class Exception
Do exceptions represent something that happens as a result of a programming error?
Generally not, but RuntimeException is a subtype of Exception that do actually indicate program errors and also rare, difficult to handle exceptional conditions.
What are the 2 ways to get information about an exception?
1. From the type of the exception - most have quite descriptive names
2. Calling the "printStackTrace()" method it inherits from the class Throwable.
What happens when you call "printStackTrace()" from an exception object?
a stack trace from where the exception occurred will be printed with the most recently called method at the top - this is called unwinding the stack
Name 4 types that can be thrown using the "throws" keyword?
Exception, Error, RuntimeException, and Throwable

Note: these all can be caught
How can you actually catch more than one type of exception in a single "catch" clause?
If the class specified in the "catch" clause has subclasses, any exception object that subclasses the specified class will be caught as well.
Is it a good idea to write exception handlers that trap many errors at once?
No - it reduces the reliability of your program because it's likely that an exception will be caught that the handler does not know how to handle.
Give an example of a single catchall exception handler.
try {
// some code
}
catch(Exception e) {
e.printStackTrace();
}
This is however a bad ides
Describe "exception matching".
When an exception is thrown, Java will try to find a "catch" clause for the exception type. If it doesn't find one, it will search for a handler for a supertype of the exception. If it does not find a "catch" clause that matches a supertype for the exception, then the exception is propagated down the call stack.
What happens if you place the handler for IOException above the handler for FileNotFoundException?
it will not compile - the handlers for the most specific exceptions must always be placed above those for more general exceptions
Does the order matter for handlers for exceptions that are siblings of each other?
No
The _____ keyword is used to list the exceptions that a method can throw.
throws
Does a method that might "throw" an exception have to declare it?
Yes it must unless it's a subclass of RuntimeException.
Note: even methods that "ducking" an exception must declare it.
All non-RuntimeExceptions are considered ______ exceptions.
checked
What is the "handle or declare requirement"?
Each method must either handle all checked exceptions by supplying a "catch" clause or list each unhandled checked exception as a checked exception.
What are "unchecked" exceptions?
RuntimeException, Error, and all of their subtypes. They don't have to be specified or handled.
Can you throw the same exception that you just caught?
Yes - but then you must both "handle" and "declare" the exception
What are the benefits of Assertions?
They let you test your assumptions during development, but the assertion code evaporates when the program is deployed, leaving behind no overhead or debugging code to track down and remove.
How do assertions work?
You always assert something is true. If your assertion turns out to be wrong (false) then a stop-the-world AssertionError is thrown.
What are the two flavors of assertions?
simple and really simple
The simple version adds a second expression, separated from the first (boolean expression) by a colon, that adds a little more information to the stack trace.
Assertions are typically _____ when an application is being tested and debugged, but _____ when the application is deployed.
enabled
disabled
What can you pass to the second expression in a simple assertion?
a primitive or an object and it will convert it into a String representation. It must resolve to a value.
What must the first expression in an assertion result in?
a boolean value
What is true about assertions in version 1.4?
they are disabled by default and so they can be used as an identifier.
Can you use "assert" as a keyword and as an identifier?
No
How flag must you use when you compile to get assertion-aware code?
javac -source 1.4
What are the command-line switches that tell the JVM to run with assertions enabled?
java -ea
or
java -enableassertions
What are the command-line switches that tell the JVm to disable assertions?
java -da
or
java -disableassertions
What are 3 ways to use command-line switches to enable and disable assertions?
1. With no arguments - enables or disables assertions in all classes, except for the system classes
2. With a package name - enables or disables assertions in the package specified and any packages below this package in the same directory hierarchy
3. With a class name - Enables or disables assertions in the class specified.
What is a subpackage?
any package in a subdirectory of the named package
Is it appropriate to handle an assertion failure?
No, even though AssertionError is a subclass of Throwable, so it can be caught - but don't do it.
Is it appropriate to use assertions to validate arguments to a public method?
No - you don't want publicly accessible code that works only conditionally, depending on whether assertions are enabled or disabled
Is it appropriate to use assertions to validate arguments to a private method?
Yes
Is it appropriate to use assertions to validate command-line arguments?
No - use the exception mechanism to enforce them
Is it appropriate to use assertions, even in public methods, to check for cases that you know are never,ever supposed to happen?
Yes - use
default: assert false
Assertions must not cause _____.
side effects - you don't want your code to behave differently depending on whether assertions are enabled.