You are on page 1of 27
UNIT Object Oriented Programming in C++ C++ REVISION TOUR CHAPTER Syllabus Basics of C++ : C++ character set, C++ Tokens (Identifiers, Keywords, Constants, Operators), Structure of a C++ Program (include files, main function); Header files, Data Types, Variables and Constants, Operators and Expressions. © Flow Control : Conditional statements, Loops and Nested Loops, Structured data type : Array, String and Character related Library functions. © Functions : User Defined Functions, Mathematical and Other Functions, Structure. TOPIC-1 Basics of C++ Quick Review 1. History of C++ mt “+ Developed at AT AT Laban ety fee ate Ra nis Also known a6“C with Classes’ roPic-3 = Itisan object oriented programming language. Artays and Functions Pay 2. C++ Character Sets roPic-4 Letters AZ, ax Structures pas Digits 09 Special Characters Space +*/>\Q 010 i718 5: % 187 # =@ White Spaces Blank spaces, horizontal tab, vertical tab, New line, Carriage return, (Other Characters 256 ASCII characters as data oF literals 3. C++ Tokens The smallest individual unit in a program is known as token, Keywords : Words that conveys a special meaning to the language compiler ¢g- int, float, char ete Identifiers: Sequence of letters & digits to identify a memory block, program unit or program objects eg, name, 4,2,A,X, date ile, filed, et. Literals: Constants never change their value during the execution of a program Type + Integer constant = Decimal - 1234, +97 (Not begins with zero) = Octal -014, 023 (preceded by 0) = Hexadecimal - (XC (preceded by 0x or OX) © Character constant (2,4, "a',V,V,'0,'3)) ‘© Floating constant (13.0, 0.00065, 152807, 0.172E-3) # String constants ‘abe'0”, “Computer Science'0”) Panctuators:[] () () ,::* = #, also known as separators. Operators: <<, >>, +4444 %, +4, fe, ||! EduSuccess Smart Learning Centers | www.edusuecessin | Contact # +91- 85 0606 1616 / 5252 EduSuccess/ Ten CATS TRESS EduSuccess: Delhi's #1 Tuition Center for Computer Science, Class ~ 12, 4. Types of Operators: “Arithmetic operators: +, %/,% Bitwise Operators: &, | Logical Operators || “Assignment Operators Relational Operators: <, >, Increment Operators: +42, a+ + Decrement Operators sa, —— Conditional Operators exp 1? exp 2? exp 3 5. #define directive ‘We can define our own names for constants that we use oftenly in our program without having to depend upon memory-consuming variables, by simply using the #define preprocessor directive. Its format is ‘#define Forexample ‘define PL 3.14159 11 This defines new constant : PI 6, Declared constants (const) With the const prefix we can declare constants with a specific type, in the same way as we would do with a variable. For example ‘const int pathwidth = 100; ‘const char tabulator = “W's Here, pathwidth and tabulator are two typed constants, They are treated just like regular variables except that their values cannot be modified after their dedaration, 7. Type conversion in C++ a. Implicit type conversion (Iype Promotion) Explicit type conversion (Iype Casting) a. Implicit type conversion (Type Promotion) : Implicit type conversion do not require any operator. They ‘are automatically performed when a value is copied to a compatible type. Usual arithmetic implicit type ‘conversions are summarized in the following table ‘Step No. | Ifeithertype of operand is _| Then resultant type of expression is Otherwise 1 | tong double Tong double step2 2 | double double step 3 3 | float Aloat step 4 | short intor int (signed or int step unsigned) or char 5 | unsigned long ‘unsigned long step 6 6 | Jong int or unsigned int () long int (provided long int can represent all | step 7 values of unsigned int) (i) unsigned long int Gall values of unsigned can't be represented by long int) Tong Tong, step 8 unsigned unsigned both operands . Explicit type conversion (Type Casting): Type casting operators allow us to convert one type to another. There are several ways to do this in C+ +. The simplest way is to precede the expression to be converted by the new type enclosed between the parentheses () int ‘The previous code converts the float number 3.14 to an integer value (3), the remainder is lost. Here, the typecasting operator was (int). Another way to do the same thing in C++ is using the functional notation Preceding the expression to be converted by the type and enclosing the expression between parentheses EduSuccess Smart Learning Centers | www.edusuecessin | Contact # +91- 85 0606 1616 / 5252 EduSuccess/ (C+ REVISION TOUR Tae ATS TRESS i= int; Both ways of type casting are valid in C++. 8. sizeof 0) operator ‘This operator accepts one parameter, which can be either a type or a variable itself and returns the size in bytes of that type or object. a= sizeof (char); ‘This will assign the value I to# because char isa one-byte long type. The value returned by sizeof isa constant, so it is alovays determined before program execution, 9. typedef keyword Using the keyword typedef, we can create a synonym for existing fundamental or compound data types in C++. Syntax: typedef where existing type is a C++ fundamental or compound type and new_type type we are defining. For example : typedef char C; typedef unsigned int WORD; typedef char * pChar; typedef char field (50); In this case we have defined four data types : C, WORD, pChar and fied as char unsigned int,chat* andl charl50] respectively, that we could perfectly use in declarations later as any other valid type C mychar anotherchar, *ptel; WORD myword; Char pte2; field name; typedef does not create different types. It only creates synonyms of existing types. That means that the type of myword can be considered to be either WORD or unsigned int, since both are in the same type, Benefits of using typedef : = typedef can be useful to define an alias fora type that is frequently used within a program, = tis also useful to define types when itis possible that we will need to change the type in later versions of ‘our program, orifa type we want to use has a name that is too long or confusing ame is the name for the new 10, Precedence of operators “+4(post increment), {post decrement) Highest ++4{pre increment), ~{pre decrement), sizeof! (no), (unary minus), + (unary plus) * (oltipy), / (divide), (modulus) (od), -ubtrat) {less than) <=(less than or equal to), > (greater than) >=(greater than or equal ©) (equal), !=(not equal) && (logical AND) | | Cogical OR) 2: (conditional expression) jgnment operator) and other assignment operators (arithmetic assignment operator) Comma operator Lowest 11. Data types AA specifier to create memory block of some specifi size and type. C++ offers two types of data types: (1). Fundamental type : Which are not composed of any other data type and defined within the compiler ie, int, ‘char float and void. (2) Derived data type: Which are made up of fundamental data types fe, array, function, class, union, ete: ‘Variable—Memory block of certain size where value can be stored and can be changed later during program ‘execution eg, int x floaty, float amount, char e; Constant—Memory block where value can be stored once but can’t be changed later during program execution eg, const int pi = 3.18; EduSuccess Smart Learning Centers | www.edusuecessin | Contact # +91- 85 0606 1616 / 5252 EduSuccess/ Ten CATS TRESS EduSuccess: Delhi’ #1 Tuition Center for Computer Science, Class ~ 12 Comment—Used for better understanding of program statements and escaped by the compiler to compile eg, single line () and maltctne Cascading—Repeated use of input or output operator(’>>" or “<-<")in one statement with in oF cout 12, First Program of C++: Let us begin a simple program of a C++ that shows a string as output onthe screen Program : # include < iostream. h > void main () { cout <<"WE ARE LEARNING C+ } Output : We are learning C++. Here, the line # include instructs the complier with declaration of predefined stream of input and output Here, Some keywords used in the above program are as follows () #, preprocessor directive void main (), is used to cal other functions for executions. i) cout <<, is used to display the text on the screen. 13, Some Standard C++ libraries Header Purpose jostream.h Defines stream classes for inputYoutput streams. stdioh Standard input and output, ype Character texts. stringh String operations. math “Mathematical functions sich as sin() and cos() stalibsh Utility functions such as malloe() and rand. Know the Terms © += :add.and-assign operator; a+ =b is roughly equivalent to defined types. cpp file: Extension used for file containing the c++ + program. + shfile : header file, <<: ostream output operator The assignment operator. The equality operator; compares values for equality returning a boolean value. + >>: iostream input operator ANSI: The American National Standards Institute, Co-operates closely with ISO over the C++ standard. © auto: In C and C++ a largely useless keyword redundantly indicating stack allocation for local variables. +b, Often a useful operation for user In C++ a keyword indicating that a variable gets its type from it auto d2 = 3°41; (42 will have type double). Block comment : comment started by * and terminated by */. + C++ : A general-purpose programming language with a bias towards systems programming that supports procedural programming, data abstraction, object-oriented programming, and generic programming, + C++ standard : The definition of C++ provided by ISO. Available from ANSI + const: Attribute ofa declaration that makes the entity to which it refers read-only. + const definition : Declaration of a const inchiding an initialize. + conversion : Explicit type conversion or implicit type conversion, ‘+ conversion aperator : Operator function specifying a conversion from a user-defined type to either another user-defined type or a built-in type. Note that constructors cannot define conversions to built-in types. + double : Double-precision floating-point number EduSuccess Smart Learning Centers | www.edusuecessin | Contact # +91- 85 0606 1616 / 5252 (C+ REVISION TOUR + escape character The character \ also called backslash, sed an initial character in representations of ‘characters that cannot be represented by a single ASCII character, such as newline (\m’) and horizontal tab (Y). © Iiteral : Notation for values of boolean, character types, integer types, or floating-point types. © main(): The function called by the system to start a C++ program. ‘© programming language : Artificial language for expressing concepts and general algorithms in a way that lends itself to solving problems using computers. There do not appear to be a general consensus on what a programming language is or should be. + sizeof : Operator yielding the size of an object. syntax : The set of grammatical rules specifying how the text of a program must be composed. For example, specifying the form of a declaration or the form ofa for-statement ‘+ typedef: Synonym for some type declared using the keyword typedef unary operator : An operator taking one operand, such as !and unary * ‘© whitespace : Characters that a represented only by the space they take up on a page or screen. The most ‘common examples are space ("), newline 1’), and tab (0!) Very Short Answer Type Questions [1 mark each] Q.1. Waite the related library function name based ‘upon the given information in C++ (Get single character using keyboard. This function is available in stdio.h file (i) Te check whether given character is alpha numeric character or not. This function is availablein ctype file. [CBSE SQP 2015] Ans. ()_getchar() pal i) isalnum () ta Q.2. Observe the following program very carefully and write the names of those header file(s), ‘which essentially needed to compile and execute the following program successfully [Delhi 2015) typedef char TEXT [60]; void main () TexT str [] = int Index = 0; while (Str [Endex}.) = *\0") AE (supper (str [Index] " Peace is supreme"; Str (Index ++] =? ‘#" else str [Index 44] = *# ; puts (s } Ans, , (Mark fr each correct header file) [Note Ignore any additional header file(s) [CBSE Marking Scheme, 2015] Q3. Name the header files that shall be needed for successful compilation of the following C++ code void main() { x); char str [20] , str (20) ; gets (str); Ans, Qa Ans, Ans, stropy (stri, str); strrev(ets) ; puts(str); pute (stri! ) [CBSE Compre, 2014) include 4 include vl Observe the following C++ code and write the name(s) of the header file(s), which will be cesentially required to run itina C++ compiler yoid main() cha cH, cinssSTR; custoupper (STR[0)); coutceSTRee*atarte with*< 4 finclude ma . Observe the following C++ code and write the name(s) of the header file(s), which will be essentially required to run itin a C++ complier void main() ( char Text [20] ,C: Cetolower (Text (0) coutecCee “is the firet cl of eText bal include bal EduSuccess Smart Learning Centers | www.edusuecessin | Contact # +91- 85 0606 1616 / 5252 EduSuccess/ Q.6. Name the header file(s), which are essentially required to run the following program segment void main() char As’K', By Af (islower(A)) Betoupper (A) ; else Bet cout ce A ta finelude 41 Q.7. Observe the following C+. + code and write the ramets} of header files, which will be essentially required to run itina C++ compiler void main() ( float Area, Side; Side = sqrt (area) cout> numbers if (abs (number ) == number) cout << “Positives << endl ) [Delhi, 2013), Ans. iostream.h, math h pA+ 4) Q.9. Write the names of header files, which are NOT NECESSARY to run the following program ¥ include ¥ include Setaio.ns # include “etring.h> 3 inelude cnath a> oid main () char STR (80) ; gets (STR) i Buts (strrev (STR)) } icese sgp 20131 ‘Ans, Header files not required are fostream.hmathh, p+) Q.10. Which C++ header file(s) are essentially required to be included to run/eweute the following C+ source code? (Note = Do not include any header file, which isfare not required) [0.D, 2012), Short Answer Type Questions-I EduSuccess: Delhi's #1 Tuition Center for Computer Science, Class ~ 12 void main() char TEXT [] = "Something"; coutc<"Renaining SMS Chars:"<<160- strlen (TEXT) < ral (ii) pa Q.11. Which C++ header file(s) are essentially required to be included to runvexecute the following C++ source code (Note : Do not include “any header file, which is/are not required)? [Delhi, 2012] void main () ( char STRING [] = “SomeThing*; coutee"Balance Charactera:"<<160- strlen (STRING) <, t+ ICBSE Marking Scheme, 2012], Q.12, Ahmed has started learning C++ and has typed the following program. When he compiled the following code written by him, he discovered that he needs to include some header files to successfully compile and execute it, Write the names of those header files, which are required tobe included in the code. [CBSE Comptt. 2016] void main() { float Radians, Value; cins>Radians: Valueesin (radians) ; couteeValueccendl jostream.h string. (s Mark for writing correct header file) Q.13, Which C++ header files) are essentially roguired to be included to runvexecute the following C++ code: {CBSE Comptt. 2016] void main() ( chartwordi "ello" .word2="Priends"; streat (wordt, word2) ; coutcexordl; ) ‘Ans, iostream.h string-h (Mark for writing correct header file) [2 marks each] Q.1. Define Macro with suitable example. (Delhi, 2012] ‘Ans. Macro are preprocessor directive created using 4 define that serve as symbolic constants. They are created to simplify and reduce the amount of repetitive coding, Forinstance, define max (a,b) a> b? a:b Define the macro max, taking two arguments a and b. This macro may be called like any C function with similar syntax. Therefore, after reprocessing EduSuccess Smart Learning Centers | www.edusuecessin | Contact # +91- 85 0606 1616 / 5252 c++ Revision TOUR omar A= max@y); void main () Becomes A=X> y 7:94 Re e254, 7 9, 20) Umar or dtd ae ae)ay oot 2, ad {mark for example Cie Taiplae0) 0.2. Wit the output ofthe flowing C++ programm eee ee code cout << §; } NOG jaune ellpemued beter estec] ee eer ee tlre being incaded inthe program, faa a Det, 2015) (ine 8 (20) = (3, 2, 5.4, 7, 9,19); ciase caic inv's > 0, By { for (p- 0: p< = 61 pee) char Grade ; { i€(stp]$2 = = 0) int Bonus; s=s+a (pl; } os cout > Speed, i if Speed > Max void main () Stop = ‘Y'; t cout <>Speed; Ans. CA if (speeasiax) —_//Erxor € ya Stop = °Y" Hat coute { EduSuccess Smart Learning Centers | www.edusuecessin | Contact # +91- 85 0606 1616 / 5252 EduSuccess/ cis = 2; void main () Ant PL = 20, P2 position (Pl) coutc // Declaration sytax error Hincludeemath. n> define PI-3.34 // Geclaration does not allow assignnent void main() { float x,a // Brror 3 symbol a coutectenter any radius"; // Bcror 4 : Character Contant mist be one or two characters long Error 1 undefined eePI*pow(r,2)// Error 5 : Bxpression syntax coutc<*Area="<>Times; Regult=pow (x, Times) coutefor cout and cin By math h>for pow) EduSuccess/ Q.12. Rewrite the following C++ code after removing any/all syntactical error with each correction ‘underlined. 10D, 2016) Note : Assume all required header files are already being included in the program, fdefine Formula(a,b)= 2asb void main () { Float x=3.2 geFormila (X,¥); coutes!Result=! < Assume all the requited header files are already being included in the code > The function random(n) generates an integer between 0 and n =I void main() EduSuccess Smart Learning Centers | www.edusuecessin | Contact # +91- 85 0606 1616 / 5252 EduSuccess/ Ten ATS TRC EduSuccess: Delhi's #1 Tuition Center for Computer Science, Class ~ 12, { randomize) int PICKER; PICKER-random(3); char COLOUR{] [5] =("BLUB", "PINK", "GREEN", "RED" for (int Tad; TesPICKER;T++) 4 for (int Te0;Jeat;J4+) cout< Assume all the required header files ae already being included > The function random(n) generates an integer between Il and n—1 void main() randomize) 7 int CHANGER; CHANGER-random (2) char crTv(] [25] ={*DSLHT", "MUMBAT the code, KOLKATA", *CHENNAT") + for (int I=0;T<=CHANGER; I++) { for (int T-0;5e-1;544) coute Hincludeciostream.hs Hncludecstring.ns Hincludecstdio.hs Class product { int product_code, qty, price; char name [20] + public, product () ( product_codes raty=0:pric cout«<"\n Enter code,aty, price"; cinssproduct_codessqty>>price; gets inane) } void tot_price() {return qtytprice; } } void main() { p product; peentry(}; cotectot_prics } Ans, #includecconio. h> Hincludeestring.h> #includecatdio.h> class product { int product_code,aty,price; char nane [20] ; public, product |) [ product_code=0; aty=0;price=0; strepy (name NULL! :} void entry() Short Answer Type Question-Il Ans (mark for option (ie)) coute<"\n Enter code, qty, price"; einssproduct_code=satys=price; ge int tot_price() {return gty*price;) h void main() { product pi p.entzy() cotcep.tot price t) } (Mark for each corection upto a maxiinum of four corrections) OR (1Mark for only identifying any 4 errors without suggesting corrections) Explain conditional, example? operator with suitable [CBSE SQP 2016) @onditional operator is also known as ternary operator because it requires thres operands and can be used to replace simple if- else code, It is used to check the condition and exucute firet expression if condition is true else execute other. syntax conditional 2 Expression 1: Expression 2: Explanation If the conditional expression is true then expression I executes otherwise expression 2 executes Bxample : Ant y = 10, x; x = y > 10? 1:0; couteex: output : 0 (1 Mark for correct explanation) (1 Mark for correct example) ICBSE Marking Scheme 2016) [3 marks] Q Find and write the output of the following C++ program code 10, 2016} Note : Assume all required header files are already being included in the program, EduSuccess Smart Learning Centers | www.edusuecessin | Contact # +91- 85 0606 1616 / 5252 EduSuccess/ a co ST EuSuccens: Delhi's Ton Center for Computer Science Clas = 12 class share void nain() ong int code; Share 8.7/0; float Rate; 5.Getcode (224,350) ; int DD, Tieetcode (1435, 250) public §.Upaate (50,28) ; share() Gichange (-25, 26) {ceds-1002 Rate-100;D0=1;) S.status(): vyoid Getcode (ong int €, float 8) Tistatua); aiseacaa0); code-c 5 mares ‘Ans, Date :28 “ it #400 Yoid Update (int change, int D) cae eae * Date: 1 Rate+=Change; 1435 # 250 boob; Date:26 } 100 #75 void statue () (0 Mark or cach correct in of ontput) { Note: Deduct only ¥ mark for not writing any or all couteccodecerd* cctateccendl; ama feta oral } [CBSE Marking Scheme 2016], TOPIC-2 Flow Control Quick Review Flow Control Ina computer program, a statement isa set of instructions given to the computer to perform the activity or action without having any action, is known as ‘null statement. A compound statement in C++ is a set of statements that are enclosed by a set of braces (]. For example. ( Statement I Statement 2; , Statements are excuted sequentially, selectively and iteratively in a program. Sequence Control : Show a flow of statement in a sequence. Selection : Selects a statement among, set of statements by applying a condition-test. For example, Ifstatement, Tfelse, Nosted-I,I-else-If switch ete Iteration : Means repeatiton of a set-of statements based on a condition-test. For example, for loop, while loop, o-while loop ete. Control Structure ‘Sequence Conditional | Multiple ‘Switch statement Loop contro control statement | choice (Alternate fori statement (while, statement(if) | (ifelse) statement celse-if) works for do awhile, for) Gfelseif only exact match Syntax Syntax Syntax Syntax Syntax if (expression) | ifexpression) | iffexpression) | switch (int/char while (expression) ( variable) { i i statements; tl statements; statements; | statements; |) case literal: ’ ’ else statements; Entry Control loop. 1 lif (expression) works for teue condition. EduSuccess Smart Learning Centers | www.edusuecessin | Contact # +91- 85 0606 1616 / 5252 (C+ REVISION TOUR EduSuccess/ else \t q statements; statements; |) ) se lt | statement; statements; do break; ( default: statements; statements; | while (expression); Exit Control Loop | execute at least once ifthe condition is false Break is compulsory statement with every ease | at the beginning, because itisnotinciuded | then the control executes next case statement until the rnextbreakisencountered | or oop orend of switch is reached | fo expression defaults optional it gets | expression; expression 3) ‘executed When no MATES [ | statement) Entry control loop works for true condl and preferred for fixed. no, of times. found. Know the Terms Block : Compound statement, Cstyle string : Zero-terminated array of chara and error-prone mechanism; where there are po if-statement : Statement selecting between two loop : A statement that expresses the nation of and a while-tatement while-statement:: A loop statement presenting, push_back(var); Short Answer Type Question: ters, supported by C standard library functions. A low-level ossible prefer strings. alternatives based on a condition. doing something, zero or more times, such as a forstatement switch-statement : Statement selecting among many alternatives based on an integer value. its condition “at the top’. For example, while (cin> >var) vec. s-1 [2 marks each] Q.1. Find syntax error(s), if any, inthe following program + (Assuming all desired header files) Treatready induded) typedef char String void main { sering S; int br for (L+0;L<26;C+4) SLi -v65; cout e typedef char Text (80) ; void main () { Text T = "Indian" int Count = strien(T); coutceP<< thas! c) (4 Mark for writing # include) [4 Mark for writing typedef char Text(80)]; (Mark for writing “has” and "characters" Rewrite the following program after removing the syntactical errors (if any). Underline each correction, 4 Anclude EduSuccess Smart Learning Centers | www.edusuecessin | Contact # +91- 85 0606 1616 / 5252 (C+ REVISION TOUR EduSuccess/ typedef char (so) string; void main () i String s = "Peace"; int erlen(s) : coute # includecciostream.h> typedef char string [80]; void main () { String s = *Peace™; int b= strlen(s); "ccLcc "characters" << end; ) (Mark for writing # include) (+ Mark for adding # before include) (Mark for writing typedef char stringl80}) (Mark for Writing “has” and “characters") Q. 11, Based on the following C++ code, find out the expected correct output (6) fom the options (9 to Gj), Also, find out the minimum and the TOPIC-3 Arrays and Functions Quick Review Ans. maximum value that can be assigned to the variable Trick usedl in the code at the time when value of count is3 void main () { char status [ ][20] = { “EXCEL”, e000", “OK"}; int Turn = 10, Trick: for (int count = count <4; count++) { Trick = random (count) } coutceTurn-Tricke int a = 20; // global variables void main() { int b = 10; //local variables cout ceacch; } 8, Passing value to function Fassing by value—In this method, separate memory is created for formal arguments and if any change is done ‘on formal variables, it will not affect the actual variables. So actual variables are pre Passing by address/reference—In this method, no separate memory is ereated for formal variables ie, formal variables share the same location of actual variables and hence any change on formal variables automatically ved in this case, reflected back to actual variables EduSuccess Smart Learning Centers | www.edusuecessin | Contact # +91- 85 0606 1616 / 5252 (C+ REVISION TOUR Example: void sample (int a, int & b) ( ae as 100; b= b+ 200; coutecace; ) void main() ( int a = 50, b= 40; cout #includeciostrean.hs void swap(int &, int 6); void swaplint, int } int main () int main() i { ine int a-10,b-20; avapia,b) swap (a,b) coutccace ea coutecace™ *a} cout< 1 return (a) Here, a and b are formal] Here, a and b are ) [CBSE Compt, 2014 Parameters. actual parameters. Ans, int find(int a[],int n) ‘Q.d. Deepa has just started working as a programmer ( in STAR SOFTWARE company. In the company Ant seal0l EduSuccess Smart Learning Centers | www.edusuecessin | Contact # +91- 85 0606 1616 / 5252 (C+ REVISION TOUR EduSuccess/ for(int xeL:xcnsx++] if (atx) Hincludecconio.h> int main () int a,b a = change 20, c fer B) a int change (int a 1b) int temp; temp = a; », return temp; a } In the above example, change () assigns a value 10 toa because itis defined by default 0.6. What isthe benefit of using function prototype for a function? Give a suitable example to illustrate itusing C++ code, [Delhi 2013) ‘Ans. The benefit of using a function prototype is to have a clear representation of the type of value that the function would retuen and type of argument the function needs a Example: Int function (int a int b); ‘The above is a prototype for a function named ‘function’ that has two integer arguments @ & b which returns as integer vale u Q.7. Write a prototype of a function named percent, which takes an integer as value parameter & return afloat type value. The parameter should have default value of 10. [CBSE SQP 2013] Ans. float percentint a = 10); i Q.8. Observe the following C++ code carefully and ‘write the sme ater removing syntax errors ddefine change (2, 8) 24 A+B: Ans, 9, Ans. Qo. void main () ( float X,Y, F cin o> Kos vy F = change (X,Y); cout <<"result i Anclude void streon(char s{1) for(int 20, 1=0;5[] 1="\or ise, 144); for(int 4-0; i Hincludecctype.h> void Mycode (char Megll,char cl) ( for(int ent=0;Meg lent] !="\o" rent+s) Msg [ent] =cH; else if lentt: Msg ent else Mag [ent vy void main() char MyText []="Imput Raw"; Mycode (MyText,'@"}; coute<"NEW TEXT: #include void main () ( randomize () Ant Gane [] = (20, 16), P, Ant Turns-random (2) + 5 for (int Tao; Tea2; T++) Perandom (2) Coute sheer, tetan; 1902) void main () Le (ero ge WEE] e=!UF) ( were; randomize 1); ) ant are] = (9) 6), y void main() int chance = random (2) + 10/7 ( for (int Ca ; Cedi H+) WORD Wrd="EDITORIALS"; { codeword (Wea); Nerandom (2) coutectrdecendl; coutecAre (N] + Chances"#* ; } i ‘Ans : DETIROAISL. 2 mark for corect output) © o4ee 0.17, Cone tefallowingcede de #78 saefine convert (2, Q) P+ 2 * ; vee void main () sj 20# 164 { ‘Ans. The outputs not expected from the program are float A, B, Result (Gand 6) ae “Minimum Value of Chance = 10 ‘Maximum Value of Chance = 11 (Mark for writing corect option for output NOT expected) (1 Mark for writing correct Minimum Value of Chance) (4 Mark for writing correct Maximum Value of Chance) 2) Q.16. Find and write the output of the following C++ program code result = Convert (A,B; cout Myspeed; if MySpeedomaxspeed wr Alert =H alert = coutecAlertecendiline; ) ‘Ans: define float Maxspeed_60.5 Harror 1, 2, 3 void main ( ) { int myspeed ; char Alert cin >Myspeed; if (myspeedsNaxspeed) Alert = °¥" ; M/acxor 4 J farcor 5 coutecAlertccendl; Mfecor 6 (/ Mark foreach correction upto a maxinrum of A corrections) oR (1 mark for only identifying any 4 errors, ‘without suggesting corrections). [CBSE Marking Scheme 2015] . Write the ouputs of the following C++ program code 10D, 2015] Note : Assume all required header files. are already being included in the program. void Location (int 6X, int ¥ = 4) ( yeray x+ey } void main () { int Px = Location 10, PY = 2; (Py) cout void in(int x,int y, int &2) zee ey) ) void out (int 2,int y, int ex) yer: 2/=txty) ) void main() 0, be30, c#10; out (a,c, ) 5 coutccace"#ecbeot i cecee" in(b,c,a) 7 coutceacc" a" cebect a" cece" ecendl; cout (a,b, e) coutecace"$tecbeo"$" cece" $*ecendl ) [CBSE Comptt, 2014] endl; 3: 20430010 a e2@300@10@ a «20530020008 i Find the output of the following program segment ICBSE SQP 2013], ant a=3; void demo (int x, int y, int & 2) ars xy z= ary; y coutcexee'#/ ceyes'#! cenecend) } void main () int as 2, be 5: EduSuccess Smart Learning Centers | www.edusuecessin | Contact # +91- 85 0606 1616 / 5252 (C+ REVISION TOUR demo (: sa, a, b) demo (: : a, a, B) TOPIC-4 Structures Quick Review Ans. 3°5* 10 14) 8°10" 20 14] ICBSE SQP 2013] Collection of logically elated different data types (Primitive and Derived) referenced under one name. ‘2g. Struct employee ( int empno; char name [30]; char design [20]; char department [20]; Declaration : employee ¢: InpulOutput : cin>>e.empno;//members are accessed using dot() cout<>e.ad.house no; members are accessed using dot () operator cout << ead houseno; Short Answer Type Questions-II [3 marks each] Q.1. Find the output ofthe following program [eth 20111 Hincludeciostream.h» void ChangeArray(int number, int ARR (1, int size) for (int L if (Lenumber) ARRIL]+ = Li 0; EeSize; Lee) eles BRR(L]* = Ly ) void show (int ARR[], int size) 7 LeSize: Le+) a: coute void SwitchOver(int A [ 1, int N, int split) { for (int K = 0) KeN; Kes) if (Keeplit) AIK] +=K; elee AIK *=K; ) void Display (int All, int ») { for (int K=O; Kelty Ke4) (K¥2-~0) ?cout struct POINT (ane mY) 2s} void Stepin(POINT &P, int Step=1) [Delhi, 2010} P.zaestep; void stepout (POINTEP, int ste; EduSuccess Smart Learning Centers | www.edusuecessin | Contact # +91- 85 0606 1616 / 5252 (C+ REVISION TOUR P.x--Step; void main () { POINT pi-{15, 25, 5}, P2={20, 30, 20}; steptn (Pi): Stepour (P2, 4); Zecendl coutccP2.Xee¥, "eeP2. Yee" Zecond Stepin (P2, 12); Zecendl; ) ‘Ans. Output: 15,24,6 6,34, 16 18,22, 28 (1 marke for each line of output) ICBSE Marking Scheme 2010] Q.6. Find the output of te following program ICBSE Comptt, 2009] #includeciostream.hs int Year float Topper; int x = 20) {8 -> Topper = (8 - > Topper + 25) - 8 -> Year 44; } void main () score arr t= { (2007, 100}, (2008, 95}): Score * Point. = Ares cnange (Posnt, 50) cout << Arr [0]. Yearce + Year <« # cePoint = > Topper << endl; ) Know the Links Download Chapter EduSuccess/ Ans. Output: 2008 # 75, 2009 # 100 (1 mark foreach correction) Q.7. Find the output ofthe following program : # include THREE_D (ane x, x 2a} a void Moveln (THREE_D & T, int 1) 7 : T : TY i 1 void THREE_D Ti=(10, 20, 5}, 12 = (30, 10, 40) Movern (72) couteeT1. Kee Zecendl ; CouteeT2. Kee", "eeT2. Yee", "eT? Zecendi ; Movern (12, 10) coutecT2.Kee zecend Ans. 11, 19,6 25, 15,35, 35,545 (1 Mark foreach line with correct values) oR (4 Mark for any two correct values in each line) Note: Deduct % Mark if any/all’ missing Deduct 6 Mark if en is not considered at the right positions ceT2.¥ee", "eet? e notes, study materials, Ten Year Solved Papers, Important Questions of Physics, Chemistry, Maths, Biology, Computer Science from www.edusuccess.in EduSuccess Smart Learning Centers | www.edusuecessin | Contact # +91- 85 0606 1616 / 5252

You might also like