OCJP(Oracle Certified Java Program)

Totel:36 Click: 1 2 3 4 5 6 7 8
scjp1.5 Interview Questions And Answers

Page 1

The following would be a very bad idea:

public void doStuff() {
assert (modifyThings());
// continues on
public boolean modifyThings() {
x++ = y;
return true;

The rule is: An assert expression should leave the program in the same state it was in before the expression!

this is an include codes blocks that should be never reached including the default of a switch statement as follows:

switch(x) {
case 2: y = 3;
case 3: y = 17;
case 4: y = 27;
default: assert false; // We're never supposed to get here!

assertion is not use for the validate argument a public method which is a type of rule , if our program requires command line argument,We'll probably use the exception mechanism to enforce them.

If we write a private method , we almost certainly wrote any code that calls it,when we assume that the logic in code calling our private method is correct.

private void doMore(int x) {
assert (x > 0);
// do things with x

The only method in the program diff that matters between the preceding example and the one before it is the access modifier.

Following code for the assertion : 

public void doStuff(int x) {
assert (x > 0);
// do things with x

A public method might be called from code that we don't control,because Because public methods are part of our exposed interface to the outside
world, we are supposed to guarantee that any constraints on the arguments will be enforced by the method itself. But since assertions aren't guaranteed to actually run.

Goto Page:

1 2 3 4 5 6 7 8