1/71
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Object in Java
An object is something that contains both state and behavior.
Difference between class and object
Objects are instances of classes. The class is the general template, and the object is the specific version.
Client of a class
Being a client of a class means that we can use its methods and functionality without necessarily understanding how it works.
Constructor in Java
A constructor allows us to create a new instance of a class, usually initializing instance variables.
Card class constructor implementation
suit = cardSuit; value = cardValue;
Instance method
An instance method is a piece of code called on a specific instance of the class. It is called with a receiver object.
Rectangle class area retrieval
The correct syntax for retrieving the area of shape is shape.getArea().
accessor method
Refers to the same idea as a getter method.
Math.abs(x)
An example of calling a static method to get the absolute value of x.
wrapper class
Allows a primitive value to be converted to an object.
A Double is unboxed
When it is converted to a primitive value.
A int is autoboxed
When it is converted to an Integer.
valid methods
Methods that can coexist in the same class with the same name due to different parameters. i, ii, and iv
// POINT A
A point in the code where the scope of variables is evaluated. sum and count
what is printed by the program?
hi
this keyword
Refers to the current instance of the class.
Circle class output Circle one = new Circle(10); Circle two = new Circle(10); System.out.println(one == two);
false
Circle class output public void run() { Circle one = new Circle(5); Circle two = new Circle(10); foo(two); System.out.println(one.equals(two)); } public void foo(Circle x) { x.setRadius(5); }
true
superconstructor call
You call it by making a call with the word super in the subclass constructor.
Movie class inheritance
Both SciFi and Fantasy inherit constructors from Movie.
Tool class hierarchy
The proper first line of each class is public class Hammer extends Tool and public class Screwdriver extends Tool.
Clothing class methods
bottom up design
list of shoppers in a grocery line
arraylist
int[] arr = new int[5];
The correct syntax to create an array of 5 integers in Java.
arr[4]
The value at the fifth position of the array, which is modified to 2 in the provided code.
findMin method replacement for some value
i and iii only
for-each loop instead of for loop
access every element and refer to elements through variable name rather than array index
you can modify the elements of an arraytraverse with a for-each loop
false
output of the code snippet
The printed values of the scores array:
80,
92,
91,
68,
88.
What will the following code snippet output?
int[ ] values = {17, 34, 56, 2, 19, 100};
for (int value : values)
{
if (value % 2 == 0)
System.out.println(value + " is even");
}
The code snippet will output the even numbers from the values array, each followed by the phrase "is even:
34 is even
56 is even
2 is even
100 is even".
Given the following code snippet
int[ ] values = {150, 34, 320, 2, 11, 100};
for (int i=0; i < values.length; i++)
{
if (values[i] % 10 == 0)
{
System.out.println(values[i] + " is divisible by 10");
}
}
which for-each
loop would produce the same output?
for (int value : values)
{
if (value % 10 == 0)
System.out.println(value + " is divisible by 10");
}
key difference between arrays and ArrayLists
Arrays are fixed size but ArrayLists can change in size.
create ArrayList
ArrayList<String> list = new ArrayList<String>();
not cause IndexOutOfBoundsException
list.add(list.size(), new Dog());
equivalent for loop
for (int i = 0; i < 20; i += 2)
{
System.out.print(i);
}
remove multiples of 3
No, this method is not written correctly, as the counter in the if statement will skip the next value, as the values will shift down in the ArrayList.
Answered
Which of these methods will properly traverse two ArrayLists and print any index that have the same value in both ArrayLists?
{
int index = 0;
int size;
if(array1.size() > array2.size())
{
size = array2.size();
}
else
{
size = array1.size();
}
while(index < size)
{
if(array1.get(index) == array2.get(index))
{
System.out.println(index);
}
index++;
}
}
grid[0].length
3
hich of the following are valid statements in Java?
I:
ArrayList<String> list = new List<String>();
II:
List<String> list = new ArrayList<String>();
III:
List<String> list = new List<String>();
ii only
A method is to be written to search a 2D array for a value that is larger than a given item and return its index. The problem specification does not indicate what should be returned if there are several values that are larger in the 2D array. Which of the following would be the best course of action?
specification should be modified to specify if there are multiple larger value indexes
Question: 41
A 2D double array terrainMap
is declared and initialized to track the terrain of a city park. Each value in the 2D array represents the height of a particular latitude and longitude above sea level. Longitude is represented by the columns in the 2D array and latitude is represented by each row in the 2D array.
Which of the following would be the correct way to print out all the indices that are more than 5 feet above sea level?
public static void above5(double[][] array)
{
for(int row = array.length-1; row >= 0; row--)
{
for(int column = 0; column < array[row].length; column++)
{
if(array[row][column] > 5.0)
{
System.out.println(row +"," + column);
}
}
}
}
he creator of this 2D array would like to look at the height of the city park along the longitude 100.0 - which traversal method would make the most sense in order to do so?
column major order
Which of the following would be a correct way to write the traversal for this 2D array?
for(double[] row: terrainMap)
{
for(double num: row)
{
System.out.print(num + " ");
}
System.out.println();
}
What method do you use to add a key-value pair into a HashMap?
put.
binary number system
The number base of the binary number system is 2.
place value of the 1 in 1002
The place value of the 1 in the binary value 1002 is 4s place.
maximum iterations for binary search in 256 elements
The maximum number of iterations needed to find an element in an array containing 256 elements is 8.
precondition for binary search
The array must be sorted.
comparisons in selection sort for 8 elements
The selection sort algorithm will make 28 comparisons on an array of 8 elements.
insertion sort after third pass
After the third pass of the for loop, the array will look like [50, 40, 30, 10, 20, 60].
count < 5 at point 1
count < 5 is always true at point 1.
count < 5 at point 2
count < 5 is sometimes true/sometimes false at point 2.
count < 5 at point 3
count < 5 is always false at point 3.
loop iteration count < x
The loop iterates x times.
loop iteration count for count <= x
The loop executes x + 1 times.
How many times does the following loop execute?
// x has been initialized with a positive int value greater than 5
int count = 5;
while (count < x)
{
count++;
}
x-5 times
Why is having efficient algorithms important?
both 1 and 2
What is the definition of recursion?
Recursion is when a method calls itself.
What is the base case in a recursive statement?
The base case is the simplest problem to solve.
Why do we use recursion?
both options
We use it to break complex problems down into simpler problems that become easier to solve, and in place of loops.
Of the following problems, which one would a recursive function work best with?
Calculating factorial. Recall: 5 factorial = 5 4 3 2 1.
Given the following code, how many calls to the printX method would be made if we called with printX(20, 15);?
Correct Answer: 6.
Using the binary search algorithm, what is the maximum number of iterations needed to find an element in an array containing 256 elements?
Correct Answer: 8.
What is the precondition for binary search to work on an array?
Correct Answer: The array must be sorted.
For a large dataset (roughly 100,000 items), which search is more efficient to find a value?
Correct Answer: Binary searches are significantly more efficient.
Which best describes how a binary search works?
Binary searches start in the middle and eliminate half of the list in each iteration until the desired value is found.
A binary search can be written either with a loop or with a recursive function.
Correct Answer: True.
Which best describes a merge sort algorithm?
Correct Answer: Merge sort is a recursive sorting algorithm that can be used to sort elements.
How does a merge sort work?
Correct Answer: Sorts an array by dividing a list into parts, then merging it back together in the correct order.
Which part of the merge sort is accomplished with recursion?
Correct Answer: Recursion is used to break the list down repeatedly until it gets to one element.
What type of data set works best for merge sorts?
Correct Answer: Neither of these. Merge sorts work the same on all lists.
Why do we use merge sorts?
Correct Answer: They are very efficient regardless of how the data is organized.