Wednesday, December 28, 2011

CBSE 2011 Computer Science Paper Solution

Q1a) What is the difference between Local Variable and Global Variable ? Also, give suitable C++ code to illustrate both.

Ans 1a)

Local Variable
Global Variable
Local variables scope exists only for the block in which it is defined.
Global variables scope is defined for the entire program
Created within the block for use
Created outside the block
#include <iostream>
void main()
{
   int aPiLocal = 3.14;
   cout  >> “Above variable is Local”
   cout >> aPiLocal;
}
#include <iostream>
int aPiGlobal = 3.14;
void main()
{
   cout  >> “Above variable is Global”
   cout >> aPiGlobal;
}


Q1b) Write the names of the header files, which is/are essentially required to run/execute the following C++ code:

void main ()
{
char C, String [] = "Excellence Overload";
for (int I=0; String[I] != '\0';I++)
            {
                        if (String [I] == ' ')
                        {
                        cout<<endl;
                        }
                        else
                                    {
                                    C=toupper(String [I]);
                                    cout<<C;
                                    }
            }
}

Ans 1b)
Minimum number of files required to run the above code are
#include <iostream> : Used for I/O funtions cout and endl
#include <string> : Used for string functions like toupper

Q1c) Rewrite the following program after removing the syntactical errors (if any).
Underline each correction.

#include [iostream.h]
typedef char Text (80);
void main ()
{
Text T=“Indian”;
int Count=strlen(T);
cout<<T<<’has<<Count<<’characters<<endl;
}

Ans 1c)
Errors are highlighted in yellow.
//#include [iostream.h]
#include <iostream.h>
#include <string>
//typedef char Text(80); //String definition incorrect
typedef char Text[80];
void main ()
{
Text T="Indian";
int Count=strlen(T); //Included string header class
//cout<<T<<'has'<<Count<<'characters'<<endl; // has and chatacters are strings not char
cout<<T<<"has"<<Count<<"characters"<<endl;
}


Q 1d) Find the output of the following program:
# include <iostream.h>
void ChangeArray (int Number, int ARR[], int Size)
{
for (int L=0; L<Size; L++)
If (L<Number)
            ARR[L] += L;
else
            ARR[L] *= L;
}
void Show(int ARR [], int Size)
{
            for (int L=0; L<size; L++)
                        (L%2!=0) ? cout<<ARR[L] <<“#” : cout<<ARR[L] <<endl;
}
void main ( )
{
            int Array [] = {30,20,40,10,60,50};
            ChangeArray (3, Array, 6);
            Show (Array,6);
}

Ans 1d)
Output is a follows

30
21#42
30#240
250#

Explanation
void ChangeArray (int Number, int ARR[], int Size)
{
for (int L=0; L<Size; L++)
// Loop breaks when L=6
If (L<Number)
            ARR[L] += L;
// L < Number
// When L = 0  Number = 3 Arr[0] = 30 Arr[0] += L is 30 + 0 = 30
// When L = 1  Size = 3 Arr[1] = 20 Arr[0] += L is 20 + 1 = 21
// When L = 2  Size = 3 Arr[2] = 40 Arr[0] += L is 40 + 2 = 42
else
            ARR[L] *= L;
// L >= Number
// When L = 3 Number = 3 Arr[3] = 30 Arr[3] *= L is 10 * 3 = 30
// When L = 4  Number = 3 Arr[4] = 20 Arr[4] *= L is 60 * 4 = 240
// When L = 5  Number = 3 Arr[5] = 40 Arr[5] *= L is 50 * 5 = 250
}
void Show(int ARR [], int Size)
{
            for (int L=0; L<size; L++)
// Loop breaks when L=6
                        (L%2!=0) ? cout<<ARR[L] <<“#” : cout<<ARR[L] <<endl;
            // When L = 0 Condition L%2 != 0 is FALSE Hence output
            // 30 <endl : i.e. change of line is printed >
            // When L = 1 Condition L%2 != 0 is TRUE Hence output
            // 21#
            // When L = 2 Condition L%2 != 0 is FALSE Hence output
            // 21#42<endl>
            // When L = 3 Condition L%2 != 0 is TRUE Hence output
            // 30#
            // When L = 4 Condition L%2 != 0 is FALSE Hence output
            // 30#240<endl>
            // When L = 5 Condition L%2 != 0 is TRUE Hence output
            // 250#
            // When L = 6 Loop breaks
}
void main ( )
{
            int Array [] = {30,20,40,10,60,50};
            ChangeArray (3, Array, 6); //  Will call the function ChangeArray
            // Array itself is changed since Arrays are passed by reference and not by value
            Show (Array,6);
}

Q1e) Find the output of the following program:

# include <iostream.h>
void main ( )
{
            int Track [] = {10,20,30,40,}, *Striker ;
            Striker=Track ;
            Track [1] += 30 ;
            cout<<"Striker>"<<*Striker<<endl ;
            * Striker -=10 ;
            Striker++;
            cout<<"Next @"<<*Striker <<endl ;
            Striker+=2 ;
            cout<<"last @"<<*Striker<<endl ;
            cout<<"Reset To"<<Track [0] <<endl ;
}

Ans 1e)
Output is as follows
Striker>10
Next @50
last @40
Reset To0

Explanation : This program uses the concepts of Arrays and pointers. Please refresh the same for ease of use.
# include <iostream.h>
void main ( )
{
            int Track [] = {10,20,30,40,}, *Striker ;
            Striker=Track ;
            //Striker and Track point to same location now i.e. Track[0] or *Stiker
            Track [1] += 30 ;
            // Track [1] = 20 Track [1] += 30 hence Track[1] = 20+30 = 50
            cout<<"Striker>"<<*Striker<<endl ;
            // This will ouput Track[0] i.e. value at location pointed by Striker.
            // Striker>10
*Striker -=10 ;
//Value at Stiker reduced by 10 i.e. 10-10 = 0 = Track[0]
            Striker++;
            //Striker now starts pointing to Track[1]
            cout<<"Next @"<<*Striker <<endl ;
            // Output is the value stored at Striker i.e Track[1] which was earlier modified to 50
            // Next @50
            Striker+=2 ;
            //Striker now starts pointing to Track[3] since it moves two integer locations.
            cout<<"last @"<<*Striker<<endl ;
            // Output is the value stored at Striker i.e Track[3]
            // last @40
            cout<<"Reset To"<<Track [0] <<endl ;
            //Output Track[0] which due to operation *Striker -=10 is already set as 0, hence
            // Reset To0
            // however this does not mean that Striker has been reset to location Track Striker is
            // still pointing to Track[3]          
}


Q 1f) Go through the C++ code shown below, and find out the possible output or
output from the suggested output options (i) to(iv). Also, write the least
value and highest value, which can be assigned to the variable guess.

# include <iostream.h>
#include <stdlib.h>
void main ( )
{
randomize ();
int Guess, High=4 ;
Guess=random (High) +50;
for (int C=Guess ; C<=55 ; C++)
cout<<C<<“#”;
}
(i) 50 # 51 # 52 # 53 # 54 # 55 #
(ii) 52 # 53 # 54 # 55 #
(iii) 53 # 54 #
(iv) 51 # 52 # 53 # 54 # 55

Ans 1f)
           
Answers (i) and (ii) are correct

Explanation
Line Guess=random (High) +50; means that Guess can take any integer value between 50 and 53 both inclusive.
Hence the for loop can start from 50 or 51 or 52 or 53 but when it starts it will continue all the way to 55 and will not break in between. This happens in two cases
(i)                 and             (ii)


Q 2a) Differentiate between members, which are present within the private visibility
mode with those which are present within the public visibility modes.

Ans 2a)
Visibility modes are used to define the access specifier for inheritable members of base class in the derived class.
Visibility modes can be public, private or protected.

Public visibility mode: The public derivation means that the derived class can access the public and protected members of the base class but not the private members of the base class. With publicly derived class, the public members of the base class become the public members of the derived class, and the protected members of the base class become the protected members of the derived class.

Private visibility mode: The private derivation means, the derived class can access the public and private members of the base class privately. With privately derived class, the public and protected members of the base class become private members of the derived class. That means the inherited members can be accessed only through member functions of the derived class.


Base class
Derived class
Access Specifier
public
private
protected
public
public
 private
protected
private
Not
inherited
Not
inherited
Not
inherited
protected
protected
protected
protected


Q 2b) Write the output of the following C++ code. Also, write the name of feature of Object Oriented Programming used in the following program jointly illustrated by the functions [I] to [IV].

#include<iostream.h>
void Print ( ) // Function [I]
{
for (int K=1;K<=60; K++) cout<< "-";
cout<<endl;
}
void Print (int N) //Function[II]
{
for (int K=1;K<=N; K++) cout<<"*";
cout<<endl;
}
void Print(int A, int B) //Function[III]
{
for(int K=1;K<=B;K++) cout<<A*K;
cout<<endl;
}
void Print(char T, int N) // Function[IV]
{
for (int K=1;K<=N;K++) cout<<T;
cout<<endl;
}
void main( )
{
int U=9,V=4,W=3;
char C ='@';
Print(C,V);
Print(U,W);
}

Ans 2b)
 Output for the program is

@@@@
91827

Feature of Object Oriented programming used is function overloading.

Explanation

#include<iostream.h>
void Print ( ) // Function [I]
{
for (int K=1;K<=60; K++) cout<< "-";
cout<<endl;
}
void Print (int N) //Function[II]
{
for (int K=1;K<=N; K++) cout<<"*";
cout<<endl;
}
void Print(int A, int B) //Function[III]
{
for(int K=1;K<=B;K++) cout<<A*K;
// when K = 1 B= 3 A = 9 K < = B print 9*1 = 9
// when K = 2 B= 3 A = 9 K < = B print 9*2 = 18
// when K = 3 B= 3 A = 9 K < = B print 9*3 = 27
// when K = 4 B= 3 A = 9 K > B loop breaks
//Note 91827 are printed in one line
cout<<endl;
//Will change line
}
void Print(char T, int N) // Function[IV]
{
for (int K=1;K<=N;K++) cout<<T;
//Will Print '@' 4 times on the screen
cout<<endl;
//Will change line
}
void main( )
{
int U=9,V=4,W=3;
char C ='@';
Print(C,V); // Funtion [IV] will be called with values '@', 4. Since [IV] matches the
                   // function calling char, int
Print(U,W);// Funtion [IV] will be called with values '@', 4. Since [III] matches the
                   // function calling int, int
}

Q 2c) Define a class candidate in C++ with following Description:
Private Members 4
·         A data member RNo (Registration Number) of type long
·         A data member Name of type string
·         A data member Score of type float
·         A data member Remark of type string
·         A member function AssignRem( ) to assign Remarks as per the Score obtained by a candidate. Score range and the respective Remarks are shown as follows:
Score Remarks
>=50 Selected
less than 50 Not selected

Ans 2c)

#include <iostream>
#include <string>
using namespace std;
class Student
{
private:
long Rno;
string Name;
float Score;
string Remark;

public:
void AssignRem()
            {
                        if (Score >= 50.0)
                                    Remark = "Selected";
                        else if (Score < 50.0)
                                    Remark = "Not Selected";
            }
};

Q 2d) Answer the question (i) to (iv) based on the following: 4
class Student
{
int Rno;
char Name[20];
float Marks;
protected:
void result( );
public:
Student ( );
void Register ( ); void Display( );
};
class Faculty
{
long FCode;
char FName [20];
protected:
float Pay;
public:
Faculty ( );
void Enter( );
void Show( );
};
class Course: public Student, private Faculty
{
long CCode [10]; char CourseName [50];
char StartDate [8], EndDate [8];
public:
Course( );
void Commence ( );
void CDetail ( );
};
(i)                  Which type of inheritance is illustrated in the above C++ code?
(ii)                Write the names of all the data members, which is /are accessible from member function Commence of class Course.
(iii)              Write the names of member functions, which are accessible from objects of class Course.
(iv)               Write the names of all the members, which are accessible from objects of class faculty.

Ans 2c)
(i)                  Multiple inheritance —> A derived class with several base classes is called multiple inheritance. Class Course is inherited from two different base classes Student and Faculty.
(ii)                Data members  accessible to function Commence of class Course are CCode, CourseName, StartDate , EndDate 
(iii)              Member Functions accessible to objects of class Course are Commence(),CDetail ( ), Register ( ), Display( ),
(iv)               Members accessible from objects of class Faculty are Enter( ), Show( )


Q3 a) Write a Get1From2( ) function in C++ to transfer the content from two arrays FIRST[ ] and SECOND[ ] to array ALL[ ]. The even places (0, 2, 4,…) of array ALL[] should get the content from the array FIRST[ ] and odd places (1,3,5,…) of the array ALL [ ]should get the content from the array SECOND[ ] .
Example:
If the FIRST[ ] array contains
30, 60, 90
And the SECOND[ ] array contains
10, 50, 80
The ALL[ ] array should contain
30, 10, 60, 50, 90, 80

Ans 3a)
#include <iostream.h>
// Function takes in 3 Arrays First Second and All along with size of first and second array
// Sizeof function is used to get the length of the Arrays first and Second
// e.g. First[] = { 30, 60, 90} sizeof[First]/sizeof(int) = 3
// e.g. Second[] = { 30, 60, 90} sizeof[Second]/sizeof(int) = 3
// So loop needs to run from 0 to 5
void Get1From2(int First[], int Second[], int All[], int firstLength, int secondLength )
{
            for (int i=0, j=0, k=0; i < (firstLength+secondLength); i++)
            {
                        //Check if even or odd number
                        if (i%2==0)
                        {
                                    //Check if items are remaining in the First Array
                                    if (j < firstLength)
                                    {
                                                // Copy First Array item to All                                         
                                                All[i] = First[j];
                                                j++;
                                    }
                                    else
                                    {
                                    // Continue copying from Second Array
                                                All[i] = Second[k];
                                                k++;
                                    }
                        }
                        else
                        {
                                    //Check if items are remaining in the Second Array
                                    if (k < secondLength)
                                    {
                                               
                                                // Copy Second Array item to All
                                                All[i] = Second[k];
                                                k++;
                                    }
                                    else
                                    {
                                    // Continue copying from First Array                                            
                                                All[i] = First[j];
                                                j++;
                                    }
                        }
                        // Print combined array
                        cout << All[i]<<' ';
            }

}
void main()
{
            int First [] = {30, 60,90,120};
            int Second[]=     {10,50};

            int All[10];
//Sizeof function is used to get the length of the Arrays first and Second
// e.g. First[] = { 30, 60, 90} sizeof[First]/sizeof(int) = 3
// e.g. Second[] = { 30, 60, 90} sizeof[Second]/sizeof(int) = 3
// So loop needs to run from 0 to 5
            Get1From2(First, Second, All, (sizeof First/sizeof(int)), (sizeof Second/sizeof(int)));
}

Q 3b) An array P[20] [50] is stored in the memory along the column with each of its
Element occupying 4 bytes, find out the location of P[15] [10], if P[0][0] is stored at 5200.

Ans 3b) 8236
Explanation.


5200
5201
5202
5203



5236
5237
5238
5239



5396
5397
5398
5399
p[0][0]
.
.
.
p[0][10]
.
.
.
p[0][50]
5400






5436









5599
p[1][0]
.
.
.
p[1][10]
.
.
.

5600

















p[2][0]
.
.
.

.
.
.








.

















.

















.










8200






8236










p[15][0]
.
.
.
p[15][10]
.
.
.



5. (a) What do you understand by Union & Cartesian Product operations in
relational algebra?

Ans 5a)
Consider two relations R and S.
UNION of R and S
The union of two relations is a relation that includes all the tuples that are either in R or in S or in both R and S. Duplicate tuples are eliminated.

Cartesian Product of R and S
The Cartesian Product is also an operator which works on two sets. It is sometimes called the CROSS PRODUCT or CROSS JOIN. It combines the tuples of one relation with all the tuples of the other relation.
Q5 b)  Write SQL commands for the following statements:

(i) To display the details of all WORKERs, descending order of DOB.
(ii) To display NAME and DESIG of those WORKERs whose PLEVEL is either P001 or P002.
(iii)To display the content of all the WORKERs table, whose DOB is in between ‘19-JAN-1984’ and ‘18-JAN-1987’.
(iv) To add a new row with the following : 19, ‘Days Kishore’, ‘Operator’, ‘P003’. ‘19-Jun-2008’, ‘11-Jul-1984’

Ans 5b)
(i)                  SELECT * FROM WORKER ORDER BY DOB DESC;
(ii)                SELECT NAME, DESIG FROM WORKER WHERE PLEVEL = ‘P001’ or PLEVEL = ‘P002’;
(iii)               SELECT * FROM WORKER WHERE DOB BETWEEN TO_DATE(‘19-JAN-1984’, ‘DD-MON-YYYY’) and TO_DATE(‘18-JAN-1987’, ‘DD-MON-YYYY’)
(iv)              INSERT INTO WORKER VALUES (19, ‘DAYS KISHORE’, ‘OPERATOR’, ‘P003’, ’19-JUN-2008’,’11-JUL-1984’)

Q5 C)
Give the output of the following SQL queries : 2
(i) SELECT COUNT (PLEVEL), PLEVEL FROM WORKER GROUP BY PLEVEL;
(ii) SELECT MAX(DOB), MIN(DOJ) FROM WORKER;
(iii) SELECT Name, Pay FROM WORKER W, PAYLEVEL P WHERE W.PLEVEL = S.PLEVEL AND P.ECODE<13;
(iv) SELECT PLEVEL, PAY+ALLOWANCE FROM PAYLEVEL WHERE PLEVEL = ‘P003’;

Ans 5C)
(i)

        1, P001
        2, P003
        2, P002
(ii) Remember max date is the earliest date and min date is the most recent date
            23-Aug-1981 22-Feb-2010
(iii)
            Radhe Shyam   26000
            Chander Nath   12000
(iv)
            P003    18000