Chapter 5: Flow Control, Exceptions and Assertions Flashcards

1
Q

To which IF belongs the else statement?

if(exam.done()) //1
if(exam.getScore() < 0.61) //2
System.out.println(“Try again”)
else System.out.println(“java master”)

A

2

The most inner if.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
What is is the result of the following expression?
int y = 5;
int x = 2;
if((x > 3) &amp;&amp; (y < 2) | true) {
System.out.println("true")
}
A

It will print nothing.

x is not greather than 3, && is a short circuit operator so it will not evaluate the rest of the statements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the result of the following?

if(b = true) {} // 1
if(x = 2) {} // 2
A

1, evaluates to true. An assingment wil return the assigned value.
2, compiler error 2 cannot be evaluated to a boolean

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are valid evaluation types for a switch statement?

A

Primitive types that can be implicitly cast to int (char, byte, short, int) or Enum.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
What is the result of the following code?
int b = 2;
int x = 0;
switch (x) {
   case b:
   System.out.println("b");
   break;
   case 2 > x:
   break;
}
A

Compiler error!

A case constant must evaluate to the same type and must be a compile time constant! (mark it final or use a direct literal).

2 > x is not valid in a switch. It can only check for equality.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
What is the result?
byte b = 2;
switch(b) {
   case 128:
}
A

Compiler error!

128 doesn’t fit into a byte.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
What is the result?
int x = 10;
switch(x) {
   case 20:
   case 40:
   case 40:
}
A

Compiler error!

You can’t define the same case statements multiple times.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does the break keyword do in a switch statement?

A

It will break the fall through of the switch cases. The first case evaulated to be equal en will execute all following cases until a break; is found.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
What is the result?
int x = 2;
switch(x) {
   case 2: System.out.println("2");
   default: System.out.println("default");
   case 3: System.out.println("3");
}
A

2, default, 3

The default case works just like any other case for fall through. The only difference is, the case itself will only match with the values that are not provided as cases.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the result?
do {
System.out.println(“loop”);
} while (false);

A

It will print loop once.

The do-while executes once and checks the expression afterwards. If evaluated to true it will execute again.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the result?

for(int x = 0, y = 1; (x > 0), (y > 5); x++, System.out.println(“loop”)) {
System.out.println(“test”);
}

A

Compiler error!

The for-loop contains three parts (seperated by a ;). The 1st (Declaration and Initialization) may contain multiple statements seperated by a ,. The same applies for the third part, the Iteration Expression. The third part can conain any piece of code, not just increment operations. The second part may only contain a single expression!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What can a loop cause to stop (apart from the condition conditioning to false)?

A

break, return, System.exit(); or an Exception.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the result?
for( ; ; ) {
System.out.println(“test”);
}

A

An endless loop.

The three parts are all optional.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the continue keyword do (unlabeled context)?

A

It will move on to the next iteration of the inner most loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
What is the result?
outer:
   for(int i = 0; i < 5; i++) {
      while(true) {
         someLabel: System.out.println("inner");
         break outer;
      }
      System.out.println("outer");
   }
System.out.prinln("end");
A

It will print “inner end”

The break statement is a labelled break. And will break out of the outer loop instead of the inner loop if used without a label.

someLabel points to a statement. But can’t be called upon, but is perfectly valid (but is has no functional use).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
What is wrong?
try {
// Do Stuff
}
A

A try always needs atleast one catch or finally block.

17
Q

Describe the exception hierachy.

A

Object
Throwable
Exception - Error
RunTimeExceptoion

18
Q
What is wrong?
try {
// Do Risky Stuff
} 
catch (IOException e) {}
catch(FileNotFoundException e) {}
A

Compiler error!

The specific exception catch should always be defined before the more generic catch.

19
Q

How do you throw an exception?

A

throw new MyException();

20
Q

What is the catch or declare rule?

A

Each method must either handle all checked exceptions by supplying a catch clause or list each unhandeled checked exception as a thrown exception.

21
Q
What is wrong?
public void doStuff() throws MyException1, MyException2 {
   try { throw new IOException();}
   catch(IOException e) { throw e; }
}
A

The method doStuff() does not declare the throws clause for IOException.

22
Q

What are assert statements in Java?

A

Validating method input parameters using assert statements during the development phase of an program.

private void methodA(int num) {
assert(num >= 0); // Throws an AssertionError
}

23
Q

Describe a assert with a String message.

A

assert(b) : “The value of b is “ + b;

24
Q
What are valid and invalid assert statements?
assert(x == 1); // 1
assert(true); // 2
assert 0; // 3
assert(b = true); // 4
assert(x == 1) : 1; // 5
assert(x == 1) : ; //6
assert true; // 7
A

Valid: 1, 2, 4, 5, 7
Invalid: 3, 6

25
Q

What is wrong?
int assert = 5;
private void assert() {}

A

assert is a keyword since java 1.4.

26
Q

How to compile a class using a specific java version?

A

javac -source OldCode.java
javac -source 1.4 OldCode.java
javac -source 4 OldCode.java

If you want to use the assert as an identifier instead of a keyword you need to compile using 1.3.

27
Q

How do you enable/disable assertions at runtime?

A

java -ea TestClass // Enable
java -enableassertions TestClass // Enable
java -da TestClass //Disable, redundant because default
java -disableassertions TestClass //Disable, redundant because default

28
Q

What do the following run statements?

  1. java -ea
  2. java -da
  3. java -ea:com.foo.Bar
  4. java -ea -da:com.foo…
  5. java -ea -dsa
A
  1. Enable assertions
  2. Disable assertions
  3. Enable assertions in class Bar
  4. Enable assertions in application,, except the classes in package com.foo.
  5. Enable assertions in application, disable assertions for system classes.
29
Q

What are the do’s and dont’s for using assertions?

A

Don’t:
Use assertions to validate arguments to public methods.
Use assertions to validate command line arguments.
Use Assert expressions that can cause side effects.

Do’s:
Use Assertions to validate arguments to a private method.
Use assertions to check for cases that you know are never, ever supposed to happen.

30
Q
What will happen?
public class TestClass
{
   public static void main(String args[])
   {
      try
      {
         RuntimeException re = null;
         throw re;
      }
      catch(Exception e)
      {
         System.out.println(e);
      }
   }
}
A

The program will compile without error and will print java.lang.NullPointerException when run.

31
Q
What is wrong?
try
        {
            m2();
        }
        finally
        {
            m3();
        }
        catch (NewException e){}
A

Compile error. The correct order is Try, optional catch blocks, optional finally block.

32
Q
Which of the following code snippets will compile without any errors?
while (false) { x=3; } // 1
if (false) { x=3; } // 2
do{ x = 3; } while(false); // 3
for( int i = 0; i< 0; i++) x = 3; // 4
A

2,3,4

1: while (false) { x=3; } is a compile-time error because the statement x=3; is not reachable;