Page 93 Questions 1 Declare an array of

  • Slides: 7
Download presentation

Page 93 Questions 1. Declare an array of four integer elements with initial values

Page 93 Questions 1. Declare an array of four integer elements with initial values 2. Declare an array of seven character elements with initial values 3. What are the differences between these declarations? int items[]=new int [5]; int items[]={10, 20, 30, 40, 50}; Answer int x[]={4, 5, 7, 10}; char x[]={a, f, c, d, b, e, g}; All values equal 0 All indexes have value

Page 94 Questions 1. Consider the following declaration: double salary[]=new double[10]; In this declaration

Page 94 Questions 1. Consider the following declaration: double salary[]=new double[10]; In this declaration , identify the following: a. The array name. b. The array size. c. The data type of each array component d. The range of values of the array index Answer a. b. c. d. salary 10 double 0 -9

Page 94 Questions 2. Identify error(s), if any , in the following array declarations

Page 94 Questions 2. Identify error(s), if any , in the following array declarations A. int [] list=new int [10]; B. int [] numlist=new int[0. . 9]; C. scores [] double = new double[50]; 3. Determine whether the following array declerations are valid. if a decleration is invalid give a correct decleration A. int [75] list; B. int size; C. double[] list=new double[5]; D. int[] test=new int [-10]; E. double[] sales=new double [40. 5]; Answer a. b. [10] c. double[] scores=new double[50]; a. b. c. d. e. int list[]=new int [75]; int size[]=new int [5]; [10] [41]

Page 95 Practical Activity 4. Write Java statements that do the following : a.

Page 95 Practical Activity 4. Write Java statements that do the following : a. Declare an array alpha of 15 elements of type int b. Output the value of the tenth element of the array alpha c. Set the value of the fifth element of the array alpha to 35 d. Set the value of the eighth element of the array alpha to -57 e. Set the value of the ninth element of the array alpha to sum of the sixth and thirteenth elements of the array alpha f. Set the value of the fourth element of the array alpha to three times the value of the eighth element

ANSWERS a. int alpha[]=new int [15]; b. System. out. println(alpha[9]); c. alpha [4]=35; d.

ANSWERS a. int alpha[]=new int [15]; b. System. out. println(alpha[9]); c. alpha [4]=35; d. alpha [7]=-57; e. alpha [8]=alpha [5]+alpha [12]; f. alpha [3]=3*alpha [7];

PAGE 99 Question Find errors : int[]marks=new int[5]; for(i=0; i<=7; i++) { System. out.

PAGE 99 Question Find errors : int[]marks=new int[5]; for(i=0; i<=7; i++) { System. out. println([i]); } Answer