What keyword would you use to return the number of elements in an ArrayList?
.size()
How would you access the element called "apple" in this array?
String [] fruits = {"apple", "banana", "orange"};
fruits[0]
Provide code for creating a file object that passes in the .txt file called "poem.txt"
File myPoem = new File("poem.txt");
Which mammal is known to spend almost all its life in eucalyptus trees?
Koalas
The main advantage of using ArrayLists over arrays.
What is being able to change the size of the ArrayList/ArrayList is more flexible?
Provide code for two examples of creating 1D arrays containing or allocating memory for 5 integers and name it "myArray"
int [] myArray = {1,2,3,4,5};
int [] myArray = new int[5];
What kind of loop and method would you use to read a file in java?
while loop, .hasNext() method
What does the “G” in 5G stand for?
Generation
The error in the code below (assume libraries are imported as necessary):
ArrayList<int> heights = new ArrayList<>();
heights.add(4);
heights.add(2);
heights.add(3);
System.out.println(heights.get(1));
What is creating an ArrayList with primitive variables?
Assume the following list:
["pear", "apple", "grape", "melon", "plum"]
What is printed?
int count = 0;
for (String fruit : fruitList)
{
if (fruit.length() > 4)
{
count++;
}
}
System.out.println(count);
3
Assume words.txt contains:
cat
dog
camel
cow
What is printed?
File file = new File("words.txt");
Scanner scan = new Scanner(file);
int count = 0;
while (scan.hasNext())
{
String word = scan.next();
if (word.startsWith("c"))
{
count++;
}
}
scan.close();
System.out.println(count);
3
Name the top 3 all time scoring performances in a single game for the NBA. You need to know the amount of points and the names of each player.
1. Wilt Chamberlain 100 pts
2. Bam Adebayo 83 points
3. Kobe Bryant 81 points
Consider the following code segment.
ArrayList<String> animals = new ArrayList<String>();
animals.add("dog");
animals.add("cat");
animals.add(1, "bird");
animals.remove(2);
animals.add("horse");
System.out.println(animals.get(1) + " and " + animals.get(2));
What is printed?
bird, horse
Consider the following code segment.
for (int i = 0; i < arrayA.length; i++)
{
if (arrayA[i] > maxValues[i])
{
arrayA[i] = maxValues[i];
}
}
The intended goal is for maxValues to store the larger value between arrayA and maxValues at each index.
Which revision fixes the algorithm?
A. Replace line 4 with maxValues[i] = arrayA[i];
B. Replace > with <
C. Replace arrayA[i] = maxValues[i]; with maxValues[i] = arrayA[i];
D. Change loop condition to i <= arrayA.length
answer: C
Goal:
maxValues should store the larger value between the two arrays.
Current code incorrectly changes arrayA.
Correct fix:
maxValues[i] = arrayA[i];
when arrayA[i] > maxValues[i].
Name two ways you can handle the FileNotFoundException
Try and catch block, or use the keyword throws in the method header with the name of the exception you want to handle
Name the three ways an NFL team can score 2 points in one play.
Two pt conversion on offense, a safety by the defense, or on a 2 pt attempt the defense returns a fumble or interception for a touchdown.
When removing items in the context of a for loop with an ArrayList, name BOTH of the ways to fix the issue of the indices not updating properly in the ArrayList.
You can either include i-- in the body of the loop after you remove an item OR create a loop that loops backwards.
Provide code for the entire algorithm of determining the highest number in an array of integers
int[] numbers = {45, 12, 98, 33, 27};
int max = numbers[0];
for (int i = 0; i < numbers.length; i++) { if (numbers[i] > max) {
max = numbers[i];
}
}
System.out.println("The max value is: " + max);
}
}
If I have the following data per line in a .txt file (2 strings and 1 integer), how would I process that data in the context of a while loop? Provide the 3 variable declarations and initialize them properly. The two strings contain the color of a jersey, the size of a jersey, and the integer contains the jersey number. Assume you have a scanner object created called scan.
String jerseyColor = scan.next();
String jerseySize = scan.next();
int jerseyNumber = scan.nextInt();
What is a group of lemurs called?
A conspiracy