1/46
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is returned by data[3], where data is defined as follows:
int[] data = new int[] {1, 2, 3, 4, 5, 6};
4
Which of the following return statements, inserted on the line marked //RETURN, causes the method to return the INDEX of the first negative value in the given array between index i (inclusive) and index j (exclusive)?
public static int findNeg(int[] a, int i, int j) {
int count = 0;
for (int x = i; x < j; x++);
if (a[x] < 0) {
count__;
// RETURN
}
}
}
return x;
In java, a variable’s type is required to be explicitly stated
when it is declared
when it is assigned a new value
never
when it is declared
Given the following declaration statement:
int[] piggyBank = new int[] {10, 5, 1, 5, 25, 10, 1};
int size = 7; //number of elements stored in the array
piggyBank is
a compact perfect size array
an oversize array
an oversize array
Which of the following statements returns a value of type boolean knowing that size is an int & data is an array of ints?
return size < data.length;
return size + data.length;
return size++;
return size < data.length;
Which of the following method signatures has the correct list of input PARAMETERS for a method which returns the number of occurrences of a given value + in a given perfect size array of doubles
public static int occurrences(int[] data, int value) {}
public static int occurrences(double[] data, double value) {}
public static int occurrences(String[] data, int size, String value) {}
public static int occurrences(double[] data, int size, double value) {}
public static int occurrences(double[] data, double value) {}
Given the following array description, which is the correct syntax for accessing the null value?
String [][] letters = { “A”, “B”, “C”} {“D”, “E”, "null} {“F”, “G”, “H”};
letters[6]
letters[5]
letters[2][3]
letters[1][2]
letters[1][2]
Which of the following conditions correctly compares the value in light with the String “RED” when placed in the space indicated by /*CONDITION*/?
String[] lights = new String[] {“GREEN”, “YELLOW”, “RED”};
Scanner scanner = new Scanner(System.in);
String light = scanner.next();
if (/* CONDITION */)
System.out.println(“Stop!”);
light == lights[3]
light == lights[2]
light.equals(lights[3])
light.equals(lights[2])
light.equals(lights[2])
In Java, an array object is
inmutable
mutable
mutable
After running this code, which type of value will grades.get(0) return?
ArrayList<Double> grades = new ArrayList<Double();
grades.add(90.5);
double
Double
Double
The variable array2 is a —— copy of array1
String[] array1 = new String[] {“A”, “B”, “C”};
String[] array2 = array1;
shallow
deep
shallow
When a new object is created in Java, it is allocated in the —- part of the JVM’s memory management system
stack memory
heap memory
heap memory
Which of the following will correctly create a Agent object and store it in an existing Agent[] array called agents?
agents.add(new Agent ());
agents.add(Agent());
agents[0] = Agent();
agents[0] = new Agent();
agents[0] = new Agent();
What will the following code print out to the console when it is run?
String s = “quiz”;
s.concat(“ 1-2”);
System.out.print(s);
quiz 1-2
quiz
1-2 quiz
A NullPointerException will be thrown
quiz
The following line results in a NullPointerException. At most, how many references in this line could be null and might cause the exception to be thrown?
String s = person.friends[3].name[2];
four
five
four
How many auto-boxing operations occur in this statement?
Integer[] integers = new Integer[] {10, new Integer(20), null, 30, 40, new integer(50)};
zero
one
two
three
four
three
A constructor which has thrown an exception will also return a reference to its constructed object
true
false
false
Check all the class (non-instance) data fields in the class Car:
public class Car {
private static String brand;
private double fuelConsumption;
public String color;
public static int maximumSpeed;
}
brand
maximumSpeed
Based on Java & CS300 naming conventions, what kind of method call is demonstrated by the following line of code:
Utils.run(otherArgs);
this is an example of an instance method call
this is an example of a static method call
this is an example of a static method call
A checked exception’s class hierarchy —- contains the class RunTimeException
must
must NOT
must NOT
To indicate that a DatFormatException may be thrown by my method, you should add the following clause to the method signature line:
throw new DataFormatException
throws DataFormatException
throws DataFormatException
To catch an IllegalArgumentException or an IllegalFormatException but NOT a NullPointerException, the following catch statement should be used:
catch (IllegalFormatException iform)
catch (IllegalArgumentException iarg)
catch (RuntimeException run)
It’s not possible to construct a single-type catch statement to catch both an IllegalArgument & IllegalFormat but not NullPointer
catch (IllegalArgumentException iarg)
Which of the following is true about exception handling in Java?
It is NOT a good practice to throw exceptions with descriptive error messages
It is NOT recommended to catch Throwable (the superclass of Exception). A throwable in a catch clause wont’ catch only all exceptions but it will also catch all errors
A correctly written tester method should NOT throw any exceptions; it should always return either true or false
If some code within a method throws an UNCHECKED exception, then the method must either handle the exception using an appropriate try-catch block or it must specify/declare the exception using throws keyword in its method signature
If some code within a method throws a CHECKED exception, then the method must either handle the exception using an appropriate try-catch block or it must specify/declare the exception using throws keyword in its method signature
It is NOT recommended to catch Throwable (the superclass of Exception). A throwable in a catch clause wont’ catch only all exceptions but it will also catch all errors
A correctly written tester method should NOT throw any exceptions; it should always return either true or false
If some code within a method throws a CHECKED exception, then the method must either handle the exception using an appropriate try-catch block or it must specify/declare the exception using throws keyword in its method signature
Given the following class header: public class Scheduler extends TaskList implements Delegate { … }, TaskList is the
parent class
child calss
interface
parent class
Complete the following statement: A base class can contain a general (default) implementation of a method that can be —— with its EXACT signature in its derived classes
overloaded
overriden
overriden
Which of the following contains only the signature of a method, which must be implemented in its implementing classes?
A parent class
An interface
An interface
Can the removePage() method in Sketchbook modify the title field directly?
public class Book {
private int numPages;
protected String title;
}
public class Sketchbook extends Book {
public void removePage() { … }
}
yes
no
Yes
Will the following code fragment (from a class with no explicit parent class) compile without errors?
@Override
public String toString() {
// some implementation
}
yes
no
yes
Which of the following best describes the usage of the @Override annotation?
The @Override tag is required on all overridden methods, otherwise the code does not compile
The @Override tag is included in the JavDoc method comment for overridden methods
The @Override tag prompts the compiler to verify that a parent class also contains this method
The @Override tag has no effect aside from improving human readability of code
The @Override tag prompts the compiler to verify that a parent class also contains this method
Given the following class definition, which methods MUST be overriden by the class grade?
public class Grade implements Comparable<Grade> {
private char letterGrade;
private double score;
public Grade(double score) {
// Correct implementation here
}
}
public String toString()
public boolean equals(Object o)
public boolean equals(Grade o)
public int compareTo(Grade o)
public int compareTo(Grade o)
class Pokemon implements Comparable <Pokemon> {}
class WaterType extends Pokemon {}
class Squirtle extends WaterType {}
class Wooper extends WaterType {]
Given the above class definitions, which one of the lines below contains a Java syntax code or throws a runtime exception
A: Pokemon p = new Wooper();
B. Object obj = p;
C. WaterType h20 = (WaterType) p;
D. Squirtle sq = (Squirtle) h20;
E. Wooper woop = (Wooper) h20;
D. Squirtle sq = (Squirtle) h20
What is the primary goal of algorithm analysis?
To evaluate time efficiency of the algorithm when a problem size grows
To determine the correctness of the algorithm considering all test scenarios
To ensure the algorithm terminates in a finite amount of time
To evaluate time efficiency of the algorithm when a problem size grows
Which of the following are considered constant-time operations when analyzing an algorithm’s time complexity with respect to problem size n?
Assigning a value to a variable
EX: x = 5
Comparing two values in a conditional statement
EX: if (a < b)
Iterating through a loop n times
Performing an arithmetic operation
EX: sum = a + b
Assigning a value to a variable
Comparing two values in a conditional statement
Performing an arithmetic operation
An algorithm always performs the following actions:
Create a 1 dimensional array of length N in 3 total basic operations
Fill the first 10 elements of the array, using 2 basic operations per index
What is the Big-o notation for this algorithm, if n is the length of the array
O(1)
O(N)
O(1)
The runtime complexity of an algorithm which performs 2*N + 1 basic operations is
constant
linear
quadratic
linear
What is meant by the “worst case” for an algorithm when analyzing its runtime complexity?
The most frequently provided input to the algorithm
The input which makes the algorithm do the least ork
The input which makes the algorithm do the most work
The input which makes the algorithm do the most work
Which of the following algorithms has the most efficient runtime complexity T(N)?
T(N) = 3N² + 100N + 5
T(N) = 100N + 500
T(N) = 2^N
T(N) = 2N³ + 50N² + 1000
T(N) = 100N + 500
What is the Big-Oh worst case runtime complexity of the following code when the problem size N grows with no limit; where the problem size N represents the length of the array candidates
public void printNames(String[] candidates) {
for (int i = 0; i < candidates.length - 10; i ++)
System.out.println(i + “:” + candidates[i]);
}
O(1)
O(N)
O(N²)
O(10^N)
O(N)
What is the runtime complexity of the following secondDegreePolynomialFunction() method, assuming that problem size is the input parameter n?
public static double secondDegreePolynomialFunction(int n) {
if (n <= 0)
return n
n - 2 n;
return n * n + n / 2.0 + 5;
}
O(1)
O(n)
O(n * log n)
O(n²)
O(1)
What is the worst case runtime complexity of the following helper() method, assuming that the problem size n is the length of the input array data?
public static boolean helper(int[] data) {
int i = 0;
while(i < data.length) {
for (int j = 0; j < i; j++) {
if (data[j] == data[i])
return true;
}
i++;
}
return false;
}
O(1)
O(n)
O(n²)
O(n²)
Which of the following Big O time complexities corresponds to the LEAST efficient algorithm?
O(1)
O(N)
O(N²)
O(N²)
When a recursive method calls itself, we say that the new activation record / stack frame for the method call foes on the — of the call stack
top
bottom
top
The case where a recursive method calls itself is called the
base case
recursive case
recursive case
Given that (char)90 gives ‘Z’ what line should be added so that a call of recursiveBeta(90) returns the String “VWYZ”?
public static String recursiveBeta(int n) {
// add line here
return ““ + recursiveBeta(n-1) + (char) n;
if (n==85) return ‘‘;
if (n-==85) return (char) n;
if (n==85) return ‘‘‘;
Given the following recursive method, which value for ARG will give a method which eventually terminates (for all values of n)?
public static int recurse(int n) {
if (n ← 0) return 50;
return n + recurse( ARG );
}
n - 1
n + 1
n - 1
In which of the following cases will a StackoverflowError be thrown from a recursive method when it runs?
No base case is defined
Only one base case is defined
The recursive case reduces the problem to a smaller version to the original problem
The recursive case does not make progress towards at least one base case
no base case is defined
The recursive case does not make progress towards at least one base case
Which of the following statements is TRUE?
Recursion always uses less memory compared to iteration when it executes
Iteration is always more efficient & simpler than recursion
Recursion is always more efficient than iteration
It is always possible to write the iterative version of a recursive algorithm
It is always possible to write the iterative version of a recursive algorithm