Warm-up Exercises
- Explain the concept of loops with sentinel, and give a small example.
Solution
Loops check for a condition to determine whether or not to repeat execution. The value that is checked in this condition is known as the sentinel value.
Example:
- Write a program containing a while loop that would display the numbers between -100 and 100 (both included) with a space between them when executed.
Solution
- Write a for loop that displays on the screen the sequence “1, 2, 3, 4, 5, 6, 7, 8, 9, 10,”.
Solution
- Write a for loop that displays on the screen the sequence “1, 2, 3, 4, 5, 6, 7, 8, 9, 10”. Notice that the final number is not followed by a comma.
Solution
- Write a for loop that displays on the screen the sequence “1 3 5 7 9”.
Solution
Questions
- A while statement can cause logic errors where the body never stops executing. This is known as a(n)
- Syntax error
- Fatal error
- Infinite loop
- None of the above.
- A _____ can be used in a repetition structure (a loop) to control the number of times a set of statements will execute.
- Declaration
- Counter
- Controller
- None of the above.
- How many times is the body of the loop below executed?
- 9
- 10
- 11
- 0
- How many times is the while statement checked in the code below?
- 9
- 12
- 11
- 0
- Which of the following increments the variable a by one?
- ++a
- a++
- a+=1
- All of the above.
- Counting loops should be controlled with _____ values.
- double
- int
- char
- None of the above.
- A common logic error known as a(n) _____ occurs when the
programmer incorrectly specifies a conditional operator, such as
<
instead of<=
.
- Fatal error
- Off-by-one error
- Syntax error
- None of the above.
- The header
for(int i = 0; i <= 10; ++i)
will causei
to be incremented:
- Before the body begins execution
- After the body begins to execute, but before it finishes
- After the entire body executes
- None of the above.
- The _____ statement, when executed in a while loop, will skip the remaining statements in the loop body and proceed with the next iteration of the loop.
- continue
- break
- next
- None of the above.
- Consider the code segment below.
This segment is equivalent to which of the following? - [ ] if (gender == 1 || age >= 65) { ++seniorFemales; } - [x] if (gender == 1 && age >= 65) { ++seniorFemales; } - [ ] if (gender == 1 AND age >= 65) { ++seniorFemales; } - [ ] if (gender == 1 OR age >= 65) { ++seniorFemales; }
- Methods that call themselves are known as _____ methods.
- Reiterative
- Self-calling
- Repeat-calling
- Recursive
- What will be displayed on the screen by the following program?
Solution
3 4 5
- Given an int variable counter, write three statements to decrement its value by 1.
Solution
Four possible ways:
- What will be displayed on the screen?
Solution
“3 and 6”
- What will be displayed on the screen by the following program?
Solution
2 3 4
- What will be displayed on the screen by the following program?
Solution
Nothing, and the program will loop indefinitely.
- What will be displayed on the screen by the following program?
Solution
7 will be displayed infinitely many times.
- What do we name a variable that is incremented at every iteration of a loop, i.e., that keeps the running total?
Solution
An accumulator.
Problems
- Write an equivalent code replacing the while loop with a for loop, and provide short justification.
Solution
Example:
The for loop contains the incrementing variable, the looping condition, and the incrementing statement needed for a loop.
- Find all syntax errors in this code
Solution
- Write a C# program that takes a single-digit number as input and then, using a for loop, displays a rectangle of that digit that is 3 columns wide and 5 rows tall.
Solution
- Assume you are given an initialized string variable
name
, and a string variablefield
. Write a small program that assigns tofield
- “CS” if name is “Turing” or “Liskov”
- “Math” if name is “Aryabhata” or “Noether”
- “Unknown” otherwise.
Solution
- Assume you are given an un-assigned string variable
letterGrade
, and an already assigned float variablenumberGrade
. Write a small program that assigns “A” toletterGrade
ifnumberGrade
is between 100 and 90 (both included), “B” ifnumberGrade
is between 90 (excluded) and 80 (included), etc., and “Invalid data” is strictly lower than 0 or strictly greater than 100. Should you use a switch statement or an if…else if…else?
Solution
An if…else if…else is the right structure for the task:
- Write a loop that displays on the screen numbers between [0.0, 1.0], using one decimal place precision, i.e. 0.0, 0.1, 0.2, 0.3…
Solution
- Write a loop that displays on the screen a value that decreases by 0.5 on each iteration. Start from 10 and iterate as long as the value remains positive.
Solution
- Write a program that computes the sum of numbers [1, n]. You can choose any value you want for n, where n > 1. For example, if you choose n = 10, then program should compute and display the result for the following: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55.