Java Object-Oriented Programming Concepts and Questions

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/71

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

72 Terms

1
New cards

Object in Java

An object is something that contains both state and behavior.

2
New cards

Difference between class and object

Objects are instances of classes. The class is the general template, and the object is the specific version.

3
New cards

Client of a class

Being a client of a class means that we can use its methods and functionality without necessarily understanding how it works.

4
New cards

Constructor in Java

A constructor allows us to create a new instance of a class, usually initializing instance variables.

5
New cards

Card class constructor implementation

suit = cardSuit; value = cardValue;

6
New cards

Instance method

An instance method is a piece of code called on a specific instance of the class. It is called with a receiver object.

7
New cards

Rectangle class area retrieval

The correct syntax for retrieving the area of shape is shape.getArea().

8
New cards

accessor method

Refers to the same idea as a getter method.

9
New cards

Math.abs(x)

An example of calling a static method to get the absolute value of x.

10
New cards

wrapper class

Allows a primitive value to be converted to an object.

11
New cards

A Double is unboxed

When it is converted to a primitive value.

12
New cards

A int is autoboxed

When it is converted to an Integer.

13
New cards

valid methods

Methods that can coexist in the same class with the same name due to different parameters. i, ii, and iv

14
New cards

// POINT A

A point in the code where the scope of variables is evaluated. sum and count

15
New cards

what is printed by the program?

hi

16
New cards

this keyword

Refers to the current instance of the class.

17
New cards

Circle class output Circle one = new Circle(10); Circle two = new Circle(10); System.out.println(one == two);

false

18
New cards

Circle class output public void run() { Circle one = new Circle(5); Circle two = new Circle(10); foo(two); System.out.println(one.equals(two)); } public void foo(Circle x) { x.setRadius(5); }

true

19
New cards

superconstructor call

You call it by making a call with the word super in the subclass constructor.

20
New cards

Movie class inheritance

Both SciFi and Fantasy inherit constructors from Movie.

21
New cards

Tool class hierarchy

The proper first line of each class is public class Hammer extends Tool and public class Screwdriver extends Tool.

22
New cards

Clothing class methods

bottom up design

23
New cards

list of shoppers in a grocery line

arraylist

24
New cards

int[] arr = new int[5];

The correct syntax to create an array of 5 integers in Java.

25
New cards

arr[4]

The value at the fifth position of the array, which is modified to 2 in the provided code.

26
New cards

findMin method replacement for some value

i and iii only

27
New cards

for-each loop instead of for loop

access every element and refer to elements through variable name rather than array index

28
New cards

you can modify the elements of an arraytraverse with a for-each loop

false

29
New cards

output of the code snippet

The printed values of the scores array:

80,

92,

91,

68,

88.

30
New cards

What will the following code snippet output?

int[ ] values = {17, 34, 56, 2, 19, 100};

for (int value : values)
{
    if (value % 2 == 0)
        System.out.println(value + " is even");
}

The code snippet will output the even numbers from the values array, each followed by the phrase "is even:
34 is even
56 is even

2 is even
100 is even".

31
New cards

Given the following code snippet

int[ ] values = {150, 34, 320, 2, 11, 100};

for (int i=0; i < values.length; i++)
{
       if (values[i] % 10 == 0)
       {
           System.out.println(values[i] + " is divisible by 10");
       }
}

which for-each loop would produce the same output?

for (int value : values)
{
if (value % 10 == 0)
System.out.println(value + " is divisible by 10");
}

32
New cards

key difference between arrays and ArrayLists

Arrays are fixed size but ArrayLists can change in size.

33
New cards

create ArrayList

ArrayList<String> list = new ArrayList<String>();

34
New cards

not cause IndexOutOfBoundsException

list.add(list.size(), new Dog());

35
New cards

equivalent for loop

for (int i = 0; i < 20; i += 2)
{
System.out.print(i);
}

36
New cards

remove multiples of 3

No, this method is not written correctly, as the counter in the if statement will skip the next value, as the values will shift down in the ArrayList.

Answered


37
New cards

Which of these methods will properly traverse two ArrayLists and print any index that have the same value in both ArrayLists?

{
int index = 0;
int size;
if(array1.size() > array2.size())
{
size = array2.size();
}
else
{
size = array1.size();
}
while(index < size)
{
if(array1.get(index) == array2.get(index))
{
System.out.println(index);
}
index++;
}
}

38
New cards

grid[0].length

3

39
New cards

hich of the following are valid statements in Java?
I:

ArrayList<String> list = new List<String>();

II:

List<String> list = new ArrayList<String>();

III:

List<String> list = new List<String>();

ii only

40
New cards

A method is to be written to search a 2D array for a value that is larger than a given item and return its index. The problem specification does not indicate what should be returned if there are several values that are larger in the 2D array. Which of the following would be the best course of action?

specification should be modified to specify if there are multiple larger value indexes

41
New cards

Question: 41

A 2D double array terrainMap is declared and initialized to track the terrain of a city park. Each value in the 2D array represents the height of a particular latitude and longitude above sea level. Longitude is represented by the columns in the 2D array and latitude is represented by each row in the 2D array.

Which of the following would be the correct way to print out all the indices that are more than 5 feet above sea level?

public static void above5(double[][] array)
{
for(int row = array.length-1; row >= 0; row--)
{
for(int column = 0; column < array[row].length; column++)
{
if(array[row][column] > 5.0)
{
System.out.println(row +"," + column);
}
}
}

}

42
New cards

he creator of this 2D array would like to look at the height of the city park along the longitude 100.0 - which traversal method would make the most sense in order to do so?

column major order

43
New cards

Which of the following would be a correct way to write the traversal for this 2D array?

for(double[] row: terrainMap)
{
for(double num: row)
{
System.out.print(num + " ");
}
System.out.println();
}

44
New cards

What method do you use to add a key-value pair into a HashMap?

put.

45
New cards

binary number system

The number base of the binary number system is 2.

46
New cards

place value of the 1 in 1002

The place value of the 1 in the binary value 1002 is 4s place.

47
New cards

maximum iterations for binary search in 256 elements

The maximum number of iterations needed to find an element in an array containing 256 elements is 8.

48
New cards

precondition for binary search

The array must be sorted.

49
New cards

comparisons in selection sort for 8 elements

The selection sort algorithm will make 28 comparisons on an array of 8 elements.

50
New cards

insertion sort after third pass

After the third pass of the for loop, the array will look like [50, 40, 30, 10, 20, 60].

51
New cards

count < 5 at point 1

count < 5 is always true at point 1.

52
New cards

count < 5 at point 2

count < 5 is sometimes true/sometimes false at point 2.

53
New cards

count < 5 at point 3

count < 5 is always false at point 3.

54
New cards

loop iteration count < x

The loop iterates x times.

55
New cards

loop iteration count for count <= x

The loop executes x + 1 times.

56
New cards

How many times does the following loop execute?

// x has been initialized with a positive int value greater than 5
int count = 5;
while (count < x) 
{
    count++;
}

x-5 times

57
New cards

Why is having efficient algorithms important?

both 1 and 2

58
New cards

What is the definition of recursion?

Recursion is when a method calls itself.

59
New cards

What is the base case in a recursive statement?

The base case is the simplest problem to solve.

60
New cards

Why do we use recursion?

both options

We use it to break complex problems down into simpler problems that become easier to solve, and in place of loops.

61
New cards

Of the following problems, which one would a recursive function work best with?

Calculating factorial. Recall: 5 factorial = 5 4 3 2 1.

62
New cards

Given the following code, how many calls to the printX method would be made if we called with printX(20, 15);?

Correct Answer: 6.

63
New cards

Using the binary search algorithm, what is the maximum number of iterations needed to find an element in an array containing 256 elements?

Correct Answer: 8.

64
New cards

What is the precondition for binary search to work on an array?

Correct Answer: The array must be sorted.

65
New cards

For a large dataset (roughly 100,000 items), which search is more efficient to find a value?

Correct Answer: Binary searches are significantly more efficient.

66
New cards

Which best describes how a binary search works?

Binary searches start in the middle and eliminate half of the list in each iteration until the desired value is found.

67
New cards

A binary search can be written either with a loop or with a recursive function.

Correct Answer: True.

68
New cards

Which best describes a merge sort algorithm?

Correct Answer: Merge sort is a recursive sorting algorithm that can be used to sort elements.

69
New cards

How does a merge sort work?

Correct Answer: Sorts an array by dividing a list into parts, then merging it back together in the correct order.

70
New cards

Which part of the merge sort is accomplished with recursion?

Correct Answer: Recursion is used to break the list down repeatedly until it gets to one element.

71
New cards

What type of data set works best for merge sorts?

Correct Answer: Neither of these. Merge sorts work the same on all lists.

72
New cards

Why do we use merge sorts?

Correct Answer: They are very efficient regardless of how the data is organized.