Programming Exam Flashcards

1
Q

Do the nested loops need to be the same loop type?

A

No, the nested loops do not need to be the same loop type.

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

When can nested loops be useful?

A

Nested loops can be useful if we are performing multiple operations, each of which having its own count or sentinel value

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

What is important about the inner (nested) loop ?

A

The inner, nested loop executes completely (executes all its iterations) for each single iteration of the outerloop

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

When does a loop repeat a set of operations for each input item?

A

A loop repeats a set of operations for each input item while a condition is true

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

What is the while loop especially useful for?

A

The while loop is especially useful for event-controlled looping.

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

How does a while loop work?

A

A while loop executes a set of operations in the loop body as long as the loop condition is true. Each execution of the loop body is an iteration of the loop

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

What occurs if the loop condition evaluates to false the first time it is evaluated?

A

If the loop condition evaluates to false the first time it is evaluated, the body of the while loop is never executed

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

What occurs if the loop condition never evaluates to false

A

The result is an infinite loop

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

How does event-controlled looping work

A

In event-controlled looping, processing of items continues until the end of the input is signaled either by a sentinel value or by reaching the end of the file

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

What is a sentinel value

A

A sentinel value is a special input that signals the end of the items to be processed.

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

What is the normal flow of control with a sentinel value

A

A priming read is performed before the while loop, then the body of the loop processes the input and then performs an update read on the next data item

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

How can we test that we have reached the end of the file when reading from an input file

A

We can test whether we have reached the end of the file by calling a hasNext method of the Scanner class

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

Describe the general setup of an accumulation programming technique

A

A total variable is initialized to 0 before the loop starts, then in the loop body we add each input value to the total. When the loop completes, the current total is the total for all of the processed input values

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

Describe the general setup of a counting program technique

A

Initialize a count variable to 0 before starting the loop. In the loop body, we increment the count variable for each input value that meets our criteria. When the loop completes, the count variable contains the number of items that met our criteria

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

Describe the general setup of finding an average

A

We have to combine accumulation and counting. We add the input values to the total and increment the count. When the loop completes, we calculate the average by dividing the total by the count. Before computing the average though, we should verify that the count != 0

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

Describe the general setup of finding a maximum or minimum value

A

We assign the first input to a running maximum or minimum. In the loop body, we compare each input value to our running max/min. In the loop body, we compare each input value to our running max/min. If the input value is less than the running min, we assign it to the running min. If it is greater than the running max, we assign the input value to the running max. When the loop completes, the running value is the max or min of all of the input values

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

How do we avoid generating exceptions when the user types characters other than the data type expected

A

We use the hasNext methods of the Scanner class

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

How do we construct a loop condition

A

We must construct the inverse of the loop termination condition

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

What are important things to test when testing a program that contains a loop?

A

Test that the program produces correct results by inputting values and comparing the results with manual calculations (avoid logical errors), also test that the results are correct if the loop body never executes. The final thing to test is what occurs when an invalid input is entered.

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

In what order does the do/while loop check the condition/execute the loop

A

The do/while loop checks the loop condition after executing the loop body.

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

How many times (minimum) does a do/while loop execute

A

A do/while loop executes at least once because the condition is checked after the loop body is executed.

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

When is it useful to use a do/while loop

A

When you want to validate input

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

Why/When are for loops used

A

The for loop is useful for count-controlled loops, aka loops in which the number of iterations is known before the loop begins

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

Briefly explain how for loops are executed

A

When the for loop is encountered, the initialization statement is executed. Then, the loop condition is evaluated. If the condition == true then the loop body executes. the loop update statement is then executed and the loop condition is reevaluated. If the loop condition is still true, it continues so on until the condition evaluates to false

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

How do we use a loop control variable in a for loop

A

We set its initial value in the initialization statement, and then increment or decrement its value in the loop update statement and then check its value in the loop condition.

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

What are the important things to test in a FOR loop

A

Test that the starting and ending values of the loop variable are correct. It is also important to test an input for which the loop body does not execute at all.

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

Describe user defined classes

A

A class that is written by a user that is used to encapsulate data and methods to then be used by application/service classes

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

What is a major benefit of incorporating methods that work with data into our class

A

Being able to hide the details associated with handling that data.

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

Give examples of data/functionality that could be encapsulated by classes

A

A person, a place, a thing, or more generally an object. A student, a college or a course..

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

Whats the common syntax related to class names

A

They are nouns and start with a capital letter

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

What goes inside the curly brackets in a class

A

The data of the class (fields) and methods.

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

Whats an important function performed by the class methods

A

Maintaining the values of the class data for client programs

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

What is another name for the data of the class

A

A field

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

What is a client program

A

The users of a class

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

What is the use of an access modifier?

A

Specifies where the class or member can be used.

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

What needs to be provided for each class and each member of the class?

A

An access modifier must be provided

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

What are all the possible access modifiers?

A

public, private, protected or no modifier at all

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

What occurs if you put no access modifier

A

The package is able to be accessed

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

What does the public access modifier allow for

A

The public access modifier allows the class or member to be used, or referenced by methods of the same or other classes.

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

What does the private access modifier allow

A

Allows the class or member to be used, or referenced, by methods of the same class

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

What does (no modifier) package access allow

A

Package access specifies that the class or member can be accessed by methods in classes that are in the same package or in the same folder

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

What is data hiding?

A

When the APIs of the methods of a class are posted but not the method body (code of the class)

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

What do the instance variables of a class hold?

A

The instance variables of a class hold the data for each object of the class

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

What can we say about instance variables

A

That they represent the properties of that object

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

What can be said about the values of the instance variables

A

They can represent the state of the object

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

What modifier is typically used for non constant instance variables of the class AND WHY

A

The private modifier. This permits only the methods of the same class to set or change the values of instance variables. We can then say the data is encapsulated, or protected

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

What data type can an instance variable be

A

Any of java’s primitive data types of a class type

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

What does the identifierList contain

A

It consists of one or more names for instance variables of the same data type and can optionally assign initial values to the instance variables

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

How do you separate more than once instance variable name

A

By using a comma

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

What is the naming convention for instance variables

A

Identifier names for instance variables are nouns and begin with a lower case letter and internal words begin with a capital letter (CAMELCASE)

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

What is the scope of the fields of a class

A

It is a class scope

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

give an example of instance variable definitions

A

private String name = “”;

private final int PERFECT_SCORE = 100, PASSING_SCORE = 60;

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

What should be defined as instance variables

A

Define instance variables for the data that all objects will have in common

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

What is the function of a method caller?

A

The method caller sends arguments or actual parameters to the method

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

What are formal parameters?

A

Actual parameters that the method refers to

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

What is the naming convention for methods

A

The method name should begin with a lowercase letter and with internal words usually having a capital letter. They’re typically verbs

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

What status is required for the access modifier for methods that provide services to client

A

A public status

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

What status is usually reserved for methods that provide services only to other methods of the class

A

A private status

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

What is the return type of a method

A

The data type of the value that the method returns to the caller.

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

What are examples of valid return types for a method

A

Any of java’s primitive data types, any class type or void

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

What is special about methods with a return type of void

A

They do not return a value to a caller

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

What is the body of a method comprised of (general terms)

A

The code that performs the methods function. Written between beginning and ending curly braces.

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

Are the curly braces for method required?

A

Yes, these curly braces are not optional. They are required regardless of the number of statements in the method body.

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

What are two examples of error that can result if you forget one or more curly braces

A

illegal start of expression

OR

’;’ expected

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

What can be done in a method body?

A

A method can declare variables, call other methods, use: if/else statements, while loops, for loops, switch statements and do/while loops

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

Do all objects of a class have their own copy of the class methods?

A

No, all objects of a class share one copy of the class methods

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

What is the purpose of a return statement?

A

A return statement is a value returning method that sends back its results to the caller using the return statement.

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

Does the data type of the expression need to match the return type of the method?

A

yes, it must match

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

How is a value returning method called

A

A value-returning method is called from an expression and when the method completes it operation, its return value replaces the method call in the expression

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

How can the return statement be used if the data type of the method is void

A

You can use the return statement without an expression (return;) or omitting the return statement all together

71
Q

Why can you omit the return statement if the data type of the method is void

A

Because control automatically returns to the caller when the end of the method is reached.

72
Q

What is a constructor

A

A constructor is a special method that is called when an object is instantiated using the new keyword.

73
Q

How many constructors can a class have

A

As many as they want

74
Q

What is the job of the class constructors

A

To initialize the fields of the new object

75
Q

What is the naming convention for a constructor

A

To have the same name as the class

76
Q

What access level status are constructors set to

A

They should be set to public so that applications can instantiate objects of the class

77
Q

What are the two ways that values can be assigned to constructors

A

A constructor can either assign default values to the instance variables or the constructor can accept initial values from the client through parameters

78
Q

Is it mandatory to have a constructor for a class

A

No it is optional

79
Q

What occurs if no constructor is written for a class

A

The compiler will provide a default constructor

80
Q

What is a default constructor

A

A constructor that takes no arguments

81
Q

What does a default constructor do

A

The default constructor assigns default initial values to all instance variables

82
Q

What is autoinitialization

A

When a default constructor assigns default initial values to all instance variables

83
Q

What is the default constructor assignment for numeric variables

A

the value of 0

84
Q

What is the default constructor assignment for characters

A

unicode null character

85
Q

What is the default constructor assignment for boolean variables

A

The value of false

86
Q

What is the default constructor assignment for object references

A

null

87
Q

What occurs if you provide a constructor but have instance variables that are not initialized?

A

They will be given the predefined default value

88
Q

If we provide a constructor, does the compiler still generate a default constructor?

A

No it doesn’t.

89
Q

What type of reference are strings considered

A

Object references

90
Q

Because a class is considered a “caretaker of it’s fields”, what is the classes responsibility?

A

It is the classes responsibility to ensure that the data for each object is valid.

91
Q

What constitutes a valid value for an instance variable?

A

A valid variable depends in part on the data type of the variable and in part on the class and it’s design decision.

92
Q

How do classes handle invalid argument values?

A

It can either generate an exception or substitute a default value - depends on design decision.

93
Q

What does overloading a method mean?

A

Providing multiple constructors for the method

94
Q

How do we overload a method

A

We provide the method with the same name but with a different number of parameters, or with the same number of parameters but having at least one parameter with a separate data type

95
Q

What is a methods signature

A

The name of the method, along with the number, data types and order of its parameters

96
Q

Is the return type part of a methods signature

A

No it is not

97
Q

What occurs when a client calls a method that is overloaded

A

Java determines which version of the method to execute by looking at the numbers, data types and order of the arguments in the method call

98
Q

What is a general statement we can make about arguments sent to an overloaded method

A

Arguments sent to an overloaded method must match the formal parameters of some version of that method

99
Q

What type of scope do instance variables have

A

A class scope, meaning they can be accessed anywhere in the class

100
Q

What type of scope do methods have

A

A class scope, meaning they can call any of the methods in the class regardless of whether they have been declared private, public or protected

101
Q

What else can a method access besides the instance variables

A

A method can also access its own parameters

102
Q

What type of scope do a methods parameters have

A

A local scope so it can access its parameters directly

103
Q

If a variable is defined within a method, what properties does it have?

A

These variables have a local scope and are accessible from the point of definition until the end of the method or the end of the block in which the variable was defined

104
Q

What error occurs if you attempt to use an identifier that is not in scope

A

cannot find symbol

105
Q

To summarize: A method in a class can access:

A

The instance variables of its class

Any parameter sent to the method

Any variable the method declares within its body from the point of declaration until the end of the method or until the end of the block in which the variable was declared

Any methods in the class

106
Q

What is generally provided by the class if it contains private instance variables

A

Classes will usually provide public accessor methods for the instance variables

107
Q

What arguments do “getters/accessors” take and what is returned

A

The method takes no arguments and simply returns the current value of the instance variable. The return type is the same data type as the instance variable

108
Q

Why are mutator/setter methods provided to clients

A

To allow them to change instance variables

109
Q

Since the instance variables are declared as private, what type of mutator/setter method is provided to the client

A

A public mutator is provided for any instance variable that client will be able to change.

110
Q

What is the return type for setters/mutators

A

void

111
Q

What is the naming convention for setters

A

the name of each mutator method starts with a lowercase word set followed by the instance variable name with an initial capital letter

112
Q

What should be validated before allowing the mutator to work

A

The body of the mutator method should validate the parameter value passed by the client. If valid - the mutator assigns that value to the instance variable

113
Q

What is a common error with writing mutator methods

A

The common error with writing mutator methods is using the instance variable name for the parameter name.

114
Q

What occurs if a method parameter has the same name as an instance variable

A

The parameter hides the instance variable due to the name precedence.

115
Q

How does the method know which objects data it should get, set or use to calculate a value?

A

Using the special object reference named THIS

116
Q

What is the implicit parameter

A

When a method begins executing, the JVM sets the object reference, THIS, to refer to the object for which the method has been called.

117
Q

What occurs when a method references an instance variable

A

It will access the instance variable that belongs to the object that the implicit parameter references. An instance variable referred to by a method is considered to be this.instanceVariable

118
Q

Give an example of how you would setModel to model using the THIS object reference

A
public Auto setModel (String model)
{
       this.model = model;
       return this;
}
119
Q

What is the benefit of returning THIS?

A

The benefit of returning this is that we can chain method calls.

120
Q

What is a dot operator?

A

The dot operator, also known as separator or period used to separate a variable or method from a reference variable.

121
Q

Give an example of chaining method calls

A

After THIS has been returned:

Model m = Model.create().withFieldA(“AAAA”).withFieldB(1234);

122
Q

The dot operator associates in what order?

A

left to right

123
Q

What is the function of toString

A

The function of the toString method is to return a printable representation fo the object data

124
Q

What is the function of the equals method

A

The equals method is designed to compare two objects for equality.

125
Q

What is commonly misunderstood about the equals method

A

The equals method takes an object reference parameter that is expected to be an Auto reference and returns true if the values of its fields are equal to the values of the fields of this Auto object, false otherwise.

126
Q

What is overriding a method?

A

Providing the same header as the methods in the object class but providing a new method body.

127
Q

Where should you add the @override annotation when overriding a method?

A

Before the method header

128
Q

Why do we add the @override annotation

A

It tells the compiler that the method that follows is overriding a method inherited from another class. And, in turn, the compiler warns us if our method header does not infact override the inherited method

129
Q

What operation does instanceof perform?

A

evaluates to true if objectReference is of Classname type; false otherwise

130
Q

What is the left and right operand of the instanceof binary operator?

A

The left operand is an object reference and the right operand is a class

131
Q

As a principal, what should communicate directly with the client

A

The class methods should communicate directly with the client and the client should handle the communications with the user

132
Q

How do you determine whether or not something should be static

A

If it makes sense to call the method before an object has been constructed then it should be static (ie: method for converting speeds.. can be used even without having a specific car object has been constructed)

133
Q

Can static methods access instance variables?`

A

No, they cannot access instance variables because static methods are associated with a class and not an object

134
Q

Can static methods access static class variables?

A

Yes they can

135
Q

Can static methods call static class methods

A

Yes they can

136
Q

Can static methods call non-static instance methods?

A

No they cannot

137
Q

Can static methods use the reference “this”

A

No they cannot

138
Q

What is another name for a non-static method

A

An instance method

139
Q

What can an instance method reference

A

An instance method can reference both class variables and instance variables, as well as class methods and instance methods

140
Q

What is an array?

A

A sequence of variables of the same data type

141
Q

What data types are accepted in an array

A

int, float, double, byte, boolean, char, short, long or a class.

142
Q

What is each variable in the array considered

A

an element

143
Q

What is the index of an array

A

the array name and subscript that refers to the elements position in the array

144
Q

How are arrays implemented into java

A

arrays are implemented as objects in java

145
Q

How do you create an array

A
  1. Declaring the object reference for the array

2. Instantiating the array

146
Q

What can be said about an array of objects?

A

Each element is an object reference which stores the location of an object

147
Q

How do you declare an array

A

We have to specify the name of the array and the data type, as we would for any other variable

148
Q

Give an example of the syntax of an array

A

double [] dailyTemps; // each element is a double

149
Q

Give an example of the syntax of an array using a boolean

A

boolean [] answers; // will hold true or false values

150
Q

Give an example of the syntax of an array using 3 int values

A

int [] cs101, bio201, hist102; // all elements are int values

151
Q

Does declaring an array allocate memory for the array?

A

No, it does not.

152
Q

What does specifying an array do

A

It does not specify how many elements the arrays will have. The declaration simply specifies an object reference for the array and the data type of the elements

153
Q

What is the syntax for instantiating the array that contains n elements

A

dailyTemps = new double [n]; // daily temps has n elements

154
Q

How do you allocate memory for an array

A

You instantiate it

155
Q

Can you combine declaration and instantiation of arrays?

A

Yes

156
Q

What is the syntax for combining declaration and instantiation of an array with n elements

A

double [] dailyTemps = new double [n];

157
Q

When is the only time an initialization list can be given with an array

A

When the array is declared.

158
Q

How can you instantiate an array by assigning initial values?

A
datatype [] arrayName = { value0, value1, value2, .. };
// valueN is an expression that evaluates to the data type of the array and is the value to assign to the element at index N
159
Q

What is special to remember about instantiating and assigning initial values to arrays

A

There is no new keyword used and there is no size specified for the array. The number of elements is determined by the number of values in the initialization list

160
Q

What is always the index of the first element in an array

A

0

161
Q

What is always the index of the last element in the array

A

n-1

162
Q

What is an index in an array

A

An elements position within the array

163
Q

What is the instance variable length used for with arrays

A

it holds the number of elements in an array

164
Q

What is the syntax for using the instance variable length in an array

A

arrayName.length

165
Q

What are the two required steps to instantiate an array with a class data type

A
  1. instantiating the array

2. instantiating the object

166
Q

What is the syntax for calling an object in an array

A

Use the array name and index along with the dot notation

167
Q

How would you approach printing all elements in an array

A

We have to use a loop that prints each element individually

168
Q

Give an example of a loop to print all elements in an array

A

for (int i = 0; i < cellBills.length; i++)
{
system.out.println (i + “\t” + cellBills[i]);
}

169
Q

How do you approach reading and summing the data into an array

A

Using for loops to prompt the user to enter data into the array

170
Q

How do you approach finding max or min values in an array

A

Using a loop to find a minimum and maximum value and note both indexes

171
Q

How would you approach copying an array

A

Use a for loop to assign a backup to each individual element

172
Q

How would you expand the size of an array while maintaining the values of the original array

A
  1. Instantiate an array with the new size, giving the new array a temporary reference
  2. Copy the elements from the original array to the new array
  3. Point the original array reference to the new array
  4. Assign a null value to the temporary array reference
173
Q

How would you compare whether two arrays are equal

A
  1. Determine if they are equal in length

2. Use a for loop to compare the coresponding elements in each array

174
Q

Why can you not use the == to compare two arrays

A

Because arrays are objects, so using the equality operator will compare whether the two array references point to the same array in memory, not whether or not the data in the two arrays are equal