Programming Exam Flashcards
Do the nested loops need to be the same loop type?
No, the nested loops do not need to be the same loop type.
When can nested loops be useful?
Nested loops can be useful if we are performing multiple operations, each of which having its own count or sentinel value
What is important about the inner (nested) loop ?
The inner, nested loop executes completely (executes all its iterations) for each single iteration of the outerloop
When does a loop repeat a set of operations for each input item?
A loop repeats a set of operations for each input item while a condition is true
What is the while loop especially useful for?
The while loop is especially useful for event-controlled looping.
How does a while loop work?
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
What occurs if the loop condition evaluates to false the first time it is evaluated?
If the loop condition evaluates to false the first time it is evaluated, the body of the while loop is never executed
What occurs if the loop condition never evaluates to false
The result is an infinite loop
How does event-controlled looping work
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
What is a sentinel value
A sentinel value is a special input that signals the end of the items to be processed.
What is the normal flow of control with a sentinel value
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 can we test that we have reached the end of the file when reading from an input file
We can test whether we have reached the end of the file by calling a hasNext method of the Scanner class
Describe the general setup of an accumulation programming technique
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
Describe the general setup of a counting program technique
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
Describe the general setup of finding an average
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
Describe the general setup of finding a maximum or minimum value
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 do we avoid generating exceptions when the user types characters other than the data type expected
We use the hasNext methods of the Scanner class
How do we construct a loop condition
We must construct the inverse of the loop termination condition
What are important things to test when testing a program that contains a loop?
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.
In what order does the do/while loop check the condition/execute the loop
The do/while loop checks the loop condition after executing the loop body.
How many times (minimum) does a do/while loop execute
A do/while loop executes at least once because the condition is checked after the loop body is executed.
When is it useful to use a do/while loop
When you want to validate input
Why/When are for loops used
The for loop is useful for count-controlled loops, aka loops in which the number of iterations is known before the loop begins
Briefly explain how for loops are executed
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 do we use a loop control variable in a for loop
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.
What are the important things to test in a FOR loop
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.
Describe user defined classes
A class that is written by a user that is used to encapsulate data and methods to then be used by application/service classes
What is a major benefit of incorporating methods that work with data into our class
Being able to hide the details associated with handling that data.
Give examples of data/functionality that could be encapsulated by classes
A person, a place, a thing, or more generally an object. A student, a college or a course..
Whats the common syntax related to class names
They are nouns and start with a capital letter
What goes inside the curly brackets in a class
The data of the class (fields) and methods.
Whats an important function performed by the class methods
Maintaining the values of the class data for client programs
What is another name for the data of the class
A field
What is a client program
The users of a class
What is the use of an access modifier?
Specifies where the class or member can be used.
What needs to be provided for each class and each member of the class?
An access modifier must be provided
What are all the possible access modifiers?
public, private, protected or no modifier at all
What occurs if you put no access modifier
The package is able to be accessed
What does the public access modifier allow for
The public access modifier allows the class or member to be used, or referenced by methods of the same or other classes.
What does the private access modifier allow
Allows the class or member to be used, or referenced, by methods of the same class
What does (no modifier) package access allow
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
What is data hiding?
When the APIs of the methods of a class are posted but not the method body (code of the class)
What do the instance variables of a class hold?
The instance variables of a class hold the data for each object of the class
What can we say about instance variables
That they represent the properties of that object
What can be said about the values of the instance variables
They can represent the state of the object
What modifier is typically used for non constant instance variables of the class AND WHY
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
What data type can an instance variable be
Any of java’s primitive data types of a class type
What does the identifierList contain
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 do you separate more than once instance variable name
By using a comma
What is the naming convention for instance variables
Identifier names for instance variables are nouns and begin with a lower case letter and internal words begin with a capital letter (CAMELCASE)
What is the scope of the fields of a class
It is a class scope
give an example of instance variable definitions
private String name = “”;
private final int PERFECT_SCORE = 100, PASSING_SCORE = 60;
What should be defined as instance variables
Define instance variables for the data that all objects will have in common
What is the function of a method caller?
The method caller sends arguments or actual parameters to the method
What are formal parameters?
Actual parameters that the method refers to
What is the naming convention for methods
The method name should begin with a lowercase letter and with internal words usually having a capital letter. They’re typically verbs
What status is required for the access modifier for methods that provide services to client
A public status
What status is usually reserved for methods that provide services only to other methods of the class
A private status
What is the return type of a method
The data type of the value that the method returns to the caller.
What are examples of valid return types for a method
Any of java’s primitive data types, any class type or void
What is special about methods with a return type of void
They do not return a value to a caller
What is the body of a method comprised of (general terms)
The code that performs the methods function. Written between beginning and ending curly braces.
Are the curly braces for method required?
Yes, these curly braces are not optional. They are required regardless of the number of statements in the method body.
What are two examples of error that can result if you forget one or more curly braces
illegal start of expression
OR
’;’ expected
What can be done in a method body?
A method can declare variables, call other methods, use: if/else statements, while loops, for loops, switch statements and do/while loops
Do all objects of a class have their own copy of the class methods?
No, all objects of a class share one copy of the class methods
What is the purpose of a return statement?
A return statement is a value returning method that sends back its results to the caller using the return statement.
Does the data type of the expression need to match the return type of the method?
yes, it must match
How is a value returning method called
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