1. What is an \"off-by-one error\"? Give an example for it from your experience.
ID: 651846 • Letter: 1
Question
1. What is an "off-by-one error"? Give an example for it from your experience.
2. What will the output of the following statements?
ArrayList<String> names = new ArrayList<String>();
names.add("Annie");
names.add("Bob");
names.add("Charles");
for (int i = 0; i < 3; i++)
{
String extra = names.get(i);
names.add (extra);
}
System.out.print (names);
3. Declare an array of 10 integers and initialize its elements to the following values: -10; -8; -6; -4; -2; 0; 2; 4; 6;8;10
4. What are nested loops? Give an example of nested loop?
5. What is an infinite loop? Write a for statement to demonstrate it.
6. Rewrite the following loops without using the "for each" (enhanced for loop) construct. Here, values have the type double.
(a) for (double element : values) sum = sum + element;
(b) for (double element : values) if (element == target) return true;
7. Write a Java program that asks the user to input the scores of 10 students. The scores entered must be stored in an array.The program must determine:
(a) The lowest score
(b) The highest score
(c) The average score.
Explanation / Answer
(1)I was off by one once I tried to figure a random variety between ten and fifty, once, in class. however that wasn't returning from a loop.
You can be off by one once you try and draw a hollow sq. of dimension forty and height forty character by character, as within the example for nested loops last week.
Off by one ar errors that happen once the start line of a condition isn't clear, like beginning with a zero or a one and reaching all the thanks to the higher limit or ending right before it. you'll be off by one once you traverse the characters in a very string as a result of you wish to recollect that the characters ar indexed beginning at zero.
(2)
Charles
Annie
Bob
(4)
The inserting of 1 loop within the body of another loop is termed nesting. once you "nest" 2 loops, the outer loop takes management of the quantity of complete repetitions of the inner loop. whereas all kinds of loops is also nested, the foremost normally nested loops area unit for loops.
for(num1 = 0; num1 <= 9; num1++)
{
cout << num1 << endl;
}
(5)
An infinite loop (sometimes referred to as Associate in Nursing endless loop ) may be a piece of secret writing that lacks a practical exit so it repeats indefinitely. In programing, a loop may be a sequence of instruction s that's frequently perennial till an explicit condition is reached. Typically, an explicit method is finished, like obtaining Associate in Nursing item of knowledge and ever-changing it, then some condition is checked, like whether or not a counter has reached a prescribed range. If the presence of the required condition can not be determined, consequent instruction within the sequence tells the program to come to the primary instruction and repeat the sequence, which usually goes on till the program terminates mechanically once an explicit period of your time, or the package terminates the program with a slip.
(7)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.