Object Oriented Programming in CPP (OLD) - Test Papers

 CBSE TEST PAPER-01

Class - 12 Computer Science (C++ Revision Tour)


General Instruction: 

  • Question No. 1 to 4 carry One marks,
  • Question No. 5 to 8 carry Two marks.
  • Question No. 9 carry Four marks.
  • Question No. 10 carry Six marks.               

  1. Observe the following program very carefully and write the names of those header file(s), which are essentially needed to compile and execute the following program successfully :
    void main()
    {
    char s1[ ] = “Delhi” ;
    strrev(s1);
    cout<<s1;
    }
  2. Rewrite the following program after removing the syntactical errors (if any).Underline each correction.
    # include<iostream.h>
    #define area(r) =  l*b
    void main( )

    cin>>r;
    a = area(r);
    cout<<”Area=”<< a;
    }
  3. Find the correct identifiers out of the following, which can be used for naming Variable, Constants or Functions in a C++ program :
    Char, side*side,  _num , my file, 1stnum, case
  4. What is sizeof() operator?
  5. What is the difference between actual and formal parameter? Give example.
  6. What is the purpose of typedef in c++? Explain with an example.
  7. Study the following program and select the possible output(s) from the options (i) to (iv) following it. Also, write the maximum and the minimum values that can  be assigned to the variable start:
    Note :
    Assume all required header files are already being included in the program. random(n) function generates an integer between 0 and n-1.
    void main()
    {
    randomize(); 
    int guess[4]={200,150,20,250};
    int start =random(2)+2;
    for (int i=start; i<4; i++)
    cout<<guess[i]<<”#”;
    }
    i)   200#150#  ii)150#20#  iii) 150#20#250#  iv)20#250#    
  8. Find output of the following program segment:
    void main( )
    {
    char *poet= "SakESpHerE" ;
    for (int i=0;i<strlen(poet);i++)
    {
    if(islower(poet[i]))
    poet[i]=poet[i-1];
    else if( isupper(poet[i]))
    if(poet[i]= ='S')
    poet[i]='X'
    else if(poet[i]= ='E')
    poet[i]=toupper(poet[i-1]);
    else
    poet[i]--;
    }
    cout<<poet;  }
  9. Find output of the following program segment:
    int a = 5;
    void demo(int x, int y, int &z)
    {          a += x+y;
    z = a+y;
    y += x;
    cout<<x<<'*'<<y<<'*'<<z<<endl;
    }
    void main()
    {           int a = 3, b = 4;
    demo(::a,a,b);
    demo(::a,a,b);
    }
  10. a) What is the difference between call by value and call by reference? Illustrate with the help of an example.
    b) Differentiate between Local and Global variables. Illustrate with the help of an example.

CBSE TEST PAPER-01
Class - 12 Computer Science (C++ Revision Tour)

[Answers]


  1. string.h (for strrev()) , iostream.h (for cout)
  2. # include<iostream.h>
    #define area(r)  l*b    //only space is required not equal sign
    void main( )
    {
    float a,r;     // a & r were not declared
    cin>>r;
    a = area(r);
    cout<<”Area=”<< a;
    }
  3. Char,  _num
  4. The sizeof operator can be used to get the size of classes, structures, unions and any other user defined data type.
    Syntax: sizeof (data type)
  5. Actual parameter is a parameter, which is used in function call to send the value from calling function to the called function.
    Formal parameter is a parameter, which is used in function header of the called function to receive the value from actual parameter.
    Example:
    void sum(int a, int b)   // a, b are formal parameters
    {
    int c=a+b;
    cout<<”Sum=”<<c;
    }
    void main()
    {
    int x=5,y=6;
    sum(x,y);    //x , y are actual parameters
    }
  6. typedef is used to define a new data type name or an alias name for an existing data type. typedef double amount;
    Now we can define any amount using the data type amount as:
    amount deposited, loan, withdraw;         
  7. iv)20#250#      Max. value for start = 3   & Min. value = 2
  8. XXXXXXGGGG
  9. 5*8*16
    13*16*32
  10. a)

    Call by value

    Call by reference

    1. In call by value, a copy of actual arguments is passed to formal arguments of the called function and any change made to the formal arguments in the called function have no effect on the values of actual arguments in the calling function.

    1. In call by reference, the location (address) of actual arguments is passed to formal arguments of the called function. This means by accessing the addresses of actual arguments we can alter them within from the called function.

    2. In call by value, actual arguments will remain safe, they cannot be modified accidentally.

    2. In call by reference, alteration to actual arguments is possible within from called function; therefore the code must handle arguments carefully else you get unexpected results.

    3. Example.

    void swap(int a, int b)
    {
     int temp;
     temp=a;
     a=b;
     b=temp;
    }
    void main() 
     int a=100, b=200;  
     clrscr();  
     swap(a, b);// passing value to    
                   //function
     cout<<"Value of a"<<a;
     cout<<"Value of b"<<b;
     getch();  

    Output

    Value of a: 100
    Value of b: 200

    3. Example.

    void swap(int &a, int &b)
    {
     int temp;
     temp=a;
     a=b;
     b=temp;
    }
    void main() 
     int a=100, b=200;  
     clrscr();  
     swap(a, b);  // passing value to 
                   //function
     cout<<"Value of a"<<a;
     cout<<"Value of b"<<b;
     getch();  

    Output

    Value of a: 200
    Value of b: 100

    b) Local variables are those variables which are declared inside a function or block and those variables can only be used within that function or block.
    Global variables are those variables which are declared outside all the functions and can be accessed by all functions.
    Example :
    #include <iostream.h>
    int a=20;                // global variable
    void main()
    {        int b=10;     // local variable
    cout<<a<<” , “<<b; 
    }