What is the difference between a private and public instance variable?
Private: Denies access of this variable to the methods of the class, have to use get methods
Public: makes the variable open and free to use to all methods of the class
A sorting algorithm that tries random orders of an array until it finds the properly sorted array.
It's average number of sorts is the factorial of n
What is Bogo sort?
The expression "hippocampus".substring(5,8) returns what?
"camp"
What is the value of list if mystery(4) is called? List = {2,3,4,5,6} before:
public void mystery(int n)
{
for (int k = 0; k < n; k++)
{
Object obj = list.remove(0);
list.add(obj);
}
}
{5,6,2,3,4}
What is the output of this code if nums = {3,2,1}:
public boolean prompt(int[] nums)
{
return (nums[0] == 3 || nums[nums.length-1] == 3);
}
True
Describe the reserved word static
Shared by all instances of the class, means the variable or method belongs to class rather than the instances
Describe the differences between a sequential and binary searching algorithm.
Sequential: Goes through each element of the array comparing to the key
Binary: Groups the arrays in half comparing values of each end to the key, finding if the key's value is greater than or less than
What is the output of this code:
String oldStr = "ABCDEF";
String newStr = oldStr.substring(1, 3) + oldStr.substring(4);
System.out.println(newStr);
Why doesn't the removeName method work as intended?
public void removeName(List<String> nameList, String nameToRemove)
{
for (int k = nameList.size - 1; k >= 0; k--)
{
if (nameList.get(k).equals(nameToRemove))
nameList.remove(k);
}
.size is a method and therefore needs paraphrases (.size()).
What is the output of the following code if nums = {1,3,4}.
public int[] fix23(int[] nums) {
if(nums[0] == 1)
if(nums[1] == 3)
nums[1] = 1;
if(nums[1] == 1)
if(nums[2] == 3)
nums[2] = 1;
return nums;
}
{1,1,4}
Descirbe the difference between a runtime error and a compile-time error
compile-time error: An error that occurs when the rules of Java syntax are broken
runtime error: An error that occurs during the execution and halts the execution from continuing
After 2 passes of running through a selection sort algorithm, sorting the array in descending order, what will the array look like? (Array: 2 8 10 6 5 13)
13 10 8 6 5 2
What is the output of the following code?
int a = 1988;
int b = 1990;
String claim = " that the world’s athletes " +
"competed in Olympic Games in ";
String s = "It is " + true + claim + a + " but " + false + claim + b + ".";
System.out.println(s);
It is true that the world’s athletes competed in Olympic Games in 1988 but false that the world’s athletes competed in Olympic Games in 1990.
What is the output of this code:
ArrayList<Integer> numList = new ArrayList<Integer>();
numList.add(4);
numList.add(2);
numList.add(1);
numList.add(1, 4);
numList.set(0, 1);
System.out.print(numList);
{1,4,2,1}
When executed, how many times is println() executed?
int[][] data = new int[5][10];
for (int j = 0; j < data.length; j++){
for (int k = 0; k < data[j].length; k++){
if (j == k){
System.out.println(data[j][k]);
}
}
}
5 times
What is wrong with this code:
interface Dog {
final int legs = 4;
static boolean tail = true;
public int getLegs(){
return legs;}
}
Dog is an interface and therefore cannot have methods besides those that are abstract. The method getLegs() is not absract
The array was already in increasing order, meaning each element was the furthest point away from its insertion point
What is the output of the following code if called with str = apple:
public static String rearrange(String str) {
String temp = "";
for (int i = str.length() - 1; i > 0; i--){
temp += str.substring(i - 1, I);
}
return temp;
}
"lppa"
Which is the correct method of initializing an Arraylist
A. ArrayList<int> hi = new ArrayList<int>();
B. ArrayList<String> = new ArrayList<String>();
C. ArrayList<Integer> hello = new ArrayList<Integer>;
D
When can replace the missing code to make the method work?
int[][] data = new int[2][3];
for (int h = 0; h < data.length; h++){
for (*missing code*){
if (h > i){
System.out.println("hi");
}
}
}
int i = 0; i < data[0].length; i++
What is 348 in octal?
534
The following code represents what type of sorting algorithm:
{
for (int j = 1; j < elements.length; j++)
{
int temp = elements[j];
int possibleIndex = j;
while (possibleIndex > 0 && temp < elements[possibleIndex - 1])
{
elements[possibleIndex] = elements[possibleIndex - 1];
possibleIndex--; // Line 10
}
elements[possibleIndex] = temp;
}
}
What is insertion sort?
What is the ouput of the following code if a = "a dog followed by smog" and b = "og" :
public static String abMethod(String a, String b){
int x = a.indexOf(b);
while (x >= 0) {
a = a.substring(0, x) + a.substring(x + b.length());
x = a.indexOf(b);
}
return a;
}
"a d followed by sm"
Why doesn't this method work as intended? (Meant to remove odd elements from myList)
public static ArrayList<Integer> removeEvens(ArrayList<Integer> myList)
{
for (int i = 0; i < myList.size(); i++)
{
if (myList.get(i) % 2 == 1)
{
myList.remove(i);
}
}
return myList;
}
D. ArrayList<Integer> help = new ArrayList<Integer>();
The method skips elements as the indexes change when elements are removed from the list
How many times will ! be executed:
int[][] arr = {{6, 2, 0, 7},
{0, 3, 1, 2}};
for (int j = 0; j < arr.length; j++){
for (int k = 0; k < arr[0].length; k++){
if (arr[j][k] > j * k){
System.out.println("!");
}
}
}
4 times