Tuesday, January 12, 2010

c++part11


Four mark Questions

1. Which of the following is a valid program to open the file and write the contents to the file.
i) void main( )
{
ofstream outfile(“out.txt”);
outfile<<”Creating an ASCII file”< outfile<<”C++ is said to superset of C”< }
ii) void main( )
{
ifstream outf(“out.txt”);
outf<<” Creating an ASCII test file< outf<<” C++ is superset of C”< }
iii) void main( )
{
ofstream outf;
outf<<”\n This is an ASCII file”;
outf<<”\n C++ is superset of C”;
}
iv) void main( )
{
ofstream outf(“out.txt”);
char text[]=” this is text line”;
int I=0;
while(text[I])
outf.put(text[I++]);
}

a. only ii b. I,iii c. I,iv d. None

2. Match the modes with their description:
1) in I) Opens the file for writing
2) out ii) Open file for binary data
3) ate iii) opens the file for reading
4) binary iv) erase file before reading or writing
a. 1- iii, 2- I, 3- iv, 4- ii b. 1- ii, 2-I, 3- iv, 4- iii c. 1-I, 2- ii, 3- iii,4- iv d.1- iv,2-iii, 3- ii, 4-1

3. What is the O/p:
void main( int argc,int *ss[])
{
int sum=0;
if (argc<3)
{
cout<<” Enter atleast two integers”; exit(0);
}
else
{
for(I=1;I cout<<” sum of the numbers is :”< }
}
c:\> ss 10 20 30 40 50

a. 0 *b. 150 c. Error d. None

4.Give the O/P
class vehicle
{ public:
void message(){ cout<<”\n vehicle message”;}
};
class car:public vehicle
{ public:
void message ( ) { cout<<”\n car message”;}
};
void main()
{ vehicle *v;
v=new vehicle();
v->message();
v=new car( );
v-> message( );

}
a. vehicle message , car message b. vehicle message, vehicle message c. Error d. None

5.Give the O/P
class vehicle
{ public:
virtual void message(){ cout<<”\n vehicle message”;}
};
class car:public vehicle
{ public:
void message ( ) { cout<<”\n car message”;}
};
void main()
{ vehicle *v;
v=new vehicle();
v->message();
v=new car( );
v-> message( );
}
a. vehicle message , car message b. vehicle message, vehicle message c. Error d. None

6.Give the O/P
class vehicle
{ public:
virtual void message()=0;
};
class car:public vehicle
{ public:
void message ( ) { cout<<”\n car message”;}
};
void main()
{ vehicle *v;
v=new vehicle();
v->message();
v=new car( );
v-> message( );
}
a. vehicle message , car message b. vehicle message, vehicle message c. Error d. None

7. Which of the following is a valid program to open the file and read the contents to the file.
i) void main( )
{ ifstream infile(“out.txt”);
char buff[80];
while(infile)
{ infle.getline(buff,80);
cout< }
}
ii) void main( )
{ ofstream infile(“out.txt”);
char buff[80];
while(infile)
{ infle.getline(buff,80);
cout< }
}
iii) void main( )
{ ofstream outf;
outf<<”\n This is an ASCII file”;
outf<<”\n C++ is superset of C”;
}
iv) void main( )
{ ifstream outf(“out.txt”);
char c;
while(outf)
{
outf.get(ch); cout< }

a. only ii b. I,iii c. I,iv d. None

8. Read the below program:
const int MAX=3;
class stack
{ int st[MAX],top;
public: class range{ };
stack(){ top=-1;}
void push(int var)
{ if(top>=MAX-1) throw range();
st[++top]=var;
}
int pop()
{ if(top<0) throw range();
return st[top--];
}

};
void main()
{ tack ss1;
try
{ s1.push(11);
s1.push(12);
s1.push(24);
cout< cout<cout<cout<
}catch(stack::range)
{
cout<<”\n stack is full or empty”;
}
cout<<” arrived after catch”;
}
events :
i)The member function throws an exception ii) Code is executing normally outside a try block
iii) control transfers to the exception handler iv) control enters the try block
v) A statement inside try block causes an error in the member function

State the correct order of the events given
a. ii, iv, v, I, iii b. iii, iv, v, I, ii c. ii, iv, i, v, iii d. ii, iv, v, I, iii






Answer – 4 Marks

1. c 2. a 3. b 4.b 5.a 6.c 7.c 8.a