Data Structures (OLD) - Test Papers

 CBSE TEST PAPER-01

Class-12 Computer Science(Classes and Objects)


General Instruction: -

  • Question No. 1 to 2 carry One marks. Question No. 3 to 8 carry Two marks. Question No. 9 to 10 carry Four marks.                

  1. What is the default access level in a class?
  2. Rewrite the following program after removing the syntactical errors (if any).Underline each correction.
    class PAYITNOW
    {
    int Charge=5;
    PUBLIC:
    void Raise(){cin>>Charge;}
    void Show{cout<<Charge;}
    };
    void main()
    {   PAYITNOW P;
    Raise.P( );
    Show.P( );
    }
  3. What is the significance of access specifiers in a class?
  4. How does a class implement data encapsulation and abstraction?
  5. What are static members of a class?
  6. When will you make a function inline and why?
  7. How is memory allocated to a class and its objects?
  8. Write the output of the following C++ program
    Note : Assume all required header files are already being included in the program.
    class Stock
    {
    long int ID;
    float Rate;
    int Date;
    public:
    Stock()
    {
    ID=100;Rate=200;Date=1;
    }
    void RegCode(long int I,float R)
    {
    ID=I;
    Rate=R;
    }
    void Change(int New,int DT)
    {
    Rate+=New;
    Date=DT;
    }
    void Show()
    {
    cout<<"Date :"<<Date<<endl;
    cout<<ID<<"#"<<Rate<<endl;
    }   };
    void main()
    {
    Stock A,B,C;
    A.RegCode(1002,100);
    A.Show();
    B.RegCode(2005,300);
    A.Change(200,50);
    B.Change(500,20);
    C.Change(20,10);
    A.Show();
    B.Show();
    C.Show();
    }
  9. Write the definition of a class PIC in C++ with following description :
    Private Members
    - Pno. //Data member for Picture Number (an integer)
    - Category //Data member for Picture Category (a string)
    - Location //Datamember for Exhibition Location (a string)
    - FixLocation //A member function to assign Exhibition Location as per category
    //as shown in the following table

    Category

    Location

    Classic

    Amina

    Modern

    Jim Plaq

    Antique

    Ustad Khan

    Public Members
    - Enter() //A function to allow user to enter values Pno, category and call FixLocation() function
    - SeeAll() //A function to display all the data members
  10. Write the definition of a class METROPOLIS in C++ with following description :
    Private Members
    - MCode  //Data member for Code (an integer)
    - MName  //Data member for Name (a string)
    - MPop  //Data member for Population (a long int)
    - Area  //Data member for Area Coverage (a float)
    - PopDens  //Data member for Population Density ( a float)
    - CalDen() //A member function to calculate Density as PopDens/Area
    Public Members
    - Enter()   //A function to allow user to enter values of
                        //Mcode,MName,MPop,Area and call CalDen() function
    - ViewALL()   //A function to display all the data members
                              //also display a message “Highly Populated Area”
                              //if the Density is more than 12000

CBSE TEST PAPER-05
Class-12 Computer Science(Classes and Objects)

[Answers]


  1. By default the class members are private.
  2. class PAYITNOW
    {
    int Charge;    //class member cannot be initialized here
    PUBLIC:
    void Raise(){cin>>Charge;}
    void Show{cout<<Charge;}
    };
    void main()
    {   PAYITNOW P;
    P.Raise( );   //objectname.member function name
    P.Show( );   //objectname.member function name
    }
  3. A class provides three access specifiers, namely private, protected and public.
    Private members and protected members are hidden from the outside world and they can only be accessible from the member functions of the class. Public members can be accessed from outside world.
  4. A class combines data and functions together thereby enforcing Data encapsulation. The private and protected members are hidden from outside world and outside world is given only essential information by public members, thereby enforcing Data abstraction.
  5. A class can have static data members and static member functions. A static data member is shared by all objects of the class. They are visible only within the class but their lifetime is the entire program. All static data members are initialized to zero when the first object is created, if no other initialization is present. We can't put it in the class definition but it can be initialized outside the class by redeclaring the static variable, using the scope resolution operator: to identify which class it belongs to.
    A member function that accesses only static data members of a class is a static member function. It cannot access other data members except static data members.
  6. A function is made inline under the following situations:
    a. The size of the function is small.
    b. The function is not returning any value and contains no return statements.
    c. The function does not contains any static variables.
    d. The function does not contain a looping construct, a switch statement or a goto statement.
    e. The function is not recursive (when a function calls itself)
    f. The purpose of using inline functions is to reduce function calling overheads which can lead to better efficiency of a program by saving time and resources.
  7. When a class is defined, memory is allocated for its member functions and they are stored in the allocated memory space. When an object is created, separate memory space is allocated for its data members. All object work with the same copy of member functions shared by all.
  8. Date:1
    1002#100
    Date:50
    1002#300
    Date:20
    2005#800
    Date:10
    100#220
  9. class PIC
    {
    int Pno;
    char Category[20];
    char Location[20];
    void FixLocation();
    public:
    void Enter();
    void SeeAll();

    void PIC::FixLocation()
    {
    if(strcmpi(Category,”Classic”)==0)
    strcpy(Location,”Amina”);
    else if(strcmpi(Category,”Modern”)==0)
    strcpy(Location,” Jim Plaq” );
    else if strcmpi(Category,”Antique”)==0)
    strcpy(Location,” Ustad Khan” );
    }
    void PIC::Enter()
    {
    cin>>Pno;
    gets(Category);
    FixLocation();
    }
    void PIC:: SeeAll()
    {
    cout<<Pno<<Category<<Location<<endl;
    }
  10. class METROPOLIS
    {
    int Mcode;
    char MName[20];
    long int MPop;
    float Area;
    float PopDens;
    void CalDen();
    public:
    void Enter();
    void ViewALL();

    void METROPOLIS::Enter()
    {
    cin>>Mcode;
    gets(MName); // OR cin>>MName;
    cin>>MPop;
    cin>>Area;
    CalDen();
    }
    void METROPOLIS::ViewALL()
    {
    cout<<Mcode<<”,” <<MName<<”,”<<MPop<<”,”<<Area<<”,”<<PopDens<<”,” ;
    if(PopDens>12000)
    cout<<”Highly Populated Area”;
    }
    void METROPOLIS::CalDen()
    {
    PopDens= PopDens /Area;  // OR PopDens = MPop/Area
    }