4
Florida International University
Review
1. Which of the following statements concerning arrays are true? Select all valid answers.
(a) An array is an object.
(b) The elements of an array can either be objects or primitive data values
(c) All of the elements in an array have the same type.
(d) The array size is fixed after it is created.
2. Which of the following statements are true about the class ArrayList? Select all valid answers.
(a) The size of the list must be specified at the time of creating the list object.
(b) Array list objects can grow dynamically.
(c) The elements of an array list must all be objects.
(d) The elements of an array list can be any data type.
3. Given the following code:
class Question
{public static void main(String []arg)
{
for (String i = “a”; i < “d”; i++)
switch(i)
{
default:
System.out.println("default");
break;
case “b”:
case “a”:
System.out.print(i + “ “);
break;
}
}
}
What will happen if you attempt to compile and execute the code? Select all valid answers.
(a) The code will not compile because the default statement should come last.
(b) The code will not compile because String type cannot be used for a loop variable.
(c) The code will compile and print the following on one line: a b default
(d) The code will not compile because the case value must be integer type.
4. Which of the following statements is true about the for loop and the while loop construct. Select all valid answers.
(a) Wherever a while statement is used it can be replaced with a for statement.
(b) Wherever a for statement is used, a while statement can replace it.
(c) The while statement and the for statement produce the same result when the while statement replaces the for statement.
(d) The while statement cannot replace the for statement at any time.
5. Write what will be printed when the following program is executed.
1. class Question
2. {
3. public static void main(String []arg)
4. {
5. int i = 1, m = 3;
6. while(m < 20)
7. {
8. System.out.println(m);
9. i++;
10. m = m * i;
11. }
12. }
13. }
8. What will be displayed on the screen when the following program is executed.
1. class Question
2. {
3. public static void main(String[] arg)
4. {
5. int y = 12, x = 10;
6. while( y != 0)
7. {
8. int r = x % y;
9. x = y;
10. y = r;
11. }
12. System.out.println(x + " " + y );
13. }
14. }
9. Consider the following program segment. How many times does the body of the while statement below get executed?
int x = 3;
while (x < 9)
x = x + 2;
x++;
(a) 3 (b) 4 (c) 6 (d) 8 (e) 9
10. Show the screen outputs of the following program.
public class MyArray
{
public static void main( String [] arg)
{
int count=10;
double [] list = new double[count];
System.out.println(list.length);
for(int i=0; i<count; i ++)
list[i] = 2 * i + 1;
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + “ “);
System.out.println();
int j=2;
while( j < list.length )
{
if (list[j] % 5 != 0);
list[j] = 0;
}
for(int i=0; i<list.length; i ++ )
System.out.print(list[i]+" * ");
}
}//end class
11. Which of the following are valid array declarations?
(a) int [] drr;
(b) int err[];
(c) int arr [][];
(d) int []brr[];
(e) int [][] crr;
12. Given the following declarations, what is the correct way to get the size of the array, assuming the array has been initialized?
int array;
Select the one right answer.
(a) array[]length()
(b) array.length()
(c) array[].length
(d) array.size()
(e) array.length
13. Which of the following array declarations and initializations are not legal?
(a) int [] drr = {{10}, {20}};
(b) int err[] = { 10, 20};
(c) int arr [][] = { 10, 20 };
(d) int []brr[] = {{10}, {20}};
(e) int [] crr = { };
14. The correct way to create an array list object is:
(a) ArrayList list = new ArrayList;
(b) ArrayList = ArrayList();
(c) ArrayList() list = new ArrayList();
(d) ArrayList list = new ArrayList();
15. Given the integr value 250, the correct way to add to the array list, above, is:
(a) add(250);
(b) list.add(250);
(c) add(new Integer(250));
(d) list.add((int)250);
(e) list.add(new Integer(250));
16. Given the following pairs of statements, which pair is the correct way to retrieve the integer value from the array list, list?
(a) int i = (Integer)list.get(0);
int value = i.intValue();
(b) Integer i = list.get(0);
int value = i.intValue();
(c) Integer i = (Integer) get(0);
int value = i.intValue();
(d) Integer i = (Integer)list.get(0);
int value = i.intValue();
(e) Integer i = (Integer)list.get(0);
int value = Integer.intValue();
17. Be able to write class that accepts array and carryout simple task such as summing values, or searching for values.