Download quiz

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Name : ___________________________ Date : _________________
A+ ARRAYLIST EXAM REVIEW
1. What is output by the code at right?
2. What is output by the code at right?
3. What is output by the code at right?
ArrayList<String> d = new ArrayList<String>();
d.add("8");
d.add("90");
d.remove("390");
d.set(0,"78");
d.set(1,"55");
Collections.sort(d);
out.println(d);
ArrayList<String> e = new ArrayList<String>();
e.add("89");
e.add("90");
e.remove("90");
e.set(0,"78");
e.add("12");
e.add("50");
e.set(1,"55");
out.println(e);
ArrayList<String> f = new ArrayList<String>();
f.add("3");
f.add(0,"11");
f.remove("3");
f.add("30");
f.set(1,"ab");
f.set(0,"xy");
f.add(0,"17");
f.add(1,"1f");
Collections.sort(f);
f.remove("90");
out.println(f);
out.println(f.indexOf("ab"));
4) if I have an arrayList<Integer> list1 that contains [0,1,2,3] which of the following would result
in a compiling error (circle all that apply)?
a) list1.add(45)
b) list.add(“32”)
c) list.add(4, 200)
d) list.add(12, 300)
e) list.set(25)
f) list.set(3,6)
g) list.set(4,200)
h)list.set(12, 300)
© A+ Computer Science – ArrayList Quiz - www.apluscompsci.com
5) Consider the following method:
public static void mystery2(ArrayList<Integer> list) {
for (int i = list.size() - 1; i > 0; i--) {
if (list.get(i) < list.get(i - 1)) {
int element = list.get(i);
list.remove(i);
list.add(0, element);
}
}
System.out.println(list);
}
If we pass the method [ 2 , 6, 4, 9] what will be printed?
6) Consider the following method:
public static void mystery1(ArrayList<Integer> list) {
for (int i = 0; i < list.size(); i += 2) {
int element = list.get(i);
list.remove(i);
list.add(element);
}
System.out.println(list);
}
If we pass the method [ 2 , 6, 4, 9] what will be printed?
© A+ Computer Science – ArrayList Quiz - www.apluscompsci.com
7) Consider the following method:
public static void mystery3(ArrayList<Integer> list) {
for (int i = list.size() - 1; i >= 0; i--) {
if (i % 2 == 0) {
list.add(list.get(i));
} else {
list.add(0, list.get(i));
}
}
System.out.println(list);
}
If we pass the method [ 2 , 6, 4, 9] what will be printed?
8)Consider the following method:
public static void mystery1(ArrayList<Integer> list) {
for (int i = list.size() - 1; i > 0; i--) {
if (list.get(i) < list.get(i - 1)) {
int element = list.get(i);
list.remove(i);
list.add(0, element);
}
}
System.out.println(list);
}
If we pass the method [ 2 , 6, 4, 9] what will be printed?
© A+ Computer Science – ArrayList Quiz - www.apluscompsci.com
9)
If we pass the method [ 2 , 6, 4, 9] what will be printed?
10) What is the output?
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 1; i <= 10; i++) {
list.add(10 * i);
}
System.out.println(list);
for (int i = 0; i < list.size(); i++) {
list.remove(i);
}
System.out.println(list);
11) Know how to make a copy of an existing list!
ArrayList<Integer> nums2 = new ArrayList<Integer>(nums);
// or
ArrayList<Integer> nums2 = new ArrayList<Integer>();
nums2 = nums;
//or you can use a for loop
for(int i = 0; i<nums.size(); i++)
{
nums2.add(i);
}
© A+ Computer Science – ArrayList Quiz - www.apluscompsci.com
12) What does the method below do? Trace the code with an array list that contains [6, 5, 6, 6, 5, 2] to help you
determine the answer.
public static void mystery3(ArrayList<Integer> list) {
}
STUDY PROGRAMMING BY DOING! Is it there or not? Finding location, Getting largest, locating largest
Know how to make a list of objects and how to print a specific instance variable for them. For example:
Account acct1 = new Account("Steve", 100);
Account acct2 = new Account("Kara", 200);
Account acct3 = new Account("Bill", 12515001);
ArrayList<Account> list1 = new ArrayList<Account>();
list1.add(acct1);
list1.add(acct2);
list1.add(acct3);
System.out.println(acct1); // only prints the 'address' of the reference e.g @9149aknwfa
System.out.println(list1); // only prints all 'addresses' of the references
System.out.println(acct1.name); //actually prints the name of acct1 "Steve"
// use a for each loop (enhanced for loop) to print the instance variables of all
// objects in the ArrayList
for(Account accounts : list1) // Account is the object type. accounts is any variable similar to i
{
// list1 is the list you are traversing.
System.out.println(accounts.name);
System.out.println("Current balance: $" + accounts.balance);
System.out.println("----------------------------------------------");
}
© A+ Computer Science – ArrayList Quiz - www.apluscompsci.com
Free response:
1) Sum the values with even indexes
public int sumEvenIndexesB(ArrayList<Integer> numsList)
{
}
2)
Count the number of odd numbers in the list
public int evenCountB(ArrayList<Integer> numsList)
{
}
3) Add all the numbers in the list, however 8s are yucky and should not be added. Also any number after an 8
should not be added.
public int yucky8(ArrayList<Integer> numsList)
{
}
© A+ Computer Science – ArrayList Quiz - www.apluscompsci.com
Related documents