Study material and hots



Download 2.32 Mb.
Page1/9
Date conversion04.09.2017
Size2.32 Mb.
  1   2   3   4   5   6   7   8   9
6


Kendriya Vidyalaya Sangathan
18, Institutional Area, Shaheed Jeet Singh Marg,
New Delhi - 110 602
.

STUDY MATERIAL AND HOTS


Class XII (Computer Science 2010-11)


STUDY MATERIAL FOR CLASS XII COMPUTER SCIENCE


Chief Patron Shri Avinash Dikshit

Commissioner

KVS, New Delhi
Patron Joint Commissioner (Admn)

KVS, New Delhi


Advisor Ms. Lakshmi Chari

Assistant Commissioner

KVS,Ahmedabad Region
Guidance Sh. M.L.Gedam

Education Officer

KVS, Ahmedabad Region

Sh.B. Rangasri

Education Officer

KVS, Ahmedabad Region
Co-ordinator Sh. Venugopal

Principal,

KV,Mehsana
Subject Coordinator Sh.Rahul Awasthi

PGT(Comp.Sc.),KV Mehsana



Sh. Rajesh Dwivedi

PGT(Comp.Sc.),KV Himmatnagar


Subject Contributors Sh.Naresh K Rana

PGT(Comp.Sc.), K.V.No.1, AFS, Bhuj



Sh. Yogesh Gupta

PGT(Comp.Sc.), K.V. Okha



Sh. Santosh K. Deval

PGT(Comp.Sc.), K.V.No.2, INF LINES, Jamnagar



Smt. Raksha Leuva

PGT(Comp.Sc.), K.V. Gandhinagar Cantt.



Sh. Om Prakesh Meena

PGT(Comp.Sc.), K.V. AFS Samana



Sh. S.S Mani

PGT(Comp.Sc.), K.V.No.2, ONGC, Surat

Sh. Abhivandan Mehta

PGT(Comp.Sc.), K.V.No. 3, AFII, Jamnagar

Salient features of this study material


  • This study material is in the form of question bank comprising of both solved and unsolved questions from each chapter of the syllabus.




  • It is a collection of a number of challenging questions based on HighOrder Thinking Skill of students. However, it shall prove to be a helping tool for all types of students.




  • It aims at providing help to very high scorer students who may miss 100 out of 100 because of not being exposed to new type of questions, being used to only conventional types of question, and not paying attention towards the topics which are given in the reference books and syllabus of Computer Science as per CBSE guidelines (Forexample Relational Algebra in database concepts and extraordinary questions based on pointers in C++ programming).




  • It contains guidelines, hints, and solutions for really challenging questions and topics.




  • It contains a number of fresh/new questions(solved & unsolved), which shall increase the confidence level of the students when they solve them as perCBSE Guidelines




  • Such questions shall draw the attention of both the students and the teachers, and the preparation to achieve 100% shall be boosted for all categories.



ALL THE BEST TO OUR DEAR STUDENTS……

CBSE Mark Distribution for different Topics(Important Lessons)



SlNo

Unit Name

Marks

1


UNIT 1 Programming in C++

30

2

UNIT 2 Data structures

14

3

UNIT 3 Database and SQL

08

4

UNIT 4 Boolean Logic

08

5

UNIT 5 Communication and open source concept

10

Total Marks

70




STUDY MATERIAL
INDEX



S.No.


TYPES OF QUESTIONS


PAGE NO.

1

STUDY MATERIAL’S THEORETICAL PART (Key Points)

6

2

SOLVED QUESTIONS

111

3

UNSOLVED QUESTIONS


191

4

PRACTICE QUESTION PAPER SETS – I

233

5

PRACTICE QUESTION PAPER SETS – II

240

6

PRACTICE QUESTION PAPER SETS – III

248

7

SOLUTION OF PRACTICE QUESTION PAPER SETS – III

259

8

TIPS FOR THE EXAM

276


STUDY MATERIAL’S THEORETICAL PART



SLNO.



TYPES OF QQUESTIONS



PAGE NO.


1

UNIT 1 : Programming in C++

6

2

UNIT 2 : Data Structures

52

3

UNIT 3 : Database and SQL


73

4

UNIT 4 : Boolean Logic

81

5

UNIT 5 : Communication and Open Source Concept

90

UNIT 1 : PROGRAMMING IN C++
KEY POINTS:

Introduction to C++

  • C++ is the successor of C language & developed by Bjarne Stroustrup at Bell Laboratories, New Jersey in 1979.

Tokens- smallest individual unit. Following are the tokens

  • Keyword-Reserve word that can’t be used as identifier

  • Identifies-Names given to any variable, function, class, union etc.

  • Literals-Value of specific data type

  • Variable- memory block of certain size where value can be stored and changed.

  • Constant- memory block where value can be stored once but can’t changed later on

  • Operator – performs some action on data

    • Arithmetic(+,-,*,/,%)

    • Relational/comparison (<,>,<=,>=,==,!=).

    • Logical(AND(&&),OR(||),NOT(!).

    • Conditional (? :)

  • Precedence of operators:

++(post increment),--(post decrement)

Highest

Low


++(pre increment),--(pre decrement),sizeof !(not),-(unary),+unary plus)

*(multiply), / (divide), %(modulus)

+(add),-(subtract)

<(less than),<=(less than or equal),>(greater than), >=(greater than or equal to)

==(equal),!=(not equal)

&& (logical AND)

||(logical OR)

?:(conditional expression)

=(simple assignment) and other assignment operators(arithmetic assignment operator)

, Comma operator


Data type- A specifier to create memory block of some specific size and type

cout - Used to display value on monitor.

cin - To prompt value for specific variable.

comment-Used for better understanding of program statements and escaped by the compiler to compile . e.g. – single line (//) and multi line(/*….*/)

Control structure :

Sequence control statement(if)

conditional statement

(if else)

case control statement (switch case)

loop control statement

(while ,do… while, for)

Syntax

Syntax

Syntax

Syntax

if(condition)

{

statements;



}


If(condition)

{

statements;



}

else


{

statements;

}


switch(expression)

{

case (expression1):



[statements

break;]


case (expression2):

[statements,

break;]

default:


Statements;

}


while(condition)

{

statements;



}

do ….while loop

do

{



statement;

} while(condition);



For loop

for(initialization;condition;incr/decr)

{

statement;



}


Nested loop -loop within loop.

exit()-to leave from the program.

break- exit from the current loop.

continue-to transfer control to loop control statement.

goto-program to jump a different location.

Character I/O Function:

  • get()-input single character from standard input device.

  • put()-output single character from standard output device

  • getch()-read from keyboard

  • putch()-write to screen.

  • getchar()-return single character from a standard input device.

  • putchar()-transmit single character to standard output device.

  • gets()-used to read a string of character from the standard input file(stdin).

  • puts()-used to copy a null-terminated string to standard output file (stdiout).

Standard C++ libraries

Header


Nome Purpose

stdio.h

File input and output

ctype.h

Character tests

string.h

String operations

math.h

Mathematical functions such as sin() and cos()

stdlib.h

Utility functions such as malloc() and rand()

Some More Useful header file.

fstream.h graphics.h conio.h ctype.h iomanip.h iostream.h

math.h stdlib.h stdio.h stream.h string.h time.h
Header files (Some important library functions):

stdio.h : fclose fcloseall feof fflush fgetchar fgets fileno fopen fprintf fputchar fputs fread fseek fwrite getc getchar gets
string.h : setmem stpcpy strcat strchr strcmp strcmpi strcpy strcspn stricmp strlen strlwr strncat strncmp strncmpi strncpy strnicmp strnset strrev strset strstr strupr
math.h : abs cos exp fabs floor fmod fmodl abs pow powl() sin sqrt tan
stdlib.h : abort abs atexit atof atoi atol calloc

div exit free rand random randomize realloc


conio.h : cgets clrscr cprintf cputs cscanf getch getche gettext putch

puttext textbackground textcolor



Some functions

  • isalpha(c)-check whether the argument is alphabetic or not.

  • islower(c)- check whether the argument is lowecase or not.
  • isupper(c) - check whether the argument is upercase or not.


  • isdigit(c)- check whether the argument is digit or not.

  • isalnum(c)- check whether the argument is alphanumeric or not.

  • tolower()-converts argument in lowercase if its argument is a letter.

  • toupper(c)- converts argument in uppercase if its argument is a letter.

  • strcat()- concatenates two string.

  • strcmp-compare two string.

  • pow(x,y)-return x raised to power y.

  • sqrt(x)-return square root of x.

  • random-return a random number between 0 and (num-1)

  • randomize-intilizesthe random number generator with a random value.



Array- Collection of element of same type that are referred by a common name.

One Dimension array

  • An array is a continuous memory location holding similar type of data in single row or single column

Two dimensional array

  • A two diamensional array is a continuous memory location holding similar type of data in of both row sand columns (like a matrix structure).

Function-Self-contained block of code that does some specific task and return a value.

Function prototypes-Function declaration that specifies the return type and data type and no of arguments

syntax: return_type function_name(argument_type(s) argument_name(s));

Passing value to function-

  • Passing by value

  • Padding by address/reference

Function overloading

  • Processing of using the same name for two or more function.

Function recursion

  • Function that call itself either directly or indirectly.

Local variables


  • Declared inside the function.

Global variables

  • Declared before main function ().

Actual variables

  • Variables associated with function name during function call.

Formal variables

  • Variables which accept the actual variable inside the function.

Structure-Collection of logically related of different datatypes referenced under one name.

Nested structure

  • A Structure definition within another structure.

typedef

  • Used to define new data type name

#define Directives

  • Use to define a constant number or function or to replace an instruction.


1 Marks questions




  1. Name the header files that shall be needed for the following code:

void main( )

{

char String[ ] = “String”;



cout << setw(2)<

}


  1. Which C++ header file(s) will be essentially required to be include to run/execute the following C++ code: [CBSE-2010]

void main()

{

int Rno=24; char name[ ]=”Alma Mater”;



cout<

}


  1. Name the header files that shall be needed for the following code:

void main( )

{

char word[]=”Board Exam”;



cout<

}


  1. Name the header file(s) that shall be needed for successful compilation of the following C++ code.

void main( )

{

char String[20];


gets(String);

strcat(String,”CBSE”);

puts(String);

}


  1. Name the header file(s) that shall be needed for successful compilation of the following C++ code.

void main( )

{

char Text[40];



strcpy(Text,”AISSCE”);

puts(Text); }



  1. Name the header file to which the following below:

    1. abs( ) (ii) isupper( )

  2. Name the header file to which the following belong:

    1. pow ( ) (ii)random( )

  3. Name the header files to which the following belong:

    1. abs( ) (ii) strcmp( )

  4. Name the header files to which the following belong: [AI 2005]

    1. puts( ) (ii) isalnum( )

  5. Write the names of the header files to which the following belong:

    1. gets( ) (ii) strcmp( ) (iii)abs( ) (iv)isalnum( )

  6. Name the header file, to which the following built-in function belongs:

    1. strcmp( ) (ii)getc( )

  7. Name the header files of C++ to which the following functions belong:

(i)get( ) (ii)open( ) (iii)abs( ) (iv)strcat( )

  1. Name the header file to be included for the use of the following built in functions: (i)getc( ) (ii)strcat()

  2. Name the header file, to which following built in function belong:

    1. isupper( ) ( ii)setw() (iii)exp( ) (iv)strcmp( )

  3. Why main( ) function is so special. Give two reasons?
  4. Name the header file of C++ to which following functions belong.


(i)strcat( ) (ii) scanf( ) (iii) getchar( ) (iv)clrscr( )

  1. Name the header files, to which the following built in functions belongs to:

(i)cos( )(ii)setw( )(iii)toupper( )(iv)strcpy( )

  1. Name the header files, to which the following built in functions belongs to:

(i)cos( )(ii)setw( )(iii)toupper( )(iv)strcpy( )

  1. Name the header file to, which following built-in functions belong:

    1. strcpy() (ii) isdigit() (iii) log() (iv) puts()

  2. Name the header file to be included for the use of following built-in functions:

    1. frexp() (ii) toupper() [CBSE Sample Paper-2]

  3. Name the header flies of C++ to which the following functions belong:

    1. write() (ii)
      arc() (iii) open() (iv) strlen()

  4. Name the header files of C++ to which the following functions belong: [AI2002]

    1. get() (ii) open() (iii) abs() (iv) strcat()

  5. Name the header files of C++ to which the following functions belong: [Comptt. 2002]

    1. read() (ii) open() (iii) get() (iv) strcmp()

  6. Name the header fLle,to which the following built-in functions belong:

    1. strcpy 0 (ii) gets()

  7. Name the header file, to which the following built-in functions belong: [AI2003]

    1. strcmp() (ii) getc()
  8. Write the names ofthe header flies to which the following belong:


    1. sqrt() (ii) isalpha() (iii) puts() (iv) strcpy()

  9. Write the names ofthe header flies to which the following belong: [AI 2004]

    1. gets() (ii) strcmp() (iii) abs() (iv) isalnum()

  10. Write the name of header flies to which the following belong: [Comptt 2004]

    1. sqrt() (ii) strcpy() (iii) isalpha() (iv) open()



2 Marks questions:




  1. Rewrite the following program after removing the syntactical error(s) if any. Underline each correction.

#include

void main( )

{ F = 10, S = 20;

test(F;S);

test(S); }

void test(int x, int y = 20)

{ x=x+y;

count<>y;}



  1. Find the output of the following program:

#include

void main( )

{ int U=10,V=20;

for(int I=1;I<=2;I++)

{ cout<<”[1]”<

cout<<”[2]”<<++V<<”&”<


  1. Rewrite the following C++ program after removing the syntax error(s) if any. Underline each correction. [CBSE 2010] include

class FLIGHT

{

Long FlightCode;



Char Description[25];

public


void addInfo()

{

cin>>FlightCode; gets(Description);



}

void showInfo()

{

cout<

} };

void main( )

{ FLIGHT F;

addInfo.F();

showInfo.F; }


  1. Rewrite the following program after removing the syntax error(s) if any. Underline each correction.

#include

void main( )

{ One=10,Two=20;

Callme(One,Two);

Callme(Two); }

void Callme(int Arg1,int Arg2)

{ Arg1=Arg1+Arg2;

Count<>Arg2; }



  1. In the following program, find the correct possible output(s)from the options:

#include

#include

void main( )

{ randomize( );

char City[ ][10]={“DEL”, “CHN”, “KOL”, “BOM”, “BNG”};

int Fly;


for(int I=0; I<3;I++) {

Fly=random(2) + 1;

cout<
Outputs:

(i) DEL : CHN : KOL: (ii) CHN: KOL : CHN:

(iii) KOL : BOM : BNG: (iv) KOL : CHN : KOL:


  1. In the following program, find the correct possible output(s)from the options:

#include

#include

void main( )

{ randomize( );

char Area[ ][10]={“ NORTH”, “SOUTH”, “EAST”, “WEST”};

int ToGo;

for(int I=0; I<3;I++) {

ToGo=random(2) + 1;

cout<
Outputs:

(i) SOUTH : EAST : SOUTH : (ii) NORTH : SOUTH : EAST :

(iii) SOUTH : EAST : WEST : (iv) SOUTH : EAST : EAST :


  1. Rewrite the following program after removing the syntactical error(s) if any. Underline each correction.

#include

const int Max 10;

void main()

{ int Numbers[Max];

Numbers = {20,50,10,30,40};

for(Loc=Max-1;Loc>=10;Loc--)

cout>>Numbers[Loc]; }


  1. In the following C++ program what is the expected value of Mysore from options (i) to (iv) given below.Justify your answer.

#include

#include

void main( )

{ randomize( );

int Score[ ] = {25,20,34,56,72,63},Myscore;

cout<

Ii) 25 (ii) 34 (iii) 20 (iv) None of the above.


  1. Find the output of the following program

#include

void main( )

{ long NUM=1234543;

int F=0,S=0;

do

{ int R=NUM % 10;



if (R %2 != 0)

F += R;


else

S += R;


NUM / = 10;

} while (NUM>0);

cout<


  1. Rewrite the following program after removing the syntactical error(s), if any. Underline each correction.

#include

const int Multiple 3;

void main( )

{ value = 15;

for(int Counter = 1;Counter = <5;Counter ++, Value -= 2)

if(Value%Multiple = = 0)

cout<

cout<

else

cout<


  1. Find the output of the following program

#include

#include

#include

void Convert(char Str[ ],int Len)

{ for(int Count=0;Count

{ if(isupper(Str[Count]))

Str[Count]=tolower(Str[Count]);

else if (islower(Str[Count]))

Str[Count]=toupper(Str[Count]);

else if(isdigit(Str[Count]))

Str[Count]=Str[Count]+1;

else Str[Count]=’*’;

} }

void main( )


{ char Text[ ]=”CBSE Exam 2005”;

int Size = strlen(Text);

Convert(Text,Size);

cout<

for(int C=0,R=Size . 1;C<=Size/2;C++,R--)

{ char Temp=Text[C];

Text[C]=Text[R];

Text[R]=Temp; }

cout<


  1. The following code is from a game, which generate a set of 4 random numbers. Praful is playing this game, help him to identify the correct option(s) out of the four choices given below as the possible set of such numbers generated from the program code so that he wins the game. Justify your answer. [CBSE 2010]

#include

#include

const int LOW=25;

void main()

{ randomize();

int POINT=5, Number;

for(int I=1;I<=4;I++)

{ Number=LOW+random(POINT);

cout<

POINT--; } }



  1. 29:26:25:28: (ii)24:28:25:26:

(iii) 29:26:24:28; (iv)29:26:25:26:
13) Rewrite the following program after removing the syntactical errors (if any). Underline each correction.

#include [iostream.h]

class MEMBER

{

int Mno;float Fees;



PUBLIC:

void Register(){cin>>Mno>>Fees;}

void Display{cout<

};

void main()



{

MEMBER M;

Register();

M.Display();

}
3 Marks questions:[Hots]




  1. Find the output of the following program; [Delhi 2008]

#include

#include

void main( )

{ char Text[ ] = “Mind@work!”;

for(int I=0; Text[I]!=’\0’;I++)

{ if(!isalpha(Text[I]))

Text[I]=”*”;

else if(isupper(Text[I]))

Text[I]=Text[I]+1;

else


Text[I] = Text[I+1]; }

cout<


  1. Find the output of the following program:

#include

#include

void main( )

{ char Mystring[ ] = "what@OUTPUT!";

for(int I=0; Mystring[I]!=’\0’;I++)

{ if(!isalpha(Mystring[I]))

Mystring[I]=’*’;

else if(isupper(Mystring[I]))

Mystring[I]=Mystring[I]+1;

else


Mystring[I] =Mystring[I+1];

} cout<


  1. Find the output of the following program.

#include

void Withdef(int HisNum=30)

{ for(int I=20;I<=HisNum;I+=5)

cout<

cout<

void Control(int &MyNum)

{ MyNum+=10;

Withdef(MyNum); }

void main()

{ int YourNum=20;

Control(YourNum);

Withdef();

cout<<.Number=.<


  1. Find the output of the following program:

#include

void Indirect(int Temp=20)

{ for(int I=10;I<=Temp;I+=5)

cout<

cout<

void Direct(int &Num)

{ Num+=10;

Indirect(Num); }

void main( )

{ int Number=20;

Direct(Number);

Indirect( );

cout<<”Number =”<


  1. Find the output of the following program:

#include

#include

void Secret(char Msg[],int N);

void main( )

{ char SMS=” rEPorTmE”;

Secret(SMS,2);

cout<

void Secret(char Msg[],int N)

{ for(int c=10;Msg[]1=’\0’;c++)

if(c%2==0)

Msg[c]= Msg[c]+N;

else if (isupper(Msg[c]))

Msg[c]=tolower(Msg[c]);

else


Msg[c]= Msg[c]-N; }

  1. Find the output of the following program: [CBSE 2010]

#include

struct three_d

{ int x,y,z; };

void movein(three_d &t, int step=1)

{ t.x+=step;

t.y+=step;

t.z+=step; }

void moveout(three_d &t, int step=1)

{ t.x-=step;

t.y+=step;

t.z-=step; }

void main()

{ three_d t1={10,20,5},t2={30,10,40};

movein(t1);

moveout(t2,5);

cout<

cout<

movein(t2,10);

cout<


  1. Write the output of the following program:

#include

int func(int &x,int y=10)

{ if(x%y==0) return ++x;else return y- -; }

void main( )

{ int p=20,q=23;

q=func(p,q);

cout<

p=func(q);



cout<

q=func(p);

cout<


  1. Find the output of the following program. [CBSE 2010]

#include

#include

void Mycode(char Msg[],char ch)

{ for(int cnt=0;Msg[cnt]!='\0';cnt++)

{ if(Msg[cnt]>='B'&& Msg[cnt]<='G')

Msg[cnt]=tolower(Msg[cnt]);

else

if(Msg[cnt]=='A'||Msg[cnt]=='a')



Msg[cnt]=ch;

else

if(cnt%2==0)

Msg[cnt]=toupper(Msg[cnt]);

else

Msg[cnt]=Msg[cnt-1];


} }

void main()

{ char MyText[]="ApEACeDriVE";

Mycode(MyText,'@');

cout<<"NEW TEXT: "<
9) Write a function in C++ to merge the contents of two sorted arrays A & B into

third array C. Assuming array A and B are sorted in ascending order and the

resultant array C is also required to be in ascending order.
10) Write a function in C++ to search for a BookNo from a binary file "BOOK.DAT",

assuming the binary file is containing the objects of the following class. 3

class

{

int Bno;



char Title[20];

public:


int RBno(){return Bno;}

void Enter(){cin>>Bno;gets(Title);}

void Display(){cout<

};
Answer to Questions


1 Marks Answer

  1. Ans) iomanip.h

iostream.h

  1. Ans) iostream.h

iomanip.h

  1. Ans: iostream.h

iomanip.h

  1. Ans) stdio.h string.h

  2. Ans: string.h, stdio.h

  3. Ans) (i) abs( ) - math.h, stdlib.h, complex.h

(ii)isupper( ) - ctype.h

  1. Ans:

      1. abs( ) - math.h, stdlib.h, complex.h

      2. random( ) - stdlib.h

  2. Ans) (i) abs( ) - stdlib.h, math.h, complex.h

strcmp( ) - string.h

  1. Ans)

(i) puts( ) - stdio.h

(ii) isalnum( ) - ctype.h



  1. Ans:

      1. gets( ) - stdio.h

      2. strcmp( ) - string.h

      3. abs( ) - math.h, stdlib.h,complex.h


      4. isalnum( ) - ctype.h

  2. Ans:

(i) strcmp( ) - string.h

(ii)getc( ) - stdio.h



  1. Ans:

      1. get( ) - iostream.h

      2. open( ) - fstream.h

      3. abs( ) - math.h, stdlib.h

      4. strcat( ) - string.h

  2. Ans:

      1. getc( ) - stdio.h

      2. strcat( ) - string.h

  3. Ans)

      1. isupper( ) - ctype.h

(ii)setw( ) - iomanip.h

(iii)exp( ) - math.h

(iv)strcmp( ) - string.h


  1. Ans)Execution of the program starts and ends at main( ). The main ( ) is the driver function of the program. If it is not present in a program, no execution can take place.

  2. Ans: (i)strcat( ) - string.h

(ii)scanf( ) - stdio.h

(iii)getchar( ) - stdio.h

(iv)clrscr( ) - conio.h


  1. Ans:

  1. cos( ) - math.h

  2. setw( ) - iomanip.h

  3. toupper( ) - ctype.h

  4. strcpy( ) - string.h

  1. Ans:

(i) cos( ) - math.h

(ii) setw( ) - iomanip.h

(iii) toupper( ) - ctype.h

(iv) strcpy( ) - string.h



  1. Ans.

(i) string.h (ii)
ctype.h (iii) math.h (iv) stdio.h

  1. Ans. (i) math.h (ii) ctype.h

  2. Ans. (i) fstream.h (ii)
    graphics.h (iii) fstream.h (iv) string.h

  3. Ans.

(i) iostream.h (ii) fstream.h (iii) math.h (iv) string.h
  1. Ans.


(i) fstream.h (ii) fstream.h (iii) iostream.h (iv) string.h

  1. Ans.

(i) string.h (ii) stdio.h>

  1. Ans.

(i) string.h (ii) stdio.h

  1. Ans.

(i) math.h (ii) ctype.h (iii) math.h (iv) string.h

  1. Ans. (i) stdio.h (ii) string.h (iii) math.h (iv) ctype.h

  2. Ans. (i) math.h (ii) strcpy.h (iii) ctype.h (iv) fstream.h


2 marks Answers




1 Ans:

#include

void test(int x,int y=20); //Prototype missing

void main( )

{ int F = 10, S = 20; //Data type missing

Text(F,S); //Comma to come instead of ;

Text(S);}

void Text(int x, int y)

{ x=x+y;

cout<

2 Ans:Output:

[1]10&15


[2]21&13

[1]11&16


[2]22&14

3Ans: #include

class FLIGHT

{

long FlightCode;



char Description[25];

public:


void addInfo()

{

cin>>FlightCode; gets(Description);



}

void showInfo()

{

cout<

} };

void main( )

{ FLIGHT F;

F.addInfo();

F.showInfo;}

4Ans:

void Callme(int Arg1,int Arg2=20);

#include

void main( )

{ int One=10,Two=20;

Callme(One,Two); //Given ; instead of ,

Callme(Two); }

void Callme(int Arg1,int Arg2)

{ Arg1=Arg1+Arg2;

cout<


5 Ans)

Since random(2) gives either 0 or 1, Fly value will be either 1 or 2.

(random(n) gives you any number between 0 to n-1) City[1] is .CHN.

City[2] is .KOL.

Since I value from 0 to 2 (ie<3), 3 iterations will takes place.

So the possible output consists 3 strings separated by :, each of

them may be either .CHN. or .KOL..

So the possible output will be

(ii) CHN : KOL : CHN:

(iv) KOL :CHN : KOL:

6Ans) Since random(2) gives either 0 or 1, ToGo value will be either 1 or 2.

(random(n) gives you any number between 0 to n-1) Area[1] is .SOUTH.

Area[2] is .EAST.

Since I value from 0 to 2 (ie<3), 3 iterations will takes place. So the possible output consists 3 strings separated by :, each of them may be either .SOUTH. or .EAST..



So the possible output will be

(i) SOUTH : EAST : SOUTH :

(iv) SOUTH : EAST : EAST :

7 Ans)#include

const int Max=10;//Constant Variable .Max. must be

//initialized. Declaration Syntax Error

void main( )

{ int Numbers[Max]={20,50,10,30,40};

for(Loc=Max-1;Loc>=0;Loc--)

cout>>Numbers[Loc];}

8 Ans: Expected Output:

(iv) None of the above.



9 Ans: Output: 2

10)Ans: #include

const int Multiple=3;

void main( )

{ int Value = 15;

for(int Counter = 1;Counter <=5;Counter ++, Value -= 2)

if(Value%Multiple == 0)


{ cout<cout<}

else

cout<

11Ans:Output:

cbse*eXAM*3116

6113*MXAe*esbc

12. 27:

27:


27:

26:


13. #include

class MEMBER

{

int Mno;


float Fees;

public:


void Register(){cin>>Mno>>Fees;}

void Display(){cout<

};

void main()



{

MEMBER M;

M.Register();

M.Display();

}

3 Marks answers




1)Ans:

Solution:

Text[ ] =





When I=0

Since Text[0] is ‘M’, Upper Case Letter,

(isupper(Text[I]) will becomes true.

So Text[I] =Text[I]+1

So Text[0]=Text[0]+1

Text[0] =77(ASCII Value of M) + 1 = 78 =N(78 is ASCII Value of N)

Now the String Text[ ] =



When I=1

Since Text[1] is ‘i’, Which is a character, but which is not Upper

case,

else part will be executed.


Ie Text[I]=Text[I+1]

Here Text[1]=Text[1+1]

=Text[2]

Ie ‘n’ will be stored in place of ‘I’

Now the String Text[ ] =




When I=2

Since Text[2] is ‘n’, Which is a character, but which is not Upper

case, else part will be executed.

Ie Text[I]=Text[I+1]

Here Text[2]=Text[2+1]

=Text[3]


Ie ‘d’ will be stored in place of ‘n’

Now the String Text[ ] =





When I=3

Since Text[3] is ’d’, Which is a character, but which is not Upper

case, else part will be executed.

Ie Text[I]=Text[I+1]

Here Text[3]=Text[3+1]

=Text[4]


Ie ‘@’, will be stored in place of ,’d’

Now the String Text[ ] =





When I=4

Since Text[4] is ‘@’, Since which is not an alphabet,

(!isalpha(Text[I])) will becomes true.

Ie if(!isalpha(Text[I]))



Text[I]=’*’;

Ie Text[4]=’*’

Ie ‘*’ will be stored in place of ’@’

Now the String Text[ ] =




When I=5

Since Text[5] is ‘W’, Upper Case Letter,

(isupper(Text[I]) will becomes true.

So Text[I] =Text[I]+1

So Text[5]=Text[5]+1

Text[5] =87(ASCII Value of W) + 1 = 88 =X(88 is ASCII Value of X)

Now the String Text[ ] =




When I=6

Since Text[6] is ‘o’, Which is a character, but which is not Upper

case, else part will be executed.

Ie Text[I]=Text[I+1]

Here Text[6]=Text[6+1]

=Text[7]


Ie ‘r’ will be stored in place of ‘o’

Now the String Text[ ] =





When I=7

Since Text[7] is ‘r’, Which is a character, but which is not Upper

case, else part will be executed.

Ie Text[I]=Text[I+1]

Here Text[7]=Text[7+1]

=Text[8]


Ie ‘k’ will be stored in place of ‘r’.

Now the String Text[ ] =





When I=8

Since Text[8] is ‘k’, Which is a character, but which is not Upper

case, else part will be executed.

Ie Text[I]=Text[I+1]

Here Text[8]=Text[8+1]

=Text[9]


Ie ‘!’ will be stored in place of ‘k’

Now the String Text[ ] =




When I=9

Since Text[9] is ‘!’, Since which is not an alphabet,

(!isalpha(Text[I])) will becomes true.

Ie if(!isalpha(Text[I]))


Text[I]=’*’;

Ie Text[9]=’*’

Ie ‘*’ will be stored in place of ‘!’

Now the String Text[ ] =



Output: Nnd@*Xrk!*



2) Ans:

Output: hat@*PVUQVU*



3)Ans:

Output:


20,25,30,

20,25,30,

Number=30

4)Ans:

Output: 10,15,20,25,30,

10,15,20,

Number =30



5)Ans:

Output: teRmttoe



6) Ans:

Output:


11, 21 ,6

25 , 15, 35

35, 25, 45

7)Ans: Output:

2023

1023

1111

8)Ans: Output:

New Text=@@@@ccddIIe


9) Ans: Output:
void AddNSave(int A[ ],int B[ ],int C[ ],int N,int M, int &K) 3

{

int I=0,J=0;



K=0;

while (I

if (A[I]

C[K++]=A[I++];

else

if (A[I]>B[J])



C[K++]=B[J++];

else


{

C[K++]=A[I++];

J++;

}

for (;I

C[K++]=A[I];

for (;J

C[K++]=B[J];

}


    1. Ans:Output:

void BookSearch()

{

fstream FIL;


FIL.open("BOOK.DAT",ios::binary|ios::in);

BOOK B;


int bn,Found=0;

cout<<"Enter Book No. to search…"; cin>>bn;

while (FIL.read((char*)&S,sizeof(S)))

if (FIL.RBno()==bn)

{

S.Display();



Found++;

}

if (Found==0) cout<<"Sorry! Book not found!!!"<

FIL.close();

}
BASIC CONCEPTS OF OOPS



Some of the important object oriented features are namely:

  • Objects

  • Classes

  • Inheritance

  • Data Abstraction

  • Data Encapsulation

  • Polymorphism

  • Overloading

  • Reusability

Objects:


Object is the basic unit of object-oriented programming. Objects are identified by its unique name. An object represents a particular instance of a class..



  • An Object is a collection of data members and associated member functions also known as methods.

Classes:


  • Classes are data types based on which objects are created.

  • Thus a Class represent a set of individual objects. Characteristics of an object are represented in a class as Properties. The actions that can be performed by objects becomes functions of the class and is referred to as Methods.

Inheritance:


  • Inheritance is the process of forming a new class from an existing class or base class. The base class is also known as parent class or super class, The new class that is formed is called derived class.
  • Derived class is also known as a child class or sub class. Inheritance helps in reducing the overall code size of the program, which is an important concept in object-oriented programming.

Data Abstraction:


  • Data Abstraction increases the power of programming language by creating user defined data types.

  • Data Abstraction also represents the needed information in the program without presenting the details.

Data Encapsulation:


  • Data Encapsulation combines data and functions into a single unit called Class.

  • Data Encapsulation enables the important concept of data hiding possible.

Polymorphism:


  • Polymorphism allows routines to use variables of different types at different times.

  • An operator or function can be given different meanings or functions.

  • Polymorphism refers to a single function or multi-functioning operator performing in different ways.

Overloading:


  • Overloading is one type of Polymorphism.

  • It allows an object to have different meanings, depending on its context.

  • When an exiting operator or function begins to operate on new data type, or class, it is understood to be overloaded.

Reusability:


  • This term refers to the ability for multiple programmers to use the same written and debugged existing class of data.

  • The programmer can incorporate new features to the existing class, further developing the application and allowing users to achieve increased performance.


IMPLEMENTATION OF OOPS IN C++

Constructors and Destructors

  • Constructors and destructors are special member functions of classes that are used to construct and destroy class objects. Construction may involve memory allocation and initialization for objects.
  • Destruction may involve cleanup and deallocation of memory for objects.


The following restrictions apply to constructors and destructors:

  • Constructors and destructors do not have return types not even void nor can they return values.

  • References and pointers cannot be used on constructors and destructors because their addresses cannot be taken.

  • Constructors cannot be declared with the keyword virtual.

  • Constructors and destructors cannot be declared static, const, or volatile.

  • Unions cannot contain class objects that have constructors or destructors.

  • The compiler automatically calls constructors when defining class objects and calls destructors when class objects go out of scope.

  • . A constructor does not allocate memory for the class object its this pointer refers to, but may allocate storage for more objects than its class object refers to. If memory allocation is required for objects, constructors can explicitly call the new operator. During cleanup, a destructor may release objects allocated by the corresponding constructor. To release objects, use the delete operator.

  • Derived classes do not inherit constructors or destructors from their base classes, but they do call the constructor and destructor of base classes.

  • Constructors are also called when local or temporary class objects are created, and destructors are called when local or temporary objects go out of scope.

  • You can call member functions from constructors or destructors.

  • constructor is a member function with the same name as its class.

For example:

class X {

public:

X(); // constructor for class X



};
  • Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed.


  • A destructor is called for a class object when that object passes out of scope or is explicitly deleted.

  • A destructor is a member function with the same name as its class prefixed by a ~ (tilde).

For example:

class X {

public:

X(); // Constructor for class X



~X(); // Destructor for class X
};

Class members that are class types can have their own destructors. Both base and derived classes can have destructors, although destructors are not inherited. If a base class A or a member of A has a destructor, and a class derived from A does not declare a destructor, a default destructor is generated.

The default destructor calls the destructors of the base class and members of the derived class.

The destructors of base classes and members are called in the reverse order of the completion of their constructor:

The destructor for a class object is called before destructors for members and bases are called.

Destructors are implicitly called when an automatic object (a local object that has been declared auto or register, or not declared as static or extern) or temporary object passes out of scope. They are implicitly called at program termination for constructed external and static objects. Destructors are invoked when you use the delete operator for objects created with the new operator.


Default Constructors and Destructors


If you don't declare a constructor or a destructor, the compiler makes one for you. The default constructor and destructor take no arguments and do nothing.

What good is a constructor that does nothing? In part, it is a matter of form. All objects must be constructed and destructed, and these do-nothing functions are called at the right time.


Using constructors and destructors.

// Demonstrates declaration of a constructors and

// destructor for the Cat class

#include // for cout
class Cat // begin declaration of the class

{

public: // begin public section



Cat(int initialAge); // constructor

~Cat(); // destructor

int GetAge(); // accessor function

void SetAge(int age); // accessor function

void Meow();

private: // begin private section

int itsAge; // member variable

};
Cat::Cat(int initialAge) // constructor definition of Cat,

{

itsAge = initialAge;



}
Cat::~Cat() // destroy the object of cat when it is no longer refered.

{

}


// GetAge, Public accessor function

// returns value of itsAge member

int Cat::GetAge()

{

return itsAge;



}
// Definition of SetAge, public accessor function
void Cat::SetAge(int age)

{

// set member variable its age to value passed in by parameter age



itsAge = age;

}
// definition of Meow method

// returns: void

// parameters: None

// action: Prints "meow" to screen

void Cat::Meow()

{

cout << "Meow.\n";



}
// create a cat, set its age, have it

// meow, tell us its age, then meow again.

int main()

{

Cat Frisky(5);



Frisky.Meow();

cout << "Frisky is a cat who is " ;

cout << Frisky.GetAge() << " years old.\n";

Frisky.Meow();

Frisky.SetAge(7);

cout << "Now Frisky is " ;

cout << Frisky.GetAge() << " years old.\n";

return 0;

}

Output: Meow.



Frisky is a cat who is 5 years old.

Meow.

Now Frisky is 7 years old.

  Copy Constructor


  • A copy constructor is a special constructor in the C++ programming language used to create a new object as a copy of an existing object.

  • Normally the compiler automatically creates a copy constructor for each class (known as a default copy constructor) but for special cases the programmer creates the copy constructor, known as a user-defined copy constructor. In such cases, the compiler does not create one.

  • Copying of objects is achieved by the use of a copy constructor and a assignment operator. A copy constructor has as its first parameter a reference to its own class type. It can have more arguments, but the rest must have default values associated with them. The following would be valid copy constructors for class X:

X(const X& copyFromMe);

X(X& copyFromMe);

X(const X& copyFromMe, int = 10);

X(const X& copyFromMe, double = 1.0, int = 40);



The following cases may result in a call to a copy constructor:

  • When an object is returned by value

  • When an object is passed (to a function) by value as an argument

  • When an object is thrown

  • When an object is caught

  • When an object is placed in a brace-enclosed initializer list

An object can be assigned value using one of the two techniques:

  • Explicit assignment in an expression

  • Initialization

Explicit assignment in an expression


Object A;

Object B;

A = B; // translates as Object::operator=(const Object&), thus A.operator=(B) is called

// (invoke simple copy, not copy constructor!)

Initialization

An object can be initialized by any one of the following ways.

a. Through declaration

Object B = A; // translates as Object::Object(const Object&) (invoke copy constructor)

b. Through function arguments

type function (Object a);



c. Through function return value

Object a = function();

The copy constructor is used only for initializations, and does not apply to assignments where the assignment operator is used instead.

The implicit copy constructor of a class calls base copy constructors and copies its members by means appropriate to their type. If it is a class type, the copy constructor is called. By using a user-defined copy constructor the programmer can define the behavior to be performed when an object is copied.




  1   2   3   4   5   6   7   8   9


The database is protected by copyright ©hestories.info 2017
send message

    Main page