Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Basic Data Structures

Total questions: 15

Worksheet time: 7mins

Name
Class
Date
1.

What are for loops used for?

a)

To run a piece of code only when a certain condition is met

b)

To run a piece of code repeatedly until a condition is met

c)

To run a piece of code repeatedly forever

d)

To run a piece of code if a condition isn't met

2.

How do you write a for loop?

a)

for (int i = 0; i < 21) {}

b)

for (int i = 0; i++) {}

c)

for (int i = 0; i < 21; i++) {}

d)

for (i) {}

3.

Which is the right way to create an ArrayList of Integers?

a)

ArrayList<Integer> list = new ArrayList<Integer>;

b)

ArrayList Integer = new ArrayList();

c)

Integer ArrayList = new Integer ArrayList();

d)

ArrayList<Integer> list = new ArrayList<Integer>();

4.

How do you add 21 to an ArrayList of Integers?

a)

list.add(21);

b)

list.put(21);

c)

21.add(list);

d)

list.add("21");

5.

Which is the right way to put 21 at the second position in an ArrayList of Integers?

(Remember zero-indexing!)

a)

list.put(2, 21);

b)

list.add(1, 21);

c)

list.add(2, 21);

d)

list.add(21, 1);

6.

How do you get the third element of an ArrayList?

(Remember zero-indexing!)

a)

list.find(2);

b)

list.get(3);

c)

list.find(3);

d)

list.get(2);

7.

What does this code print out?

a)

Nothing

b)

0

1

2

3

c)

3

2

1

0

d)

2

1

0

8.

How do you write a for each loop that iterates through an ArrayList of Strings?

a)

for each (String s in list) {}

b)

for each (String s : list) {}

c)

for (Integer i : list) {}

d)

for (String s : list) {}

9.

What does a HashMap store?

a)

Values

b)

Keys and values

c)

Keys

d)

Strings

10.

How do you create a HashMap with Strings as keys and Integers as values?

a)

HashMap<String, Integer> map = new HashMap<String, Integer>();

b)

HashMap<String> map = new HashMap<Integer>();

c)

HashMap<Integer, String> map = new HashMap<Integer, String>();

d)

HashMap map = new HashMap(String, Integer);

11.

How do you add an element to a HashMap with String keys and Integer values?

a)

map.put(Stuff, 123);

b)

map.add("Stuff", 123);

c)

map.put("Stuff", 123);

d)

map.add(Stuff, 123);

12.

How do you get a value from a HashMap?

a)

map.get(index);

b)

map.get(key);

c)

map.get(value);

d)

map.valueOf(key);

13.

How do you iterate through a HashMap using a for each loop?

a)

for each (String key : map.keySet()) {}

b)

for each (String key : map) {}

c)

for (String key : map) {}

d)

for (String key : map.keySet()) {}

14.

What number does this print out?

a)

0

b)

1

c)

2

d)

3

15.

What does this print out?

a)

95

b)

Chemistry

c)

100

d)

92