Missed Questions Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

(double) (12/5) returns what?

A
  1. The cast is performed too late
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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)
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which algorithm provides the most efficient way of finding the largest of 2000 elements stored in an array (not necessarily sorted)?

A

Selection Sort (terminated after the first five iterations). Selection Sort sorts by finding the largest elements in the list, etc…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

If an abstract class has no implemented constructors or methods, it is better to make it an _______

A

interface. All the advantages of inheritance are preserved, pus a class that implements the interface can potentially extend some other class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can an abstract class extend another abstract class?

A

YES

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which of the following code segments correctly traverses a two-dimensional int array m, row by row?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Can an abstract class implement an interface?

A

YES

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can you declare an array of objects of an abstract class type, but not of an interface type?

A

NO

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

A method cannot take a parameter of an interface type nor abstract class type

A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Object has neither ______ nor ______ method

A

substring, compareTo

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

when removing elements from a list, be careful using a standard loop because:

A

list.remove(i) shifts the subscripts of the subsequent elements down by one, so only every other element is removed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

In the GridWorld design, why is the Actor class not made abstract, with the act method abstract?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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]?

A
  1. Binary search will look at arr[63], arr[31], arr[47], arr[39]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Assuming a and b are Boolean variables, what is the same thing as the following:

!(!a || b) || (!a && b)

A

= (a && !b) || (!a && b). This evaluates to true if and only if a and b have different values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

If Game is an interface and Fun a class, then:

Game game = fun; is _____

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Integer.MAX_VALUE is determine by ____

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How to compare if two strings hold the same values?

A

str1.equals(str2) OR str1.compareTo(str2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

A class does not NEED to define a ________

A

constructor. If it does not, then Object’s is called

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

When overriding Object’s equals method, the input to the function must be of type _____

A

Object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Constructors’ return type must be declared void

A

FALSE. constructors do not have return types

22
Q

If a class implements an interface and fails to define ALL the methods of that interface, then the class must be declared

A

ABSTRACT

23
Q

_____ AND ______ have NO “.equals” methods, but ______ and ________ DO

A

int, double, Integer, Double

24
Q

if (!somethingIsFalse) explained

A

the “if” will only be passed if the !somethingIsFalse is true. that means that somethingIsFalse must be false.

25
Q

You cannot ________ an abstract class, but you can declare ________ or _________ of its type.

A

instantiate, variables, arrays

26
Q

If “list” is an ArrayList with two elements, attempting list.get(list.size()) will return a __________ error.

A

IndexOutOfBounds

27
Q

String[] msg = new String[2]

msg[0].substring(0, 2)

A

msg[0] is not defined so you cannot call its methods. This will throw a NullPointerException

28
Q

When designing a class hierarchy:

A

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.

29
Q

In general, if the recursive call is made (assuming everything else works) BEFORE the printing:

A

the printed recursion will go in order. vice versa

30
Q

if ( (a+b) * (a-b) != (ab) - (bb) )

is what:

A

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.

31
Q

Adding elements into an ArrayList at n

A

inserts it at n and pushes all other elements (including the one previously at n) down one in the list

32
Q
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:
A

perform(Dance dance) OR perform(Object dance). BOTH are valid

33
Q

Even when given an instance of a class in another class, calling c.value (with value private in class C) will:

A

give a syntax error UNLESS the call is made in c.

34
Q

If HouseForSale extends House, making a new HouseForSale will run HouseForSale’s constructor which will call _______

A

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.

35
Q

If a superclass and its subclass each have methods with the same name and input(s):

A

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

36
Q

Static methods cannot access or modify any _____ variables and cannot refer to ________ because ________ is undefined when a static method is running

A

instance, this, this

37
Q

Non-static methods, however, CAN refer to ______ variables

A

static

38
Q

ArrayList IS a _____

A
List. it inherits all of its methods. ArrayList list = new List();
IS ILLEGAL CODE
39
Q

Interfaces can be used when:

A

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.

40
Q

A feature of data that is used for a binary search but not necessarily for a sequential search is:

A

order of data. The binary search algorithm depends upon the list being sorted.

41
Q

When sorted biggest to smallest with insertionSort, which list will need the fewest changes of position for individual elements?

A

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).

42
Q

When sorted biggest to smallest with insertionSort, which list will need the greatest number of changes in position?

A

The list that is sorted in reverse order (or closest to reverse order).

43
Q

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?

A

merge two sorted parts of arr into a single sorted array

44
Q

Creating a class Library that has an ArrayList of type Book, and testing the code on Book first is an example of what development?

A

bottom-up

45
Q

if secondTestMethod() changes an array, then System.out.print(array) returns

A

the original array.

46
Q

list.add(list.size(), something) does NOT return an error because:

A

list.add(list.size(), something) adds the variable to the end of the list.

47
Q

The number of _______ for _____ sort is independent of the initial arrangement of elements

A

comparisons, selection

48
Q

String s = “holy”
s.substring(4)
returns ?

A

an empty string. NOTE: if substring(5) is called (greater than the length of “holy”) then you will get an IndexOutOfBoundsException

49
Q

Composition Definition:

A

Composition is the has-a relationship. (vs. inheritance -> is-a relationship) For this question: ArrayList list, was in the Library class

50
Q

Encapsulation definition:

A

Combining an object’s data and methods into a single unit called a class is known as encapsulation.