Q5 8-57 Flashcards

1
Q
Predict the output of this program fragment: 
i = 0;
while(i , i, 10 - i);
    i = i + 1; 
}
A
  • •0 ••10
  • •1 ••9
  • •2 ••8
  • •3 ••7
  • •4 ••6
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is displayed by this program fragment for an input of 8?

scanf("%d", &n);
ev = 0;
while (ev < n){
    printf("%3d", ev);
    ev = ev + 2 ;
}
printf("\n");
A

••0••2••4••6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What output values are displayed by the following while loop for a data value of 5? Of 6? Of 7? In general, for a data value of any number n, what does this loop display?

printf(“Enter an integer> “);
scanf(“%d”, &x);
product = x;
count = 0;

while (count

A
(5):
5
25
125
625

It lists the powers of given integer from 1 to 4.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What values are displayed if the call to printf comes at the end of the loop instead of at the beginning? (Pertains to previous question)

A

It will list the powers of the given integer from 2 to 5.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The following segment needs revision. Insert braces where they are needed and correct the errors. The corrected code should take five integers and display their sum.

count = 0;
while (count , sum);

A

int count = 0, next_num, sum = 0;

while (count , sum);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Write a program segment that computes 1 + 2 + 3 + … + (n - 1 ) + n , where n is a data value . Follow the loop body with an if statement that compares this value to (n * (n + 1)) / 2 and displays a message that indicates whether the values are the same or different. What message do you think will he displayed?

A

int n, sum=0, count=1;
printf(“Input number to add to: “);
scanf(“%d”, &n);

for(; count , n, sum);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Trace the execution of the loop that follows for n = 8. Show values of odd and sum after the update of the loop counter for each iteration.

sum = 0;
for (odd = 1; odd

A

ODD: 1, 3, 5, 7
SUM: 1, 4, 9, 16

Sum of positive odd numbers less than 8 is 16.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
Consider the following definitions:
#define CBEGIN 10
#define CLIMIT -5
#define CSTEP 5
indicate what values of celsius would be at the end of the following for loops:
for(celsius = CLIMIT; celsius = CBEGIN; celsius += CSTEP);
for(celsius = CSTEP; celsius >= CBEGIN; celsius += CLIMIT) ;
for(celsius = CLIMIT; celsius
A

1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Rewrite the code shown in the previous question so the effect is equivalent but no increment/decrement operator appears in an expression with another arithmetic operator.

A

x

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What errors do you see in the following fragment? Correct the code so it displays all multiples of 4 from 0 through 100.

for mult4 = 0;
mult4

A

for(mult4=0; mult4, mult4);

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Trace the following program fragment:

j = 10;
for(i = 1; i , i, j);
j -= 2;
}

A
1 10
2 8
3 6
4 4
5 2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Rewrite the previous program fragment so that it produces the same output but uses 0 as the initial value of i.

A
j = 10;
for(i = 0; i < 5; ){
    printf("%d %d\n", ++i, j);
    j -= 2;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Write a program to display a centimeters-to-inches conversion table. The smallest and largest number of centimeters in the table are input values. Your table should give conversions in 10-centimeter intervals. One centimeter equals 0.3937 inch.

A

x

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

There are 9,870 people in a town whose population increases by 10 percent each year. Write a loop that displays the annual population and determines how many years it will take for the population to surpass 30,000.

A

int pop=9870, count=0;
float perc=1.10;
while(pop, count);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
What is displayed by the following program segments?
m = 3;
n = 5;
for(i = 1; i < i; ++j){ 
        printf("*");
    }
    printf("\n");
}
A

*

  • *
  • **
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
What is displayed by the following program segments?
m = 3;
n = 5;
for(i = n; i > 0; --i){
      for(j = m; j > 0; --j){
        printf("*");
      }
      printf("\n");
}
A
17
Q

Show the output displayed by these nested loops:

for (i = 0; i < 3; ++i){
    printf("Outer %4d\n", i);
    for (j=0; j 0; --k){
        printf(" Inner%3d%3d\n", i, k);
    }
}
A
Outer ••••0
 Inner ••0••0
 Inner ••0••1
 Inner ••0••2
 Inner ••0••1
Outer ••••1
 Inner ••1••0
 Inner ••1••0
 Inner ••1••2
 Inner ••1••1
Outer ••••2
 Inner ••2••0
 Inner ••2••1
 Inner ••2••2
 Inner ••2••1
18
Q

Write a program that displays the multiplication table for numbers 0 to 9.

A

define NINE 9

int x=0, y=0, product;
for(x=0; x);
}

19
Q

Rewrite the following code using a do-while statement:
sum = 0;
for (odd = 1; odd < n; odd = odd + 2)
sum = sum + odd;

A
sum=0;
odd=1;
do{
    sum = sum + odd;
    odd += 2;
} while(odd<n);