test 3 java final test review

Ace your homework & exams now with Quizwiz!

public abstract class MyClass { //body } Given the class definition above, will the following statement compile? Explain your answer MyClass obj = new MyClass();

no, Abstract classes cannot instantiate objects

A subclass can override ______. a constructor in its parent's class a method in its parent's class

a method in its parent's class

A finally clause will execute : only if the try statement that precedes it does not throw an exception in any circumstance only if the try statement that precedes it throws an exception that is caught only if the try statement that precedes it throws an exception that is not caught only if the try statement that precedes it throws an exception, whether it is caught or not

in any circumstance

The relationship between a parent class and a child class is referred to as a(n) ________ relationship.

inheritance (is a)

Match the following(superclass, subclass, package) common characteristics and behaviors additional and specific characteristic and behaviors protected

1. superclass. 2. subclass. 3.package

Suppose we declare a Speaker interface as follows: public interface Speaker { public void speak(); public void announce (String str); } Given a class named Philosopher that implements the Speaker interface and has a method named meditate(). Given another class named Bird that implements the Speaker interface and has a method fly(). Explain whether EACH of the following statements is valid. 1. Speaker bird = new Bird(); 2. Philosopher philosopher = new Philosopher(); 3. philosopher.meditate(); 4. philosopher = bird;

1. yes you can use interface to call an object. 2. yes creating a philosopher object. 3 yes, philosopher class has a mediate method. 4. no, they cannot point to each other, where the bird object is from type speaker

9 4 12 2 6 8 18 Which of the following lists of numbers would accurately show the array after the fourth pass through the Selection Sort algorithm? 4, 9, 12, 2, 6, 8, 18 2, 4, 12, 9, 6, 8, 18 2, 4, 6, 8, 12, 9, 18 2, 4, 9, 12, 6, 8, 18 9, 4, 12, 2, 6, 8, 18

2, 4, 6, 8, 12, 9, 18

9 4 12 2 6 8 18 Which of the following lists of numbers would accurately show the array after the fifth pass of the Insertion Sort algorithm? 2, 4, 12, 6, 8, 9, 18 2, 4, 9, 6, 12, 8, 18 2, 4, 12, 9, 6, 8, 18 9, 4, 12, 2, 6, 8, 18 2, 4, 6, 8, 9, 12, 18

2, 4, 6, 8, 9, 12, 18

Suppose we declare a Speaker interface as follows: public interface Speaker { public void speak(); public void announce (String str); } Given a class named Philosopher that implements the Speaker interface and has a method named meditate(). Given another class named Bird that implements the Speaker interface and has a method fly(). State which of the following statements is/are valid: 1. Speaker bird = new Speaker(); 2. Philosopher phil = new Philosopher(); 3. Speaker bird = new Bird(); 4. Given statement 3, bird.fly(); 5. phil.announce ("Chirp, Chirp"); (3 answers) 1 4 3 5 2

3, 5, 2

Explain why an abstract method should not be declared final.

An abstract class should never be declared final because it is usually at the top of the super class in the hierarchy diagram. Final should only be declared at the child level and will be at bottom of hierarchy diagram

public class AClass { private int x; private int y; public AClass(int a, int b) { x = a; y = b; } public int addEm( ) { return x + y; } public void changeEm( ) { x++; y--; } public String toString( ) { return "" + x + " " + y; } } Consider that you want to extend AClass to BClass. BClass will have a third int instance data variable z. Which of the following would best redefine the toString method for BClass? A) public String toString(int z) { return " " + x + " " + y + " " + z; } B) public String toString( ) { return super.toString( ); } C) public String toString( ) { return super.toString( ) + " " + z; } D) public String toString( ) { return super.toString( ) + " " x + " " + y + " " + z; } E) public String toString( ) { return " " + x + " + y + " " + z; } D C A E B

C

public class AClass { private int x; private int y; public AClass(int a, int b) { x = a; y = b; } public int addEm( ) { return x + y; } public void changeEm( ) { x++; y--; } public String toString( ) { return "" + x + " " + y; } } Consider that you want to extend AClass to BClass. BClass will have a third int instance data variable, z. Which of the following would best define BClass's constructor? A) public BClass(int a, int b, int c) { super(a, b, c); } B) public BClass(int a, int b, int c) { x = a; y = b; z = c; } C) public BClass(int a, int b, int c) { z = c; } D) public BClass(int a, int b, int c) { super(a, b); z = c; } E) public BClass(int a, int b, int c) { super( ); } B A C D

D

If the search is done on a list once or twice, which search algorithm (linear and binary) can be used and would be more efficient? Explain your answer.

If the list is not sorted then linear search is more efficient , but if the list is sorted already then binary is faster.

public abstract class Example { public abstract void show(String message) { //body} } Will the class above compile? Explain your answer.

No the code will not compile because abstract methods end with a semi colon and not {}.

public class AClass { protected int x; protected int y; public AClass(int a, int b) { x = a; y = b; } public int addEm( ) { return x + y; } public void changeEm( ) { x++; y--; } public String toString( ) { return "" + x + " " + y; } } Consider that you want to extend AClass to BClass. BClass will have a third int instance data variable z. You want addEm to now add all three values and return the sum and changeEm to change x and y, but leave z alone. Which should you do? Redefine changeEm to call super.changeEm( ) and then set z = x + y, but leave addEm alone Redefine addEm and changeEm without referencing super.addEm( ) or super.changeEm( ) Redefine addEm to return the value of z + super.addEm( ) and redefine changeEm to call super.changeEm( ) and then set z = x + y Redefine addEm to return the value of z + super.addEm( ), but leave changeEm alone Redefine changeEm to call super.changeEm( ) without doing anything to z, and redefine addEm to return super.addEm(

Redefine addEm to return the value of z + super.addEm( ), but leave changeEm alone

State the relationships among the following classes: Table, Furniture, CoffeeTable, Computer.

Table is a/inheritence furniture CoffeeTable is a/inheritence Furniture. Table has a (aggregation) Computer

A final class should only be declared at the ________ level Object level parent child middle

child

List the three methods of the Object class, separated with a space in ALPHABETICAL ORDER. No paremeter or ( ) is necessary, just the names

clone, equals, toString

Inheritance through an extended (derived) class supports which of the following concepts? modularity correctness information hiding interfaces code reuse

code reuse

Use the code below to answer the question. Note that the catch statements in the code are not implemented, but you will not need those details. Assume filename is a String, x is an int, a is a double array and i is an int. Use the comments i1, i2, i3, e1, e2, e3, e4, e5 to answer the questions (i for instruction, e for exception handler). try { BufferedReader infile = new BufferedReader (new FileReader(filename)); // i1 int x = Integer.parseInt(infile.readLine( ));// i2 a[++i] = (double) (1 / x); // i3 } catch (FileNotFoundException ex) {...} // e1 catch (NumberFormatException ex) {...} // e2 catch (ArithmeticException ex) {...} // e3 catch (ArrayIndexOutOfBounds ex) {...} // e4 catch (IOException ex) {...} // e5 An exception raised by the instruction in i3 would be caught by the catch statement labeled (two answers:) e4 e2 e3 e5 e1

e3, e4

Given that Guest is an Interface, the following statement is valid: Guest g1 = new Guest(); true False

false

public static void main(String[ ] args) { try{ ExceptionThrowerCode etc = new ExceptionThrowerCode( ); etc.m1( ); etc.m2( ); } catch (ArithmeticException ae) { ... } } public class ExceptionThrowerCode { ... public void m1( ) { ... } public void m2( ) { try{ m3( ); } catch(ArithmeticException ae) {...} catch(NullPointerException npe) {...} } public void m3( ){ try { ... } catch(ArithmeticException ae) {...} } } If a NullPointerException arises in the try statement in m3 it is not caught leading to the program terminating it is caught in main it is caught in m3 it is caught in m1 it is caught in m2

it is caught in m2

public static void main(String[ ] args) { try{ ExceptionThrowerCode etc = new ExceptionThrowerCode( ); etc.m1( ); etc.m2( ); } catch (ArithmeticException ae) { ... } } public class ExceptionThrowerCode { ... public void m1( ) { ... } public void m2( ) { try{ m3( ); } catch(ArithmeticException ae) {...} catch(NullPointerException npe) {...} } public void m3( ){ try { ... } catch(ArithmeticException ae) {...} } } If an ArithmeticException arises in the try statement in m3 it is caught in m1 it is caught in m3 it is caught in m2 it is caught in main it is not caught leading to the program terminating

it is caught in m3

public static void main(String[ ] args) { try{ ExceptionThrowerCode etc = new ExceptionThrowerCode( ); etc.m1( ); etc.m2( ); } catch (ArithmeticException ae) { ... } } public class ExceptionThrowerCode { ... public void m1( ) { ... } public void m2( ) { try{ m3( ); } catch(ArithmeticException ae) {...} catch(NullPointerException npe) {...} } public void m3( ){ try { ... } catch(ArithmeticException ae) {...} } } If a NullPointerException arises in the try statement in the main method, it is not caught leading to the program terminating it is caught in m1 it is caught in m3 it is caught in main it is caught in m2

it is not caught leading to the program terminating

What is the Time Complexity/Efficiency for Binary Search? Explain why.

its log2n for the search alone. but if you include sorting time its sorting time plus log2n

The worst case scenario of a Linear Search is to make ___________ comparison(s) half of the list 1 n n^2

n

Selection Sort and Insertion Sort have the Order of ___________ (meaning makes ___ number of comparisons) n/2 n-1 n^2 n

n^2

Given the following method, and an class named MyException, explain if it will compile. public boolean myMethod( char c){ MyException me = new MyException(); if (c != '$') throw me; return true; }

no it will not compile because you don't have exception in the method header

Late binding means the version of the method body to use is decided at _________ time.

run

Which of the following messages passed to the String str could throw a StringIndexOutOfBoundsException? str.charAt(2); str.equals(str); str.replace('a', 'A'); String s = str.substring(str.length());

str.charAt(2); String s = str.substring(str.length());

1. What is the problem with the following code segment if any? 2. What is the output? //scan is a Scanner object already declared and initialized //ex is an Exception object already declared and initialized System.out.println ("Please enter a number: "); int myNum = scan.nextInt(); throw ex; System.out.println ("The number is entered but not accepted."); \

the throw statement is '"thrown" every time the code runs and never is caught. The code never reaches the last statement after the "throw ex" line. Output is everything before the "throw ex"

Given that both Volunteer and Hourly inherit from the StaffMember class, state whether the following statements are valid: StaffMember sm1 = new Volunteer(); Hourly h1 = new Hourly(); sm1 = h1; True False

true

public class A1 { public int x; private int y; protected int z; ... } public class A2 extends A1 { protected int a; private int b; ... } public class A3 extends A2 { private int q; ... } Which of the following lists of instance data are accessible in class A2? x, z, a, b a, b z, a, b x, y, z, a, b x, y, z, a

x, z, a, b


Related study sets

Explosives and Hazardous Materials Emergencies

View Set

304 Final Exam - In Class Quizzes

View Set

Complete Set: AP World History Vocab

View Set

Internet Based Capabilities (IBC) Assessment

View Set

Series 7 top-off - Chapter 18 **copy**

View Set

Biology of Organisms: Chapter 29 PLANTS

View Set

HW2 Intro, specialization and trade

View Set

Real Estate Principles - Chapter Six; The Law of Agency

View Set