Chapter 3: Debugging Exercise

3.1

What will happen when you execute the following code?

Answer

i = 400*400/400; Here, 400*400 = 160000 which exceeds the maximum value of int variable. So wrong output will be shown when this program will be run.
Correction :

should be changed as

to see the correct output.

3.2

Identify the error in the following program.

Answer

num [1] = [1] num?. You should write index number after array name but here index number is mention before array name in [1] num

So expression syntax error will be shown.

Correction : num[1] = num[1]? is the correct format.

3.3

Identify the errors in the following program.

Answer

The above code will cause the following situation:
Program will be continuing while value of i is 1 and value of i is updating. So infinite loop will be created.
Correction: At last line i- should be changed as i–;

3.4

Identify the errors in the following program.

Answer

Assignment operator should be used in the following line:

That means the above line should be changed as follows:

3.5

Missing

3.6

Find errors, if any, in the following C++ statements.

  • (a) long float x;
  • (b) char *cp = vp; // vp is a void pointer
  • (c) int code = three; // three is an enumerator
  • (d) int sp = new; // allocate memory with new
  • (e) enum (green, yellow, red);
  • (f) int const sp = total;
  • (g) const int array_size;
  • (h) for (i=1; int i<10; i++) cout << i << “/n”; (i) int & number = 100; (j) float *p = new int 1101; (k) int public = 1000; (l) char name[33] = “USA”;
Answer
No. Error Correction
(a) Too many types float x; or double x;
(b) type must be matched char *cp = (char*) vp;
(c) no error
(d) syntax error int*p=new int[10];
(e) tag name missing enum color (green, yellow, red)
(f) address have to assign instead of content int const*p = &total;
(g) C++ requires a const to be initialized const int array-size = 5;
(h) undefined symbol i for(int i=1;i<10;i++)

cout<<i<<“/n”;

(i) invalid variable name int number = 100;
(j) wrong data type float *p = new float[10];
(k) keyword can not be used as a variable name int public1 = 1000;
(l) array size of char must be larger than the number of characters in the string chat name[4] = “USA”;

Next Previous