AP CS Unit 7: Interfaces Exercises 1. Select the TRUE statement(s). a) This code will not compile because a method cannot specify an interface as a parameter. public class Testing { public static void main( String [] args ) { Beans b = new Beans( 12 ); methoda( b ); b) This code will compile if the Beans class implements the Commodity interface. c) The keyword static is not required for methoda. d) This code will compile as long as the Beans class has a getprice method. The Beans class does not have to define a setprice method. public static void methoda( Commodity c ) { System.out.println( c.getprice() ); // separate file public interface Commodity { public double getprice(); public void setprice( double c ); 2. Select the TRUE statement(s). a) Two must be the name of an interface. b) Both methods must be required by the interface. c) At least one of the methods is required by the interface. d) It is possible that neither methoda nor methodb are required by the interface. public class One implements Two { public void methoda(){ // implementation code public void methodb(){ // implementation code 3. Select the TRUE statement(s). If this line compiles, then a) ABC may refer to an interface and XYZ a class that implements that interface. b) XYZ may refer to an interface and ABC a class that implements that interface. c) if ABC is an interface, methody must be a method defined by that interface. d) if ABC is a class, methody must be a method defined by that class. e) whatever ABC is, XYZ must be a class that has, or inherits, a methody. ABC x = new XYZ(); x.methody(); 4. All methods in an interface must be public. True False 5. If a class implements an interface, then it must define at least one of the methods listed by the interface. True False 6. An interface may include implementing code. True False 1
7. What is displayed? List<String> x = new ArrayList<String>(); x.add( "grape" ); x.add( 0, "lime" ); x.add( "peach" ); System.out.println( s ); 8. What is displayed? List <String> x = new ArrayList<String>(); x.add( "red" ); x.add( "green" ); x.set( 0, "blue" ); x.add( 0, "yellow" ); x.remove( 1 ); 9. What is displayed? (This is actually a lot trickier than it looks. Remember that when you remove an object, you change the size of the list and the indices of every element that follows.) 10. Gridworld has a Location class that represents a specific location (row and column). What is displayed? System.out.println( s ); List <String> x = new ArrayList<String>(); x.add( "0" ); x.add( "1" ); x.add( "1" ); x.add( "0" ); if ( s.equals( "1" ) ) x.remove( n ); System.out.println( x.get( n ) ); List <Location> list = new ArrayList<Location>(); list.add( new Location( 5, 7 ) ); list.add( new Location( 3, 3 ) ); list.add( 0, new Location( 5, 6 ) ); int index = 0; while ( index < list.size() ) { Location loc = list.get(index); if ( loc.getrow() == 5 ) list.remove( index ); else index++; System.out.println( list.size() ); 2
11. What is displayed? List<Integer> list = new ArrayList<Integer>(); list.add( 13 ); // java autoboxes each int; it list.add( 45 ); // automatically creates an Integer list.add( 0, 8 ); // object to contain each int System.out.println( list.set( 1, -6 ) ); for ( int n = list.size() - 1; n >= 0; n-- ) System.out.print( list.remove(n) + ", " ); System.out.println(); System.out.println( list.size() ); 12. What is displayed? ArrayList<Integer> a = new ArrayList<Integer>(); for ( int n = 3; n < 20; n = 2*n ) a.add( n ); for ( Integer i : a ) System.out.print( i + " "); 13. What is displayed? import java.util.*; public class Runner { public static void main(string[] args) { ArrayList<String> a = new ArrayList<String>(); a.add( "G" ); a.add( "E" ); a.add( "T" ); change( a ); for ( String s : a ) System.out.print( s + " "); System.out.println(); change( a ); for ( String s : a ) System.out.print( s + " "); public static void change( List<String> x ){ String str = x.remove(0); x.add( str ); 14. Write the correct code so that the boat s move method can be called. Floatable x = new Boat(); x.move(); // there s an error here /* Boat has a move method no arguments and returns a void */ 3
15. Select the FALSE statement. a) The first statement creates an array object. b) Radicals must be an Interface. c) The first statement does not create any Radical objects. d) Radicals may be an abstract class. Radicals [] c = new Radicals[2]; c[0] = new Joe(); c[1] = new Moe(); 16. Select the FALSE statement. a) Every method in an abstract class is an abstract method. b) Every class inherits methods from the Object class. c) A class method can be called from a client without creating an object of that class. d) A private method in a superclass can not be invoked by a subclass. 17. Select the TRUE statement. a) PDQ must be a subclass of XYZ. b) XYZ must be an interface. c) PDQ cannot be an abstract class. d) XYZ must be an abstract class. /* Assume the code compiles and runs. */ XYZ betty = new PDQ(); 18. Select the FALSE statement. a) PDQ cannot be an interface. b) XYZ might be a subclass of PDQ. c) XYZ might be an interface. d) XYZ might be an abstract class. 19. A list of a class s public methods is called the class s. 20. Write an interface named Floatable that has one method: boolean floats(); 21. Assume that you have a Boat class that implements Floatable. In the space below, create an array of 10 Floatable objects. Fill the array with 10 Boats (assume that its constructor has no formal parameters). 4
22. The code will work if a) guess refers to an object of the Late class b) guess refers to an Interface that What implements. c) guess refers to an object that is a subclass of What d) all of the above. // client code, guess defined elsewhere Late bob = new Late(); bob.tired( guess ); // part of the code for the Late class public void tired( What w ) { //implementation code 23. Will this class compile? YES NO 24. What is printed as a result of executing the code segment? 25. What is printed as a result of executing the code segment? public abstract class Moon { private String color; public Moon(String c) { color = c; public abstract void dothis(); ArrayList<String> list = new ArrayList<String>(); list.add(0, "P"); list.add(0, "R"); list.add(0, "S"); list.set(1, T ); System.out.println(list); ArrayList<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); list.add("c"); list.remove(2); list.add(2, "E"); list.add("f"); System.out.println(list); 26. Assume that ArrayList letters contains the following values: ["0", "0", "4", "2", "5", "0", "3", "0"] What will ArrayList letters contain as a result of executing question3()? a) ["0", "0", "4", "2", "5", "0", "3", "0"] b) ["4", "2", "5", "3" ] c) ["0", "0", "0", "0", "4", "2", "5", "3"] d) ["3", "5", "2", "4", "0", "0", "0", "0"] e) ["0", "4", "2", "5", "3"] private ArrayList<String> letters; public void question3() { int k = 0; String ans = 0 ; while (k < letters.size() ) { if (letters.get(k).equals(ans) ) letters.remove(k); k++; 5