(Computer Science) CBSE Class XII Important Questions Computer Science (2008)

Disclaimer: This website is NOT associated with CBSE, for official website of CBSE visit - www.cbse.gov.in

Computer Science : CBSE Class XII Important Questions Computer Science (2008)


Chapter 6 – Inheritance

Q. 21. What is Containership? How does it differ from Inheritance?

Ans: When a class contains objects of another class as its members, this kind of relationship is called containership or nesting. Inheritance lets you create or define a specialized object of a class that shares the properties of the class and at the same time adds new features to it. But containership is just a way to define an object which itself is collection of objects of other classes.

 

Q .22. Given the following class definitions answer the questions that follow:

class book
{
            char title[20];
            char author[20];
            int no_of_pages;
public:
            void read( );
            void show( );
};
class textbook:private book
{
            int no_of_chapters, no_of_assignments;
protected:
            int standard;
public:
            void readtextbook( );
            void showtextbook( );
};
class physicsbook:public textbook
{
            char topic[20];
public:
            void readphysicsbook( );
            void showphysicsbook( );
};

  1. Name the members, which can be accessed from the member functions of class physicsbook.

  2. Name the members, which can be accessed by an object of class textbook.

  3. Name the members, which can be accessed by an object of class physicsbook.

  4. What will be the size of an object (in bytes) of class physicsbook.

Ans:

  1. Data Members: standard, topic
    Member Functions: readtextbook( ), showtextbook( ), readphysicsbook( ); showphysicsbook( ).

  2. Member Functions: readtextbook( ), showtextbook( ).

  3. Member Functions: readtextbook( ), showtextbook( ), readphysicsbook( ), showphysicsbook( ).

  4. 68 bytes.

     

 

Chapter 7 – Data File Handling

Q. 23. Write a user defined function in C++ to read the content from a text file STORY.TXT, count and display the number of alphabets present in it.

Ans:
            #include<fstream.h>
            void main( )
            {
                        ifstream fin(“STORY.TXT”);
                        char ch;
                        int count = 0;
                        if(!fin)
                        {
                                    cout<<”\n File does not exist”;
                                    return;
                        }
                        while(1)
                        {
                                    fin.get(ch);
                                    if(ch = = EOF)
                                                break;
                                    if((ch>=’A’ && ch<=’Z’)||(ch>=’a’&& ch<=’z’))
                                                count++;
                        }
                        cout<<”\nNumber of alphabets are:”<<count;
                        fin.close( );
            }

 

Q. 24. Name two member functions common to the classes ifstream and ofstream.

Ans: open ( ), close ( ).

 

Q. 25. Assuming the class Computer as follows:

class computer
{
            char chiptype[10];
            int speed;
public:
            void getdata( )
            {
                        gets(chiptype);
                        cin>>speed;
            }
            void showdata( )
            {
                        cout<<”Chip”<<chiptype<<”Speed=”<<speed;
            }
};

Write a function readfile( ) to read all the records present in an already existing binary file SHIP.DAT and display them on the screen, also count the number of records present in the file.

Ans:
            void readfile( )
            {
                        ifstream fin;
                        fin.open(“SHIP.DAT”,ios::in|ios::binary);
                        computer c;
                        while(!fin.eof( ))
                        {
                                    fin.read((char*)&c, sizeof(c));
                                    count++;
                                    c.showdata( );
                        }
                        cout<<”Total Number of Records are”<<count;
            }

 

Q. 26. Differentiate between write and put functions of ostream class.

Ans: The prototypes of put ( ) and write ( ) are:
            ostream & put (char ch);
            ostream & write ((char *) & buf, int sizeof(buf));

The put ( ) writes the value of ch(one character) to the stream and returns a reference to the stream. The write ( ) function writes sizeof (buf) bytes to the associated stream from the buffer pointed to by buf. The data written to a file using write( ) can only be read accurately using read( ).

 

Q. 27. Write a program that displays the size of a file in bytes:

Ans:
            #include<fstream.h>
            #include<process.h>
            #include<conio.h>
           
          void main( )
            {
                        clrscr( );
                        char filename[13];
                        cout<<”Enter filename:\n”;
                        cin.getline(filename, 13);
                        ifstream infile(filename);
                        if(!infile)
                        {
                                    cout<<”Cannot open “<<filename<<” file”;
                                    exit(-1);
                        }
                        int no_bytes = 0;
                        char ch;
                        while(cin.get(ch))
                                    no_bytes++;
                        cout<<”File size is”< no_bytes<<” bytes”;
                        getch( );
}

 

Chapter 8 – Pointers

Q. 28. Distinguish between:

int * ptr = new int(5);

int  *ptr = new int[5];

Ans:

The first statement allocates memory of one integer to ptr and initializes it with value 5. The second statement allocates memory of 5 contiguous integers (i.e. an array of 5 integers) and stores beginning address in pointer ptr.

 

Q. 29. What will be the output of the following code fragment:

#include<iostream.h>
#include<conio.h>
void main( )
{
            clrscr( );
            int a[ ] = {3, 5, 6, 7};
            int *p, **q, ***r, *s, *t, **ss;
            p = a;
            s = p + 1;
            q = &s;
            t = (*q + 1);
            ss = &t;
            r = &ss;
            cout<<*p<<’\t’<<**q<<’\t’<<***r<<endl;
}

Ans: 3, 5, 6.

 

Q. 30. Identify syntax errors, if any, in the following program. Also give reason for errors.

void main( )
{
            const int i = 20;
            const int * const ptr = &i;
            *ptr ++;
            int j = 15;
            ptr = &j;
}

Ans:

Erroneous statements

Errors and corrections

*ptr ++

Cannot modify const object

ptr = &j

Cannot modify const object
j = *ptr;

 

Chapter 9 –Array

Q. 31. How is computer memory allotted for a 2D array?

Ans: For two-dimensional array, the computer memory is allocated either in row-major form or in column-major form.

Row Major form stores the 2-D array row wise i.e., firstly the first row is stored, then the second row, then third row, and so forth.

Column Major form stores the 2-D array column wise i.e. firstly the first column, then the second column, then third and so forth. The default form is Row-Major.

 

Q. 32. Binary search is to be used on the following sorted array to search for 30 and 60.

Array

Index

1

2

3

4

5

6

7

8

9

10

Value:

11

22

30

33

40

44

55

60

66

70

Give the index of the element that would be compared with at every step. Repeat the process replacing 30 by 60.

Ans:

For 30
Step                             Index                           Result
1.                                 4                                  Unsuccessful
2.                                 1                                  Unsuccessful
3.                                 2                                  Successful

For 60
Step                             Index                           Result
1.                                 4                                  Unsuccessful
2.                                 7                                  Successful.

 

Q. 33. Consider the single dimensional array AAA [45] having base address 300 and 4 bytes is the size of each element of the array. Find the address of AAA [10], AAA [25] and AAA [40].

Ans:

lb=0, b=300, s=4 bytes.
AAA [I] = b + (l – lb)*s
AAA [10] = 300 + (10-0)*4 = 340
AAA [25] = 300 + (25-0)*4 = 400
AAA [40] = 300 + (40-0)*4 = 460.

The addresses of AAA [10], AAA [25], AAA [40] are 340, 400 and 460 respectively.

 

Q .34. Given two dimensional array A[10][20], base address of A being 100 and width of each element is 4 bytes, find the location of A[8][15] when the array is stored as a) column wise b) Row wise.

Ans:

1. Column wise

            Address[i][j] = B + w[n(J-0)+(I-0)]
            Address A[8][15] = 100 + 4[10(15-0) + (8-0)] = 100 + 4[150 + 8] = 100 + 632 = 732.

2. Row wise

Address[i,j] =   B + w[n(I-0) + (J-0)]
Address A[8][15]=100 + 4[20(8 – 0) + (15 – 0)]= 100 + 4[160 + 15]= 100 + 700 = 800

 

Q. 35. Write a C++ function to find and display the sum of each row and each column of a 2 dimensional array of type float. Use the array and its size as parameters with float as the return type.

Ans:
            void rowcolsum(float a[10][10], int r, int c)
                        {
                                    int i,j;
                                    float rs[10], cs[10];
                                    for(i=0;i<r;i++)
                                    {
                                                rs[i] = 0;
                                                for(j=0;j<c;j++)
                                                            rs[i] = rs[i] + a[i][j];
                                    }

for(j=0;j<c;j++)
                                    {
                                                cs[j] = 0;
                                                for(i=0;i<r;i++)
                                                            cs[j] = cs[j] + a[i][j];
                                    }
                        //Display 2 D array with row sum and column sum
                                    for(i=0;i<r;i++)
                                    {
                                                for(j=0;j<c;j++)
                                                            cout<<a[i][j]<<” “;
                                                cout<<rs[i]<<endl;
                                    }
                        for(j=0;j<c;j++)
                                    cout<<cs[j]<<” “;
                        cout<<endl;
}

 

 

  Previous | Next