Shared Flashcard Set

Details

SCJP 6 Chapter 5
Flashcards for the Sierra/Bates SCJP 6 exam prep book. Chapter 5 - Flow Control, Exceptions, and Assertions
41
Software
Professional
03/09/2011

Additional Software Flashcards

 


 

Cards

Term
What does an unlabeled 'break' statement do?
Definition
Exit out of the innermost looping construct and proceed with the next line of code beyond the loop block.
(p. 353)
Term
What does an unlabeled 'continue' statement do?
Definition
Stops the current iteration of the loop and proceed with the next iteration (if the conditional test is true) or exits (if the conditional test is false)

(p. 354)
Term
What is the syntax for a labeled statement?
Definition
A label statement must be placed just before the statement being labeled, and it consists of a valid identifier that ends with a colon (:).

foo:
for(int x = 3; x < 20; x++) {
while (y > 7) {
y--;
}
}

(p. 354)
Term
T/F: A label name can be anything that is not a Java keyword.
Definition
F: It must adhere to the rules for a valid variable name
(p. 354)
Term
What is the syntax for using 'break' or 'continue' with a label?
Definition
The keyword followed by the label name followed by a semicolon.
outer:
for(int i=0; i<5; i++) {
while (true) {
break outer;
}
}

(p. 355)
Term
T/F: If you have one or more 'catch' blocks, they must immediately follow the 'try' block.
Definition
T
(p. 358)
Term
T/F: With respect to catch blocks, order matters.
Definition
T
(p. 358)
Term
T/F: Execution transfers out of the 'try' block as soon as an exception is thrown.
Definition
T
(p. 359)
Term
T/F: A finally block will execute even if there is a return statement in a try block (during the execution of which no exception was thrown)
Definition
T
(p. 359)
Term
T/F: Finally clauses are optional
Definition
T
(p. 360)
Term
T/F: It is legal to have try and finally blocks without at least one catch.
Definition
T
(p. 360)
Term
When does a finally block execute?
Definition
Immediately after the try block if no exceptions occurred, or immediately after the proper catch block completes if an exception occurred.
(p. 359)
Term
java.lang.Error is a subclass of (Choose all that apply):

a) java.lang.Exception
b) java.lang.Object
c) java.lang.RuntimeException
d) java.lang.Throwable
Definition
b, d (pg 366)
Term
True/False - java.lang.RuntimeException is a subclass of java.lang.Error and java.lang.Object
Definition
False. java.lang.RuntimeException is not a subclass of java.lang.Error
Term
class DoStuffClass {
public static void main (String [] args) {
try {
doStuff();
} catch (Exception e) {

}
System.out.println("main");
}

static void doStuff() {
doMoreStuff();
System.out.println("doStuff");
}


static void doMoreStuff() {
int [] x = {8,5,1};
try {
x[3] = 2;
} catch(ArithmeticException ae) {
// Some fancy code
}
System.out.println("doMoreStuff");
}
}


Choose all that applies:

a) "main" will be printed
b) "doStuff" will be printed
c) "doMoreStuff" will be printed
d) Nothing will be printed
e) This code will not compile
Definition
a. x[3] = 2; will throw an ArrayIndexOutOfBoundsException which is not caught, and doMoreStuff() is "ducking" the exception.
Term
class TestClass {
public static void main (String [] args) {
try {
doTestingStuff();


}
System.out.println("main");
catch (Exception e) {}
}

static void doTestingStuff() {
doMoreTestingStuff();
System.out.println("doTestingStuff");
}

static void doMoreTestingStuff() {
int a = 1/0;
System.out.println("doMoreTestingStuff");
}
}

Choose all that applies:

a) "main" will be printed
b) "doTestingStuff" will be printed
c) "doMoreTestingStuff" will be printed
d) Nothing will be printed
e) This code will not compile
Definition
e. catch clause must immediately follow the try block.
Term
Which of the following are legal while expressions?
int x = 1;

A. while(x){}
B. while(x = 5){ ]
C. while (x==5){}
D. while(true) {}
Definition
C and D
Term
for(int x = 1; x < 2; x++){
System.out.println(x);
}
System.out.println(x);

What is the result?
A. 0 1 1
B. 1 1
C. 0 1 2 2
D. 1 2 2
E. Will not compile.
Definition
E
Term
ArrayList strList = new ArrayList(); strList.add("One"); strList.add("Two"); strList.add("Three"); for(final String tempString: strList){ System.out.println(tempString); } What would be the result of the above statement? A. One One One B. One Two Three C. Does not compile due to tempString.
Definition
B.
One
Two
Three
Term
What is the result?
int x = 5;
int y = 10;
if(x > 5)
y = 2;
x = 10;
System.out.println(x + y);
Definition
Result is 20. Since there are no braces x will always be set to 10.
Term
What is the result?
int isTrue = 1;
if(isTrue) System.out.println("TRUE!");
else System.out.println("FALSE!");
Definition
Compilation error. isTrue is an int and therefore not a valid expression.
Term
What are the two places exceptions originate from?
Definition
JVM Exceptions - Those exceptions or errors that are either exclusively or most logically thrown by the JVM.

Programmatic exceptions - Those exceptions that are thrown explicitly by application and/or API programmers.

pg 379
Term
What is the two assertion syntaxes?

How do they work?
Definition
assert(boolean Expression);
or

assert(boolean Expression) : (expression that can resolove to a string);

ie:
assert (y > x): "y is " + y + " x is " + x;

Both will throw an assertion error if the boolean expression resolves to false. The second option will just provide more detail.

pg 385
Term
Describe the following exceptions and identify what they are thrown by (JVM or Programmatically).

ArrayIndexOutOfBoundsException
ClassCastException
IllegalArgumentException
IllegalStateException
NullPointerException
NumberFormatException
AssertionError
ExceptionInInitializerError
StackOverflowError
NoClassDefFoundError
Definition
ArrayIndexOutOfBoundsException :
Thrown when attempting to access an array
with an invalid index value (either negative
or beyond the length of the array).
By the JVM

ClassCastException :
Thrown when attempting to cast a reference
variable to a type that fails the IS-A test.
By the JVM

IllegalArgumentException :
Thrown when a method receives an argument
formatted differently than the method
expects.
Programmatically

IllegalStateException :
Thrown when the state of the environment
doesn’t match the operation being attempted,
e.g., using a Scanner that’s been closed.
Programmatically

NullPointerException :
Thrown when attempting to access an object
with a reference variable whose current value
is null.
By the JVM

NumberFormatException :
Thrown when a method that converts a
String to a number receives a String that it
cannot convert.
Programmatically

AssertionError :
Thrown when a statement’s boolean test
returns false.
Programmatically

ExceptionInInitializerError :
Thrown when attempting to initialize a static
variable or an initialization block.
By the JVM

StackOverflowError :
Typically thrown when a method recurses
too deeply. (Each invocation is added to the
stack.)
By the JVM

NoClassDefFoundError :
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.
By the JVM

pg 382
Term
Given:
void noReturn() { }
int aReturn() { return 1; }
int x = 1;
boolean b = true;

Which of the following assert statements are legal?
a) assert(x = 1)
b) assert(b) : aReturn();
c) assert(x == 1) ? "ok" : "oops!";
d) assert(true) : noReturn();
Definition
b is legal.
a fails because the assert condition must be a boolean. c fails as its attempting to mix the assert and conditional operator syntaxes. d will not compile since the expression portion must return a value, and noReturn() is a void method.
p. 386
Term
In which version of Java did assert become a keyword?
Definition
1.4
p. 387
Term
Given the following in a TestAsserts class:
int assert = getInitialValue();
if(assert == getActualResult()) {
//do something
}

What will be the result of compiling the class using the commands below:
a) javac -source 1.3 TestAsserts.java
b) javac -source 4 TestAsserts.java
c) javac -source 1.5 TestAsserts.java
d) javac TestAsserts.java
Definition
a) Code compiles with warnings
b) Does not compile: 4 is not a valid argument for the -source option
c and d both fail to compile because assert is used as an identifier rather than as a keyword.
p. 387-389
Term
What are the four command line options for enabling and disabling assertions at runtime?
Definition
java -enableassertions
java -ea
java -disableassertions
java -da
(p. 391)
Term
At runtime, are assertions enabled or disabled by default in Java 6?
Definition
disabled
(p. 391)
Term
What is the command for selectively enabling assertions:
a) in class Bar located in the com.foo package
b) in package com.foo and any of its subpackages
c) enabling assertions in general, but disabling assertions in the system package
Definition
a) java -ea:com.foo.Bar
b) java -ea:com.foo...
c) java -ea -dsa
(p. 391)
Term
T/F:
The following is a correct use of assertions:
public void doStuff(int x) {
assert(x > 0);
//do things with x
}
Definition
F: Although the code is legal and will compile, one should not use assertions to validate arguments to public methods. The use of the terms "appropriate" or "correct" in the exam with respect to assertions should be interpreted in the context of how one SHOULD use assertions rather than how they legally COULD be used.
(p. 392)
Term
T/F: It is appropriate to use assertions in public methods to check for cases that should never happen.
Definition
T: This is an appropriate use of assertions, and can include code blocks that should never be reached (e.g. the default of switch statement).
(p. 393)
Term
T/F:
The following is an appropriate use of assertions (why or why not):
public void doStuff() {
assert(someOtherMethod());
//continues on
}
public boolean someOtherMethod() {
y = x++;
return true;
}
Definition
F: Assertion expressions should not cause side effects
(p. 394)
Term

Will the following code compile?

 

boolean boo = false;

if (boo = true) {}

Definition
Will compile because boo is set equal to true and not testing to see if it is true
p334
Term

 

Will the following code compile?

int x =3;

if (x =5) {}

Definition
will not compile because x is not a boolean
p334
Term

 

True or False

 

Does a case constant have to evaluate to the same type as the switch expression?

Definition
True
p336
Term

 

 

True of false

 

Does the switch statement only check for equality?

Definition
True

p336
Term

 

True or False

 

The default case must always be at the end of the switch?

Definition
False.

p341
Term
What is the result?
(a) Prints nothing
(b) Prints "IOException"
(c) Prints "FileNotFoundException"
(d) Compilation Error

public class ReadData {
	public static void main(String args[]) {
		try {
			RandomAccessFile raf = new RandomAccessFile("myfile.txt", "r");
			byte b[] = new byte[1000];
			raf.readFully(b, 0, 1000);
		} catch (IOException e) {
			System.err.println("IOException");
		} catch (FileNotFoundException e) {
			System.err.println("FileNotFoundException");
		}
	}
}
Definition
(d) Compilation error.

Unreachable catch block for FileNotFoundException. It is already handled by the catch block for IOException
Term
What is the result?
(a) Prints nothing
(b) Prints "1"
(c) An IOException occurs
(d) Compilation Error

class Test {
	public static void main(String[] args) {
		System.out.println(myMethod1());
	}

	public static int myMethod1() throws IOException {
		return 1;
	}
}
Definition
(d) Compilation Error.

Unhandled exception type IOException
Term
What is the result?
(a) Prints nothing
(b) Prints "1"
(c) An IOException occurs
(d) Compilation Error

class Test {
	public static void main(String[] args) throws IOException {
		System.out.println(myMethod1());
	}

	public static int myMethod1() throws IOException {
		return 1;
	}
}
Definition
(b) Prints "1".
Supporting users have an ad free experience!