Java 2D Array Exercise: Favorite Foods by Country
Instructions:
Fill in the blanks (____) with the correct Java code. Choose the best option for each
blank from the three provided.
Code:
public class FavoriteFoods {
public static void main(String[] args) {
// Declare and initialize a 2D array with countries and their famous dishes
String[][] foods = new String[____][____];
3 3 // (1) and (2)
foods[0][0] = "Italy";
foods[0][1] = "Pizza";
foods[0][2] = "Pasta";
foods[1][0] = "Japan";
foods[1][1] = "____"; // (3)
foods[1][2] = "Ramen";
foods[2][0] = "Mexico";
foods[2][1] = "Tacos";
foods[2][2] = "____";
Burrito // (4)
[Link]
// Loop through the 2D array foods[i][0]
for (int i = 0; i < ____; i++) { // (5)
[Link]("Country: " + ____); // (6)
[Link]("Famous Foods:");
for (int j = 1; j < ____; j++) { // (7) foods[i].lenght
[Link]("- " + ____); // (8)
} foods[i][j]
[Link]();
}
}
}
Fill in the blanks with the correct option:
1. (1) Number of rows (countries) in the array
• a) 3
• b) 5
• c) 2
2. (2) Number of columns (1 country + 2 foods per row)
• a) 2
• b) 3
• c) 4
3. (3) A famous Japanese food
• a) “Sushi”
• b) “Burgers”
• c) “Paella”
4. (4) A famous Mexican food
• a) “Burrito”
• b) “Sushi”
• c) “Steak”
5. (5) Condition for looping through rows
• a) [Link]
• b) foods[0].length
• c) 3
6. (6) How to print the country name
• a) foods[i][0]
• b) foods[0][i]
• c) foods[i][j]
7. (7) Condition for looping through food items
• a) foods[i].length
• b) foods[i].length - 1
• c) [Link]
8. (8) How to print the food name
• a) foods[j][i]
• b) foods[i][j]
• c) foods[0][j]
Java 2D Array Exercise: Countries and Capitals
Instructions:
Fill in the blanks (____) with the correct Java code. Choose the best option for each
blank from the three provided.
Code:
public class CountryCapitals {
public static void main(String[] args) {
// Declare and initialize a 2D array with countries and their capitals
String[][] countries = new String[____][____]; // (1) and (2)
countries[0][0] = "USA";
countries[0][1] = "Washington D.C.";
countries[0][2] = "____"; // (3)
countries[1][0] = "Germany";
countries[1][1] = "Berlin";
countries[1][2] = "____"; // (4)
countries[2][0] = "Japan";
countries[2][1] = "Tokyo";
countries[2][2] = "____"; // (5)
// Loop through the 2D array
for (int i = 0; i < ____; i++) { // (6)
[Link]("Country: " + ____); // (7)
[Link]("Capital: " + ____); // (8)
[Link]();
}
}
}
Fill in the blanks with the correct option:
1. (1) Number of rows (countries) in the array
• a) 3
• b) 5
• c) 2
2. (2) Number of columns (1 country + 2 cities per row)
• a) 2
• b) 3
• c) 4
3. (3) A major city in the USA (other than Washington D.C.)
• a) "Los Angeles"
• b) "Toronto"
• c) "London"
4. (4) A major city in Germany (other than Berlin)
• a) "Hamburg"
• b) "Munich"
• c) "Paris"
5. (5) A major city in Japan (other than Tokyo)
• a) "Kyoto"
• b) "Seoul"
• c) "Bangkok"
6. (6) Condition for looping through rows (countries)
• a) [Link]
• b) countries[0].length
• c) 3
7. (7) How to print the country name
• a) countries[i][0]
• b) countries[0][i]
• c) countries[i][j]
8. (8) How to print the capital city
• a) countries[i][1]
• b) countries[i][2]
• c) countries[i][0]