Free Response  Self Divisor B¶
The following is part b of a free response question from 2007. It was question 1 on the exam. You can see all the free response questions from past exams at https://apstudent.collegeboard.org/apcourse/apcomputersciencea/exampractice.
Question 1. A positive integer is called a “selfdivisor” if every decimal digit of the number is a divisor of the number, that is, the number is evenly divisible by each and every one of its digits. For example, the number 128 is a selfdivisor because it is evenly divisible by 1, 2, and 8. However, 26 is not a selfdivisor because it is not evenly divisible by the digit 6. Note that 0 is not considered to be a divisor of any number, so any number containing a 0 digit is NOT a selfdivisor. There are infinitely many selfdivisors.
Part b. Write method firstNumSelfDivisors, which takes two positive integers as parameters, representing a start value and a number of values. Method firstNumSelfDivisors returns an array of size num that contains the first num selfdivisors that are greater than or equal to start. For example, the call firstNumSelfDivisors(10, 3) should return an array containing the values 11, 12, and 15, because the first three selfdivisors that are greater than or equal to 10 are 11, 12, and 15. Be sure to use the method isSelfDivisor in your answer which we wrote in an earlier section.
How to solve this problem¶
The first thing to do is try to solve the example by hand. The question tells us to return an array of size num so we need to create an array of that size. We need to loop as long as we haven’t found 3 self divisors and try the current value. If the current value is a selfdivisor then we add it to the array. When we have found 3 self divisors then return the array. We will need to keep track of the number of self divisors that we have found. We would try 10 (false), 11 (true so add to the array), 12 (true so add to the array), 13 (false), 14 (false), 15 (true so add to the array and return the array since we found 3).

7121: Which loop should you use to solve this problem?
 (A) for
 Use a for loop when you know how many times a loop needs to execute. Do you know that here?
 (B) for each
 Use a for each loop when you want to loop through all values in a collection. Do we have a collection here?
 (C) while
 Use a while loop when you don't know how many times a loop needs to execute.

7122: Which of the following correctly declares and creates the array to return?
 (A) int[] retArray = new int[3];
 Don't just use the size for the array from the example. The question says to return an array of size num and num could be anything.
 (B) retArray = new int[num];
 Don't forget to declare your variables.
 (C) int retArray = new int[num];
 Don't forget that it is an array.
 (D) int[] retArray = new int[num];
 This declares an array of ints called retArray and creates it with a size of num.
 (E) int[] retArray;
 This declares the array, but doesn't create it.
Try to write the code for firstNumSelfDivisors. Run the main to check your answer. It should print 11, 12, and 15.
Video  One way to code the solution¶
There are many possible solutions to this problem. The video below shows one solution.