review – final test chapters 8,10,11. locate error in following statement try i =...

23
Review – Final Test Chapters 8,10,11

Upload: franklin-wilkins

Post on 04-Jan-2016

215 views

Category:

Documents


1 download

TRANSCRIPT

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

Review – Final Test

Chapters 8,10,11

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

• Locate error in following statement

tryi = Integer.pareseInt(str);

catch(NumberFormatException e)System.out.println(“Input is not

integer”);

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

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

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

• Locate the error in the following statement and show how to fix itswithch(channel){

case 1: System.out.println(“Channel 1”);case 3: System.out.println(“Channel 2”);case 5: System.out.println(“Channel 3”);

}

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

• 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;

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

• Locate error in following statement:

do i /= 2

while (i > 10);

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

• There's no semicolon at the end of the loop body. A corrected version:

do i /= 2;

while (i > 10);

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

• 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);

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

• (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.

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

• 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");

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

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

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

• 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?

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

• (a) char

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

• 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?

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

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

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

• 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.

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

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

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

• 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 (.)

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

• B (a) Can be public or privateI (b) Can access instance variablesB (c) Can access class variablesB (d) Can have parametersI (e) Can call instance methods in the same classB (f) Can call class methods in the same classB (g) Can be overloadedI (h) Can use the keyword thisI (i) Must be called by an objectN (j) Calls must always include a dot (.)

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

• 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; }}

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

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++;

} }

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

• 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.

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

public boolean equals(Object obj) { if (!(obj instanceof Complex))

return false; Complex c = (Complex) obj; return realPart == c.realPart && imaginaryPart == c.imaginaryPart; }