cmpt 270 midterm new

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

1/103

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.

104 Terms

1
New cards

what is the difference between method overloading and method overriding in Java?

method overloading means having the same method name but different parameters, while method overriding means a subclass changes a method that it inherits from its superclass

2
New cards

what is the purpose of the final keyword in Java?

means that a variable’s value, a method’s behaviour, and a class’ inheritance cannot be changed

3
New cards

what is the difference between a checked exception and an unchecked exception

checked exceptions must be caught with try-catch or declared in the method, but unchecked exceptions happen during the program and don’t have to be handled explicitly

4
New cards

what is the difference between a constructor and a method in Java?

a constructor initializes a new object, while a method performs actions on an object

5
New cards

what does the this keyword do in a constructor or method?

refers to the current instance of the class

6
New cards

what is the purpose of the super keyword in java?

refers to the superclass and is used to call parent class methods or constructors

7
New cards

explain the difference between == and equals() in Java

one checks if two objects are the exact same thing in memory, while the other checks if their contents or values are the same

8
New cards

what is the difference between a static method and a non-static method

one belongs to the class while the other belongs to an instance of the class

9
New cards

what is a final variable, and how is it different from a regular variable?

a final variable cannot be changed once assigned, whereas a regular variable can be changed

10
New cards

how does the try-catch block work in exception handling?

the try block contains code that might throw an exception, and the catch block handles the exception if it occurs

11
New cards

what is the purpose of throw and throws keywords in Java exceptions?

throw is used to cause an exception right away, while throws tells that a method might cause an exception

12
New cards

what is the difference between String and StringBuilder in Java?

String is immutable while StringBuilder is not mutable

13
New cards

what is polymorphism and how is it used in Java?

it lets different kinds of objects be used like they are the same type, usually by replacing methods with new versions

14
New cards

what is an abstract class in Java?

is a class you can’t make objects from, but other classes can inherit from it

15
New cards

what is the difference between an interface and an abstract class in Java?

an interface is a list of method names without code that any class can use, an abstract class can have methods with or without code and other classes can extend it

16
New cards

what is a constructor overloading in Java?

occurs when multiple constructors with different parameters are defined in a class

17
New cards

what does instanceof operator do in Java?

checks if an object is of a certain type, it verifies the object’s class when the program is being ran

18
New cards

what is the purpose of the private access modifier in Java?

restricts access to a field or method to within the same class only

19
New cards

what is the null keyword in Java?

represents the absence of a value or reference for a variable

20
New cards

what is the purpose of the continue statement in Java?

skips the current iteration of a loop and moves to the next iteration

21
New cards

what is the difference between ArrayList and LinkedList?

ArrayList stores items in a growable array and lets you quickly get any item, while LinkedList stores items in a chain and lets you add or remove items faster

22
New cards

what does the default keyword do in an interface?

lets a method in an interface have a default code that classes can use or override

23
New cards

what is method chaining?

lets you call methods one after another on the same object in one line, because each method returns the object itself

24
New cards

what is the difference between ArrayList and HashSet?

ArrayList lets you use store methods in an ordered list, because it keeps insertion order, while HashSet stores unique elements in no order because it does not allow duplicates

25
New cards

what is the purpose of the static keyword?

lets you use methods or variables without creating an object, because it belongs to the class itself

26
New cards

what is the difference between a method signature and a method declaration?

method signature includes the name and parameters, because it identifies the method while the method declaration includes the return type an access modifiers, because it defines the method

27
New cards

what is the purpose of the default constructor?

creates an object without setting any values because it is automatically provided if no constructor is given

28
New cards

what is the difference between Thread.sleep() and wait()?

thread.sleep() pauses the thread for a certain time, while wait() pauses it until another thread tells it to continue

29
New cards

what does super() do in a constructor?

calls the parent class constructor, because it ensures the parent class is properly set up

30
New cards

what is the purpose of the final keyword in method parameters?

lets you make sure a parameter’s value cannot be changed, prevents reassignment

31
New cards

what does the this keyword do in a constructor?

refers to the current object, because it helps distinguish instance variables from parameters

32
New cards

what is a hashcode?

uniquely identifies objects because it helps quickly locate and compare objects in hash-based collections

33
New cards

the major difference between stack memory and heap memory is that the stack is used to store the order of method execution and local variables while the heap memory stores the objects and it uses dynamic memory allocation and deallocation

true or false

true

34
New cards

in java, the default value for a class variable of type int is -1.

true or false

false

35
New cards

instance variables are used to represent an object’s state

true or false

true

36
New cards

subclasses can access private instance variables of the superclass

true or false

false

37
New cards

when writing regression tests, it is ok to directly interact with private variables

true or false

false

38
New cards

a uml use case diagram is considered a behavioural diagram, as opposed to a structural diagram

true or false

true

39
New cards

in java, the toString() method automatically collects a class’ attributes and generated a formatted string representation of the class

true or false

false

40
New cards

you need to provide the size of an arrayList when you are creatin the arrayList

true or false

false

41
New cards

polymorphism can be used with both inherit super types and implemented supertypes

true or false

true

42
New cards

which of the following is not a class found in java’s collection framework

a) arraylist

b) hashmap

c) hashset

d) dictionary

dictionary

43
New cards

which of the following are not important for a regression test program?

a) constructing objects are calling methods

b) displaing the results of the method calls

c) displaying the values that you expect to get

d) reading input from the user

reading input from the user

44
New cards

suppose the class coffee extends the class drink. which one of the following assignments is legal?

  • drink x = new drink();

  • coffee y = new coffee();

a) y = x;

b) x = y;

c) y = new drink();

d) new drink() = new coffee();

x = y;

45
New cards

suppose you are to design an inheritance hierarchy with the following classes: mouse, cat, mammal, and tiger. which of these is the superclass?

a) tiger

b) mammal

c) mouse

d) cat

mammal

46
New cards

which programming technique should be used if you want a method to have different behaviour depending on the class of the object that is invoking it?

a) polymorphism

b) the instaceof operator

c) an if-else statement

d) the getclass method

polymorphism

47
New cards

which of the following java snippets would be used to declare a hashmap that contains items of type exam and is accessed by a key of type string?

a. hashmap<exam> exams = new hashmap<string>();

b. hashmap<t> exams = new hashmap<t>();

c. map<exam, string> exams = new hashmap<exam, string>();

d. map<string, exam> exams = new hashmap<string, exam>();

d

48
New cards

when should you delete your testing code from a class?'

a) if it is a simple class you don’t need to write proper regression tests

b) when all tests are passed and you know the class wont be changing

c) your regression tests should never be deleted

d) after several months without errors it makes sense to delete your tests to make your code easier to read

your regression tests should never be deleted

49
New cards

consider the following code fragment:

Object obj = “CMPT 270”;

System.out.println(ob.length());

which of the following statements are true?

a) the code compiles are runs without issue

b) the code will not compile because the Object class does not have a method called length()

c) the code will not compile because a String object cannot be assigned to something of type object

d) the code will compile but throw an exception

the code will not compile because the Object class does not have a method called length()

50
New cards

software development is all about learning a programming language

false

51
New cards

object oriented programming

a program consisting of classes and objects organized to manage the complexity of large-scale systems

52
New cards

procedural programming

a program consisting of methods/functions calling each other

53
New cards

method

implements behaviors relevant to the purpose of the system

54
New cards

field or attribute

stores data relevant to the purpose of the system

55
New cards

container object

data structure to store entities

56
New cards

entity object

model of a real-world thing in an application

57
New cards

interface object

facilitates communication between the system and the outside world

58
New cards

control object

organizes computation or flow of the program

59
New cards

a uml class diagram is a behavioral diagram

true or false

false

60
New cards

given 3 classes: plant, tree, flower, which of the following best describes their relationship to one another

inheritance

61
New cards

suppose you have 2 classes: house, and wall, where the house is a container of wall objects. which of the following best describes this relationship?

composition

62
New cards

suppose you have 2 classes: book, and bookshelf. which of the following best describes the relationship between these 2 classes?

aggregation

63
New cards

suppose you have 2 classes: hammer and nail, which of the following best describes the relationship between these 2 classes

association

64
New cards

which principle should be used if you want a method to have different behaviour depending on the specific class the method is being invoked on

polymorphism

65
New cards

which of the following statements describes an inheritance relationship?

a textbook is a book

66
New cards

interfaces and inheritance are different name’s for the same concept

true or false

false

67
New cards

using uml class diagrams you can express relationships between two classes. which of the following cannot be expresed in a uml diagram?

polymorphism

68
New cards

encapsulation

grouping related variables and functions together to reduce complexity and increase reusability

69
New cards

polymorphism

refactor ugly if and switch / case statements

70
New cards

inheritance

eliminate redundant code

71
New cards

abstraction

hide details and shows the essentials; isolate the impact of changes

72
New cards

which of the following data types is not a primitive data type in Java

a) string

b) double

c) boolean

d) int

string

73
New cards

all data types in java are references to data allocated on the heap

true or false

false

74
New cards

if you wanted to store measurement data, which Java data type is recommended?

double

75
New cards

Just like in Python, single quotes (' ') and double quotes (" ") are both used to denote a String literal.

true or false

false

76
New cards

Which of the following for loops is correct syntax in Java?

for(int i=0; i<10; i++);
{
    System.out.println("Hello"); 
}
for(int i=0; i<10; i++)
{
    System.out.println("Hello");  
}
for(int i in Range(10))
{
    System.out.println("Hello"); 
}
for(int i=0, i<10, i++)
{
    System.out.println("Hello"); 
}

for(int i=0; i<10; i++)
{
    System.out.println("Hello");  
}

77
New cards

What would be the output from the following code snippet?

int x = 10;
int y = 4;
if(x >= 10)
{
    System.out.println(x/y);
}
else
{
    System.out.println(y/x);
}

a) 2

b) 2.5

c) 0.4

d) 0

2

78
New cards

Assume you have the following String declarations. Which of the following expressions evaluate to true? (select all that apply)

 

String s1 = "Hello";
String salutation = "Hi";
String howdy = "hELLo";
String greeting = new String("Hello");

a) s1.equals(howdy);

b) s1.equals(greeting);

c) s1 == greeting

d) s1.equalsIgnoreCase(howdy);

e) s1.compareTo(salutation);

s1.equals(greeting); s1.equalsIgnoreCase(howdy);

79
New cards

what does a toString() method do in Java?

it allows a programmer to define a String representation of their class

80
New cards

the tree class does not have an explicit ‘chopDown’ method. which of the following describe the chopDown functionality in the system

a) the Tree object's height is set to 0

b) the Tree object has a boolean variable named 'isDead', which is set to true

c) the Tree object's memory is manually deallocated using free()

d) the Tree variable is set to null

the Tree variable is set to null

81
New cards

why doesn’t the Tree class have a setter/mutator to set the height attribute?

a) it does have one, the method grow() acts as a mutator

b) it doesn't need one, because the height attribute is public so any class can change it directly

c) it does have one, the method getHeight() also sets the height attribute

d) it doesn't need one, because the height attribute should never change

it does have one, the method grow() acts as a mutator

82
New cards

which class acts as the main/client program

a) treePlanter

b) inventory

c) forest

d) tree

treePlanter

83
New cards

the lecture followed a specific development process. Which of the following steps was omitted from the lecture (for now)?

a) Implementing/coding methods

b) UML

c) JUnit Testing

d) Creating stub classes/methods

e) Documentation

JUnit Testing

84
New cards

in the development process followed in the lecture, which of the following steps was last?

a) Creating stub class/methods

b) Implementing/coding methods

c) UML

d) Unit Testing

e) Documentation

Implementing/coding methods

85
New cards

debugging

inferring the cause of undesired behavior or incorrect results

86
New cards

fault

your program exhibits behaviour that is not intended / an undesired behaviour or incorrect result

87
New cards

error

the “thing” that causes a fault / the cause of an undesired behaviour or incorrect result

88
New cards

validation

any method to increase confidence in the correctness of a software system

89
New cards

failure

inability of the system to do what is required

90
New cards

coverage

how much of the code is tested

91
New cards

manual testing

a human types some input to a program

92
New cards

automated testing

the computer runs a script performing the tests

93
New cards

which of the following statements about the relationship between errors and faults is FALSE?

a) it's possible for one error to produce multiple faults

b) if there is no error in the code, there are no faults

c) it's possible for code to have an error but produce no faults

d) it's possible for a fault to be observed, but no error to cause it

it's possible for a fault to be observed, but no error to cause it

94
New cards

when should you delete your testing code from a class?

your regression tests should never be deleted

95
New cards

For CMPT 270, where will you be writing your unit tests?

In the static main method of the class being tested

96
New cards

when writing regression tests, we should print the status of each test to the console.

true or false

false

97
New cards

using the inheritance structuer discussed in the lecture, assume you have a container of type Water Monster. Which of the following types could be stored inside the container?

a) squiiirtle

b) starfishy

c) peekatu

d) monster

e) electric monster

f) water monster

squiirtle, starfisgy, water monster

98
New cards

effective use of polymorphism is good because it increases coupling in a software system

true or false

false

99
New cards

which keyword is used to indicate inheritance in java?

extends

100
New cards

polymorphism can be used with both inherited super types and implented interface types

true