AP CS Unit 7: Interfaces Exercises 1. Select the TRUE statement(s).

Similar documents
AP CS Unit 7: Interfaces Exercises Assume all code compiles unless otherwise suggested.

AP CS Unit 7: Interfaces. Programs

CS 200 Objects and ArrayList Jim Williams, PhD

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

AP CS Unit 8: Inheritance Exercises

AP CS Unit 6: Inheritance Exercises

CMSC 202. Containers

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Constructor. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Unit 4: Classes and Objects Notes

AP CS Unit 6: Inheritance Notes

CS 314 Midterm 1 Spring 2014

Binghamton University. CS-140 Fall Chapter 7.7. Lists. Java Library Data Structures

CMSC 202H. Containers and Iterators

Set<Integer> s = new TreeSet<Integer>(); s.add( 7 ); s.add( 7 ); System.out.println( s.size() );

public static<e> List<E> removeoccurrences(list<e> origlist, E remove) {

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

1. Write the output generated by the following code. (8pts) 2. Write the output generated by the following code (8pts)

CS 314 Exam 1 Spring 2017

Class, Variable, Constructor, Object, Method Questions

Slides are adapted from the originals available at

Inheritance (Part 2) Notes Chapter 6

Java Bytecode (binary file)

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

1.00 Tutorial 3. Methods, Classes Arrays & ArrayLists. September 26 & 27, 2005

Timing ListOperations

CS Week 14. Jim Williams, PhD

Language Features. 1. The primitive types int, double, and boolean are part of the AP

CSE 11 Midterm Fall 2008

CSE 143 Lecture 14. Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4

PROGRAMMING FUNDAMENTALS

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism

Unit 5: More on Classes/Objects Notes

1- Differentiate between extends and implements keywords in java? 2- What is wrong with this code:

AP Computer Science A Unit 7. Notes on Arrays

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Do not start the test until instructed to do so!

INTRODUCTION TO SOFTWARE SYSTEMS (COMP1110/COMP1140/COMP1510/COMP6710)

COURSE 4 PROGRAMMING III OOP. JAVA LANGUAGE

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers-Weeda Sjaak Smetsers Ana Tanase

CSCI 355 Lab #2 Spring 2007

QUIZ 2 Introduction to Computer Science (COMP 250) Mon. March 2, 2009 Professor Michael Langer

APCS Semester #1 Final Exam Practice Problems

Lesson 26: ArrayList (W08D1)

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

Unit 10: Sorting/Searching/Recursion

First Name Last Name ID#

1. ArrayList and Iterator in Java

public static void main(string args[]){ arraylist(); recursion();

Chapter 11 Inheritance and Polymorphism

Ans: Store s as an expandable array of chars. (Double its size whenever we run out of space.) Cast the final array to a String.

CS 307 Midterm 2 Spring 2008

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

CS 302 Week 9. Jim Williams

CS108, Stanford Handout #8. Java Generics

Java Identifiers, Data Types & Variables

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

University of Palestine. Mid Exam Total Grade: 100

I. True/False: (2 points each)

CSCI 355 LAB #2 Spring 2004

Which code fragment must be inserted at line 6 to enable the code to compile?

AP Computer Science Midterm Review Part 1

Exam Part 3. Q.1) What is the output of the following code?

01. Which of the following statement describes dynamic resizing as is applies to the ArrayList class?

I. True/False: (2 points each)

CS 113 PRACTICE FINAL

Name Section Number. CS210 Exam #4 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

CS 314 Exam 1 Fall 2017

Unit 4: Classes and Objects Exercises

COMP110 MT2 Study Guide

Back public class HelloWorld { public static void main ( String arg[] ) { Front Basic Setup. Java Quick Sheet. ~ 35 Flashcards. 1 AP CS - Rodriguez

Rules and syntax for inheritance. The boring stuff

CS111: PROGRAMMING LANGUAGE II

Array Based Lists. Collections

CMSC 132: Object-Oriented Programming II

CS 106A, Lecture 19 ArrayLists

CSE 143 Lecture 4. Preconditions

Advanced Topics in Java and on Security in Systems Effective Generics. Eirik Eltvik 26 th of February 2007

Use the scantron sheet to enter the answer to questions (pages 1-6)

CS165 Practice Final Exam

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

CSE 143 Lecture 3. More ArrayList; object-oriented programming. reading: 10.1;

Inheritance and Interfaces


After the code has executed, what is the capacity of list? What is its size? c

Object- Oriented Analysis, Design and Programming

The Java language has a wide variety of modifiers, including the following:

CS141 Programming Assignment #6

Part 3: GridWorld Classes and Interfaces

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Final Exam Practice. Partial credit will be awarded.

1) Consider the following code segment, applied to list, an ArrayList of Integer values.

CS1083 Week 2: Arrays, ArrayList

Why Use Generics? Generic types Generic methods Bounded type parameters Generics, Inheritance, and Subtypes Wildcard types

All answers will be posted on web site, and most will be reviewed in class.

1.00 Lecture 13. Inheritance

Transcription:

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