The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exam Objectives  Flow Control. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exam Objectives  Flow Control." Watch "Exam Objectives  Flow Control." New topic

Exam Objectives Flow Control.

Joe Wolfe
Ranch Hand

Joined: Apr 25, 2007
Posts: 87
Here are my notes.

I went to the Sun site and picked out one section for the SCJP exam and put down everything that I think might be needed for the exam. This is only section 2.

I put a part of it up here before to get feedback and here is the rest. Some of this is repeated.

If you see something wrong please let me know and if you know somethig else that should be here also post a response so I can look it up.

And of course if you have questions ask them cause some smart eson will wonder by and answer them for us all.

thanks, Joe.

Section 2: Flow Control

I. Develop code that implements an if or switch statement; and identify legal argument types for these statements.

II. Develop code that implements all forms of loops and iterators, including the use of for, the enhanced for loop (for-each), do, while, labels, break, and continue; and explain the values taken by loop counter variables during and after loop execution.

III. Develop code that makes use of assertions, and distinguish appropriate from inappropriate uses of assertions.

IV. Develop code that makes use of exceptions and exception handling clauses (try, catch, finally), and declares methods and overriding methods that throw exceptions.

V. Recognize the effect of an exception arising at a specified point in a code fragment. Note that the exception may be a runtime exception, a checked exception, or an error.

VI. Recognize situations that will result in any of the following being thrown: ArrayIndexOutOfBoundsException,ClassCastException, IllegalArgumentException, IllegalStateException, NullPointerException, NumberFormatException, AssertionError, ExceptionInInitializerError, StackOverflowError or NoClassDefFoundError. Understand which of these are thrown by the virtual machine and recognize situations in which others should be thrown programatically.

I. If and Switch

if has the following forms:

if (condition) statement;

if (condition)


if (condition) statement; else statement;

if (condition)



The switch statement has the following syntax

int choice;
switch (choice)
case 1:

case 2:

case 3:

case 4:



If break not there then execution falls thru to next case so multiple cases may be executed. Break, breaks out of the current loop. If no cases selected then default executed.

II. Loops.
while statement has the following forms.

while (condition) statement;

while (condition)


A do while statement has the following forms.

do statement while(condition);



The do while checks at bottom of lop so it is always executed once.

III. Assertions.

An assertion is a JAVA statement that asserts that a condition or rule is true. If the condition is false an AssertError is thrown and an optional expression is printed. An assert statement can be use to replace the many print statements inserted into code during testing.

assert x>= 0;

or assert x>= 0: x+y;

to turn assertions on

JAVA -enableassertions Myjava

JAVA -disableassertions Myjava to disable assertions.

For short use �ea and �da

Works for a specific class only as in.

JAVA �ea:Mysubclass Myjava

. assertions should be fatal errors.
. assertions should not be used to recover from errors in other parts of program.
. assertions for testing purposes only.
. assertions not to catch conditions resulting in exeptions.
. assertions not to verify method arguments have valid values.

For statement has the form

for (init; cond; incr) statement;

a block {�} may be substituted for statement.

Almost any JAVA statement can go into for definition.

The scope of variables declared in for are the end of for loop.

The for each or enhanced for has the form

for (variable: collection) statement;

Statement can be a block { � } The variable is set to the value of each element in collection in succession.

The collection should implement the iterable interface.

The break breaks out of the current loop and starts with next statement.

A labeled break, breaks out of the loop with the label on it.

The following can be used in conditions.
< <= > >= instanceof
== !=
& && | || ^ !

IV. Exceptions Code

To declare:

public FName(String name) throws FileNotFoundException

to declare more than one:
public FName(String name) throws FileNotFoundException, MalformedURLException

To throw:
throw new EOFException();
EOFExcption e = new EOFException();
throw e;
throw new EOFException(gripetext);

when to declare:
1. your method may throw a checked exception 2. you call a method that says it throws exception. 2) you throw a checked exception but do not handle it.

To catch and handle:

{ �
catch( ExceptionType e)
{ �
{ �

Code goes in try block. Handler code goes in catch block. Always code into finally block.

Useful methods:
e. initCause();
e. printStackTrace();

A try block requires either a catch block or finally block. For each try block, there can be multiple catch blocks, but no more than one finally block.

If there are multiple catch blocks, they should be ordered from most-specific to least-specific, since the first matching catch block will be used.

A return statement in the finally block will cause any caught exception to be "lost," since the method will return normally.

Constructors are not inherited, so (unlike methods) they can throw Exceptions that are not declared in base class constructors. However, since derived constructors call base constructors, the derived constructors must still declare any base Exceptions.

V. Exceptions Explained.

Exception Hierarchy: all exception classes inherit from Throwable. Error and Exception descend from Throwable. Checked exceptions include Exception and all its subclasses other than RuntimeExceptions. IOException is often used in programs. Error exceptions are internal runtime system errors and beyond a programmers scope. They should not be thrown. Runtime exceptions represent coding errors that should be corrected within the code. It is not good programming practice to catch these. Exceptions that may occur in a method should normally be caught and handled within the method. Checked exceptions must be caught or declared.

Runtime exceptions: bad cast, array access out of bounds, null pointer access ...

IOExceptions: file not found, reading past EOF, open malformed URL �

Checked exceptions require you to provide code to handle them. The compiler checks to see if you handle checked exceptions. Either catch or declare (throw) them.

When an exception is thrown, the runtime system checks for a matching catch block. The catch block is executed and then the finally block is executed. Execution then continues after the end of the try block.

If no matching catch block is found the finally block is executed and then control passes back to the calling method which checks for a matching catch block.

If no try block is specified then control passes back to the calling method which searches for a catch block.

When over riding a method you cannot throw new or more general exceptions than in overridden method.

VI. Exceptions Examples

ArrayIndexOutOfBoundsException: Runtime Exception: Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

ClassCastException: Runtime Exception: Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.

IllegalArgumentException: Runtime Exception: Thrown to indicate that a method has been passed an illegal or inappropriate argument.

IllegalStateException: Runtime Exception: Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or Java application is not in an appropriate state for the requested operation.

NullPointerException: Runtime Exception: Thrown when an application attempts to use null in a case where an object is required.

NumberFormatException: Runtime Exception: Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.

AssertionError: Error : Thrown to indicate that an assertion has failed.

ExceptionInInitializerError: Error: Signals that an unexpected exception has occurred in a static initializer. An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable.

StackOverflowError: Error: Thrown when a stack overflow occurs because an application recurses too deeply.

NoClassDefFoundError: Error: Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

ArithmeticException - Thrown when an exceptional arithmetic condition has occurred. For example, an integer "divide by zero" throws an instance of this class.

ConcurrentModificationException - This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.

IndexOutOfBoundsException - Thrown to indicate that an index of some sort (such as to an array, to a string, or to a vector) is out of range.

NegativeArraySizeException - Thrown if an application tries to create an array with negative size.

If not now then never.
Joe Wolfe
Ranch Hand

Joined: Apr 25, 2007
Posts: 87
I made a few additions and moved all the loop stuff into the loop section.

Here is what I added by section.

Please review and comment.

I. If and Switch.

The switch statement has the following syntax. Choice can be char byte short int.

int choice;
switch (choice)

II. Loops.

The break breaks out of the current loop and starts with next statement.
The continue, continues with the next iteration of the loop.
A labeled break, breaks out of the loop with the label on it.
A labeled continue, continues with the next iteration of the labeled loop. This is can break out of inside loops.

Init can be zero or more commas separated expressions.
Incr can be zero or more comma separated statements.
For (; is always true and valid.

IV. Exceptions.

If an exception of a subclass of an exception occurs in a gui element then a message prints on the console and the exception is ignored.

If you throw a checked exception you must also declare it with throws.
debasmita pattnayak
Ranch Hand

Joined: Apr 12, 2007
Posts: 94
hi joe,
i think the notes are really substantial. just some more additions to it.
you should mention the valid expression for conditional statements

1) A switch's expression must evaluate to a char, byte, short, int or enum(compilation if you violate).
2) A case constant must evaluate to the same type as the switch expression

rest all is superb
[ May 14, 2007: Message edited by: debasmita pattnayak ]

Joe Wolfe
Ranch Hand

Joined: Apr 25, 2007
Posts: 87
Thats a good response. Thanks.

I need to work on the enums a bit. Looks like I can have string enums and use the switch/case for strings. Not sure on this yet.

thanks for the help.

Maybe if I post these things in small pieces I'll get more responses.
Joe Wolfe
Ranch Hand

Joined: Apr 25, 2007
Posts: 87
Sorry about the long post. I guess it is hard to read. Shorter next time.
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
The collection of your questions is good.It realy helps.
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Its good joe keep it up. Not a problem even if its long.
debasmita pattnayak
Ranch Hand

Joined: Apr 12, 2007
Posts: 94
hi joe,
you are on avery good job by posting all the notes..
keep up the good work...
never mind if it is long..
it really helps a lot...
but one suggestion to you...
you can divide one chapter by objectives and send a post for each objective rather than going for the whole chapter at a time
I agree. Here's the link:
subject: Exam Objectives Flow Control.
It's not a secret anymore!