Advertisement
Guest User

Kappa

a guest
Feb 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.04 KB | None | 0 0
  1. Pre-AP® Computer Science Exercises 13.03-06 Date:2/21/18
  2. Name: Aiden Montoya Period:5th
  3.  
  4.  
  5. 1. Write the Java code to declare an int array called ages.
  6. int ages[];
  7.  
  8. 2. Refer to the previous question. Write the Java code that will allocate memory for 30 ages.
  9. ages = new int[30];
  10.  
  11. 3. Each element of an array has an _____________, which is placed between brackets.
  12. index
  13. (ex: [30]; 30 is the index)
  14.  
  15. 4. What is the first array index?
  16. 0
  17.  
  18. 5. What is the last array index?
  19. 29
  20.  
  21.  
  22. 6. Assume you have created the ages array mentioned a few questions ago.
  23. Write the Java code necessary to store the age of 16 in the very first element.
  24. ages [0] = 16;
  25.  
  26. 7. Refer to the previous question.
  27. Write the Java code necessary to store the age of 18 in the very last element.
  28. Ages [29]= 18;
  29. 8. Write the Java code that will both declare and allocate memory for an array of 40 Strings called names in one single programming statement.
  30. String names [] = new String [40];
  31.  
  32. 9. Which of these is correct int grades[]; or int[] grades; ?
  33. Both are correct
  34.  
  35. 10. Write the Java code that will both declare and allocate memory for an initialized array of double values called bob. The initialized values will be 1.1, 2.2, 3.3, 4.4, and 5.5.
  36. Double bob= {1.1,2.2,3.3,4.4,5.5};
  37.  
  38. 11. Refer to the previous question. In this situation, does the size of the array need to be specified?
  39. no
  40.  
  41. 12. Look at program JavaStaticArrays06.java. If you were to add one name to the list2 array, for a total of 7 names, would the 7th name be displayed? Why or why not?
  42. A seventh name would not be displayed because the loop only is to print out 6 names.
  43.  
  44. 13. Refer to the previous question. What if instead of adding a name, you removed a couple names so now there are only 4 left in the array. What will happen when the program is executed?
  45. The program would crash
  46.  
  47. 14. What does length do when used with a Java Static Array?
  48. Length becomes a flexible variable that retrieves the number of elements in a Java Static Array.
  49.  
  50. 15. Refer to the previous question. Is length a method?
  51. no its an attribute
  52.  
  53. 16. Look at program JavaStaticArrays07.java. If you were to add one name to the name array, for a total of 5 names, would the 5th name be displayed? Why or why not?
  54. Yes because the names.length is used in the for loop, and the attribute value will change when you add names.
  55.  
  56. 17. Refer to the previous question. What if instead of adding a name, you removed a name so now there are only 3 left in the array. What will happen when the program is executed?
  57. The program will execute without an error.
  58.  
  59. 18. Can the length field be changed?
  60. No, it is a final variable
  61.  
  62. 19. Look at program JavaStaticArrays08.java. Change one statement in the program to make it store random 4-digit numbers instead of random 3-digit numbers?
  63. List[k]= Expo.random(1000,9999);
  64.  
  65. 20. Look at program JavaStaticArrays09.java. This program displays random sentences. How is that possible?
  66. There are 4 parts to the sentence each with 7 possibilities. The parts are each randomized, but always go in the same order to make a sentence.
  67.  
  68.  
  69.  
  70. 21. Whether you have an array of integers, an array of real numbers, or an array of strings, what data type will the index always be?
  71. An integer value
  72.  
  73. 22. What error do you get if you use an index value that is too large?
  74. ArrayIndexOutOfBoundsException
  75.  
  76. 23. What is displayed if you put an object, like an array, in a println statement?
  77. You will get the memory address
  78.  
  79. 24. Program design includes two related concepts. What are they?
  80. Program design includes the business of abstraction and, the process of utilizing program features without any concern about the implementation of such features, and the process of implementing such features based on certain specifications.
  81.  
  82.  
  83.  
  84. 25. Array processing includes 4 things. List them.
  85. Element display, assigning values, sorting elements, and searching elements
  86.  
  87.  
  88. 26. Method toString, like all Arrays methods, is_____________, which means that you need to use the Arrays identifier.
  89. static
  90.  
  91.  
  92. 27. How does the toString method display elements of an Arrays object?
  93. The toString method converts the array to a string that can be printed automatically adding commas, spaces and brackets.
  94.  
  95.  
  96. 28. What is the biggest advantage of assigning data to an array using an initialize list?
  97. The initializer list can assign different values to every array element location.
  98.  
  99.  
  100. 29. What method allows you to assign the same value to every element of an Arrays object?
  101. The fill method
  102.  
  103. 30. Why is sorting important?
  104. Because there is such a need to search data
  105.  
  106. 31. Look at program ArraysMethods03.java and its output. The last line of output is supposed to show animals sorted alphabetically. Why is “aardvark” after “HARE”?
  107. Capital letters are have a smaller int value in the language than lowercase values.
  108.  
  109. 32. In what order does the Arrays method sort arrange the data?
  110. The Arrays method sort arranges the data in ascending order.
  111.  
  112. 33. If names is an array of Strings, and “Brenda” is one of the names somewhere in the array, what would be the output of this statement:
  113. System.out.println(Arrays.binarySearch(names, “Brenda”));
  114. The index of Brenda which would be an integer
  115.  
  116. 34. If names is an array of Strings, and “Jessica” is the first name in the array, what would be the output of this statement:
  117. System.out.println(Arrays.binarySearch(names, “Jessica”));
  118. 0
  119.  
  120. 35. If names is an array of Strings, and “Diana” is NOT one of the names in the array, what would be the output of this statement:
  121. System.out.println(Arrays.binarySearch(names, “Diana”));
  122. a negative number
  123.  
  124. 36. Look at program ArraysMethods05.java and its output. Why does the program not properly display the index values of the numbers being searched for?
  125. The list has not been sorted
  126.  
  127. 37. Look at how we have been calling the Arrays methods in this chapter.
  128. Are these class methods or object methods?
  129. The arrays methods are class methods
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement