AP COMPUTER SCIENCE A

STUDY DAY

STARTER PROGRAMMING QUESTIONS

 

 

Solutions can be found at the bottom of this page.

 

 

QUESTION 1 (FOR LOOP)

 

Write a program that uses a for-loops to output all odd numbers from 1 to 100 to screen.  All numbers should appear on a single line separated by a comma.

 

QUESTION 2 (ARRAY, FOR LOOP)

 

Consider the array:

 

int[] arr = {4, 5, 2, 9, 0, 4, 2, 8, 1, 9, 3, 0, 2, 9}

 

Write the code needed (using a for loop) to total up the array.  Output the total to screen.

 

QUESTION 2B (2D ARRAY)

 

Create a 2D array that represents the number grid below using nested for loops to initialize each element.  Then, using two more nested for loops, output your 2D array to screen.

 

1 2 3 4 5

2 3 4 5 6

3 4 5 6 7

 

QUESTION 3 (ARRAYLIST)

 

Do the following steps relating to Arraylists:

 

a)    Create an arraylist named al that holds integers. 

b)    Add the value 8 to it. 

c)     Add the value 9 to it.

d)    Add the value 3 at the beginning of it.

e)    Add the value 4 to the end of it.

f)      Add any three other values to the arraylist.

g)    Output the size of the arraylist to screen.

h)    Output the arraylist to screen.

i)       Remove the first element from the arraylist.

j)       Set the first element to 22.

k)     Output to screen the element at index 3.

l)       Output the arraylist to screen.

m)   Write a for loop that will go over the arraylist to find the maximum value in the list.  Output that max value.

 

QUESTION 4 (IMPLEMENTING CLASSES)

 

Do the following problem sourced from our course’s OOP Guide.

 

Consider the incomplete Dice class below and do the work below.

 


public
 class Dice

{

   public int sides;   //number of sides on die

   public int value;   //value of top face of die

  

   public Dice(int sides)

   {

       

   }

  

   public void roll()

   {

       

   }

  

   public String toString()

   {

       

   }

}

 

WORK

 

  1. Implement the constructor.  Because there is a conflict between the instance variable sides and the local parameter sides, you will have to make use of this.

  2. Implement the roll() method.  It should simply set the instance variable to a new value between 1 and sides inclusively. 

  3. Implement the toString() method.  It should return a String that contains something similar to:

 

     "D6 is showing a 4"

 

In the same class, add a main function.  Yes, in the same class.  Inside main, create a few Dice objects, roll them and display what their result is.

Note: To minimize confusion, we usually place main in a different class such as DiceTester.  This usually helps students separate the implementation from the using of the class.

However, because main is a static function, it doesn’t affect the objects that get created by the Dice class.  So really, we can place it inside the Dice class and it doesn’t affect anything. 

 

QUESTION 5 (INTERFACES)

 

Consider the following Area interface and the Rectangle class.  Make the Rectangle class implement the interface.

 

public interface Area

{

     public double getArea();

}

public class Rectangle

{

     private double width;

     private double height;

    

     public Rectangle(double w, double h)

     {

           width = w;

           height = h;

     }

    

     public double getWidth()

     {

           return width;

     }

    

     public double getHeight()

     {

           return height;

     }

}

 

 

SOLUTIONS

 

 

Question 1 | Question 2 | Question 2B | Question 3 | Question 4 | Question 5