AP Computer Science A
Exercises
180 exercises. Your progress is kept in this browser only.
Using Objects and Methods
Volume of a box
Declaring variables and using them in an expression
Perimeter of a rectangle
Combining parameters in an arithmetic expression
Hours to seconds
Converting between units with multiplication
Average of three
Using double for a value that is not a whole number
Positive or not
Returning a boolean from a comparison
Area of a rectangle
Calling a method with parameters and returning a value
Even or odd
Using the remainder operator and returning a boolean
What is left over
Using the remainder operator
Last digit
Extracting a digit with the remainder operator
Tens digit
Combining integer division and remainder
Money in cents
Converting to a single unit before calculating
How many quarters
Using integer division deliberately
Minutes left over
Reading a total as whole units plus a remainder
Say hello
Printing with System.out.println and joining Strings
Say it twice
Calling System.out.println more than once
Label a value
Joining text and numbers with +
Put it in quotes
Escaping a double quote inside a String
Two columns
Using the tab escape sequence
A Windows path
Escaping a backslash
A tiny receipt
Printing several lines that build one block of output
Add a tip
Updating a variable with +=
Spend some money
Updating a variable with -=
Double it twice
Updating a variable with *=
Halve it twice
Updating with /= and seeing integer division bite
Chop off the decimals
Casting a double to an int throws away the fraction
Round to the nearest whole number
Rounding with Math.round and casting the result
Average of two whole numbers
Avoiding integer division when the answer is not whole
What percentage
Ordering an expression so integer division does not destroy it
Celsius to Fahrenheit
Arithmetic with doubles, and integer division as a trap
Milliseconds in a day
Choosing a type large enough for the result
How long is it
Calling length() on a String
First letter
Taking one character out with substring
Last letter
Using length() to find the end of a String
The middle letter
Using substring with two positions
Shout it
Calling toUpperCase
Whisper it
Calling toLowerCase
Where does it appear
Calling indexOf and understanding -1
Are they the same word
Comparing Strings with equals rather than ==
Same word, any capitals
Calling equalsIgnoreCase
Initials
Calling String methods and building a String
Distance from zero
Calling Math.abs
How far apart
Combining subtraction with Math.abs
Square root
Calling Math.sqrt
Raise to a power
Using Math.pow and casting its double result
The bigger one
Calling Math.max
The largest of three
Nesting one method call inside another
Tidy up the spaces
Calling an instance method that returns a new object
Make it a hashtag
Calling replace on a String object
Is it in there
Calling contains and returning what it gives back
Put a name together
Chaining instance methods together
Selection and Iteration
Is it bigger
Using a comparison operator as an expression
At least this big
Telling > apart from >=
Are they equal
Using == rather than =
Are they different
Using the not-equal operator
Does it divide evenly
Combining the remainder operator with a comparison
Between two numbers
Joining two comparisons with &&
Absolute value, by hand
Writing an if statement that changes what is returned
The bigger one, by hand
Returning different values from two branches
Pass or fail
Returning a String chosen by a condition
Bulk discount
Changing a value inside an if before returning it
Never below zero
Using an if to put a floor under a value
Even or odd, in words
Combining the remainder operator with a branch
Letter grade
Ordering an else-if chain from the strictest condition down
Positive, negative or zero
A three-way branch
Ticket price by age
A chain where the bands are not in one direction
Name the day
Mapping several values with a chain, including a fallback
Ice, water or steam
Handling both ends of a range before the middle
Small, medium or large
Chained bands with an exclusive upper edge
Both above zero
Joining two conditions with &&
At least one above zero
Joining two conditions with ||
Not a teenager
Negating a compound condition with !
Leap year
Combining && and || with brackets that matter
Eligible to vote
Combining a comparison with a boolean parameter
Outside the range
Choosing between || and a negated &&
Starts with a vowel
Comparing a character against several possibilities
Yes or no
Using equalsIgnoreCase inside a compound condition
The longer word
Comparing lengths and returning an object
Same first letter
Comparing two Strings with equals rather than ==
Count down
Printing inside a while loop
How many digits
Looping until a value runs out
Add up the digits
Peeling digits off with % and /
Steps down to one
Counting iterations of a loop
Reverse the digits
Building a number up while taking another apart
First power of two past a limit
Looping until a condition is met rather than a fixed count
Add up the numbers
Using iteration to accumulate a value
Sum of squares
Using the loop variable in the calculation
Count the multiples
Counting inside a for loop
Factorial
Accumulating with multiplication rather than addition
Count a letter
Walking a String one position at a time
Reverse a word
Counting downwards in a for loop
Print a times table
Printing a formatted line inside a for loop
The biggest digit
Keeping a running best-so-far
Count the vowels
Counting with a compound condition inside a loop
Add up a range
Accumulating across a range that is given, not fixed
Is it prime
Looping to disprove something, then concluding
Are they all even
Proving something holds for every item
A triangle of stars
An inner loop whose length depends on the outer one
A square of stars
An inner loop of fixed length
A multiplication grid
Building a line inside a nested loop without a trailing separator
Count the pairs that add up
A nested loop that starts where the outer one left off
Class Creation
A point on a grid
Declaring fields and a constructor that fills them
A book
A class with fields of different types
Starting from empty
A constructor that takes no arguments
Two ways to build it
Overloading a constructor
A rectangle that knows its own area
Methods that calculate from fields rather than parameters
A student record
A method that returns a judgement about a field
An elevator
Two mutators with limits at both ends
A battery
Clamping a value at both ends
A clock that wraps around
Carrying from one field into another
A shopping cart
Keeping two fields consistent with each other
A combination locker
A mutator whose effect depends on its argument matching a field
Stock on a shelf
Combining a guarded decrement with an unguarded increase
A thermostat
One range check reused by two mutators
A stopwatch
A method whose effect depends on another field
A circle
Instance methods using a constant and a field
A countdown timer
A mutator with a floor, and a method that reports state
A box of words
Building up a String field across several calls
A running average
Two fields kept in step, and guarding against dividing by zero
A loaded die
Validating against another field rather than a constant
Dollars and cents
Several methods reading one field in different ways
Distance between two points
A method that takes another object of the same class
Describing itself
A method that renders an object as text
A fraction
Two fields read together, and integer division avoided deliberately
A pet
A mutator, a test and a description on one object
A vending machine
A method that answers a question about two fields
How far through the book
A calculation that needs a cast to survive integer division
A password checker
Comparing a String field against a parameter
Moving a point
Remembering an original value so it can be restored
A playlist
Deriving one unit from another stored unit
A dimmer switch
Two mutators sharing one pair of limits
A bank account
Mutator methods that change a field
A light switch
A mutator that toggles a boolean field
A setter that refuses bad values
A mutator that validates before assigning
Keeping score
Several mutators on one field
Changing a name
A method that builds a value from a field
Numbering every object
A static field that counts instances
How many have been made
A static method reading a static field
A helper that needs no object
Static methods that depend on nothing but their arguments
When the names collide
Using this to reach a field a parameter is hiding
A value that never changes
A static final constant used by a static method
Data Collections
The last position
A search that must not stop at the first match
Binary search
Binary search on a sorted array
Is it in order
Comparing each item with the one before it
Sort it yourself
A sorting algorithm written by hand
The second largest
Tracking two best-so-far values at once
Add up a grid
A nested traversal of a 2D array
The biggest in a row
Reaching one row of a 2D array
Count matches in a grid
Counting across a nested traversal
Count the words
Text processing with split
From an array to a list
Moving between the two collection types
How many in the list
Reading an ArrayList’s size
Add up a list
Traversing an ArrayList
The first item
Using get, and guarding against an empty list
Count the big ones
Counting matches in an ArrayList
Double every number
Building a new ArrayList with add
Keep only the even numbers
Filtering into a new list rather than removing while traversing
Is it in the list
Searching an ArrayList
The longest word
Best-so-far over a list of Strings
Join the words
Building a String across a list without a trailing separator
Average of a list
Combining a traversal with a cast
How many items
Reading an array’s length
First plus last
Indexing from both ends of an array
Add up an array
Accumulating across a traversal
The largest value
Keeping a running best-so-far across an array
The smallest value
The same traversal with the comparison reversed
Count the even numbers
Counting matches during a traversal
Is it in there
Searching, and returning early
Where is it
A search that reports where, not just whether
Average of an array
Avoiding integer division after a traversal
Reverse an array
Building a new array from an old one
Count the negatives
Counting during a traversal
Are they all positive
Proving something holds for every item
Every other one
Using the index rather than the value
Swap the ends
Copying an array and then altering the copy
The spread
Two traversal results combined
The same both ways
Walking in from both ends at once
Take a value out
Filtering a list by exclusion
Count a character
Traversing a String by index
Down the diagonal
Using one counter for both dimensions
Initials from a list
Building a String from a list of Strings
