Chapter 2: Debugging Exercise

2.1

Identify the error in the following program.

#include<iostream.h>
void main()
{
    int i = 0;
    i = i + 1;
    cout « i « " ";
    /*comment \*//i = i + 1; 
    cout << i;
}
Answer

/* comment\*//i=i+1;   ->  Syntax error

2.2

Identify the error in the following program.

#include<iostream.h>
void main()
{
     short i=2500, j=3000;
     cour>> "i+j=">> -(i+j);
}
Answer

cout >> “i+j=”>> -(i + j); -> Illegal structure operation.

2.3

What will happen when you run the following program?

#include<iostream.h>
void main()
{
     int i=10, j=5;
     int modResult=0;
     int divResult=0;
     modResult = i%j;
     cout<<modResult<<" ";
     divResult = i/modResult;
     cout<<divResult;
}
Answer

divResult = i/modResult;  -> floating point Error or divide by zero

Note: If this kind of Error exist in a program, the program will successfully compile but it will show Run Error.

2.4

Find errors, if any, in the following C++ statements.
(a) cout.<<“x=” x; (b) m = 5; // n = 10; // = m + n: (c) cin >>x; >>y;
(d) cout <<\h ‘Name:” <<name;
(e) cout <<“Enter value:”; cin >> x:
(f) /*Addition*/ z = x + y;

Answer
Question Error Correction
a Statement Missing
cout<<"x="<<x;
b NO Error
c Expression-syntax-error
cin>>x>>y;
d Illegal Character ‘\’. Statement Missing.
cout<<"\n Name"<<name;
e No Error
f No Error

Next Previous