[Solved]Q1 Difference Pretest Loop Posttest Loop 4 Pts Provide Examples Q2 Find Explain Errors Fo Q37078592
Q1. What is the difference betweena pretest loop and a posttestloop ?
(4 pts) Provide examples ofeach.
Q2. Findand explain the errors in the following codefragments. Assume all variables have
(4 pts) been suitably declared.
- int a = 25 ;
boolean flag = TRUE;
while (flag ==TRUE)
if ( a>= 100 )
{
cout << a << endl ;
flag = FALSE ;
}
- int a = 1;
while ( a > 0)
{
int num = 5;
cout << num<< endl;
}
cout << a << endl;
Q3. What is the OUTPUT from each of the following codefragments?
(5 pts)
inta = 5, b = 90;
do
{
b = ( b/a ) –5;
if ( b > a )
b = a + 30;
} while ( b>= 0);
cout << a << b <<endl;
Q4. Write ado-while loop that asks the user to enter twonumbers. The numbers should be
(5 pts) added and the sum displayed. The usershould be asked if he or she needs to perform
theoperation again. If so, the loop should repeat; otherwise it shouldterminate.
Q5. The followingcode segment is supposed to write out the evennumbers between 1
(6 pts) and 15. It hastwo flaws in it.
int n = 2;
while ( n != 15 )
{
n = n + 2 ;
cout << n << ‘ ‘ ;
}
- What is the output of the code aswritten?
- Correct the code so that it works asintended.
Q6. Given the inputdata
( 5 pts) 25 10 6 -1
What is the output of the following code fragment?
int number, sum = 0;
cin >> number;
while ( number != -1 )
{
cin >>number;
sum = sum +number;
}
cout << sum << endl;
Q7. Convert the following forloop into a while loop;
(5 pts) for ( int i = 20 ; i > 10 ; i–)
. cout << i * i;
Q8. Use a nested for loop toproduce the following output: 2 4 8
(6 pts) 2 6 18
Q9. True/False
(5 pts) 1) It is possible that the body of awhile loop may not execute at all.
2) When a while loop terminates, the controlfirst goes back to the statement just
Before the while statement, and then the control goes to thestatement immediately
following the while loop.
3) If k = 5, then the expressionk++ + ++k evaluates to 12.
4) switchis a selection statement in C++
5) For switch statement we canput range for case labels, such as case 1..3
Q10. Multiple choice
(5 pts)
1. What wrong? while ( (i < 5)&& (i > 20) )
- Logical operator && cannot be used in testcondition
- test condition is always true
- test condition is always false
- The while loop is an exit-condition loop
2. A continue statement causes execution toskip to
- The return 0; statement
- The first statement after the loop
- The statement following the continue statement
- The next iteration of the loop
3. In a group of nested loops, which loop isexecuted the most number of times?
- The outermost loop
- The innermost loop
- All loop are executed the same number of times
- Cannot be determined without knowing the size of theloops.
4. What’s wrong? for (int k = 2, k<= 12, k++)
- Increment should always be ++k
- The variable must always be the letter i when using a forloop
- There should be a semicolon at the end of the statement
- The commas should be semicolons
5. Which looping process is best used when thenumber of iterations is known?
- for
- while
- do-while
- all looping processes require that the iterations beknown.
Expert Answer
Answer to Q1. What is the difference between a pretest loop and a posttest loop ? (4 pts) Provide examples of each. Q2. Find and e… . . .
OR

