Solutions to the Practice Questions#

Data Types#

  1. Data types allow the correct amount of space to be allocated for the variable in RAM and allow the static environment to know which operators/methods are valid.

  2. Many correct answers including: String myName = "Rosanna";

  3. Many correct answers including: short daysFebLeapYear = 29;

  4. Many correct answers including: double pi = 3.1415926535897932384626433; The value of pi is not finite, even in Mathematics, hence the value is not exact.

  5. double dBigNum = Integer.MAX_VALUE 1; This value will be exact since doubles have about 15 significant digits. If the value was declared as a float, then it would not be exact.

  6. a. banana is 7
    b. jasper is 3.0 (Note: 3 is incorrect)

  7. int[] oddNums = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};

  8. double[] nums = new double[20]; The largest valid index is 19.

  9. String firstNameJavaInventor = "James";
    String lastNameJavaInventor = "Gosling";
    String nameJavaInventor = firstNameJavaInventor + " " + lastNameJavaInventor;
    System.out.println("Original inventor of Java: " + nameJavaInventor);
    
  10. StringBuilder javaInventor = new StringBuilder("James");
    javaInventor.append(" Gosling");
    System.out.println("Original inventor of Java: " + javaInventor);
    
  11. (theChar >= 'A' && theChar <= 'Z')
    
  12. In the case of my name “ROSANNA”

    (theChar == 'R' || theChar == 'O' || theChar == 'S' || theChar == 'A' || theChar == 'N')
    

Back to Questions

if Statement#

  1. a. no b. yes c. yes
    d. yes e. no f. yes
    g. yes h. yes i. yes

  1. a. 1., 2. Evaluates to true, 3., 4., 5.
    Output:
    10
    11

    b. 1., 2. Evaluates to true, 3., 4., 8.
    Output:
    10

    Memory diagrams are the same for both a) and b). Indices are not stored in memory, but are in the diagram for clarity.

Back to Questions

for-each Loop#

  1. 1., 2., 3., 4., 5., 2., 3., 4., 5., 2., 3., 4., 5., 2., 3., 4., 5., 2., 6.
    Output: 20&22&30&26&DONE

  2. n is an array of integers

  3. value is the iterator, the name used to move through the array. value will take on each item in the array and this value is set when execution returns to the top of the loop

int[] odds11To29 = {11, 13, 15, 17, 19, 21, 23, 25, 27, 29};
int answer = 0;
for (int oddNum : odds11To29){
   answer = answer + oddNum;
}
System.out.println("The sum of the odd numbers from 11 to 29 is:");
System.out.println(answer);
The sum of the odd numbers from 11 to 29 is:
200
String[] stringList = {"kiwi", "apple", "orange", "watermelon"};
for (String item : stringList){
   System.out.println(item.charAt(0));
}
k
a
o
w
String[] stringList = {"kiwi", "apple", "orange", "watermelon"};
for (String item : stringList){
   System.out.println(item.charAt(item.length() - 1));
}
i
e
e
n

Back to Questions

while Loop#

  1. 1., 2. true, 3., 4., 5., 2. true, 3., 4., 5., 2. true, 3., 4., 5., 2. true, 3., 4., 5., 2. false, 6.
    Output: 25&12&6&3&1&DONE


  2. n is the control variable for the while loop.

  3. An infinite loop.

int answer = 0;
int oddNum = 11;
while (oddNum < 30){
    answer = answer + oddNum;
    oddNum = oddNum + 2;
}
System.out.print("The sum of the odd numbers from 11 to 29 is:");
System.out.println(answer);
The sum of the odd numbers from 11 to 29 is:200
String[] stringList = {"kiwi", "apple", "orange", "watermelon"};

int index = 0;
while (index < stringList.length){
   System.out.println(stringList[index].charAt(0));
   index = index + 1;
}
k
a
o
w
String[] stringList = {"kiwi", "apple", "orange", "watermelon"};

int index = 0;
while (index < stringList.length){
   String item = stringList[index];
   System.out.println(item.charAt(item.length() - 1));
   index = index + 1;
}
i
e
e
n

Back to Questions

Methods#

  1. void

  2. public static int sumOf2(int num1, int num2){

  3. public static String embedInInts(String aString, int number){

  4. public static double secantSlope(double x1, double y1, double x2, double y2){
       return (y2 - y1) / (x2 - x1); //rise over run
    }
    
  5. double secSlopePoints = secantSlope(4, 5, 3, 12);
    

    Formal parameters: x1, y1, x2, y2 (from question 4 code)
    Actual parameters: 4, 5, 3, 12

  6. System.out.println(secSlopePoints);
    

    or if you want a specific number of decimal places, say 4, to print:

    System.out.printf("%.4f", secSlopePoints);
    
  7. /**
     * Prints aChar the given number of times to standard output.  No
     * newline is added.
     *
     * @param num - the number of times to print the char
     * @param aChar - the char to print
     */
    public static void printNumChars(int num, char aChar){
       while (num > 0){
          System.out.print(aChar);
          num = num - 1;
       }
    }//printNumChars
    
  8. /**
     * Makes a String that is num copies of aChar. 
     *
     * @param num - the number of copies of the character
     * @param aChar - the char to copy
     *
     * NOTE:  if the number is less than or equal to zero, the
     *        empty String is returned.
     */
    public static String stringNumChar(int num, char aChar){
       String answer = ""; //start empty
       while (num > 0){
          answer = answer + aChar;
          num = num - 1;
       }
       return answer;
    }//stringNumChar
    

Back to Questions



Congratulations! You are now a Java programmer!