Chapter 2: Debugging Exercise

2.1

Identify the error in the following program.

Answer

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

2.2

Identify the error in the following program.

Answer

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

2.3

What will happen when you run the following program?

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
b NO Error
c Expression-syntax-error
d Illegal Character ‘\’. Statement Missing.
e No Error
f No Error

Next Previous