WorksheetsJava Basic Data Structures
Total questions: 15
Worksheet time: 7mins
What are for loops used for?
To run a piece of code only when a certain condition is met
To run a piece of code repeatedly until a condition is met
To run a piece of code repeatedly forever
To run a piece of code if a condition isn't met
How do you write a for loop?
for (int i = 0; i < 21) {}
for (int i = 0; i++) {}
for (int i = 0; i < 21; i++) {}
for (i) {}
Which is the right way to create an ArrayList of Integers?
ArrayList<Integer> list = new ArrayList<Integer>;
ArrayList Integer = new ArrayList();
Integer ArrayList = new Integer ArrayList();
ArrayList<Integer> list = new ArrayList<Integer>();
How do you add 21 to an ArrayList of Integers?
list.add(21);
list.put(21);
21.add(list);
list.add("21");
Which is the right way to put 21 at the second position in an ArrayList of Integers?
(Remember zero-indexing!)
list.put(2, 21);
list.add(1, 21);
list.add(2, 21);
list.add(21, 1);
How do you get the third element of an ArrayList?
(Remember zero-indexing!)
list.find(2);
list.get(3);
list.find(3);
list.get(2);
What does this code print out?
Nothing
0
1
2
3
3
2
1
0
2
1
0
How do you write a for each loop that iterates through an ArrayList of Strings?
for each (String s in list) {}
for each (String s : list) {}
for (Integer i : list) {}
for (String s : list) {}
What does a HashMap store?
Values
Keys and values
Keys
Strings
How do you create a HashMap with Strings as keys and Integers as values?
HashMap<String, Integer> map = new HashMap<String, Integer>();
HashMap<String> map = new HashMap<Integer>();
HashMap<Integer, String> map = new HashMap<Integer, String>();
HashMap map = new HashMap(String, Integer);
How do you add an element to a HashMap with String keys and Integer values?
map.put(Stuff, 123);
map.add("Stuff", 123);
map.put("Stuff", 123);
map.add(Stuff, 123);
How do you get a value from a HashMap?
map.get(index);
map.get(key);
map.get(value);
map.valueOf(key);
How do you iterate through a HashMap using a for each loop?
for each (String key : map.keySet()) {}
for each (String key : map) {}
for (String key : map) {}
for (String key : map.keySet()) {}
What number does this print out?
0
1
2
3
What does this print out?
95
Chemistry
100
92
