Final Exam Review - ITSC 1213 - Intro to Comp Sci II Java

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

1/150

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.

151 Terms

1
New cards

Java is an ______ ________ programming language

object oriented

2
New cards

Class

Defines new data types as a blueprint for objects

3
New cards

Objects

Instances of a class that are variables. These do all the work

4
New cards

Variable

Generic term for a name that stores a value in memory

5
New cards

Instance Variables

A field created when an object is instantiated inside a class but outside a method, they define the properties or attributes, usually private

6
New cards

Methods

What an object does, usually public

7
New cards

Constructor

Initialize attributes in new objects

8
New cards

Overloading

More than one constructor to one class. Has different parameter signature

9
New cards

Overriding

Has the same parameter signature but in a different class (parent, child)

10
New cards

Formal Parameter

Description of the type of variable being passed

11
New cards

Actual parameter

Values passed

12
New cards

Polymorphism

The ability of objects of different classes to be treated as objects of a common parent class

13
New cards

Fields

Hold the attributes/data/state of an object

14
New cards

Accessor Method

Gets and returns the value of an instance variable

15
New cards

Mutator Method

Sets the instance variable

16
New cards

Primitive Data Type

Store values directly. boolean, int, double, char, long, float

17
New cards

Reference Data Type

Stores a memory location. Strings are reference types

18
New cards

Final Keyword

Variable can't be changed

19
New cards

Type Casting

Converting a variables values between types

20
New cards

Data Encapsulation

Bundling the attributes and methods that operate on the data into a single class, and controlling access to that class through access modifiers. This helps in data security and maintaining the integrity of the program.

21
New cards

Difference between ! , && , and ||

! - not

&& - and

|| = or

22
New cards

ArrayList methods: what do these do?

.get(i)

.size()

.contains(x)

.set(i,x)

.add(x)

.add(i,x)

.remove(i)

.remove(x)

Import statement?

Type declaration?

.get(i) - Returns the object at index i.

.size() - Returns # of objects in the ArrayList.

.contains(x) - Returns true if the ArrayList contains x.

.set(i,x) - Replaces object at index i with object x.

.add(x) - Adds object x to the end of the ArrayList.

.add(i,x) - Adds object x at position i of the ArrayList.

.remove(i) - Removes object at index i.

.remove(x) - Removes object x from the ArrayList.

Import: import java.util.ArrayList;

Type declaration: ArrayList list = new ArrayList<>();

23
New cards

How many parents can a class have?

One

24
New cards

Keyword for inheritance?

Extends

25
New cards

Sequential / Linear Search

Looks through all items until it either finds desired value and returns the index of it or returns -1 if it did not find it

26
New cards

Binary Search

Can only be used on sorted data or data stored in order. It checks the middle value to see if it is less than, equal to, or greater than, the desired value. Cuts search space in half each time

27
New cards

Selection Sort

Select the smallest item from the current location on to the end of the array and swap it with the value at the current position. Do this from index 0 to the array length - 2. You dont have to process the last element in the array, it will already be sorted when you compare the prior element to the last element

28
New cards

Insertion Sort

Insert the next unsorted element in the already sorted part of the array by moving larger values to the right. Start at index 1 and loop through the entire array

29
New cards

Merge sort

Break the elements into two parts and recursively sort each part. An array of one item is sorted (base case). Then merge the two sorted arrays into one

30
New cards

NullPointerException

This exception occurs when you try to access or call methods on an object reference that points to null. In other words, you're trying to use an object that hasn't been initialized or doesn't exist

31
New cards

ArrayIndexOutOfBoundsException

This exception occurs when you try to access an index of an array that is outside of its bounds

32
New cards

InputMismatchException

This exception occurs when the input provided by the user does not match the expected type or format. It is often used in input/output operations such as reading from the console or files

33
New cards

ArithmeticException

This exception occurs when an arithmetic operation encounters an exceptional condition. This typically includes division by zero or other mathematical operations that result in an undefined value

34
New cards

FileNotFoundException

This exception occurs when attempting to access a file that does not exist or cannot be found at the specified path

35
New cards

StringIndexOutOfBoundsException

This exception occurs when you attempt to access characters in a string using an index that is outside the bounds of the string

36
New cards

NumberFormatException

This exception occurs when you try to convert a string to a numeric type (like int or double), but the string does not contain a valid representation of the desired type

37
New cards

Main Method

Where execution starts in a Java program. Begins when you hit run

38
New cards

Abstract Classes

Are classes that cannot be instantiated; they have to be inherited from to be used. They can have abstract and concrete methods but concrete classes can't have abstract methods

39
New cards

Interfaces

All methods are abstract

No constructor

All methods from an interface have to be overridden

Can use interface datatype for objects that implement it

Built-in interfaces like 'Comparable'

Can be implemented 'infinity'

40
New cards

Try Block

Needs 1. Contains code that might create exceptions you want to handle

41
New cards

Catch Block

Needs 1 but can have as many as you want. A segment of code that can handle an exception that might be thrown

42
New cards

Finally Block

Not necessary but can have 1 finally block. Follows all catch blocks, and executes after the program exits

43
New cards

Exception Handling

Use of try, catch, and sometimes finally blocks to handle errors that may stop a program.

44
New cards

Throwing Exceptions

Creates an exception object using a throw keyword to handle a situation directly. Used to create custom exceptions with specific error conditions

45
New cards

Throws Keyword

To declare that the method might throw an exception and it is at the end of the method signature

46
New cards

Code to create a new Scanner?

Scanner scanner = new Scanner(System.in);

47
New cards

What should be inserted after goToClass() ?

public interface MyInterface {

public void goToClass()

}

; because it is an interface so the method is abstract

48
New cards

Code to create an interface?

public interface InterfaceName {}

49
New cards

Which of the following lines is the constructor?

public abstract class Person {

private String name;

public Person(String name) {

this.name = name;

}

public Person() {

this.name = "";

}

}

public Person(String name) {

this.name = name;

}

You know this because the constructor name matches the class name. Constructors also do not have a return type

50
New cards

What are Person and Schedule?

public class Student extends Person implements Schedule {}

Person is a parent class, Schedule is an interface

51
New cards

Which of the following is equivalent to the statement below?

! ((a <= b) && (b < 0))

a. (a >= b) && (b >= 0)

b. !(a > b) || !(b >= 0)

c. (a >= b) || (b > 0)

d. (a > b) || (b >= 0)

e. (a > b) && (b >= 0)

d. (a > b) || (b >= 0)

The code is saying that not both conditionals - (a <= b) and (b < 0) - are true.

Per De Morgan's Laws:

- !(A && B) is equivalent to (!A || !B)

So: (!A <= B) || (!B < 0)

- >= turns into < and vice versa

So: !(a <= b) is equivalent to (a > b)

- < turns into >= and vice versa

So: !(b < 0) is equivalent to (b >= 0)

52
New cards

What does mystery(4) return?

public int mystery(int m) {

if (m == 1) {

return 3;

}

else {

return 3 * mystery(m - 1);

}

}

mystery(4) returns 81.

Since mystery(4) ! = 1, it goes to the else block. It will return: 3 mystery(m - 1) which simplifies to 3 mystery(3).

mystery(3) has to be evaluated and put back into the program the same way which results in 3 * mystery(2).

Continue until you get 3 * mystery(1). And now since mystery(1) can be input since m == 1 we can work our way up. m == 1 so it returns 3.

mystery(1) returns 3

mystery(2) is 3 mystery(1) so 3 3 = 9

mystery(3) returns 3 * mystery(2) so 27

mystery(4) returns 3 mystery(3) which is 3 27 = 81

So mystery(4) returns 81

53
New cards

In which of these cases will an ascending order (from smallest to largest) insertion sort have the fastest run time?

I. An array that is in reverse order (from largest to smallest).

II. An array that is in sorted order already (from smallest to largest). III. An array that is in random order (not already sorted).

II only

I only

I and II

II and III

III only

II only because the list would already be in order.

54
New cards

Which of these loops will output 01234?

int max = 5;

//Loop I

for (int i = 0; i < max; i++)

{ System.out.print(i); }

//Loop II

int j = 0;

while (j < max)

{ System.out.print(j); j++; }

//Loop III

int k = 0;

for (int i = max; i > 0; i--)

{ System.out.print(i); }

I only

II only

II and III only

I and II only

I, II, and III

I and II

Loop I iterates from 0 to max - 1 with a for loop

Loop II iterates from 0 to max - 1 with a while loop

Loop III iterates from max to 1 and would output 54321

55
New cards

Consider the following block of code. What are the first and last numbers printed after running the code?

int value = 15;

while (value < 30)

{

value++;

System.out.println(value);

}

First: 16

Last: 30

When 15 is input it is +1 into 16 then printed.

When 29 is input it is output as 30 for the same reason. 30 Cannot pass back into the while loop so it ends there.

56
New cards

Consider the following block of code. What value is returned from solution(5)?

public int solution(int limit)

{

int s = 0;

for (int outside = 1; outside <= limit; outside++)

{

for (int middle = 1; middle <= limit; middle++)

{

for (int inside = 1; inside <= limit; inside++)

{ s++;

}

}

}

return s;

}

125

The outer loop (outside) runs from 1 to limit.

For each iteration of the outer loop, the middle loop (middle) also runs from 1 to limit.

And for each combination of outside and middle, the innermost loop (inside) runs from 1 to limit.

For each iteration of the innermost loop, s is incremented by 1.

So, the code iterates through all combinations of outside, middle, and inside, and for each combination, it increments s by 1.

This results in a total of limit limit limit iterations, where limit is the input parameter to the solution method.

Therefore, at the end 5 5 5 iterations will have run and would return an s of 125

57
New cards

Given that both count and n are integer values, which of the following statements is true about both code blocks?

// Code block I

for (count = 0; count <= n; count++) {

System.out.println(count);

}

//Code block II count = 0;

while (count <= n) {

count = count + 1;

System.out.println(count);

}

I and II are equivalent for all input values n

I and II are only equivalent when n is even

I and II are only equivalent when n is 0

I and II are equivalent for all values except n = 0

I and II will never have the same exact output

I and II will never have the same exact output

In block II the increment count = count + 1 happens at the beginning of the loop body and is then printed. So when the number 0 is passed 1 will be printed

On the contrary, in block I the increment count++ happens at the end of the iteration so when 0 is passed 0 will be printed and then +1 after that

58
New cards

Consider the following class declarations. Which statements are true?

public class Animal {

/ Some code /

}

public class Cat extends Animal {

/ Some code /

}

I. Cat inherits the constructors of Animal

II. Cat cannot add new methods and private instance variables that Animal does not have

III. Cat can override existing public methods of Animal

I only

II only

III only

I and II

II and III

III only

Animal is a parent class of Cat

I - False - Subclasses need to specify its own constructors

II - False - A subclass can add new methods and instance variables that the parent class does not have

III - True - Cat can override public methods in Animal

59
New cards

Consider the following code. What is the maximum amount of times that HELLO could possibly be printed?

for (int i = 0; i <= k; i++) {

if (arr[i] < someValue) {

System.out.print("HELLO") ; }

}

k

k + 1

k - 1

1

0

k + 1

The maximum amount of times "HELLO" could possibly be printed is when all elements from index 0 to index k (inclusive) in the array arr are less than someValue.

So since it is from 0 to k inclusive it is k + 1. If it were not inclusive it would just be k

60
New cards

When will the method stringRecursion produce a run time error?

public void stringRecursion(String s) {

if (s.length() < 16)

{

System.out.println(s);

}

stringRecursion(s + "*");

}

It will never produce a run time error

It will always produce a run time error

Only when the input length is >= 16

Only when an empty string is input

Whenever the input string length is < 16

It will always produce a run time error

The method takes an input s and checks if it is less than 16. If it is it will print it out and then call stringRecursion with s + "*" as an argument

The method stringRecursion does not have any termination condition which results in an infinite recursion. The stringRecursion needs to be inside of the if loop, not outside.

61
New cards

Consider the following class definitions. Which of I, II and III below would cause an error when used in place of the missing code in the main method?

public class A

{

public void method1() { };

}

public class B extends A

{

// Instance variables and other methods not shown

public void method1()

{

/ implementation not shown /

}

}

public class C extends B

{

//Instance variables and other methods not shown

public void method2(C o) {

/ implementation not shown /

}

public static void main(String[] args) {

C objectC = new C();

B objectB = new B();

// Missing code

}

}

I objectC.method1();

II objectB.method2(objectC);

III objectC.method2(objectB);

I only

II only

II and III

III only

I, II, and III

II and III

I does not cause an error because the line calls method1 of an instance of class C. method1 is defined in class A which is inherited by C

II creates an error because objectB is an instance of classB so it does not have access to method2 which is defined in class C

III creates an error because since method2 expects a C argument, it cannot take an objectB argument

62
New cards

Which of these declarations will not cause an error?

I ArrayList stringList = new List();

II ArrayList intList = new ArrayList();

III ArrayList stringList = new ArrayList();

III ArrayList stringList = new ArrayList();

I causes an error because you need to say ArrayList and not just List

II causes an error because you cannot use primitive types such as int for an Arraylist and you need to say Integer

III would not cause an error, it's written perfect

63
New cards

Suppose that the following method takes in a two dimensional array called matrix. After the method call printMatrix(matrix) what will the output be? Possible options are listed below the method definition.

/ assume that matrix has the following values /

7654

3210

4567

0123

public static void

printMatrix(int[ ][ ] matrix) {

for (int i = 0; i < matrix.length; i++) {

for (int t = 0; t < i; t++) {

System.out.println(matrix[i][t]);

}

System.out.println();

}

}

Possible output:

I. 7654 3210 4567 0123

II. 7 32 456 0123

III. 3 45 012

IV. 7 3 4 0

ArrayIndexOutOfBoundsException thrown

III

3

45

012

For the first row (index 0), the inner loop does not execute because t < i is false. So, no elements are printed for this row.

For the second row (index 1), the inner loop executes once (t = 0). It prints the element at position [1][0], which is 3.

For the third row (index 2), the inner loop executes twice (t = 0 and t = 1). It prints the elements at positions [2][0] and [2][1], which are 4 and 5, respectively.

For the fourth row (index 3), the inner loop executes thrice (t = 0, t = 1, and t = 2). It prints the elements at positions [3][0], [3][1], and [3][2], which are 0, 1, and 2, respectively.

64
New cards

If randomList is an ArrayList of Integer objects and is initially set to {0, 1, 2, 3}, what will randomList look like after the following code is executed?

randomList.add(5);

randomList.add(7);

int randomNum = randomList.get(2);

randomList.remove(2);

randomList.add(randomNum, 4);

randomList.set(1, 8);

[0, 8, 4, 3, 5, 7]

[0, 1 , 2 , 3] is our start list.

Add 5 adds 5 to the end: [0, 1 , 2 , 3, 5]

Add 7 adds 7 to the end: [0, 1 , 2 , 3, 5, 7]

randomNum is grabbing the number at index 2 in the list which is 2 and then removes it: [0, 1 , 3, 5, 7]

integer 4 is inserted at index 2 because randomNum is 2: [0 , 1, 4, 3, 5, 7]

Lastly, index 1 is set to 8:

[0, 8, 4, 3, 5, 7]

65
New cards

Consider the following code segment. What will be printed as a result of executing the code below?

String str = "fedcba"; int counter = 0;

while(counter < str.length() - 1) {

System.out.print(str.substring(counter + 1, counter + 2)); counter++; }

edcba

edcb

Nothing is printed because an IndexOutOfBoundsException is thrown.

feeddccbba

fededcdcbcba

edcba

The while loop says: while the counter is less than the string length - 1, it will print out the substring starting at index counter + 1 (inclusive) and ends at counter + 2 (exclusive) and then counter is incremented

When counter is 0, it prints the character at index counter + 1, which is 'e'.

When counter is 1, it prints the character at index counter + 1, which is 'd'.

When counter is 2, it prints the character at index counter + 1, which is 'c'.

When counter is 3, it prints the character at index counter + 1, which is 'b'.

When counter is 4, it prints the character at index counter + 1, which is 'a'

66
New cards

Consider the following class declarations. Which of the following statements will not compile?

public class B {

public int myValue;

public B() {

myValue = 0;

}

public B(int x) {

myValue = x;

}

}

public class C extends B {

public C() {

super(0);

}

}

C c1 = new C();

B b1 = new B();

B c2 = new C();

B b3 = new B(10);

C c3 = new C(24);

C c3 = new C(24);

This will not compile because class C does not define a constructor to take an integer amount. It only has default constructor C( ) and the constructor that takes super(0). Class C needs to explicitly create a constructor to take integers to create an object that will accept an integer argument

67
New cards

Consider the following method. Assume that String s = "rain"; and int b = 4; have been executed. What are the values of s and b after test(s,b) is executed?

public static void test(String str, int y) {

str = str + "bow";

y = y * 2;

}

s="rainbow"; b=8;

s="rain"; b=8;

s="rainbow"; b=4;

s="rain"; b=4;

s=bow"; b=4;

s="rain"; b=4;

s = "rain"

b = 4

str = "rain" + "bow"

y = 4 * 2

After test executes s is still rain and b is still 4.

str is "rainbow" and y is 8. These modifications are only applied to the copies of s and b within this method. The original variables won't be affected

68
New cards

Which of the following is/are true about using insertion sort versus using merge sort?

I. Insertion sort requires more storage space than mergesort

II. Insertion sort is only more efficient than mergesort in the case that we have a very small and nearly sorted array

III. Insertion sort is almost always less efficient than mergesort.

II and III

I - False - Insertion sort required less storage space compared to merge sort. Merge sort involves merging sorted subarrays into a single sorted array which takes more memory

II - True - Insertion sort performs well on small/sorted arrays because

III - True - Insertion sort has the ability to perform well in smaller lists, but as data sets get larger a merge sort is ideal. Merge sort requires more space but has efficient time complexity.

69
New cards

What would the contents of matrix, a 2-D array of integers, be after a call to alter(1)? The method alter is defined below.

private int[][] matrix;

/* matrix looks like this initially

1 3 5 7

2 4 6 8

3 5 7 9 */

public void alter(int c)

{

for (int i = 0; i < matrix.length; i++) {

for (int j = c + 1; j < matrix[0].length; j++) {

matrix[i][j - 1] = matrix[i][j];

}

}

}

I.

1 7 7 7

2 8 8 8

3 9 9 9

II.

1 5 7

2 6 8

3 7 9

III.

1 3 5 7

3 5 7 9

IV.

1 3 5 7

3 5 7 9

3 5 7 9

V.

1 5 7 7

2 6 8 8

3 7 9 9

V

This method iterates over each row of the matrix (indexed by variable i) and each column starting from index c + 1 (indexed by variable j). It then shifts the elements in each row one position to the left, effectively discarding the element at index c in each row.

Row 1 (i = 0):

The element at index 2 (5) is shifted to index 1, replacing the element at index 1 (3).

The element at index 3 (7) is shifted to index 2, replacing the element at index 2 (5).

Row 2 (i = 1):

The element at index 2 (6) is shifted to index 1, replacing the element at index 1 (4).

The element at index 3 (8) is shifted to index 2, replacing the element at index 2 (6).

Row 3 (i = 2):

The element at index 2 (7) is shifted to index 1, replacing the element at index 1 (5).

The element at index 3 (9) is shifted to index 2, replacing the element at index 2 (7).

70
New cards

The portion of code that begins and controls execution when a java program is called what?

The main method

71
New cards

What is the rule regarding class names and file names?

The class must be in a file with the same name ending in the java extension

72
New cards

What are some good practices for variable names?

The names must make sense and follow camel case

73
New cards

Write the main method header

public static void main(String[] args)

74
New cards

Java contains two variable types. What are they?

Primitive and Reference

75
New cards

What is the line of code that declares a scanner variable and initializes it to a scanner object?

Scanner scnr = new Scanner(System.in);

76
New cards

What is the import statement to use a Scanner?

import java.util.Scanner;

77
New cards

A scanner object exists. For each input type below, what is the method that should be used?

double

boolean

String

int

scnr.nextDouble();

scnr.nextBoolean();

scnr.nextLine();

scnr.nextInt();

78
New cards

What is the import line for ArrayLists?

import java.util.ArrayList;

79
New cards

Which of the following would correctly create an ArrayList of Strings? multi choice

ArrayList myList = new ArrayList(5);

ArrayList myList = new ArrayList();

ArrayList myList = new ArrayList ();

ArrayList myList = new ArrayList();

ArrayList myList = new ArrayList();

ArrayList myList = new ArrayList();

ArrayList myList = new ArrayList(5);

ArrayList myList = new ArrayList();

ArrayList myList = new ArrayList();

80
New cards

Which of the following are types of loops in Java?

for-each loop

while-each loop

for loop

while loop

for-each loop

for loop

while loop

81
New cards

What are these three conditional block statements in order?

else

else if

if

if

else

else if

82
New cards

Given the following Date class

public class Date {

private int year;

private int month;

private int day;

public Date()

{ implementation not shown }

public Date (int year, int month, int day)

{ implementation not shown }

public void print()

{ implementation not shown }

}

Which of the following creates a new Date object?

I Date a = new Date();

II Date b = new Date ( 2022, 1, 10);

III int year = 2022;

String month = "January";

int day = 10;

Date c = new Date(year,month,day);

I and II

83
New cards

Consider the IceCream Class which will contain a String and an int attribute for ice cream flavor and number of scoops and a constructor.

public class IceCream{

}

Which of the following code would be the most appropriate implementation of the class?

I

private String flavor;

private int scoopCount;

private IceCream(String flvr, int scpCnt){}

II

private String flavor;

private int scoopCount;

public IceCream(String flvr, int scpCnt){}

III

public String flavor;

public int scoopCount;

public IceCream(String flvr, int scpCnt){}

IV

public String flavor;

private int scoopCount;

private IceCream(String flvr, int scpCnt) {}

II

private String flavor;

private int scoopCount;

public IceCream(String flvr, int scpCnt){}

variables should be private with a public constructor

84
New cards

Consider the following class definition:

public class Cat {

private String color;

private boolean isHungry;

// missing constructor

}

The following statement appears in a method in a class other than Cat. It is intending to create a new Cat object c with its attributes set to "black" and true.

Cat myCat = new Cat("black", true);

Which of the following can be used to replace the missing constructor code?

I public Cat(String c, boolean h) {

color = c;

isHungry = h; }

II public Cat(String c, boolean h){

c = "black";

h = "true"; }

III public Cat(String c, boolean h){

c = "black";

h = true;}

I

public Cat(String c, boolean h) {

color = c;

isHungry = h; }

85
New cards

Consider the following Party class. The getNumOfPeople method is intended to allow methods in other classes to access a Party object's numOfPeople but it is not working as intended.

public class Party{

private int numOfPeople;

public Party(int num){

numOfPeople = num;

}

private int getNumofPeople(){

return numOfPeople;

}

}

What explains why the method isn't working as intended?

The getNumOfPeople method should be declared public

86
New cards

The following class will not compile:

public class Student

{

private int id;

public getid()

{

return id;

}

// Constructor not shown

}

The accessor method is intended to return the id of a Student object. Which of the following best explains why the class will not compile?

a The id instance variable should be public

b The return type of the getId method needs to be defined as int

c The return type of the getId method needs to be defined as void

d The getId method requires a parameter

e The getId method should be private

b The return type of the getId method needs to be defined as int

87
New cards

What is the parent class of all classes in Java?

I Java

II Class

III String

IV Object

IV Object

88
New cards

Which of the following is not a type of loop in Java?

I Until

II For-each

III While

IV For

I Until

89
New cards

Under which conditions would overloading a method work?

I The methods have different number of parameters

II Two methods with the same name can never be included in the same class

III The methods have different return types

IV The methods have different parameter names

I The methods have different number of parameters

90
New cards

Can you use overridden and overloaded methods in the same class?

Yes.

91
New cards

When a class implements an interface what must it do?

Redefine each constant

Provide an implementation for at least one method declared in the interface

Define a variable for each constant defined in the interface

Provide an implementation for each method declared in the interface

Provide an implementation for each method declared in the interface

92
New cards

Shader and Tracer are two Java interfaces. You are defining a new class called GraphicDesign. It Should implement both Shader and Tracer. What would be an acceptable header for GraphicDesign?

public class GraphicDesign implements Shader and Tracer

public class GraphicDesign extends Shader, Tracer

public class GraphicDesign extends Shader and Tracer

public class GraphicDesign implements Shader, implements Tracer

public class GraphicDesign implements Shader, Tracer

public class GraphicDesign implements Shader, Tracer

93
New cards

A Java class...

Can or can not inherit from multiple superclasses?

Can or can not implement multiple interfaces?

Can or can not inherit from only one superclass?

Can implement multiple interfaces and may inherit from only one superclass.

May not inherit from multiple superclasses

94
New cards

Say you are writing a Customer class and want to implement the Comparable interface. What method has to be used?

I Customer compareTo(Object other)

II boolean compareTo(T other)

III int compareTo(Customer other)

III int compareTo(Customer other)

95
New cards

Imagine you are planning out a new java project, but can't decide if what you are writing should be a concrete class, abstract class, or interface. 3 subclasses will eventually extend or implement what you are writing. There is at least one method that will have the exact same implementation in all three subclasses. At least one method that these subclasses inherit will need a completely different implementation in each subclass. You won't need to create any object of the superclass type. Should this be a concrete class, abstract class, or interface?

Concrete class

96
New cards

Consider the class Party which keeps track of the number of people at the party.

public class Party

{

// num of people at the party

private int numOfPeople;

// missing header of set method {

numOfPeople = people;

}

}

Which of the following method signatures could replace the missing header?

I public int getNum(int people)

II public int setNum()

III public int setNum(int people)

IV public int setNumofPeople(int p)

V public void setNum(int people)

V public void setNum(int people)

97
New cards

Which of the following constructors would be valid for Point3D?

public class Point2D{

public int x;

public int y;

public point2D() {}

public point 2D(int x, int y){

this.x = x;

this.y = y;

}

}

public class Point3D extends Point2D{

public int z;

// other code

}

I. public Point3D() {}

II. public Point3D(int x, int y, int z){

super (x,y);

this.z = z;

}

III. public point 3D(int x, int y){

this.x = x;

this.y = y

this.z = z;

}

All of them

98
New cards

What type of relationship is between the two classes books and authors?

A has-a relationship. The book class has an author attribute

99
New cards

What type of relationship is between the two classes Toyota and a car?

An is-a relationship

100
New cards

Which of the following are valid reasons for using an inheritance hierarchy?

I Object methods from a superclass can be used in a subclass without rewriting or copying code

II Objects from a subclass can be passed as arguments to a method that takes an argument of a parent type

IIIObjects from subclasses can be stores in the same array of the parent type

All of them