final-exam1-solution

final-exam1-solution - CSE 143 SUMMER 2010 FINAL EXAM...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CSE 143 SUMMER 2010 FINAL EXAM SOLUTION (PART 1) 1. (12 points) Using recursion , write a method mod that takes two integers a and b as parameters and returns the remainder of a divided by b , i.e. , a % b . You may use + , - , comparison operators ( > , >= , < , <= , == , != ), and Boolean operators ( , || ). You may NOT use * , / , % , any loops ( i.e. , for or while ) or any method from the Java library. You may assume that a is greater than or equal to zero and b is greater than zero. You may find the following equality useful: a % b = (a – b) % b public static int mod( int a, int b) { if (a < b) { return a; } else { return mod(a - b, b); } } A bunch of people did everything properly except to say “return” in the recursive call. That was worth 3 points. 2. (12 points) Using recursion , write a method parity that takes a string as a parameter and returns the string "odd" if the string has an odd number of characters and "even" if the string has an even number of characters. For example, the call parity("abcdef") will return "even" , while will return "odd" . You may NOT use any loops (
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 12/17/2011.

Page1 / 4

final-exam1-solution - CSE 143 SUMMER 2010 FINAL EXAM...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online