Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Q1. Which of the following choices is the correct synt ax for declaring/initializing an array of integers? 1. int[] a = new int[10]; 2. int a[10] = new int[10]; 3. []int a = [10]int; 4. int a[10]; 5. int[10] a = new int[10]; Q2. Which of the following choices is the correct synt ax for quickly declaring/initializing an array of integers to store a parti cular list of values? 1. int[] a = {17, -3, 42, 5, 9, 28}; 2. int a {17, -3, 42, 5, 9, 28}; 3. int[] a = new int[6] {17, -3, 42, 5, 9, 28}; 4. int[6] a = {17, -3, 42, 5, 9, 28}; 5. int[] a = new {17, -3, 42, 5, 9, 28} [6]; Q3. Fill in the array with the values that would be stored after the code executes: int[] data = new int[8]; data[0] = 3; data[7] = -18; data[4] = 5; data[1] = data[0]; int x = data[4]; data[4] = 6; data[x] = data[0] * data[1]; Q4. Fill in the array with the values that would be stored after the code executes: int[] list = {2, 18, 6, -4, 5, 1}; for (int i = 0; i < list.length; i++) { list[i] = list[i] + (list[i] / list[0]); } Q5. What values are stored in the array at the comment in main? Note that the incrementAll returns void, but does take an int[] parameter. public class ArrayReference { public static void main(String[] args) { int[] nums = {2, 4, -1, 3}; incrementAll(nums); // HERE! } public static void incrementAll(int[] data) { for (int i = 0; i < data.length; i++) { data[i]++; } } } Q6. Suppose that each array below were passed as a parameter to the mystery method. Fill in the boxes with the array contents after e ach method call. int[] a1 = {2, 4}; mystery(a1); int[] a2 = {1, 3, 6}; mystery(a2); int[] a3 = {7, 2, 8, 4}; mystery(a3); int[] a4 = {5, 2, 7, 2, 4}; mystery(a4); int[] a5 = {2, 4, 6, 3, 7, 9}; mystery(a5); public static void mystery(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] < a[i + 1]) { a[i] = a[i + 1]; } } } Q7. The below attempted solution t o "max" has 7 problems. Identify and fix the errors. Q8. When an array of strings is created, w hat is the default value of each element of the array? Q9. ( a) Write a declaration for an array of double values named a. Do not allo cate space for the elements of the array. (b) Write a statement that allocates 100 elements for the array described in part (a). (c) Write a statement that assigns 2000 to the last element of the array described in part (a). Q10. Suppose that a is an array. Write a declaration of another array named b w ith the same length as a. The elements of b should be integers. Q11. Suppose that a is an array of integers. Write a statem ent that com putes the average of the first and last elements in a and stores the result in a variable named average. Q12. (a) Write a declaration of an array nam ed titles that has room to store 100 strings. (b) Write an if statement that tests w hether the first element of the titles array is equal to "Java Programming". The statement should print Match if the titles match and No match if they don't match. Q13. The follow ing statem ent is supposed to print the integers from 1 to 10, but it contains an error. for (i = 1; i >= 10; i++) System.out.println(i); (a) What w ill be printed w hen the statement is executed? (b) Describe the error and show how to fix it. Q14. Write a for statement that is equivalent to the follow ing series of statements: i = 10; while (i > 0) { System.out.println(i); i--; } Q15. (a) Wh at is the last line of output produced by the follow ing loop? for (i = 99; i > 1; --i) System.out.println(i + " bottles of beer on the wall"); (b) Convert the for statement in part (a) into an equivalent while statement. Q16. The follow ing Java fragment contains a single error. Describe the error and show how to fix it. for (int i = 1; ; i++) if (i * i > 500) break; System.out.println("Result: " + i); Q17. How m any times w ill the follow ing loop print the message "Foo"? for (int i = 1; i < 20; i += 5) System.out.println("Foo"); Q18. How m any times w ill the follow ing loop print the message "Foo"? for (int i = 1; i <= 20; i *= 5) System.out.println("Foo"); Q19. Assume that a is an array containing integers. Write a for statement that m ultiplies every element in a by 10. Q20. Suppose that a is an array of integers. Write a for statement that com putes the product of all the elements in a, leaving the result in a variable named product. Q21. Wh ich one of the follow ing statements about arrays is true: (a) arrays are objects, (b) space for an array must be allocated at the time an array variable is declared, (c) finding the length of an array requir es a method call, or (d) none of the above? Q22. Each of the follow ing Java fragments contains a single error. Describe the error and show how to fix it. (a) for (int d = 2; d * d <= n; d++) if (n % d == 0) break; System.out.println("Answer: " + d); (b) for (int i = 1; i <= 10; i++); System.out.println(i + " squared is " + i * i ); (c) int sum = 0; for (int i = -1; i <= -10; --i) sum += i; Q23. Draw a representation of what the computer's memory and screen (if relevant ) looks like at the end of each of these program s: (a) public class Array-Declarations { public static void main(String [] args) { int [] w; int [] x = null; int [] y = new int[3]; int [] z = {1, 3, 5, 7, -14}; } } (b) public class Array-Assignment { public static void main(String [] args) { int [] x = new int[3]; int [] y = {3, 5, 9, 2}; x[2] = y[3]; x[0]++; y[1] += y[2] * y[0]; int [] z = x; x = y; } } (c) public class Array-Length { public static void main(String [] args) { int [] x = new int[4]; int [] y = {}; int [] z = {0}; System.out.println("x has " + x.length + " elements"); System.out.println("y has " + y.length + " elements"); System.out.println("z has " + z.length + " elements"); } } (d) (e) (f) Q24. Store10Ints.java Write a program that reads in 10 ints from the keyboard, and stores them all in an array Q25. DaysAboveAverage.java Write a program that reads in 10 temperature values (as doubles) for 10 days of weather, compute s the average temperature, an d displays the number of days that were hotter than the average. Q26. PrintMaxMin.java Read in 10 ints from the keyboard, and store them in an array. Find the maximum and minimum values in the array, and displ ay them on the screen. Q27. SwapM axMin.java Read in 10 ints from the keyboard, and store them in an array. Find the position (or index) of the maximum and minimum values in the array, and swap them (move the biggest element to the position of the smallest, and move the smallest eleme nt to the position of the biggest ). Q28. EvenEvens.java Read 10 int s from the keyboard, and store them in an array. Display "true" on the screen if there is an even number of even numbers among these 10. Otherwise, di splay " false". Q29. Sales.java Use a one-dimensional array to sol ve the following problem: A company pays its salespeople on a commission basis. The salespeople receive $200 per week plus 9% of their gross sales for that week. For example, a salesperson who grosse s $5000 in sales in a week receives $200 plus 9% of $5000, or a tot al of $650. Write an applet (using an array of counters) that determines ho w many of the salespeople earned salaries in each of the following ranges (assume that each salesperson’s salary i s trunc ated to an integer amount ): a) $200–299 b) $300–399 c) $400–499 d) $500–599 e) $600–699 f) $700–799 g) $800–899 h) $900–999 i) $1000 and over Q30. Roll36.java Write a program to simulate the rolling of t wo dice. The program should use M ath.random once to roll the first die and again to roll the second die. The sum of the t wo values should then be calculated. Each die can show an integer value from 1 to 6, so the s um of the values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums. Figure below shows the 36 possible combinations of the two dice. Your program should roll the dice 36,000 times. Use a one -dimensional array to tally the numbers of times each possible sum appears. Di splay the results in tabul ar format. Also, determine whether the tot als are reasonable (i.e., there are six ways to roll a 7, so approximately one -sixth of the roll s should be 7). The program should reset the elements of the one-dimensional array to zero be fore rolling the dice again. Sample run: