Ticker

6/recent/ticker-posts

Class 10 Computer Engineering Object oriented Programming 2072 Solved Question Paper

 

SEE 2072

Group A

1.       How would you make a function inline?

àI would make a function inline by using inline keyword before the function name .

 

2.       What do you mean by static member function?

à A static member function is a special function in a programming language, which is used to access static data members and other static member functions.

 

3.       Define a string.

àA string can be defined as an array of characters.

 

4.       What is operator function?

àAn overloaded operator is called an operator function. Operator functions are the same as normal functions. The only differences are, name of an operator function is always operator keyword followed by symbol of operator and operator functions are called when the corresponding operator is used.

 

Group B

5.       Why is C++ called object oriented programming language

 Ã  C++ views a problem in terms of objects involved rather than the procedure for doing it. Similarly, other concepts such as inheritance, polymorphism, abstraction, encapsulation are also possible in C++. So, it is called object-oriented programing language.

 

6.       WAP in C++ to display following output using single cout statement.

à          #include<iostream>

using namespace std;

int main(){

                cout<<"Math = 90"<<endl<<"Physics=77"<<endl<<"Chemistry=56";

                return 0;

}

Output:

Math = 90

Physics=77

Chemistry=56

 

7.       What do you understand by explicit type conversion?*

à

8.       WAP to display smallest number among two number

#include<iostream>

using namespace std;

 

int main(){

                int a,b;

                cout<<"Enter two different numbers : ";

                cin>>a>>b;

                if(a<b)

                cout<<a<<" is smallest";

               

                else

                cout<<b<<" is smallest.";

               

                return 0;

}

 

9.       What is the advantage of passing arguments by reference to a function? Explain.

àAdvantages:

·         No new copy of variable is made, so overhead of copying is saved.

·         Array or Object can be pass

·         Changes to parameter values within the function also affect the original arguments which is sometimes useful.

 

10.   What do you mean by function overloading?

àFunction overloading is the feature of OOP that allows to create multiple functions of same name with different implementations.

 

11.   WAP to read a set of number into an array and display the even number.

à

#include<iostream>

using namespace std;

int main(){

                int a[10],n,i;

                cout<<"Enter the number of elements:";

                cin>>n;

                cout<<"enter the numbers of array:";

                for(i=0;i<n;i++){

                                cin>>a[i];

                }

                cout<<"The even numbers are:";

                for(i=0;i<n;i++){

                                if(a[i] %2==0)

                                cout<<endl<<a[i];

                }

                return 0;

}

 

12.   State the contents of objects S1 and S3 after executing the following segment of program.*

string S1("SLC"),S2;

S2=S1;

String S2=S1;

String S3 ("Technical"+S2);

à

 

13.   WAP to add two variable and display the sum using class and object.

à

#include<iostream>

using namespace std;

class Sum{

    public:

    int a,b,s;

    void Add(){

        cout<<"Enter two numbers: ";

        cin>>a>>b;

        s=a+b;

        cout<<"The sum of two numbers is "<<s;

    }

};

int main(){

    Sum obj;

    obj.Add();

    return 0;

}

 

14.   Describe the importance of destructor.*

àDestructor is important as it can be very useful to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed.

15.   Class A is derived from Class B. The class A doesn’t contain any data members of its own.  Does class A requires Constructors? Why? *

à

 

16.   Write any four errors that handle the function during file operation.

àFunctions that handle error during file operation are:

·         int bad()

·         int eof()              

·         int fail()

·         int good()

 

Group C

17.   WAP to display all prime numbers from 1 to 100.

à

#include<iostream>

using namespace std;

 

int main(){

                int i,j,count;

                cout<<"The prime numbers are : ";

                for(i=1;i<=100;i++){

                                count=0;

                                for(j=1;j<=i;j++){

                                                if(i%j==0)

                                                count++;

                                }

                               

                                if(count==2){

                                                cout<<i<<endl;

                                }

                }

                return 0;

}

 

18.   What do you understand by default argument? Explain with example.*

à

 

19.   WAP to read a set of number into an array and display the count of odd and even numbers.

à

#include<iostream>

using namespace std;

 

int main(){

                int a[25],n,i,even=0,odd=0;

                cout<<"Enter the size of array : ";

                cin>>n;

               

                cout<<"Enter the elements of array :";

                for(i=0;i<n;i++){

                                cin>>a[i];

                }

               

                for(i=0;i<n;i++){

                               

                                if(a[i]%2==0){

                                                even++;

                                }

                                else

                                                odd++;

               

                }

                cout<<"the count of even numbers  :"<<even<<endl;

                cout<<"the count of odd numbers :"<<odd;

               

               

                return 0;

}

 

20.   WAP to overload '<' operator to compare two objects.*

à

                #include<iostream>

using namespace std;

class Distance{

                int feet;

                public:

                                Distance(int a){

                                                feet=a;

                                }

                                bool operator < (Distance o2){

                                                if(feet<o2.feet)

                                                                return true;

                                                else

                                                                return false;      

                                }

};

int main(){

                Distance s1(5),s2(6);

                if(s1<s2)

                cout<<"s1 is smaller";

               

                else

                cout<<"s2 is smaller";

                return 0;

}

 

21.   What do you mean by ambiguity in multiple inheritance? How do you solve it? Explain with example.

àSuppose, two base classes have a same function which is not overridden in derived class.  If you try to call the function using the object of the derived class, compiler shows error. It is because compiler doesn’t know which function to call. This problem is called ambiguity in multiple inheritance. For example:

#include<iostream>

using namespace std;

class base1{

                public:

                 void SomeFunction(){

                                cout<<"base1";

                 };

                 

};

class base2{

                public:

                                void SomeFunction(){

                                                cout<<"base2";

                                }

};

class derived : public base1, public base2{

               

};

int main(){

                derived obj;

                obj.SomeFunction() //Error!

                return 0;

}

The best solution to this problem is to use the scope resolution operator with the function to specify that which function of which class is being called. In above example, we should write this in main function:

                int main(){

                derived obj;

                                obj.base1:: SomeFunction(); //function of base1 class is called

                                obj.base2:: SomeFunction(); //function of base2 class is called

                                return 0;

}

 

22.   WAP to read item name and cost from keyboard and write it to the file called item.txt and display the information on screen. *

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

int main(){

   fstream newfile;

   char str[200];

   cout<<"Enter the name of items and their cost :";

   gets(str);

   newfile.open("items.txt",ios::out);  // open a file to perform write operation using file object

   if(newfile.is_open())     //checking whether the file is open

   {

      newfile<<str<<"\n"; //inserting text

      newfile.close(); //close the file object

   }

   newfile.open("items.txt",ios::in); //open a file to perform read operation using file object

   if (newfile.is_open()){   //checking whether the file is open

      string tp;

      while(getline(newfile, tp)){  //read data from file object and put it into string.

         cout << tp << "\n";   //print the data of the string

      }

      newfile.close();   //close the file object.

   }

}

 

Or #include<iostream>

#include<fstream>

using namespace std;

int main(){

                string st;

                cout<<"Enter the string : ";

                getline(cin,st);

                fstream newfile;

                newfile.open("items.txt", ios::out);

                if(newfile.is_open()){

                                newfile<<st;

                                newfile.close();

                }

                newfile.open("items.txt", ios::in);

                if(newfile.is_open()){

                                string s;

                                while(getline(newfile, s)){

                                                cout<<st<<endl;

                                }

                                newfile.close();

                }

}

Reactions

Post a Comment

0 Comments