Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. 1. Consider the following method:
  2.  
  3.  
  4.  
  5. public static void tryMe (int [ ] x, int size)
  6.  
  7. {
  8.  
  9. for (int i = 0; i < size; i++)
  10.  
  11. System.out.println( “ [ “ + i + “ ] = “ + x[i]);
  12.  
  13. }
  14.  
  15.  
  16.  
  17. and the declarations:
  18.  
  19.  
  20.  
  21. int [ ] list = new int [100];
  22.  
  23. int [ ] score = new int[50];
  24.  
  25. double [ ] gpa = new double [50];
  26.  
  27.  
  28.  
  29. Which of the following method calls are valid?
  30.  
  31. A. tryMe(list, 100);
  32.  
  33. valid
  34.  
  35. B. tryMe(list, 75);
  36.  
  37. valid
  38.  
  39. C. tryMe(score, 100);
  40.  
  41. Not valid
  42.  
  43. D. tryMe(score, 49);
  44.  
  45. Valid
  46.  
  47. E. tryMe(gpa, 50);
  48.  
  49.  
  50. Not Valid
  51.  
  52.  
  53. 2.
  54.  
  55. A. What does it mean to say an array index is out of bounds?
  56. The index has no value in an array; the array is not large enough to have a value there.
  57.  
  58. B. If an array index is outside the bounds, what happens when you run the program?
  59. It fails to run and you get a ArrayIndexOutOfBoundsException
  60.  
  61.  
  62.  
  63. 3. Determine whether the following array declarations are valid. If a declaration is invalid, give a correct declaration.
  64.  
  65.  
  66.  
  67. A. int [75] list;
  68. int [] list = new int[75];
  69. B. int size;
  70. double [] list = new double [size];
  71. int size = 87;
  72. double [] list = new double [size];
  73. C. int[ ] test = new int[-10];
  74. int[] test = new int[10];
  75. D. double[] sales = new double [40];
  76. valid
  77.  
  78. 4. Suppose list is an array of five elements of type int. What is stored in list after the following java code executes?
  79.  
  80.  
  81.  
  82. for (int i = 0; i < 5; i++)
  83.  
  84. {
  85.  
  86. list[ i ] = 2 * i + 5;
  87.  
  88.  
  89.  
  90. if (i % 2 == 0)
  91.  
  92. list[ i ] = list[ i ] - 3;
  93.  
  94. }
  95. 2
  96. 7
  97. 6
  98. 11
  99. 10
  100. 5. Suppose we have the following two-dimensional array:
  101.  
  102.  
  103.  
  104. int [ ] [ ] temp = { { 6, 8, 12, 9},
  105.  
  106. {17, 5, 10, 6},
  107.  
  108. {14, 13, 16, 20} };
  109.  
  110.  
  111.  
  112. Write Java statements to accomplish the following. Note: For each statement, you are asked to output a single element.
  113.  
  114.  
  115.  
  116. A. Output the contents of the first row and first column element of temp.
  117. System.out.println(temp[0][0]);
  118. B. Output the contents of the first row and last column element of temp.
  119. System.out.println(temp[0][3]);
  120. C. Output the contents of the last row and first column element of temp.
  121. System.out.println(temp[2][0]);
  122. D. Output the contents of the last row and last column element of temp.
  123. System.out.println(temp[2][3]);
  124.  
  125.  
  126.  
  127.  
  128. 6. The following program has five errors (Which could be compile or logic errors). Note that some errors involve multiple lines. For each statement that contains an error:
  129.  
  130.  
  131.  
  132. A. Statement the line number containing the error and the reason for the error.
  133.  
  134. B. Rewrite the statement so that the statement is correct.
  135.  
  136.  
  137.  
  138. 1. public class Errors
  139.  
  140. 2. {
  141.  
  142. 3. static Scanner console = new Scanner(System.in);
  143.  
  144. 4.
  145.  
  146. 5. public static void main(String[ ] args)
  147.  
  148. 6. {
  149.  
  150. 7. int[ ] myList = new[10] ; int[ ] myList = new[10] ;
  151.  
  152. 8.
  153.  
  154. 9. for (int i = 0; i <= 10; i++) for (int i = 0; i < 10; i++)
  155.  
  156. 10. myList = console.nextInt(); myList[i] = console.nextInt();
  157.  
  158. 11.
  159.  
  160. 12. for(int j = 0; j < myList.length(); j++) for(int j = 0; j < myList.length; j++)
  161.  
  162. 13. System.out.print(myList[ i ] + “ “ ); System.out.print(myList[ j ] + “ “ );
  163.  
  164. 14. System.out.println();
  165.  
  166. 15. }
  167.  
  168. 16. }
  169.  
  170.  
  171.  
  172.  
  173.  
  174. 7. What is the exact output of the following program?
  175.  
  176.  
  177.  
  178. public class List
  179.  
  180. {
  181.  
  182. public static void main(String[ ] args)
  183.  
  184. {
  185.  
  186. int[ ] alpha = new int[ 5 ];
  187.  
  188.  
  189.  
  190. alpha[ 0 ] = 5;
  191.  
  192. for (int i = 1; i < 5; i++)
  193.  
  194. {
  195.  
  196. alpha[ i ] = 5 * i + 10;
  197.  
  198. alpha[ i -1 ] = alpha [ i ] - 4;
  199.  
  200. }
  201.  
  202.  
  203.  
  204. System.out.print( “List elements: “);
  205.  
  206.  
  207.  
  208. for (int i = 0; i < 5; i++)
  209.  
  210. System.out.print(alpha[ i ] + “ “);
  211.  
  212. }
  213.  
  214. }
  215.  
  216.  
  217. List elements: 11 16 21 26 30
  218.  
  219.  
  220. 8. Write Java statements that do the following:
  221.  
  222.  
  223.  
  224. A. Declare an array alpha of 15 elements of type int.
  225. int[ ] alpha = new int[15];
  226. B. Output the value of the array alpha at index 9.
  227. System.out.println(alpha[9]);
  228. C. Set the value of the array alpha at index 8 to the sum of the elements at indices 5 and 12.
  229. alpha[8] = alpha[5] + alpha[12];
  230. D. Set the value of the array alpha at index 3 to three times the value at index 7 and then subtract by 57.
  231. alpha[3] = 3 * alpha[7] - 57;
  232. E. Output alpha so that five elements per line are printed.
  233.  
  234. int i = 0;
  235.  
  236. while (i < 15)
  237. {
  238. System.out.print(alpha[i]);
  239. i++;
  240.  
  241. if (i % 5 == 0)
  242. System.out.println();
  243. }
  244.  
  245.  
  246. 9. Write Java statements that do the following:
  247.  
  248.  
  249.  
  250. A. Declare an array alpha of 10 rows and 20 columns of type int.
  251. int[ ][ ] alpha = new int[10][20];
  252. B. Initialize each element of the array alpha to 5.
  253.  
  254. int i = 0;
  255. int j = 0;
  256.  
  257. while (j < 20)
  258. {
  259.  
  260. while (i < 10)
  261. {
  262. alpha[i][j] = 5;
  263. i++;
  264. }
  265.  
  266. i = 0;
  267. j++;
  268. }
  269.  
  270. C. Store 1 in all elements in the first row, and store 2 in the remaining rows.
  271. int i = 0;
  272.  
  273. while (i < 20)
  274. {
  275. alpha[0][i] = 1;
  276. i++;
  277. }
  278.  
  279.  
  280. i = 1;
  281. int j = 0;
  282.  
  283. while (j < 20)
  284. {
  285.  
  286. while (i < 10)
  287. {
  288. alpha[i][j] = 2;
  289. i++;
  290. }
  291.  
  292. i = 1;
  293. j++;
  294. }
  295. D. Store 5 in all elements in the first column, and the value of each remaining column is twice the value of the previous column.
  296. int i = 0;
  297.  
  298. while (i < 10)
  299. {
  300. alpha[i][0] = 5;
  301. i++;
  302. }
  303.  
  304.  
  305. i = 0;
  306. int j = 1;
  307.  
  308. while (j < 20)
  309. {
  310.  
  311. while (i < 10)
  312. {
  313. alpha[i][j] = 2 * alpha[i][j-1];
  314. i++;
  315. }
  316.  
  317. i = 0;
  318. j++;
  319. }
  320. E. Print the array alpha one row per line with a space between each element.
  321. int i = 0;
  322. int j = 0;
  323.  
  324. while (i < 10)
  325. {
  326.  
  327. while (j < 20)
  328. {
  329. System.out.print(alpha[i][j]);
  330. System.out.print(" ");
  331.  
  332. if (j == 19)
  333. System.out.println();
  334.  
  335. j++;
  336. }
  337.  
  338. j = 0;
  339. i++;
  340. }
  341.  
  342. 10. Write Java statements to define and initialize the following arrays using a single line of code.
  343.  
  344.  
  345.  
  346. A. Array of doubles called heights. Define and initialize the array of the following values: 5.2, 6.3, 5.8, 4.9, 5.2, 5.7, 6.7, 7.1, 5.10, 6.0
  347. double[ ] heights = {5.2, 6.3, 5.8, 4.9, 5.2, 5.7, 6.7, 7.1, 5.10, 6.0};
  348. B. Array of integers called weights. Define and initialize the array to the following values: 120, 125, 137, 140, 150, 180, 210
  349. int[ ] weights = {120, 125, 137, 140, 150, 180, 210};
  350. C. Array of characters called specialSymbols. Define and initialize the array to the following values: ‘%bodyamp;rsquo;, ‘#’, ‘%’, ‘&’, ‘!’, ‘^’
  351. char[ ] specialSymbols = {‘%bodyamp;rsquo;', ‘#’, ‘%’, ‘&’, ‘!’, ‘^’}
  352. char[ ] specialSymbols = {'#', '%', '&', '!', '^'};
  353. D. Array of strings called seasons. Define and initialize the array to the following values: “fall”, “winter”, “spring”, “summer”
  354. String[ ] seasons = {"fall", "winter", "spring", "summer"};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement