Missed Questions Flashcards
(double) (12/5) returns what?
- The cast is performed too late
Given that List doNothing(List list) simply returns “list” and is in class Test, what will happen in:
ArrayList nums = new ArrayList(); nums = Test.doNothing(nums)
doNothing returns a List, not an ArrayList, and you can’t assign a List object to an ArrayList variable. An ArrayList IS an List, however, so calling doNothing(ArrayList) IS valid.
Which algorithm provides the most efficient way of finding the largest of 2000 elements stored in an array (not necessarily sorted)?
Selection Sort (terminated after the first five iterations). Selection Sort sorts by finding the largest elements in the list, etc…
If an abstract class has no implemented constructors or methods, it is better to make it an _______
interface. All the advantages of inheritance are preserved, pus a class that implements the interface can potentially extend some other class
Can an abstract class extend another abstract class?
YES
Which of the following code segments correctly traverses a two-dimensional int array m, row by row?
A 2D array is implemented as a 1D array of its rows, where each row is a 1D array. Therefore, the elements of m have the type int[], not in.
Can an abstract class implement an interface?
YES
Can you declare an array of objects of an abstract class type, but not of an interface type?
NO
A method cannot take a parameter of an interface type nor abstract class type
TRUE
Object has neither ______ nor ______ method
substring, compareTo
when removing elements from a list, be careful using a standard loop because:
list.remove(i) shifts the subscripts of the subsequent elements down by one, so only every other element is removed.
In the GridWorld design, why is the Actor class not made abstract, with the act method abstract?
To be able to create Actor objects in GridWorld applications and explore their attributes and behavior. You cannot create an object of an abstract class.
Suppose an array contains 127 different random values arranged in ascending order, and a most efficient searching algorithm is used to find a target value. How many elements of the array will be examined when the target equals arr[39]?
- Binary search will look at arr[63], arr[31], arr[47], arr[39]
Assuming a and b are Boolean variables, what is the same thing as the following:
!(!a || b) || (!a && b)
= (a && !b) || (!a && b). This evaluates to true if and only if a and b have different values.
A project needs two related classes, X and Y. A programmer has decided to provide an abstract class A and derive both X and Y from A rather than implementing X and Y completely independently of each other. Which of the following is nOT a valid rationale for this design decision.
Being able to cast objects of type X into Y and vice-versa. This is NOT a good idea. They are both different animals, and both merely extend a super class, but NOT each other.
If Game is an interface and Fun a class, then:
Game game = fun; is _____
an indication that Fun implements Game. You CANNOT instantiate (make objects of) an interface. Game game = new Game(); is NOT legal code. You can, however, refer to an object that implements an interface by the type of the interface.
Integer.MAX_VALUE is determine by ____
the size of int, which is ALWAYS 4 bytes, no matter the platform that Java is running. Java source code is compiled into bytecodes, which may then be run on any computer that has a Java Virtual Machine installed.
How to compare if two strings hold the same values?
str1.equals(str2) OR str1.compareTo(str2)
A class does not NEED to define a ________
constructor. If it does not, then Object’s is called
When overriding Object’s equals method, the input to the function must be of type _____
Object
Constructors’ return type must be declared void
FALSE. constructors do not have return types
If a class implements an interface and fails to define ALL the methods of that interface, then the class must be declared
ABSTRACT
_____ AND ______ have NO “.equals” methods, but ______ and ________ DO
int, double, Integer, Double
if (!somethingIsFalse) explained
the “if” will only be passed if the !somethingIsFalse is true. that means that somethingIsFalse must be false.
You cannot ________ an abstract class, but you can declare ________ or _________ of its type.
instantiate, variables, arrays
If “list” is an ArrayList with two elements, attempting list.get(list.size()) will return a __________ error.
IndexOutOfBounds
String[] msg = new String[2]
msg[0].substring(0, 2)
msg[0] is not defined so you cannot call its methods. This will throw a NullPointerException
When designing a class hierarchy:
A superclass should contain the data and functionality that are common to all subclasses that inherit from the superclass. A superclass’ data does NOT necessarily all have to be public.
In general, if the recursive call is made (assuming everything else works) BEFORE the printing:
the printed recursion will go in order. vice versa
if ( (a+b) * (a-b) != (ab) - (bb) )
is what:
true. Roundoff error makes the if condition true. Exact equality never works. One should instead take the difference and see if it’s less than some acceptable value.
Adding elements into an ArrayList at n
inserts it at n and pushes all other elements (including the one previously at n) down one in the list
Classes Salsa and Swing implement Dance. Both perform(new Salsa()) and perform(new Swing()) are valid. The definitions of the performer method in Dance could be:
perform(Dance dance) OR perform(Object dance). BOTH are valid
Even when given an instance of a class in another class, calling c.value (with value private in class C) will:
give a syntax error UNLESS the call is made in c.
If HouseForSale extends House, making a new HouseForSale will run HouseForSale’s constructor which will call _______
House’s constructor BEFORE anything else, whether it is explicitly written or not. If House does not have a no args constructor, than you MUST manually call “super(something)” BEFORE anything else, otherwise you will get a syntax error.
If a superclass and its subclass each have methods with the same name and input(s):
then calling the method from the subclass will run the subclass’ version of the method. If it cannot find the method in the subclass, then the parent’s version will be run
Static methods cannot access or modify any _____ variables and cannot refer to ________ because ________ is undefined when a static method is running
instance, this, this
Non-static methods, however, CAN refer to ______ variables
static
ArrayList IS a _____
List. it inherits all of its methods. ArrayList list = new List(); IS ILLEGAL CODE
Interfaces can be used when:
two classes exist that are not connected at all, but both have similar methods (maybe both are washable). Then implement this interface on the two classes.
A feature of data that is used for a binary search but not necessarily for a sequential search is:
order of data. The binary search algorithm depends upon the list being sorted.
When sorted biggest to smallest with insertionSort, which list will need the fewest changes of position for individual elements?
Whichever list has the fewest elements out of order, regardless of how far away they are from their correct position in the list. (Also regardless of whether this element needs to be moved to the right or to the left).
When sorted biggest to smallest with insertionSort, which list will need the greatest number of changes in position?
The list that is sorted in reverse order (or closest to reverse order).
Assuming mergeSort will be used to sort an array arr of n integers into increasing order. What is the purpose of the merge method in the mergeSort algorithm?
merge two sorted parts of arr into a single sorted array
Creating a class Library that has an ArrayList of type Book, and testing the code on Book first is an example of what development?
bottom-up
if secondTestMethod() changes an array, then System.out.print(array) returns
the original array.
list.add(list.size(), something) does NOT return an error because:
list.add(list.size(), something) adds the variable to the end of the list.
The number of _______ for _____ sort is independent of the initial arrangement of elements
comparisons, selection
String s = “holy”
s.substring(4)
returns ?
an empty string. NOTE: if substring(5) is called (greater than the length of “holy”) then you will get an IndexOutOfBoundsException
Composition Definition:
Composition is the has-a relationship. (vs. inheritance -> is-a relationship) For this question: ArrayList list, was in the Library class
Encapsulation definition:
Combining an object’s data and methods into a single unit called a class is known as encapsulation.