DEBUGGING
DEBUGGING
DEBUGGING
NO.2:
#include<iostream.h>
void main()
{
short i=2500,j=3000;
cour>>”i+j=”>>-(i+j);
}
Answer:
cout>>”i+j=”>>-(i+j);
(illegal structure operation)
NO.3:
#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)
NO.4:
#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.
NO.5:
#include<iostream.h>
using namespace std;
void main()
{
int i=5;
while(i)
{
switch(i)
{
default:
case4:
case 5:
break;
case1:
continue;
case 2:
case 3:
break;
}
i-;
}
}
Answer:
case 1:
continue;
The above code will cause the following situation:
Program will be continuing while the 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-;
NO.6:
#include<iostream>
#define pi 3.14
using namespace std;
int squareArea(int &);
int circleArea(int &);
void main()
{
int a-10;
cout<<squareArea(a)<<” “;
cout<<circleArea(a)<<” ;
cout<<a<<endl;
{
int squareArea(int &a)
{
return a*==a;
}
int circleArea(int &r)
{
return r=pi*r*r;
}
}
}
Answer:
Assignment operator should be used in the following line.
return a*==a;
Correction:
return a*=a;
NO.7:
#include<iostream>
using namespace std;
struct Room
{
int width;
int length;
void setValue(int w,int l)
{
width=w;
length=l;
}
};
void main()
{
Room objRoom;
objRoom.setValue(12,1,4);
}
Answer:
void setValue(int w,int l)
Function must be public.
NO.8:
#include<iostream.h>
using namespace std;
void main()
{
int num[]={1,2,3,4,5,6};
num[1]==[1]num?cout<<”Success”:cout<<”Error”;
}
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
Correction:
num[1]=num[1]?
NO.9:
#include<iostream>
using namespace std;
class staticfunction
{
static int count;
public:
static void setCounto()
{
count++;
}
void displayCount()
{
cout<<count;
}
};
int staticFunction::count=10;
void main()
{
staticFunction obj1;
obj1setCount(5);
staticFunction::setCount();
obj1.displayCount();
}
Answer:
setCount() is a void argument type, so here obj1.setCount(5);
replace with obj1.setCount();
NO.10:
#include<iostream.h>
using namespace std;
class item
{
private:
static ont count;
public:
item()
{
count++;
}
int getCount()
{
return count;
}
int* getCountAddress()
{
return count;
}
};
int item::count=0;
void main()
{
item objitem1;
item objitem2;
cout<<objitem1.getCount()<<’ ‘;
cout<<objitem2.getCount()<<’ ‘;
cout<<objitem1.getCountAddress()<<’ ‘;
cout<<objitem2.getCountAddress()<<’ ‘;
}
Answer:
int* getCountAddress()
{
return &count;
}
Note: All other code remain unchanged.
NO.11:
#include<iostream>
using namespace std;
class Room
void area()
{
int width, height;
class Room
{
int width, height;
public:
void setvalue(int w, int h)
{
width=w;
height=h;
}
void displayvalues()
{
cout<<(float)width<<’ ‘<<(float)height;
}
};
Room objRoom1;
objRoom1.setValue(12,8);
objRoom1.displayvalues();
}
void main()
{
Area();
Room objRoom2;
}
Answer:
Undefined structure Room in main() function.
Correction:
void main()
{
Area();
}
NO.12:
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
1 cout<<”x=”<<x;
b NO Error
c Expression-syntax-error
1 cin>>x>>y;
d Illegal Character ‘\’.
Statement Missing.
1 cout<<"\n Name"<<name;
e No Error
f No Error
NO.13:
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
(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 chat name[4] = “USA”;
number of characters in the string
NO.14:
#include <iostream>
using namespace std;
int fun()
{
return 1;
}
float fun()
{
return 10.23;
}
void main()
{
cout <<(int)fun() << ' ';
cout << (float)fun() << ' ';
}
Answer:
Here two function are same except return type. Function overloading can
be used using different argument type but not return type.
#include<iostream>
using namespace std;
int fun()
{
return 1;
}
float fun1()
{
return 10.23;
}
void main()
{
cout<<fun()<<" ";
cout<<fun1()<<" ";
}
NO.15:
#include <iostream>
using namespace std;
void display(const Int constl=5)
{
const int const2=5;
int arrayl[constl];
int array2[const2];
for(int 1=0; i<5; 1++)
{
arrayl[i] = i;
array2[i] = i*10;
cout <<arrayl[i]<< ' ' << array2[i] << ' ' ;
}
}
void main()
{
display(5);
}
Answer:
#include<iostream>
using namespace std;
void display()
{
const int const1=5;
const int const2=5;
int array1[const1];
int array2[const2];
for(int i=0;i<5;i++)
{
array1[i]=i;
array2[i]=i*10;
cout<<array1[i]<<" "<<array2[i]<<" ";
}
}
void main()
{
display();
}
NO.16:
#include <iostream>
using namespace std;
int gValue=10;
void extra()
{
cout << gValue << ' ';
}
void main()
{
extra();
{
int gValue = 20;
cout << gValue << ' ';
cout << : gValue << ' ';
}
}
Answer:
Here cout << : gvalue << ” “; replace with cout <<::gvalue<< ” “;
#include <iostream>
using namespace std;
int gValue=10;
void extra()
{
cout << gValue << ‘ ‘;
}
void main()
{
extra();
{
int gValue = 20;
cout << gValue << ‘ ‘;
cout <<::gvalue<< " ";
}
}
NO.17:
Find errors, if any, in the following function definition for displaying a
matrix: void display(int A[][], int m, int n)
{
for(1=0; i<m; i++)
for(j=o; j<n; j++)
cout<<" "<<A[i][j];
cout<<"\n";
}
Answer:
First dimension of an array may be variable but others must be constant.
Here int A [ ] [ ] replace by the following code:
int A [ ] [10];
int A[10] [10];
int A[ ] [size];
int A [size] [size];
Where const int size = 100;
any other numerical value can be assigned to size.
NO.18:
#include <iosream>
using namespace std;
class Length
{
int feet;
float inches;
public:
Length()
{
feet = 5;
inches = 6.0;
}
Length(int f, float in)
{
feet = f;
inches=in;
}
Length addLength(Length 1)
{
1.inches this->inches;
1.feet += this->feet;
if(1.inches>12)
{
1.inches-=12;
1.feet++;
}
return 1;
}
int getFeet()
{
return feet;
}
float getInches()
{
return inches;
}
};
void main()
{
Length objLength1;
Length objLenngth1(5, 6.5);
objLength1 = objLength1.addLength(objLength2);
cout << objLenth1.getFeet() << ' ';
cout << objLength1.getInches() << ' ';
}
Answer:
#include<iostream>
using namespace std;
void main()
{
Length objLength1;
Length objLength2(5,6.5);
objLength1=objLength1.addLenghth(objLenghth2);
cout<<objLength1.getFeet()<<" ";
cout<<objLength1.getInches()<<" ";
}