Check your Arrays knowledge in Java






READ the directions and answe r the followi ng question s below. B e sure to sho w your wor k wherever necessary for PARTIAL CREDIT. This test will be graded out of 100 points.
True/ alse Questions (2 points e ch):
Decide whether he following state                             ents are True or False.

1. A line ar search is better for a n unsorted array
____True ______
2. Similar to Arrays, ArrayLists can hold p rimitive an d reference types
______False____
3. The length of an array can d ynamically change

__True________

4. Assum ing int[] arr = {1, 2, 3} and int[ ] arr2 = {1, 2, 3}, determine the output for the f ollowing line: arr == arr2

____False______

5.   When an array of objects is c reated, the objects themselves ar e also created

____True______
6.   The fo llowing loop will go through every element i n an array c alled arr:

for(int i = array.length –  1; i > 0; i--)


ouble is
_____false_____
7. An array of type
reference type


__false________
8. The fo llowing pr
gram will
not run:


___false_______








Multiple Choice Questio s (3 poin ts each):

Select the best re sponse fo r the follo wing question/stat ements. Be sure to circle your answer and write the let ter in the space provided.


9. Deter mine the values in the following a rray

__C________


in t[] arra y =

new int[5];


a) {0, 1, 2, 3, 4}

c) “0”

b) 0

d) n ull


10.   In w hich of the f ollowing situations wo uld an Arr ayList b e more useful than an Array?

_____C_____

a) Storin g primitive data

c) Array must be able to chang e size

b) Storing objects

d) Sorting data

11.     Sup pose an Ar ayList named list stored St ring objects. Which expression g ives the leng th of the 3rd element l ist?

_______B___


a) list[2].length()

c) (String)(list[3].length())

b) l ist.get( d) l ist.get(

3).leng

2).leng

h()

h()

12. What is the value of count after the fo llowing co de has been executed?                       ____A______















a) 2














b) 1














c) 0














d) 3


13. After

the following code is

executed, w hat are the values in


array2?

____C___













a) {4, 3, 0, 0}                           b) {4, 1, 3, 0}                                      c) {2, 4, 3, 0}                                d) {2, 4, 1, 3}

14. Whic h of the following stat ements is a valid conclusion. Assu me that variable b is an array of k integers nd that the following is true:

b[0 ] != b[k ] for al l k such such that 1 <= k


a) The value in b[0] does not occur anyw here else in

the array

___D_______


b) Array b is sorted
c) Array b is not sor ted
d) Array b contains no duplica tes


15. Whic h sorting algorithm uses two sub arrays and only passes through t he array once?

___B_______

a) Selection

b) M

erge


c) Insertion

d) B inary

16. Determine the o utput                                                                                                                           ___D_______













a) 5
b) 5.5
c) 10
d) 5.0
Open‐ Ended Questions (6 points each):



17.   Assume the arra y arr contains the following integers {34, 23, 67, 89, 12}. Write the values of the followi ng expressi ons:

(a) arr 7%5]                                                                                        (b) arr[2] + arr[3]  





(c) arr 2+3]                                                                                          (d) arr[arr[1]/6]


A = 67
B= 156
C = java.lang.ArrayIndexOutOfBoundsException
D=  89


18. Determine the contents of t he two‐dim ensional a rray create d by the following code












ANS: Array bound exception
19.   Use nested loops to create shown below.













a two‐dime nsional arr ay named array with the content s


1
1
1
1











2
4
8
16






3
9
27
81






4
16
64
256






public class Dog {
                public static void main(String[] args) {

                                int[][] values = { { 1, 1, 1,1 }, { 2, 4, 8,16 }, { 3, 9, 24,81 }, { 4, 16, 64,256 } };

                                for (int row = 0; row < 4; row++) {
                                                for (int col = 0; col < 4; col++) {
                                                                System.out.print(values[row][col] + " ");
                                                }

                                                System.out.println();
                                }
                }
}


Use the resource class below to answer questions 20‐24



























20.   (a) Create an ar ray named people of type Pers n of length 2


public class Person {
                public static void main(String[] args) {
                                Person people[]=new Person[2];
                }
}


(b) C reate an A rayList named list of type P erson




public class Person {
                public static void main(String[] args) {
                                Person people[]=new Person[2];
                                ArrayList<Person> list=new ArrayList<Person>();
                }
}

21.   (a) W hat is the c urrent valu e of the elements in th e people?



[null, null]

(b)       hat is the current val e of the ele ments in th e list?

[]




22. (a) Set the 2nd el ement in people as a Person n amed Ricky who is 17


                                Person people[]=new Person[2];
                                people[1].name="Rickey";
                                people[1].age=17;

(b) O utput the age of 2nd el ement in p ople


                Person people[]=new Person[2];
                                people[1].name="Rickey";
                                people[1].age=17;
                                people[0].name="Naresh";
                                people[0].age=20;
                        System.out.println(people[1].name);

23. (a) Add the elements of type Person to list with the criteria shown


Fred
22

Austin
12

                                Person p1=new Person("Fred",22);
                                Person p2=new Person("Austin",12);
                                               
                                ArrayList<Person> list = new ArrayList<Person>();

                                list.add(p1);
                                list.add(p2);

(b)    Output the name of 1st element in list

System.out.println("Names : " + list.get(1).getName()+ " Age : " + list.get(1).getAge());


24.   Use an enhanced for loop to print the names and ages of each Person in list



package com.pratice;

import java.util.ArrayList;
import java.util.Arrays;

public class Person {
                String name;
                int age;

                public Person(String name, int age) {
                                this.name = name;
                                this.age = age;

                }

                public String getName() {
                                return name;
                }

                public void setName(String name) {
                                this.name = name;
                }

                public int getAge() {
                                return age;
                }

                public void setAge(int age) {
                                this.age = age;
                }

                public static void main(String[] args) {
                                Person people[]=new Person[2];
                                people[1].name="Rickey";
                                people[1].age=17;
                                people[0].name="Naresh";
                                people[0].age=20;

                               
                                Person p1=new Person("Fred",22);
                                Person p2=new Person("Austin",12);
                                               
                                ArrayList<Person> list = new ArrayList<Person>();

                                list.add(p1);
                                list.add(p2);
                                for(int i=0;i<list.size();i++){
                                System.out.println("Names : " + list.get(i).getName()+ " Age : " + list.get(i).getAge());
                                }
                }
}







25.   Show contents of the array after each pass of the outer loop in the (ascending)

selection sort algorithm
15  42     72           25           31           35




package com.pratice;

import java.util.ArrayList;
import java.util.Arrays;


class SelectionSort {
                void sort(int arr[]) {
                                int n = arr.length;

                                                for (int i = 0; i < n - 1; i++) {
                                                int min_idx = i;
                                                for (int j = i + 1; j < n; j++)
                                                                if (arr[j] < arr[min_idx])
                                                                                min_idx = j;
                                                int temp = arr[min_idx];
                                                arr[min_idx] = arr[i];
                                                arr[i] = temp;
                                }
                }
                void printArray(int arr[]) {
                                int n = arr.length;
                                for (int i = 0; i < n; ++i)
                                                System.out.print(arr[i] + " ");
                                System.out.println();
                }

                public static void main(String args[]) {
                                SelectionSort ob = new SelectionSort();
                                int arr[] = { 1542, 72, 25, 31, 35 };
                                ob.sort(arr);
                                System.out.println("Sorted array");
                                ob.printArray(arr);
                }
}













26.   Write a method named removeA() that accepts an ArrayList of character values and removes any letter A’s from the list (Assume the list is LOWERCASE)



package com.pratice;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

public class Person {
                public static void main(String args[]) {
                                ArrayList<String> list = new ArrayList<String>();
                                list.add("suresh");
                                list.add("ram");
                                list.add("naresh");

                                Iterator<String> it = list.iterator();
                                while (it.hasNext()) {
                                                String element = it.next();
                                                if (element.contains(Character.toString('a'))) {
                                                                list.remove(element);
                                                }
                                }

                }
}


EmoticonEmoticon