T or F: In a static method we can use the "this" current object reference
What is False?
When you compile a Java program the compiler generates this.
What is Java Bytecode
What is another expression equivalent to x = x * 12?
What is x*=12;
What is recursion
recursion is a problem solving technique where the solution to our given problem depends on solutions to smaller instances of the same problem. The method solving the problem calls itself after breaking the problem down into smaller pieces.
When we add an element to an ArrayList using the add method we are adding a copy of the object associated with the provided reference
What is False?
What is the difference between a static method and a non-static method?
Static = No instance of the class needed to operate on it.
Non-static = needs to be called on an object of that class.
T or F: Some objects are created in the heap and some in the stack.
What is False?
How many objects are present in the following:
StringBuffer[] p = new StringBuffer[3];
What is 1?
What is tail recursion and why is it useful?
A tail recursive function is a recursive function in which the function calls itself at the end (the “tail” of the function) of the function in which no computation is done after the recursive call. Using tail recursion allows us to write more optimized code since we only need to store the recursive call in memory.
Example: return recursivecall(parameters);
How many objects are present in the following array definition?
int[][] a = new int[4][3];
What is 12?
A class called Apple does not define an equals method (one that returns a boolean and has an Object reference parameter). If m and p are Apple references, m.equals(p) is equivalent to:
m == p
Which of the methods below OVERLOADS the following method?
public int setSys(float x);
a. public int setSys(float x);
b. public float setSys(float x);
c. public int setSys(String x);
What is C?
The value printed by the following code fragment:
int x = 2, y = 3;
if (x >= 1 || ++y == 4) {
x = 100;
}
System.out.println(y);
What is 3?
Consider the following recursion code:
static int mystery(int x) {
if (x == 0) {
return 0;
}
return x%10 + mystery(x/10);
}
Show the calls to mystery in the order they are made. For each call, show the argument to mystery and the value returned. Start with mystery(567)
mystery(567) -- 18
mystery(56) -- 11
mystery(5) -- 5
mystery(0)--0
The method returns a deep copy of the data array parameter. You can assume the array parameter will never be null, however, it can be empty. This method will not throw any exceptions.
public static int[] makeArrayCopy(int[] data)
public static int[] makeArrayCopy(int[] data) {
int[] copy = new int[data.length];
for (int i = 0; i < data.length; i++) {
copy[i] = data[i];
}
return copy;
}
A class called Road has no constructors and we are able to create instances of the class as follows: new Road(). After adding one constructor that takes an integer as parameter, we cannot create objects using new Road(). Why can't we no longer create objects using new Road()?
What is the class has no default constructor?
/*
Which of the methods below OVERRIDES the following method?
public int setSys(float x);
a. public int setSys(float x);
b. public float setSys(float x);
c. public int setSys(String x);
*/
What is A?
Given the following code:
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
if (name != null && name.length() >= 5){
System.out.println(name);
}
scanner.close();
Does it makes a difference if the expression name != null appears after name.length() >= 5?
What is Yes?
Write a method named getSum that, given an array of ints named arr, recursively finds the sum of its elements and returns it.
getSum(int[] arr){
getSumHelper(arr, arr.length);
}
getSumHelper(int[] arr, int N){
if (N <= 0){
return 0;
}
return (getSumHelper(A, N - 1) + A[N - 1])
Define a method called hasThreeOfAKind that returns true if an array of length 5 has three values that are the same and false otherwise.
public boolean hasThreeOfAKind(int[] arr){
int counter = 0;
for(int i =0; i< arr.length-1; i++){
counter = 1;
for(int j =i; j < arr.length; i++){
if arr[i] == arr[j] {
counter++;
}
}
if(counter >= 3){
return true;
}
}
}
The Phone interface specifies a non-static method named call().Which of the following is valid?
a. Phone p = new Phone();
b. Phone.call();
c. Phone p = null;
What is C?
Why does the following swapWrong method not swap the values?
public static void wrongSwap(int x, int y) {
int temp = x;
x = y;
y = temp;
}
public static void main(String[] args) {
int a = 10, b = 20;
wrongSwap(a, b);
System.out.println(a + " " + b); }
They are primitive types, not object references.
In the expression x.process(y), x and y are reference variables of the same type. Inside of the process method, what object is associated with the current object reference ("this")?
The same object associated with x.
Write a method where the method is a recursive method that returns a string where all digits present in the str parameter have been removed.
public static String removeDigits(String str) {
if (str.length() == 0) {
return str;
} else {
char elem = str.charAt(0);
if (Character.isDigit(elem)) {
return removeDigits(str.substring(1));
} else {
return elem + "" + removeDigits(str.substring(1));
}
}
}
Write a function named averageLength that, given an array containing Strings, returns the average length of the Strings in the array. For example, with input ["I", "love", "pizza", "!"], return 2.75
avgLength(String[] arr){
int length;
for(String word: arr){
length += word.length;
}
length = length / arr.length();
return length;
}