JHTP自测题_第七章_数组及动态数组(Array and ArrayList)

来源:互联网 发布:mac 终端显示路径 编辑:程序博客网 时间:2024/05/01 20:28

很基础的问题,不容得出错;出错只能说明一个问题:基础不牢固。敲打


Self-Review Exercises


7.1Fill in theblank(s) in each of the following statements:


a) Lists and tables ofvalues can be stored inArray and Collections.


b) An array is a group ofvariables(called elements or components) containing values that allhave the same type.


c) Theenhanced forstatement allows you to iterate through an array’s elements without using acounter.


d) The number used torefer to a particular array element is called the element’sindex (subscriptor position number).


e) An array that uses twoindices is referred to as a(n)two-dimensional array.


f) Use the enhancedforstatementfor(double n: numbers) to walk through doublearraynumbers.


g) Command-line argumentsare stored in anarray of String, called args by convention.


h) Use the expressionargs.lengthto receive the total number of arguments in a command line. Assume thatcommand-line arguments are stored inString[] args.


i) Given the commandjavaMyClass test, the first command-line argument istest.


j) A(n)ellipse(…)in the parameter list of a method indicates that the method can receive avariable number of arguments.


7.2Determinewhether each of the following istrueorfalse. Iffalse, explainwhy.


a) An array can storemany different types of values.False


b) An array index shouldnormally be of typefloat.False


c) An individual arrayelement that’s passed to a method and modified in that method will contain themodified value when the called method completes execution.False


d) Command-line argumentsare separated by commas.False


7.3Perform thefollowing tasks for an array calledfractions:


a) Declare a constantARRAY_SIZEthat’s initialized to 10.  final int ARRAY_SIZE=10;


b) Declare an array withARRAY_SIZEelements of typedouble, and initialize theelements to0.double[] d=new double[ARRAY_SIZE];


c) Refer to array element4.d[4]


d) Assign the value1.667to arrayelement 9. d[9]=9;


e) Assign the value3.333to arrayelement 6. d[6]=6;


f) Sum all the elementsof the array, using aforstatement. Declare the integer variablexas a controlvariable for the loop.


 


double sum=0.0;


for (intx=0;x<d.length;x++){


sum+=d[x];


}


 


7.4Perform thefollowing tasks for an array calledtable:


a) Declare and create thearray as an integer array that has three rows and three columns.


Assume that the constantARRAY_SIZEhas been declared to be3.


 


int[][] i=new int[3][3];


b) How many elements doesthe array contain? 9


c) Use aforstatement toinitialize each element of the array to the sum of its indices. Assume that theinteger variablesxandyare declared as control variables.


 


7.5Find andcorrect the error in each of the following program segments:


a)final intARRAY_SIZE=5;


ARRAY_SIZE=10;


b) Assumeint[] b =new int[10];


for(inti =0; i <=b.length; i++)


b[i] =1;


c) Assumeint[][] a ={{1,2}, {3,4}};


a[1,1] =5;



0 0
原创粉丝点击