Imagine that you have the following class. How would you write a getter for the instance variable bar?
Note:
1) Don't ever use the static keyword! This is almost always wrong, especially for question type 2.
2) All methods should always be public
3) Make sure to include the return type
1) What code would you use to create an ArrayList called list to store whole numbers?
2) How would you create an array of size 10 called arr to store whole numbers?
ArrayList:
ArrayList<Integer> list = new ArrayList<Integer>();
Note: Use Integer, not int, for ArrayLists!
Array:
int[] arr = new int[10];
Note: Please make sure you know the difference between how to create a Array and a ArrayList
double nums = new double[5][4];
Suppose you have two integers x and y. How do you calculate the average (mean) of those two ints?
double average = ((double) (x + y)) / 2;
Note: make sure to cast before dividing! The following code doesn't work:
double average = (double) ((x + y) / 2);
Remember that this is the opposite order when compared to converting from doubles to int, where you want to cast last:
int rand = (int) (Math.random() * 10);
You want to create a GroceryList class to keep track of a list of possible items and a total budget. How might you declare the instance variables for this class?
private ArrayList<String> list;
private int budget;
(Note: instance variables are always private)
Consider the following method which finds the largest value in a non-empty array:
Which of the following replacement(s) for /* some value */ will result in the code working properly?
I and III only
Consider the following method which takes a 2D array as input:
How would you best describe what this method does?
It sets the value of all elements along the diagonal to the absolute value of that element
What is the output of mystery(212)?
212
21
10
1
0
You want to create two classes to keep track of your progress for studying for the AP Exam. One class, ProblemInfo, will keep track of information for each individual practice problem that you've studied, and another class, APStudy, would consolidate/keep track of all of the ProblemInfo objects. How might you set up the instance variables for these two classes?
ProblemInfo:
private String problemName;
private int selfGrade;
private String notes;
APStudy:
private ArrayList<ProblemInfo> questions;
Consider the two following methods which intend to add 1 to every value in an inputted array:
Do both, either, or none of these methods work as intended? Why or why not?
Even though change2 is a void method, it works because it is being passed the address to the array.
Change1 does not work because it creates a new array. Setting arr to point to the new array doesn't change any values in the original array.
Consider the following method which takes a 2D array as input:
How would you best describe what this method does?
It "mirrors" the top rows to the bottom rows (it copies the values from the top row to the bottom row, second from the top row to the second from the bottom row, etc.)
Consider the following code:
Assuming that num is always positive, which is a true statement about the segment?
I. If 100 <= num <= 1000 initially, the final value of newNum must be in the range 10 <= num <= 100
II. There is no initial value of num that will cause an infinite while loop.
III. If num <= 10 initiailly, newNum will have a final value of 0
II and III only
What is the output of the code:
Widget w = new Thingy();
w.methodA();
1243
Write a method that is given an array of Strings and returns an ArrayList containing all of the Strings from the array in reverse order to how they appeared in the array.
OR
Write a method that reverses the elements in each row of a 2D array of integers
Write a method that is given three Strings as input and returns whether or not exactly 2 of them are equal
Note: use equals, not ==, to compare Strings!
However, when comparing any primitive data type (e.g. int, double, boolean), use ==!!
Suppose we are given this class to represent a street address:
Write the class for the subclass ApartmentAddress which will be used as follows:
Suppose that we are given the following class that represent a train ticket:
Write a method that is given an ArrayList of tickets as input. The method checks that there are no two seats that are "double-booked". It returns true if no tickets share the same row and seat letter and false otherwise.
Write a method that "removes" a given column from a 2D array of integers. After the column is removed, all values should be shifted to the left accordingly, leaving a columns of 0's in the rightmost column.
Write a method that takes two numbers and returns the largest single digit between the two numbers.