Unit 4: Classes and Objects Exercises

Similar documents
Introduction to Java Exercises for Packet 4: Classes and Objects

Unit 4: Classes and Objects Notes

Introduction to Computer Science Unit 2. Exercises

1 Short Answer (15 Points Each)

Unit 5: More on Classes/Objects Exercises

CS 101 Exam 1 Spring 200 Id Name

NATIONAL UNIVERSITY OF SINGAPORE

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

AP Computer Science A

AP CS Unit 3: Control Structures Notes

AP CS Unit 7: Arrays Exercises

Practice Midterm 1. Problem Points Score TOTAL 50

Practice Midterm 1 Answer Key

WES-CS GROUP MEETING #9

CS111: PROGRAMMING LANGUAGE II

Unit 10: Sorting/Searching/Recursion

1 Short Answer (10 Points Each)

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CSC 1051 Data Structures and Algorithms I

AP Computer Science A Unit 7. Notes on Arrays

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

CS 1301 Ch 8, Part A

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

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

Unit 10: Sorting/Searching/Recursion

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

AP Computer Science Unit 1. Programs

CS Week 5. Jim Williams, PhD

CSE 8A Lecture 12. Reading for next class: : Sounds! Today s topics: Logical Operators:! && Start PSA 6: Chromakey!

CS141 Programming Assignment #6

PROGRAMMING FUNDAMENTALS

AP CS Unit 4: Classes and Objects Programs

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

Tutorial # 4. Q1. Evaluate the logical (Boolean) expression in the following exercise

CSE 143 SAMPLE MIDTERM

Sierpinski Valentine. q Our journey of introducing conditionals

Installing Java. Tradition. DP Computer Science Unit 4.3: Intro to programming 1/17/2018. Software & websites

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

Final Exam CS 152, Computer Programming Fundamentals December 9, 2016

CS 112 Introduction to Programming

Sierpinski Valentine.

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

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Problem Grade Total

Fall 2002 Page 1 of 9 READ THIS NOW!

King Saud University College of Computer and Information Sciences Computer Science Department

Full file at

CSE 11 Midterm Fall 2008

CS 200 Using Objects. Jim Williams, PhD

Unit 5: More on Classes/Objects Notes

Selection Statements and operators

Introduction to Computer Science Unit 5. Programs: Strings

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET

AP COMPUTER SCIENCE A

Intro to Strings. Lecture 7 CGS 3416 Spring February 13, Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, / 16

COE 212 Engineering Programming. Welcome to Exam I Tuesday November 11, 2014

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

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

CS 101 Fall 2005 Midterm 2 Name: ID:

CS163/164 Final Exam Study Session

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

AP CS Unit 7: Interfaces. Programs

Strings in Java String Methods. The only operator that can be applied to String objects is concatenation (+) for combining one or more strings.

Creating Strings. String Length

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Final Exam Practice. Partial credit will be awarded.

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Netbeans tutorial:

CS 1083 Introduction to Programming I for Computer Scientists Sample Final Questions

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

STRINGS AND STRINGBUILDERS. Spring 2019

Coding Standards for Java

Algorithms and Conditionals

AP CS Unit 6: Inheritance Notes

Midterm Examination (MTA)

Chapter 6 Lab Classes and Objects

Introduction to Objects. James Brucker

Introduction to Programming (CS112): Sample

Chapter 6 Lab Classes and Objects

COE 211/COE 212 Computer/Engineering Programming. Welcome to Exam II Thursday December 20, 2012

DM550 Introduction to Programming part 2. Jan Baumbach.

Introduction to Computer Science Exercises for Unit 4A: Classes and Objects 1. This compiles and runs. What is displayed?

COE 212 Engineering Programming. Welcome to Exam II Thursday April 21, Instructors: Dr. Salim Haddad Dr. Joe Tekli Dr. Wissam F.

Unit 10: Sorting/Searching/Recursion

CS 180. Recitation 8 / {30, 31} / 2007

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 2009

AP Computer Science A Unit 2. Exercises

CS212 Midterm. 1. Read the following code fragments and answer the questions.

CS141 Programming Assignment #8

Supplementary Test 1

Slide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 Advance mode: Auto

Place your name tag here

1. An operation in which an overall value is computed incrementally, often using a loop.

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

2.8. Decision Making: Equality and Relational Operators

Selected Questions from by Nageshwara Rao

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

Programming with Java

CS 112 Introduction to Programming

Transcription:

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