Unit 4: Classes and Objects Exercises AP CS A 1. What is boolean a = 5 < 7; System.out.println( a ); 2. What does this display? boolean b = false; for ( int n = 1; n < 4; n++ ){ b =!b; System.out.print( b + " " ); 3. List all the values of b and c that will cause a to be true. boolean a, b, c; // b and c are assigned values a = (b && c) (!b &&!c); 4. What is String s = "ok go!"; // a space between words System.out.println( s.length() ); 5. What is String s1 = "apple"; s1 = s1.substring( 2 ); System.out.println( s1 ); 6. What is String s1 = "apple"; s1.substring( 2 ); System.out.println( s1 ); 7. What is String s = "soda"; for ( int n = 0; n <= 2; n++ ) System.out.print( s.substring( n, n + 2 )); 8. What is String s = "ape"; for ( int n = 2; n >= 0; n-- ) System.out.print( s.substring( n ) ); 9. What is String s = "doorway".substring( 3 ); System.out.println( s ); 10. If the user enters Karen, what is 11. If the user enters Matthew, what is Scanner scan = new Scanner( System.in ); for ( int i=0; i < nm.length(); i += 2 ){ String s = nm.substring( i, i+1 ); System.out.print( s ); 12. What is the return type of the substring method used in problem 9? 13. How many parameters does the length method have? 1
14. What is a) hop, hop b) HOP, hop 15. What is a) Tree, TREE b) TREE, TREE String s4 = "HOP"; String s5 = s4.tolowercase(); System.out.println( s4 + ", " + s5 ); String s8 = "Tree"; String s9 = s8.touppercase(); s8.touppercase(); System.out.println( s8 + ", " + s9 ); 16. If the user enters JOE, what is Scanner scan = new Scanner( System.in ); 17. If the user enters joe, what is Note. this is a better version of #16 if you do not care whether the input string is upper or lower case. Not good for passwords. if ( nm.equals( "Joe" ) nm.equals( "joe" ) ) System.out.println( "Hi Joe" ); else System.out.println( "You're not Joe" ); Scanner scan = new Scanner( System.in ); String nm_caps = nm.touppercase(); if ( nm_caps.equals( "JOE" ) ) System.out.println( "Hi Joe" ); else System.out.println( "You're not Joe" ); 18. How many parameters does the touppercase method have? 19. What is the return type of the touppercase method? 20. How many parameters does the equals method have? 21. What is the return type of the equals method? 22. What is System.out.println( "animal".indexof( "a" ) ); 23. What is System.out.println( "animal".indexof( "a", 1 ) ); 24. What is System.out.println( "animal".indexof( "A" ) ); 25. What is String s = "Havana"; System.out.println( s.indexof( "a", 3 ) ); 26. What is String s = "floor"; for ( int n = 0; n < s.length(); n++ ) System.out.print( s.indexof("o", n ) + " "); 27. What is String s = "The theater is the best."; System.out.println( s.indexof( "the") ); 28. What is String s1 = ""; int n = s1.length(); System.out.println( n ); 2
29. If str is never, what is 30. If str is else where, what is 31. If str is Elsie what is int count = 0; int index = str.indexof( "e" ); while ( index!= -1 ){ count++; index = str.indexof( "e", index+1 ); System.out.println( count ); 32. Select the TRUE statement a) This does not compile. b) This displays A c) This displays "A" 33. Select the TRUE statement a) This does not compile. b) This displays B c) This displays "B" 34. What does this display? Be sure to get the line breaks correct. String x = "\"A\""; System.out.print( x ); String y = ""B""; System.out.print( y ); String z = "\\\n\\\\\nh"; System.out.print( z ); System.out.print( "W" ); 35. The first line of the Arg class constructor is: a) public Arg() b) public void Arg( int a, int b ); c) public Arg( int a, int b ) 36. The first line of the avast method is: a) public avast() b) public void avast() c) public void avast( int x ) 37. The first line of the parrot method is: a) public void parrot ( double x ) b) public double parrot () c) public void parrot ( int x ) d) public int parrot ( ) The code below compiles and runs. public class PirateRunner { Arg g = new Arg( 5, 6 ); g.avast(); g.parrot( 4.78 ); int x = g.ayematey(); System.out.println( x ); 38. The first line of the ayematey method is: a) public int ayematey( double x ) b) public int ayematey() c) public int ayematey( int x ) d) public void ayematey() 3
39. Write what is displayed in the space provided. public class Runner { Thing w = new Thing(); w.set( 3 ); int num = w.get(); System.out.println( num ); Thing joe= new Thing( 12 ); joe.set( -5 ); num = joe.get(); System.out.println( num ); public class Thing{ private int x; public Thing() { x = 5; public Thing( int n) { x = n; public int get() { return x; public void set( int h) { x = x + h; 40. Write what is displayed in the space provided. public class Bunny { Rumpus roo = new Rumpus( 3, 7 ); int n = roo.clang( 5 ); System.out.println( n ); roo.roar(); n = roo.get(); System.out.println( n ); 41. In the Rumpus class, name all the: parameters: instance variables : local variables : public class Rumpus{ private int a, b; public Rumpus ( int n1, int n2 ) { a = n1; b = n2; public int clang( int k ) { int n = a + b + k; return n; public void roar() { int c = a; a = b; b = c; public int get(){ return a; 4
42. Assuming the code compiles, select the TRUE statement(s). This is a method. This is a constructor. The name of the class is Superman. There is not enough information to determine the name of the class. public Superman( int x, int y ) { double w = x + y; v = w; 43. Name the following (if none, write none ) The parameter(s). The instance variables(s). The local variable(s). 44. The Apple class has at least how many different constructors? 45. What is the most likely return type of the spit method? 46. What is the most likely return type of the eat method?. 47. What is the most likely return type of the mmm method? // code inside the main method Apple core = new Apple( 8 ); core.spit(); Apple sauce = new Apple(); int x = sauce.eat( 71 ); System.out.println(core.mmm()); 48. The Banana class has at least how many different constructors? 49. What is the return type of the remove method? 50. Write the header for the eat method in the space below: public 51. Write the header for the add method in the space below: // code inside the main method Banana peel = new Banana(); Banana split = new Banana(); if ( peel.remove() ) peel.eat( 32 ); split.add( peel ); public This one is a little tricky. 5