Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review – Final Test Chapters 8,10,11. Locate error in following statement try i = Integer.pareseInt(str); catch(NumberFormatException e) System.out.println(“Input.

Similar presentations


Presentation on theme: "Review – Final Test Chapters 8,10,11. Locate error in following statement try i = Integer.pareseInt(str); catch(NumberFormatException e) System.out.println(“Input."— Presentation transcript:

1 Review – Final Test Chapters 8,10,11

2 Locate error in following statement try i = Integer.pareseInt(str); catch(NumberFormatException e) System.out.println(“Input is not integer”);

3 try and catch blocks must be enclosed within curly braces. A corrected version:

4 Locate the error in the following statement and show how to fix it swithch(channel) { case 1: System.out.println(“Channel 1”); case 3: System.out.println(“Channel 2”); case 5: System.out.println(“Channel 3”); }

5 There should be a break statement at the end of each case: switch (channel) { case 2: System.out.println(“channel 1"); break; case 5: System.out.println(“channel 2"); break; case 11: System.out.println(“channel 3"); break;

6 Locate error in following statement: do i /= 2 while (i > 10);

7 There's no semicolon at the end of the loop body. A corrected version: do i /= 2; while (i > 10);

8 Which one of the following statements are not equivalent to other two: (a)while(i<10)System.out.println(i); (b)for(;i<10;)System.out.println(i); (c)Do System.out.println(i); while(i<10);

9 (c). All three statements are potentially infinite loops. (c) is different from the other two, however, because it always prints the initial value of i, even if i is greater than or equal to 10.

10 What will be printed when the following code is executed? int a = 1, b = 0, c; try { c = a / b; System.out.println("Division completed"); } catch (ArithmeticException e) { System.out.println("You can't catch me!"); } System.out.println("Time to move on");

11 You can't catch me! Time to move on

12 Which one of the following is a legal type for the controlling expression in a switch statement: (a) char, (b) double, or (c) String, or (d) none of the above?

13 (a) char

14 What is the role of the break statement inside switch statements: (a) required at the end of each case; (b) not required at the end of each case, but usually needed; or (c) not required at the end of each case, and usually not needed?

15 (b) not required at the end of each case, but usually needed

16 For each of the following situations, indicate which type of statement would be more appropriate: an if statement or a switch statement. Assume that each test will be accompanied by a different action. (a) Testing whether a String variable is equal to one of fifty different state abbreviations (such as "GA" or "AL"). (b) Testing whether an int variable is between 0 and 59, between 60 and 69, or between 70 and 100. (c) Testing whether an int variable matches one of 50 different TV channels. (d) Testing whether a double variable matches one of the values 0.0, 0.5, 1.0, 1.5,..., 10.0. (e) Testing whether the length of a String variable matches one of the values 1, 2, 3,..., 10.

17 (a) if statement (b) if statement (c) switch statement (d) if statement (e) switch statement

18 For each of the following properties, answer – I if the property applies only to instance methods, – C if it applies only to class methods, – B if it applies to both, and – N if it applies to neither. (a) Can be public or private (b) Can access instance variables (c) Can access class variables (d) Can have parameters (e) Can call instance methods in the same class (f) Can call class methods in the same class (g) Can be overloaded (h) Can use the keyword this (i) Must be called by an object (j) Calls must always include a dot (.)

19 B(a) Can be public or private I(b) Can access instance variables B(c) Can access class variables B(d) Can have parameters I(e) Can call instance methods in the same class B(f) Can call class methods in the same class B(g) Can be overloaded I(h) Can use the keyword this I(i) Must be called by an object N(j) Calls must always include a dot (.)

20 Given the Account class, create a Checking Account class by extending Account class. The CheckingAccount will store the number of checks written against the account. It will need a method that returns the number of checks written so far and a method that writes a check for the given amount. public class Account { private double balance; public Account(double initialBalance) { balance = initialBalance; } public void deposit(double amount) { balance += amount; } public void withdraw(double amount) { balance -= amount; } }

21 public class CheckingAccount extends Account { private int checksWritten = 0; public CheckingAccount(double initialBalance){ super(initialBalance); } public int getChecksWritten() { return checksWritten; } public void writeCheck(double amount) { withdraw(amount); checksWritten++; }

22 For a Complex class that stores a complex number with real and imaginary parts in instance variables realPart and imaginaryPart respectively, write a an equals method that for the complex class. For two complex numbers to be equal, they must have the same real part and the same imaginary part.

23 public boolean equals(Object obj) { if (!(obj instanceof Complex)) return false; Complex c = (Complex) obj; return realPart == c.realPart && imaginaryPart == c.imaginaryPart; }


Download ppt "Review – Final Test Chapters 8,10,11. Locate error in following statement try i = Integer.pareseInt(str); catch(NumberFormatException e) System.out.println(“Input."

Similar presentations


Ads by Google