CSC121
What is the advantage of using a sentinel?
Sometimes the user has a list of input values that is very long, and doesn't know the number of items there are. When the sentinel value is entered, it signals the end of the list, and the user doesn't have to count the number of items in the list.
Why must the value chosen for use as a sentinel be carefully selected?
The value chosen for a sentinel must be carefully selected because you want it to be unique enough that it will not be mistaken for a regular value in a list you are working with.
Describe the difference between the while loop and the do-while loop.
The while loop is a pretest loop and the do - while loop is a posttest loop.
Which loop should you use in situations where you want the loop to repeat until the boolean expression is false, and the loop should not execute if the test expression is false to begin with?
The while loop.
Describe a programming problem that would require the use of an accumulator.
There are many possible examples. A program that asks the user to enter a business's daily sales for a number of days, and then displays the total sales is one example
What does it mean to let the user control a loop?
controls the number of times the iterations performed by a loop.
What is a file's read position? Where is the read position when a file is first opened for reading?
marks the location of the next item that will be read from the file. When a file is opened, its read position is set to the first item in the file. When the item is read, the read position is advanced to the next item in the file. As subsequent items are read, the internal read position advances through the file.
What does it mean to append data to a file?
writing new data to the end of the data that already exists in the file.
How does a file buffer increase a program's performance?
A buffer is a small "holding section" of memory. When a program writes data to a file, that data is first written to the buffer. When the buffer is filled, all the information stored there is written to the file. This technique increases the system's performance because writing data to memory is faster than writing it to a disk. The close method writes any unsaved data remaining in the file buffer.
Explain what is meant by the phrase "conditionally executed."
A conditionally executed statement is performed only when a certain condition is true.
Explain the purpose of a flag variable. Of what data type should a flag variable be?
A flag is a boolean variable that signals when some condition exists in the program. When the flag variable is set to false, it indicates the condition does not yet exist. When the flag variable is set to true, it means the condition does exist.
When writing data to a file, what is the difference between the print and the println methods?
After the println method writes its data, it writes a newline character. The print method does not write the newline character.
Why is it good advice to indent all the statements inside a set of braces?
By indenting the conditionally executed statements, you are causing them to stand out visually. This is so you can tell at a glance what part of the program the if statement executes.
When does a constructor execute? What is its purpose?
The object memory is allocated, the field variables with initial values are initialized, and then the constructor is called, but its code is executed after the constructor code of the object super class.
Describe a programming problem requiring the use of nested loops.
There are many possible examples. One example is a program that asks for the average temperature for each month, for a period of five years. The outer loop would iterate once for each year and the inner loop would iterate once for each month.
What risk does a programmer take when not placing a trailing else at the end of an if-else-if statement?
There is a chance that the program will not compile because Java is looking for other factors that may be possible. No code executed.
Explain why a misplaced semicolon can cause an if statement to operate incorrectly.
A misplaced semicolon can cause an if statement to operate incorrectly because the if statement isn't complete without its conditionally executed statement
Describe the difference between pretest loops and posttest loops.
A pretest loop tests its test expression before each iteration. A posttest loop tests its test expression after each iteration.
Why is it critical that accumulator variables are properly initialized?
An accumulator is used to keep a running total of numbers. In a loop, a value is usually added to the current value of the accumulator. If it is not properly initialized, it will not contain the correct total.
What is an infinite loop? Write the code for an infinite loop
An infinite loop is a loop that has no way of stopping, it occurs when a programmer forgets to write code inside the loop that makes the test condition false.
Why are the statements in the body of a loop called conditionally executed statements?
Because they are only executed when a condition is true
Why should you indent the statements in the body of a loop?
By indenting the statements, you make them stand out from the surrounding code. This helps you to identify at a glance the statements that are conditionally executed by a loop.
Why should a program close a file when it's finished using it?
Closing a file writes any unsaved data remaining in the file buffer.
Briefly describe the difference between the prefix and postfix modes used by the increment and decrement operators.
In postfix mode the operator is placed AFTER the operand. In prefix mode the operator is placed BEFORE the variable operand. Postfix mode causes the increment or decrement operation to happen AFTER the value of the variable is used in the expression. Prefix mode causes the increment or decrement to happen BEFORE.
Briefly describe how the && operator works.
It takes two boolean expressions as operands and creates a boolean expression that is true only when both subexpressions are true
What happens when you compare two String objects with the == operator?
It will only evaluate it as true because == is used for primitive operators such as boolean, int, and double.
.Which loop should you use in situations where you want the loop to repeat until the boolean expression is false, but the loop should execute at least once?
The do - while loop.
What is a potential error that can occur when a file is opened for reading?
The file does not exist.
Which loop should you use when you know the number of required iterations?
The for loop.
Briefly describe how the || operator works.
The program must fit either case in the || operator in order for the code to complete.
Why are the relational operators called "relational"?
They determine whether a specific relationship exists between two values. The relationships are greater-than, less-than, equal-to, not equal-to, greater-than or equal-to, and less-than or equal-to.
How do you open a file so that new data will be written to the end of the file's existing data?
You create an instance of the FileWriter class to open the file. You pass the name of the file (a string) as the constructor's first argument, and the boolean value true as the second argument. Then, when you create an instance of the PrintWriter class, you pass a reference to the FileWriter object as an argument to the PrintWriter constructor. The file will not be erased if it already exists and new data will be written to the end of the file.
What does the Scanner class's hasNext method return when the end of the file has been reached?
false