hardest
What can a generic class be parameterized for? For example, Set<E> has a type parameter of E, what does E represent?
a. Properties
b. Primitive data types
c. Class type
d. Methods
c. Class type
Select the best option to complete the code in the following statement:
Comparable ___ c = new Date();
a. <String>
b. <?>
c. <Date>
d. <E>
c. <Date>
When is it necessary to declare a method static?
I. It calls another method from the same class that is static.
II. It accesses fields (at the class level) from the same class that are static.
III. It accesses field (at the class level) variables from the same class that are non-static.
I and II only
The concept in object-oriented programming that allows classes to gain methods and data by extending other classes' fields and methods.
A standardized language for modeling systems and structures in programming.
A keyword that allows subclasses to access methods, data, and constructors from their parent class.
A programming philosophy that promotes protecting data and hiding implementation in order to preserve the integrity of data and methods.
A keyword in Java that indicates that the class provides all the specified functionality of an interface type.
Classes that inherit methods and fields from more general classes.
A keyword in Java that indicates that the class is a derived or subclass of some other specified class
Which term describes the ability of a reference type to reference objects of several different types?
The visibility modifier that's generally used for constructors of a class
The visibility modifier that allows access from within the class, exclusively.
Inheritance, UML, super, encapsulation, implements, subclasses, extends, polymorphism, public, private
Which data structure does the Java Virtual Machine use with recursion?
Stack
[ABSTRACTION] When is a method abstract?
No reasonable default implementation is provided, and it must be defined in the inheritance hierarchy. Consists of a method signature, but not a method body. Signature is followed by a semicolon, not curly braces. If a class contains an abstract method, the class must be abstract as well. Any child class must either override the abstract method or declare itself abstract.
Read the following three classes. What is displayed when the main method is executed?
public class Parent {
public void fire(int a) {
for(int i = 0; i < a; i++)
System.out.print("V"); }
public void dance() {
System.out.print("C"); }}
public class Child extends Parent {
public void fire(int a) {
for(int i = 0; i < a; i=i+2)
System.out.println("U"); }
public void dance() {
System.out.println("Ram"); }}
public class TestParentChild{
public static void main (String[] arg){
Parent item1 = new Parent();
Parent item2 = new Child();
item1.fire(2);
item1. dance( ) ;
item2.fire(2);
item2.dance();
item1.fire (2); }}
VVCU
Ram
VV
UML QUESTION ON TEST A
DRAW IT OUT AND ANSWER IF THESE ARE VALID OR INVALID:
Vehicle baby= new Convertible();
Bicycle greta = new Car();
Car buddy= new Vehicle();
Valid
Invalid
Invalid
Objects of a subclass can be assigned to
variables of its super class.
What does this program return?
public int doSomething(int n){
if(n < 10)
return n;
return n % 10 + doSomething(n / 10);
The sum of the digits of n
What is the process of writing a recursive method?
Define the Problem: Understand the problem and how it can be broken down into smaller, identical subproblems.
Identify the Base Case: Determine the simplest instance of the problem that can be solved without recursion. This case prevents infinite recursion by providing a clear stopping point.
Define the Recursive Case: Specify how each recursive call will break down the problem, moving closer to the base case.
Ensure Progress Toward the Base Case: Each recursive call should simplify the problem, gradually approaching the base case.
Combine Results: If necessary, combine the results of each recursive call to construct the final solution.
Consider the following code segment:
int[] nums = {6,5,4,3,2};
for(int i = 0; i < nums.length - 1; ++i)
nums[i] += nums[i+1] + i;
What are the contents of nums after the code is executed?
{11, 10, 9, 8, 2}
A class Poem is declared with a private instance variable called text of type String. Poem has a method public String get Text () that returns a reference to text, and Poem also has a method public boolean sameText ( Poem other) that returns true if the calling object and the object passed as a parameter have identical text, and false otherwise.
Which of the following lines will compile and give the desired result for sameeText?
I. return test.equals(other.text);
II. return text.equals(getText(other));
III. return text.equals(other.getText());
IV. return getText().equals(other.getText());
III and IV only
Write a binary search method that is recursive, generic, and can be found in a non-generic class
public static<E extends Comparable<E>>int
binarySearch(E[] arr, E target, int low, int high) {
if (low > high) {
// target not found in array
return -1;
}
int mid = (low + high) / 2;
if (arr[mid].equals(target)) {
// target found at middle index
return mid;
} else if (arr[mid].compareTo(target) > 0)) {
// target is in left half of array
return binarySearch(arr, target, low, mid - 1);
} else {
// target is in right half of array
return binarySearch(arr, target, mid + 1, high);
}
}
What is the output of a call to display(-6) ?
public static void display(int n) {
if (n == 0){
System.out.print(" " + n);
else if (n > 0){
System.out.print(" " + n);
display(n-1);
} else {
display(n+1);
System.out.print(" " + n);
}
}
0 -1 -2 -3 -4 -5 -6
Explain recursion vs iterative approaches
Recursion is a method of solving problems where a function calls itself as a subroutine. It differs from iterative approaches in that it solves the problem by breaking it down into smaller, similar subproblems, rather than using loops.
In the derived class, which data members of the base class are visible (in other words, which data members can be accessed directly)?
Those declared as public only
Those declared as protected only
Those declared as public or protected
Those declared as public or private
All data members are visible
Those declared as public or protected
Suppose you have a properly declared array of String values called strAr1. Consider the following code segment:
String[] strArr2 = new String[strArrl.length];
int i = 1;
for (String s: strArrl){
strArr2[strArrl.length - i] = s;
i += 1;
}
Correctly describe the behavior of this code segment.
strArr2 refers to an array that has references to strings that are copies of the strings referenced by the array referenced by strArr1, in the reverse order; strArr1 remains unchanged
We have triangle made of blocks. The topmost row has 1 block, the next row down has 2 blocks, the next row has 3 blocks, and so on. Compute recursively (no loops or multiplication) the total number of blocks in such a triangle with the given number of rows.
triangle(O) - 0
triangle(1) - 1
triangle(2) - 3
public int triangle(int rows) {
if(rows == 0){
return 0;
} else if (rows == 1){
return 1;
}
return rows + triangle(rows-1);
}
Segment (a):
int[] nums1 = {2, 3, 4, 5, 6};
for (int i: numsl) {
i += 1;
}
Segment (b):
int[] nums2 = {2, 3, 4, 5, б};
for (int 1 = 0; 1 < nums2. length; ++i) {
nums2 [i] += 1;
}
Describe the behavior of these code segments
Segment (a) will leave the array referenced by nums1 unchanged;
Segment (b) will result in nums2 referencing an array with values (3, 4, 5, 6, 7)
Given base and n that are both 1 or more, compute recursively (no loops) the value of base to the n power, so powerN(3, 2) is 9 (3 squared).
powerN(3, 1) → 3
powerN(3, 2) → 9
powerN(3, 3) → 27
-------------------------------------------------------------
Given a string, compute recursively (no loops) the number of lowercase 'x' chars in the string.
countX("xxhixx") → 4
countX("xhixhix") → 3
countX("hi") → 0
public int powerN(int base, int n) {
if(n == 1){
return base;
}
return base * powerN(base, n -1);
}
----------------------------------------------------------
public int countX(String str) {
if(str.length() == 0){
return 0;
}
if(str.charAt(0) == 'x'){
return 1 + countX(str.substring(1,str.length()));
} else {
return countX(str.substring(1,str.length()));
}
}
Suppose you have defined a class called Tor that contains a class-level variable of type int called ONTO. If another class can reference the field directly using Tor.ONTO, which of the following must be true?
I. The class Tor must have a public method called with signature public int ONTO(
II. The variable ONTO must be public
III. The variable ONTO must be static
IV. The variable ONTO must be a constant
II. The variable ONTO must be public
III. The variable ONTO must be static
Suppose a class has the following method:
public int mystery(int[] nums) {
--nums[1];
return (nums[1] * 3;
And another method in the same class has the following statements:
int[] myNums = {3, 5, 7};
int m = mystery(myNums);
Correctly describe the state of the variables myNums and m after these lines are executed?
myNums refers to the array {3, 4, 7} and m has the value 12
Using the method header shown below, write a method that accepts a two-dimensional array of type int and one other int variable as formal parameters. The getColumnTotal method returns the total of all integers in the specified column of the 2-dimensional array of integers. For example, in the table shown below, a call to getColumnTotal (table, 2) would return 26 (i.e. the sum of column 2 is 6 + 14 + 0 + 6 + 0 = 26. Note: The method should throw an lllegalArgumentException if the number passed for the column is invalid.
int getColumnTotal(int[] [] table, int column) {
public int getColumnTotal(int [][] table, int column){
if(column < 0 || column >= table[0].length)
throw new IllegalArgumentException();
int total = 0;
for(int row = 0; row < table.length; row++)
total += table[row][column];
return total;
}
[from lab] WRITE OUT THESE RECURSIVE METHODS:
----------------------------------------------------------------------------------------------
returns true if the parameter String, str is a palindrome false otherwise
public static boolean isPalindrome(String str, int begin, int end)
----------------------------------------------------------------------------------------------
returns true if the int array parameter is sorted from smallest to largest, false otherwise. Process this recursively beginning with the first element.
public static boolean isSmallestToLargest(int[] values, int firstIndex)
----------------------------------------------------------------------------------------------
// Base case: if the pointers have crossed each other, it's a palindrome
if (begin >= end) {
return true;
} // Check if the current characters are not the same
if (str.charAt(begin) != str.charAt(end)) {
return false;
} // Recursive call: move towards the middle of the string
return isPalindrome(str, begin + 1, end - 1); }
----------------------------------------------------------------------------------------------
// Base case: If the firstIndex reaches the end of the array, return true
if (firstIndex == values.length - 1) {
return true;
} // Check if the current element is greater than the next element
if (values[firstIndex] > values[firstIndex + 1]) {
return false;
} // Recursive case: Call the method with the next element
return isSmallestToLargest(values, firstIndex + 1);
[from lab] WRITE OUT THESE RECURSIVE METHODS
----------------------------------------------------------------------------------------------
returns a String of character, ch. The length is determined by the second parameter, length.
public static String buildStringOfCharacters(char ch, int length)
----------------------------------------------------------------------------------------------
returns an int array that has the elements in reverse order of the parameter array, nums. Process this recursively beginning with the last element.
public static int[] reverseNumArray(int[] nums, int backIndex)
// Base case: no more characters to add
if (length == 0) {
return "";
} // Recursive case: add the character and reduce the length by 1
return ch + buildStringOfCharacters(ch, length - 1); }
----------------------------------------------------------------------------------------------
int temp = 0;
if (backIndex >= (nums.length / 2)) {
temp = nums[backIndex];
nums[backIndex] = nums[(nums.length -1) - backIndex];
nums[(nums.length -1) - backIndex] = temp;
reverseNumArray(nums, backIndex - 1);
}
return nums;
}