Computer Science XII Important Concepts for CBSE Examination Questions LEARN FOLLOWIING GIVEN CONCEPS 1. Encapsulation: Wraps up data and functions under single unit through class. Create a class as example. Example : 2. Abstraction: To show only essential features without representing background details. Private and Protected members of class implements the Abstraction or Data Hiding. 3. Object and Class: Object is an identifiable entity with some characteristics (data items of class) and behavior (member functions of a class). Class : Class is a way to bind data and associated function as a unit. Class makes a data type used to create object. As an example write C++ class. 4. Object Oriented Programming :- OOP views a problem in terms of objects involved rather than procedure for doing it. This programming model is more close to real world. 5. Modularity : breaking down a complex problem into simpler and cohesive units (modules). 6. Inheritance : Property of an OOP through which characteristics of one class can inherit to another class. The base class can share its characteristics to derived class through inheritance. Reusability implements through inheritance. 7. Polymorphism : Ability though which data can be processed in more than one form. (Poly means many, morphs means forms.) Polymorphism is implemented through function overloading. 8. Function Overloading: When two or more than two functions have same name with different parameters is known as function overloading. It implements polymorphism. Ex:- float area( int r) // to calculate area of circle return (3.14*r*r); long area(int len, int wid ) // to calculate are of rectangle return ( len * wid ); Things to remember:- 9. Procedural Programming: It more emphasis on functions (procedures) than the data. Not close to real world problem solving approach. Limited scope. Always give function RETURN TYPE. Argument list must be different. Avoid ambiguity when calling overloaded function Always write valid function definition. 10. Token: Smallest individual unit of a program. C++ use 5 types of tokens : a. Keywords :- reserved words having special meaning in programming language. Ex. int, for, while etc. b. Identifier:- User specified name for variables, functions, class, structure etc. c. Literals: items that never change their value, i.e constents. Types : integer constant (1, -3, 11), floating point constant ( 2.34, 45.33), character constant ( A, b, 5, # ) string constant ( India, computer, A, A23 ) d. Punctuators : ;,, :, ::, //, etc e. Operators : >, >=, <=,? :, ==, =, +, -, %, *, / 11. Pointer: Pointer is a variable that stores the address of another variable. It is declared with * symbol. Example : int a, *ptr ; // a is normal variable, * ptr is pointer variable a= 10; ptr= &a; // Note: ptr is holding the address (&) of variable a 12. Function: - Named unit of block of programming statements that referred by function name. It can be invoke (call) from other parts of a program. 1 P a g e
13. Function prototype:- Declaration of a function that tells about return data type, function name, parameter list with their data type. 14. Example of a class that can be used for encapsulation, class, and data hiding. 15. C++ Jump Statements: a. goto statement:- transfer the program control anywhere in the program marked by label (: ) b. break statement:- the keyword break terminates the loop and execute statement given just after loop. c. continue statement :- It transfers the program control at beginning of loop and skip the statements given after continue statement in the loop. 16. C++ Conditional Operator : The? and : is known as conditional operator. If marks are more than 60 then grade is A grade is B, by using C++ conditional operator it will be:- grade = marks > 60? A : B 17. Inline Function : A function which is not invoked at the time of function call but its code is replaced in the program at the place of function call during compilation. It saves overhead of function call. For ex. Inline void square(int x) cout<<(x*x)<<endl; 18. # define : It is use to define constant that replaced with their value by the pre -processor prior to compilation. 2 P a g e
#define MAX 100 # define is also used to define a MACRO as : # define SQUARE(X) (X*X) Variable Type Size in byte 19. const : keyword used to define constant value for a variable. Ex. const int MAX = 200; Constant variable must be initialized and value cannot be changed in the program. 20. typedef The typedef command defines a new name (alias) for an existing type. For example typedef [80] char String; // a new data type defined for char[80] as string String StdName; // by using new datatype int char float long double 2 Byte 1 Byte 4 Byte 4 Byte 8 Byte For example : int x[5] occupies 5 x 2 = 10 bytes 21. sizeof() It an operator that find the size of an object /variable SPACE LEFT FOR YOUR IMP. NOTES Write answers of following questions: 1 Explain the usage of following with the help of an example: (i) constant (ii) reference (iii) variable (iv) union 2 State why are following expression invalid? (i) asm = 5100 val < 35 (ii) age > 70 && < 90 (iii) income >= 500 && val < 500 (iv) res!> 20! X > 20 3. Find the output of the following program: void main() int A=5,B=10; for(int I=1;I<=2;I++) cout<<"line1"<<a++ <<"&"<<B-2<<endl; cout<<"line2"<<++b <<"&"<<A+3<<endl; 4. Rewrite the following program after removing all the syntactical error(s), if any. Underline each correction. void main() One=10,Two=20; Callme(One;Two); Callme(Two); void Callme(int Arg1,int Arg2=20) Arg1=Arg1+Arg2 cout<<arg1>>arg2; 3 P a g e
INBUILT FUNCTIONS AND HEADER FILE Functions in string.h strcmp( ) strcmpi( ) strcpy( ) strcat( ) strrev( strlen( ) strupr( ) strlwr( ) Use This header file functions works on string i.e char array. Compare first string with second string. Return 0 if both strings are same Return +ve value if first string is big Return ve value if first string is small Compare first string with second string but ignore the strings case sensitivity. copy second string into first Append one string to another Reverse the string Return the length of string in integer Converts string into UPPER case Converts string into lower case Example char stra[20]= Hello, strb[10]= MyWorld ; char strc[20]= MYWORLD ; // to check whether two string are same or not if(strcmp( strb, strc )== 0 ) cout<< Both strings are same ; cout<< Both Strings are not same ;.. cout<< Length of strc is <<strlen(strc); cout<< Reverse of stra is <<srtrev(stra); strcpy(stra,strb); cout<< Now stra is <<stra; // prints MyWorld Copy (Store) Null to string variable T_Code; char T_Code[20];. strcpy( T_Code, Null ); strlwr(strc); // It will print myworld Functions in ctype.h isalpha( ) isdigit( ) isalnum( ) islower( ) isupper( ) tolower( ) toupper( ) isspace( ) iscntrl( ) ispunct( ) Use This header file functions works on single char ( ) and used to check the char. It checks whether character is alphabet or not. It checks whether character is digit (numeric) or not. It checks whether character is alphabet or number. It checks whether character is lower case/upper CASE or not. Converts upper case char to lower case. Converts lower case char to upper case. Checks for space Checks for control key Check for punctuations as (., : ;?) Example char ch, nch= 6 ; char mych= P ; char sentence[80] ; isalpha(nch); // return false i.e 0 isalpha(mych); // return true i.e non zero value isdigit(nch); // return non zero value. Application To find number of alphabets, digits, spaces, punctuators and special characters used in sentence or in a file. In for. OR while loop use functions as: for(int I = 0; sentence[i]!= \0 ; i++) ch= sentence[i]; if (isalpha(ch) ) alpha ++; if(isdigit(ch)) digit++; if(isupper(ch)) UChar ++; iomanip.h Functions used for formatted output Example Char str[10]= Japan ; setw( ) setprecession( ) setw( ) sets the width to print the output. cout<<setw(20)<<str; 4 P a g e
Assignment 2 Answer the following Questions 1. Find the output of the following program: void SwitchOver(int A[],int N,int split) for(int K=0;K<N;K++) if(k<split) A[K]+=K; A[K]*=K; void Display(it A[],int N) for(int K=0;K<N;K++) (K%2==0)?cout<<A[K] <<"%":cout<<a[k]<<endl; void main() int H[]=30,40,50,20,10,5; SwitchOver(H,6,3); Display(H,6); 2 The following code is from a game, which generates 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. #include<stdlib.h> const int LOW=25; void main() randomize(); int POINT=5,Number; for(int I=1;I<=4;I--) Number=LOW+random(POINT); cout<<number<<":"; POINT--; (i) 29:26:25:28: (ii) 24:28:25:26: (iii) 29:26:24:28: (iv) 29:26:25:26: 3. Study the following program and select the possible output from it: #include<stdlib.h> const int LIMIT=4; void main() randomize(); int Points; points=100+random(limit); for(int P=Pints;P>=100;P--) cout<<p<<"#"; cout<<endl; (i) 103#102#101#100# (ii) 100#101#102#103# 5 P a g e
(iii) 100#101#102#103#104# (iv)104#103#102#101#100# 5. Go through C++ code show below, and find out the possible output or outputs from the suggested Output Options (i) to (iv. Also, write the least value and highest value, which can be assigned to the variable MyNum. #include<stdlib.h> void main() randomize(); int MyNum,Max=5; MyNum=20+random(Max); for(int N=Mynum;N<=25;N++) cout<<c<<"*"; (i) 20*21*22*23*24*25 (ii) 22*23*24*25 (iii) 23*24* (iv) 21*22*23*24*25 6. Find the output of the following #include<ctype.h> void main() char Line[]="Good@LOGIC"; for(int I=0;Line(I)!='\0';I++) if(!isalpha(line[i])) Line[I]='$'; if(islower(line[i])) Line[I]=Line[I]+1; Line(I)=Line[I+1]; cout<<line; 7. Find the output of the following program: #include<ctype.h> void Encode<char Info[],int N); void main() char Memo[]="Justnow"; Encode(Memo,2); cout<<memo<<endl; void Encode<char Info[],int N) for(int I=0;Info[I]!='\0';I++) if(i%2==0) Info[I]=Info[I]-N; if(islower(info[i])) Info[I]=toupper(Info[I]); Info[I]=Info[I]+N; 6 P a g e