1stpu

Embed Size (px)

Citation preview

  • 8/10/2019 1stpu

    1/30

    CHAPTER 06OBJECT ORIENTED CONCEPTS

    One Mark Answer Questions1. Define Structure programming.Ans. Structured programming, sometimes known as modular programming, is a subset of proceduralprogramming that enforces a logical structure on the program being written to make it more efficient and easierto understand and modify.

    2. Define top down design.Ans. Top down design involves dividing a problem into sub problem and further dividing the sub problem into

    smaller sub problems until it leads to sub-problems that can be implemented as program statements.

    3. What is bottom up approach?Ans.Bottom up approach is the vice versa where solutions to smaller modules are integrated together to find

    the solution of the overall program.

    4. Define an object.Ans. Object is an identifiable entity with some characteristics and behavior.

    5. What is object oriented programming (OOP)?Ans. OOP is a concept that combines both the data and functions that operate on that data into a single unit

    called the object.

    6. What is a class?Ans. A class is a template that represents a group of objects which share common properties and relationships.

    7. What is modularity?Ans. Modularity is a technique adopted to divide a complex problem into a number of self-contained

    Independent program segments.

    8. What is abstraction?Ans. Abstraction is an act which represents the essential features of an entity without including explanations or

    any background details about it.

    9.

    What is data encapsulation?Ans. Data Encapsulation: The wrapping of data and functions into a single unit called as class known asencapsulation.

    10. What is inheritance?Ans. Inheritance is the process by which objects of one class acquires the properties of the objects of another

    class.

    11. What is polymorphism?Ans. Polymorphism is the ability for a message to be processed in more than one form.

    12. What is Dynamic binding?Ans. Dynamics Binding means that the code associated with a given procedure call is not known until the time of

    the call at run-time. It is associated with polymorphism and inheritance.

  • 8/10/2019 1stpu

    2/30

    13. What is message passing?Ans.Message passing means a request for execution of procedure for an object. Therefore a message invokes aprocedure in the receiving object to generate the desired output.

    14. Mention any one benefit of OOP. OOPs model the real world entities very well.

    15. Mention any one advantage of OOP. Inheritance extends the use of existing classes and eliminates redundant code hence support code

    reusability.

    16. Mention any one disadvantage of OOP. OOPs use tricky methods to do the programming.

    17. Mention any one application of OOP. Parallel programming and neural networks

    Two Marks Answer Questions1. Differentiate between structure programming and object oriented programming.Ans. Structured programming, sometimes known as modular programming, is a subset of proceduralprogramming that enforces a logical structure on the program being written to make it more efficient and easierto understand and modify.OOP is a concept that combines both the data and functions that operate on that data into a single unit calledthe object.

    2. Mention any two characteristics of OOP.Ans. Portability

    Flexibility

    3. Briefly explain about data encapsulation.Ans. Data Encapsulation : The wrapping of data and functions into a single unit called as class known asencapsulation. The concept of insulating the data from direct access by the program is called data hiding.4. Briefly explain about Inheritance.Ans. Inheritance is the process by which objects of one class acquires the properties of the objects of anotherclass.

    5. Briefly explain about data polymorphism.Ans. Polymorphism is the ability for a message to be processed in more than one form.

    The process of making an operator to exhibit different behaviors in different instances is known asoperator overloading. Using a single function name to perform different types of tasks is known as functionoverloading.

    Three Marks Answer Questions1. Explain in detail any four characteristics of OOP.Ans. Module is a logically self-contained unit that can be tested and executed independently.

    Modularity is a technique adopted to divide a complex problem into a number of self-containedIndependent program segments.Abstraction is an act which represents the essential features of an entity without including explanations orany background details about it.

  • 8/10/2019 1stpu

    3/30

    Data Encapsulation : The wrapping of data and functions into a single unit called as class known asencapsulation. The concept of insulating the data from direct access by the program is called data hiding.Inheritance is the process by which objects of one class acquires the properties of the objects of another class.

    2. Explain in detail about data Encapsulation and inheritance.Ans.Data Encapsulation : The wrapping of data and functions into a single unit called as class known as

    encapsulation. The concept of insulating the data from direct access by the program is called data hiding.

    Inheritance is the process by which objects of one class acquires the properties of the objects of another class.3. Explain in detail about polymorphism and dynamic binding.Ans. Polymorphism is the ability for a message to be processed in more than one form.

    The process of making an operator to exhibit different behaviors in different instances is known as operatoroverloading. Using a single function name to perform different types of tasks is known as function overloading.Dynamics Binding : Binding means linking of a procedure call to the code to be executed in response to the call.Dynamics Binding means that the code associated with a given procedure call is not known until the time of the callat run-time. It is associated with polymorphism and inheritance.

    4. Briefly explain about dynamic binding and message passing.Ans. Binding means linking of procedure call to the code to be executed in response to the call.Dynamic binding : means that code associated with a given procedure call is not known until the time of callat the run time. It is associated with polymorphism and inheritance.Message passing means a request for execution of procedure for an object. Therefore a message invokes aprocedure in the receiving object to generate the desired output.

    5. Mention any two advantages and disadvantages of OOP.Advantages:

    OOPs model the real world entities very well.

    Multiple instances of an object co exist without any interference.Disadvantages:

    OOPs use tricky methods to do the programming. Proper planning and design is required before programming using OOPs technique.

    6. Mention any four benefits of OOP.Ans. Refer question No. 7

    7. Mention any four advantages of OOP. OOPs model the real world entities very well. Inheritance extends the use of existing classes and eliminates redundant code hence support code

    reusability. Data hiding helps to build secure programs that cannot be invaded by code in other parts of the program. Multiple instances of an object co exist without any interference.

    8. Mention any four applications of OOP. Objects oriented databases. Hypermedia, expert text and hypertext. Artificial intelligence and expert systems

    Decision support systems and office automation systems

    Two Marks Answer Questions

  • 8/10/2019 1stpu

    4/30

    1. What is OOP? Describe in detail the various characteristics of OOP with suitable examples.2. Mention the various advantages and two disadvantages of OOP.

    Advantages: OOPs model the real world entities very well. Inheritance extends the use of existing classes and eliminates redundant code hence support code

    reusability.

    Data hiding helps to build secure programs that cannot be invaded by code in other parts of the program. Multiple instances of an object co exist without any interference. The work can be divided easily since the projects are object based and each object can be developed by

    different resulting in faster code development.Disadvantages:OOPs use tricky methods to do the programming.Proper planning and design is required before programming using OOPs technique.

    3. Mention the various applications of OOP.Ans. The most popular application of OOPs has been in the area of user interface design of Windows operatingsystem. Some of the areas of OOPs application

    Objects oriented databases. Hypermedia, expert text and hypertext. Artificial intelligence and expert systems Decision support systems and office automation systems Parallel programming and neural networks CAD, CAM, CIM system. Simulation and modeling.

    4. Mention the various benefits of OOP.Ans. OOPs is indeed a very beneficiary tool which help in efficient programming, because the very idea of usingOOPs technique was to overcome the drawbacks of procedural and structure programming approach.The major advantage/benefits of OOPs are:

    OOPs model the real world entities very well. Inheritance extends the use of existing classes and eliminates redundant code hence support code

    reusability. Data hiding helps to build secure programs that cannot be invaded by code in other parts of the program. Multiple instances of an object co exist without any interference. The work can be divided easily since the projects are object based and each object can be developed by

    different resulting in faster code development. OOPS can be easily upgraded from small to large systems Complexity of the software is easily manageable, tested and maintained.

  • 8/10/2019 1stpu

    5/30

    Chapter 7Introduction to C++

    One Mark Answer Questions.1. Who developed C++?Ans. Bjarne Stroustrup developed C++ at AT& T, Bell laboratories in Murray hill, New jersey.

    2. C++ is a subset of C. True or False?Ans. True.

    3. Mention any two characteristics of C++. Object Oriented Programming Machine Independent

    4. Define token.

    Ans. A token is smallest individual unit in a program or a lexical unit.

    5. Mention any two tokens of C++ Identifiers Keywords

    6. What is an identifier?Ans. An identifier is a name given to the programming elements such as variable, arrays, functions etc.it can contain letter digits and underscore.

    7. Keyword can be used to name an identifier. True or False?Ans. False

    8. Mention any two keywords in C++. New Extern Enum Int Float

    9. What is a keyword?Ans. Keyword is a predefined word and has special meaning to the complier. The programmer is notallowed to change its meaning.

    10. What is a constant?Ans. constant is fixed value that does not change during the execution of program.

    11. What is an integer constant?Ans. integer constant are constants that have no fractional parts or exponent. They always begin with adigit.

  • 8/10/2019 1stpu

    6/30

    12. What is an octal constant?Ans. An octal constant is a constant which should always begin with 0 followed by a sequence of digitsin the range 0 through 7.

    13. What is Hexadecimal constant?Ans. An hexadecimal constant is a constant which should always begin with 0x or 0X followed by the

    sequence of digits in the range 0 through 7, and A to F (which represents digits from 10 to 15)

    14. Give an example of integer constant.Ans. int a; int a=1587;

    15. Give an example of octal constant.Ans. int a=0123;

    16. Give an example of hexadecimal constant.

    Ans.int a=0x5ABD

    17. Give an example of float constant.Ans. float a; float = 18.3 example: 23.46e0 :-is equal to 23.46X1=23.46

    18. Give an example of character constant.Ans. char ch=p; // specify normal character constant

    19. Give an example of string constant.

    Ans. char str*10+= computer;

    20. What are escape sequence?Ans. Escape sequence is a special string used to control output on the monitor and they arerepresented by a single character and hence occupy one byte space.

    21. Mention any two escape sequence. \n :- new line \t :- horizontal tab.

    22. What are punctuators?Ans. Punctuators in C++ have syntactic and semantic meaning to the complier but to not by themselvesspecify an operation that yields a value.

    23. Mention any two punctuators. ! :- used along with = to indicate not equal to ; used to represent statement terminator.

    24. What is an operator?Ans. An operator is a symbol that tells the compiler to perform specific mathematical or logicalmanipulations.

  • 8/10/2019 1stpu

    7/30

    An operator is a symbol that represents an operation to the complier.

    25. What are unary operators?Ans. unary operations have only one operand they are evaluated before any other operationscontaining them gets evaluated.

    26. Mention any two unary operators. ++ Increment. is used to increment an operand by 1 -- Decrement. Is used to decrement an operand by 1

    27. Give the output for the expression y=++x-x++ if x=10.Ans. x=11

    28. What are binary operators?Ans. The binary operators are those operators that operate on two operands.

    29. Mention any two binary operators. Arithmetic operators Relational operators

    30. Mention the operators supported by arithmetic operators.Ans. +, - , x, /, %.

    31. Mention the operators supported by relational operators.Ans. ==, >, =,

  • 8/10/2019 1stpu

    8/30

    38. What is operator precedence?Ans. operator precedence determines the grouping of terms in an expression.

    39. Convert the expression 2\/a 2 + b 2 Ans. can be written as 2*sqrt(a*a+b*b)

    40. Convert the expression

    Ans. can be written as A= 3.142*r*r

    41. Convert the expression a 2 + b 2 +b2 = c 2 into equivalent C++ expressionAns. Can be written as a*a+b*b+b*b=c*c

    42. What is type casting?Ans. Converting an expression of a given type into another type is known as type-casting or typeconversion.

    43. Mention types of type casting. Implicit Explicit

    44. Mention any two math .h functions Sin(x) Cos(x)

    45. Mention any two ctype .h functions Isalpha(c) Isdigit(c)

    46. Mention any two functions of stdio.h Getchar() : it returns a single character from a standard input device (keyboard). It takes no

    parameter and retruned value is the input character. Putchar(): it takes one argument, which is the character to be sent to output device. It also

    returns this character as result.

    47. Mention any two functions stdlib .h Randomize(): it initializes the random number generator with a random number. Random(n): it generates a random number between 0 to n-1.

    48. Mention any two string .h functions strlen(s): calculate length of a string strcat(s1,s2): concatenate two strings.

    49. Differentiate between getch() and getche(). getch(): it does not echo the character on the screen getche(): it echoes the character on the screen

  • 8/10/2019 1stpu

    9/30

    Two marks answer questions.

    1. Mention any two characteristics of C++ . Modular programming Machine independent

    2. Mention any four tokens of C++ . Identifiers Keywords Constants or literals Punctuators

    3. Mention any two rules for naming an identifier. Identifiers are sequence of characters which should begin with the alphabet either from A-Z or

    a-z or _underscore.

    No special character is allowed except _(underscore)

    4. Mention the types of constants in C++ Integer constant Floating constant Character constant String literal

    5. Explain integer constant with suitable example.Ans. Integer constants are constant that have no fractional parts or exponents. They always begin witha digit. We can specify integer constants in decimal, octal or hexadecimal form. They can specify signedor unsigned types and long or short types. Example int a =1587; // decimal constant, int b =-148; // anegative decimal constant.

    6. Explain octal constant with suitable example.Ans. when we specify an octal constant, we should always begin with 0 followed by a sequence of digitsin the range 0 through 7. The digits 8 and 9 are errors in specifying an octal constant. Example int a

    =0374; // octal constant.

    7. Explain hexadecimal constant with suitable example.Ans when we specify a hexadecimal constant, we should always begin the specification with 0x or 0X followed by a sequence of digits in the range 0 through 9 and a or A through F are alphabetsrepresents values from 10 to 15) example: int a= 0x5fff; int b =-0X3FFF;

    8. Explain float constant with suitable example.Ans. Floating point constant are also called as real constants. These values contain decimal point(.)

    and can contain exponents they are used to represent values that will have a fractional part and can berepresented in two forms(i.e., fractional form and exponent form).

  • 8/10/2019 1stpu

    10/30

    9. Explain character constant with suitable example.Ans. characters constant are specified as single character enclosed in pair of single quotation marks forexample: char ch=p;

    10. What are escapes sequences? Mention any two escapes sequences.Ans.A class of characters which cannot be displayed on the screen but have special meaning to the

    compiler and are used for special purpose. For example tab key, carriage return, backspace suchcharacters are called escape sequences.

    \0 : null character \n : line feed new line

    11. Explain string constant with suitable example.Ans. a string consist of zero or more characters enclosed by double quotation marks () multiplecharacter constants are called as string constant and they are treated as an array of char. Ex: charstr*15+=c programming;

    12. Explain unary operators with suitable example.Ans.unary operations have only one operand they are evaluated before any other operationscontaining them gets evaluated. Example:

    13. Explain arithmetic operators with suitable example.Ans. The operators supported by C++ are +, -, *, /, %

    Let us assume that variable a=10 and variable b=20 thenOperator description Example

    + Adds two operand a+b will give 30- Subtract second operand from first a-b will give -10* Multiply both operands a*b will give 200/ Divide numerator by denominator b/a will give 2% Modulus operator and remainder of

    after integer division b%a will give 0

    14. Explain logical operators with suitable example.Ans. The logical operators supported by C++ are &&, || and !

    Let us assume that variable a=0 and variable b=1 thenOperator description Example

    && Called as logical AND operator. If both the operandsarenon zero then the condition becomes true. (a&&b) is false

    || Called as logical OR operator. If both the operandsare non zero then the condition becomes true. (a||b)is true

    ! Called as Logical NOT operator. Use to reverses theLogical state of its operand. If a condition is true !(a&&b) is trueThen logical NOT operator will make it false. !(a) is true. !(b) is false

  • 8/10/2019 1stpu

    11/30

    15. Explain bitwise operator with suitable example.Ans. Bitwise operator works on bits and performs bit by bit operation. The truth table for bitwise and(&), bitwise or (|), and bitwise xor (^) are as follows.

    A B A&B (bitwise and) A|B (bitwise or) A^B (bitwise xor)0 0 0 0 00 1 0 1 11 1 1 1 01 0 0 1 1

    16. Explain the type conversion briefly.Ans. converting an expression of a given type into another type is known as type-casting or typeconversion. Type conversions are of two types.

    Implicit Conversion Explicit Conversion

    17. Give an example of simple C++ program.Ans. #include

    void main(){

    Cout

  • 8/10/2019 1stpu

    12/30

    3. Summarize the rules of naming an identifier. Identifiers are a sequence of characters which should begin with alphabet either A-Z(

    uppercase) a-z(Lowercase) of_ underscore. C++ treats uppercase and lowercase letter differently for example: DATA is not same as data. No special character is allowed except _(underscore). A keyword cannot be use for naming an identifier.

    Identifier should be of reasonable (but ANSI C bot not enforced any restriction on this aspect).

    4. Explain about integer constant with suitable examplesAns. Integer constants are constant that have no fractional parts or exponents. They always begin witha digit. We can specify integer constants in decimal, octal or hexadecimal form. They can specify signedor unsigned types and long or short types. Example int a =1587; // decimal constant, int b =-148; // anegative decimal constant.

    5. What are escapes sequences? Mention any four escapes sequences.

    Ans. A class of characters which cannot be displayed on the screen but have special meaning to thecompiler and are used for special purpose. For example tab key, carriage return, backspace suchcharacters are called escape sequences.

    \0 : null character \n : line feed new line \b : back space \r: carriage return

    6. Explain any four unary operators.Operator Name

    ! Logical NOT. Use to reserves the logical state of its operand if a condition is true thelogical NOT will make it false.

    & Address of. Used to give the address of operand++ Increment. Used to increment an operand by 1-- Decrement. Used to decrement an operand by 1

    7. What is an expression? Explain the precedence of operators with suitable examples.Ans. An expression is a combination of opcode and operands.

    If the expression contains multiple operators, the order in which operations are carried out iscalled precedence of operators. It is also called as priority or hierarchy.

    Example: x= 7+3*2;Here x is assigned 13, not 20 because operator * has higher precedence than + so it first getmultiplied with 3*2 and then adds into 7.

    8. Explain implicit and explicit type conversions with suitable examples Implicit conversion: do not require any operator. They are automatically performed when a

    value is copied to a compatible type. Example : short a =2000; int b; b=a Explicit conversion: C++ is a strong typed language. Many conversion, especially those thatimply a different interpretation of the value, require an explicit conversion.Example : short a =2000;

  • 8/10/2019 1stpu

    13/30

    Int b;b=(int)a; //c-like cast notationb=int(a); // functional notationExplicit conversion is performed by the user as his need.

    9. Explain detail structure of C++ program.

    Ans.//simple c++ program #include // header file includedusing namespace std;

    int main(){

    int a=10,b=34;cout

  • 8/10/2019 1stpu

    14/30

    Body of main() Section : The body of the main() function begins with . Local Variable Declaration : In this the variables which are used in the body o f the main() functions

    are declared. These are called the local variables as their scope is limited within the main() functiononly, unless they are declared globally outside the main() function. int a=10, b=34; in the aboveprogram represents local variables

    Statements to Execute : This section includes statements for reading, writing and executing data

    using I/O functions, library functions, formulas, conditional statements etc. Above written programhas many executable statements like cout

  • 8/10/2019 1stpu

    15/30

    code. In addition it allows a greater reusability of code in a more logical and productive way.

    Portability: You can practically compile the same C++ code in almost any type of computer and operating systemwithout making any changes. C++ is the most used and ported programming language in the world.

    Brevity: Code written in C++ is very short in comparison with other languages, since the use of specialcharacters is preferred to key words, saving some effort to the programmer (and prolonging the life ofour keyboards!).

    Modular programming: An application's body in C++ can be made up of several source code files that are compiled separatelyand then linked together. Saving time since it is not necessary to recompile the complete applicationwhen making a single change but only the file that contains it. In addition, this characteristic allows to

    link C++ code with code produced in other languages, such as Assembler or C.

    C Compatibility: C++ is backwards compatible with the C language. Any code written in C can easily be included in a C++program without making any change.

    Speed: The resulting code from a C++ compilation is very efficient, due indeed to its duality as high-level andlow-level language and to the reduced size of the language itself.

    2. Explain the various tokens in C++ Identifiers:An identifier is a name given to the programming elements such as variable, arrays,

    functions etc. it can contain letter digits and underscore.Identifiers are sequence of characters which should begin with the alphabet either from A-Z ora-z or _underscore.No special character is allowed except _(underscore) example: int student , where variablestudent is an identifier

    Keywords:Keyword is a predefined word and has special meaning to the complier. The programmeris not allowed to change its meaning. Eg: int, long, enum, float etc.

    Constants or literals :constant is fixed value that does not change during the execution of program.Types of constants are:

    Punctuators:Punctuators in C++ have syntactic and semantic meaning to the complier but to not bythemselves specify an operation that yields a value.

    Types: !used along with = to indicate not equal to ; used to represent statement terminator.

    http://www.booksamillion.com/p/C-Programming/Jonas-Fagerberg/9781494208394http://www.booksamillion.com/p/C-Programming/Jonas-Fagerberg/9781494208394
  • 8/10/2019 1stpu

    16/30

    Operators : An operator is a symbol that tells the compiler to perform specific mathematical orlogical manipulations.

    An operator is a symbol that represents an operation to the complier

    3. Explain the constants in C++ with suitable examples. Integer constants are constant that have no fractional parts or exponents. They always begin with a

    digit. We can specify integer constants in decimal, octal or hexadecimal form. They can specifysigned or unsigned types and long or short types. Example int a =1587; // decimal constant, int b =-148; // a negative decimal constant.

    Octal constant: when we specify an octal constant, we should always begin with 0 followed by asequence of digits in the range 0 through 7. The digits 8 and 9 are errors in specifying an octalconstant. Example int a =0374; // octal constant.

    hexadecimal constant when we specify a hexadecimal constant, we should always begin the

    specification with 0x or 0X followed by a sequence of digits in the range 0 through 9 and a or A through F are alphabetsrepresents values from 10 to 15) example: int a= 0x5fff; int b =-0X3FFF;

    Floating point constant are also called as real constants. These values contain decimal point(.) andcan contain exponents they are used to represent values that will have a fractional part and can berepresented in two forms(i.e., fractional form and exponent form). Example : float a=5.5;

    characters constant are specified as single character enclosed in pair of single quotation marks forexample: char ch=p;

    String constant: A string consist of zero or more characters enclosed by double quotation marks ()multiple character constants are called as string constant and they are treated as an array of char.Ex: char str*15+=c programming;

    4. What are escape sequences? Explain the various escape sequences.Ans.A class of characters which cannot be displayed on the screen but have special meaning to thecompiler and are used for special purpose. For example tab key, carriage return, backspace suchcharacters are called escape sequences.

    \0 : null character \n : line feed new line \ : Single quote \r : Carriage return \t : Horizontal tab \\ : Back slash

    5. What are binary operators? Explain the various binary operators with suitable examples.Ans. The binary operators are those operators that operate on two operands.

    The various binary operators are:

  • 8/10/2019 1stpu

    17/30

    Arithmetic Operator: The operators supported by C++ are +, -, *, /, %Let us assume that variable a=10 and variable b=20 then

    Operator description Example+ Adds two operand a+b will give 30- Subtract second operand from first a-b will give -10* Multiply both operands a*b will give 200

    / Divide numerator by denominator b/a will give 2% Modulus operator and remainder of

    after integer division b%a will give 0

    logical operators The logical operators supported by C++ are &&, || and !Let us assume that variable a=0 and variable b=1 then

    Operator description Example&& Called as logical AND operator. If both the operands

    are non zero then the condition becomes true. (a&&b) is false

    || Called as logical OR operator. If both the operandsare non zero then the condition becomes true. (a||b)is true

    ! Called as Logical NOT operator. Use to reverses theLogical state of its operand. If a condition is true !(a&&b) is trueThen logical NOT operator will make it false. !(a) is true. !(b) is false

    Bitwise operator works on bits and performs bit by bit operation. The truth table for bitwise and(&), bitwise or (|), and bitwise xor (^) are as follows.

    A B A&B (bitwise and) A|B (bitwise or) A^B (bitwise xor)0 0 0 0 00 1 0 1 11 1 1 1 01 0 0 1 1

    assignment operator. : += example: c+= a is equivalent to c=c+a sizeof operator: sizeof: operator returns the size of a variable. For example sizeof(a), where a is integer

    and will return 2.

    6. Explain unary and ternary operators with suitable examplesAns. unary operations have only one operand they are evaluated before any other operationscontaining them gets evaluated.Example :

    ++ Increment. is used to increment an operand by 1 -- Decrement. Is used to decrement an operand by 1

    Ternary operators are those operators that operate on three or more operands. And is also called asconditional operator.Example : ? conditional operator. If condition is true expression in the TRUE is selected. Otherwiseexpression in the FALSEis selected.

    7. Explain the precedence of operator with suitable examples.Ans.If the expression contains multiple operators, the order in which operations are carried out iscalled precedence of operators. It is also called as priority or hierarchy.

  • 8/10/2019 1stpu

    18/30

    Example: x= 7+3*2;Here x is assigned 13, not 20 because operator * has higher precedence than + so it first getmultiplied with 3*2 and then adds into 7.

    8. Explain detail structure of C++ program with suitable programing example.//simple c++ program

    #include // header file includedusing namespace std;

    int main(){

    int a=10,b=34;cout

  • 8/10/2019 1stpu

    19/30

    Statements to Execute : This section includes statements for reading, writing and executing datausing I/O functions, library functions, formulas, conditional statements etc. Above written programhas many executable statements like cout

  • 8/10/2019 1stpu

    20/30

    One Mark questions and answers1. Define variables.Ans. A variable is an object or element and it is allowed to change during the execution of the program.Variable represent name of the memory locations.

    2. Give the syntax of declaring a variable

    Ans. datatypevariable_name

    3. Give the syntax of initilizing a variableAns. datatypevariable_name= value

    4. Differentiate lvalue and rvalueAns. lvalue is the location value. For example location of variable p =2000;rvalue is the data value. For example data u input in to variable p=100;

    5. Mention data types of C++. Fundamental Derived And user defined data types

    6. Mention the types of modifiers Signed Unsigned Long Short

    7. What are derived data typesAns. Those data types which are constructed using simple or fundamental data types. Such as arrays,function, pointers

    8. What are user defined datatypesAns. These data types are also constructed using simple or fundamental data types. Such as structure,

    union, class enumerated.

    9. What is an enumerated data type?Ans. An enumerated is a user defined type consisting of a set of named constants called enumerators.

    Two Marks questions and answers1. What is a variable and give its declaration? Ans. A variable is an object or element and it is allowed to change during the execution of the program.Variable represent name of the memory locations.

    Syntax or declaration:datatypevariable_name

    2. Explain lvalue and rvalue with an example

  • 8/10/2019 1stpu

    21/30

    Ans. lvalue is the location value.it holds memory address location For example if variable p is assignedwith a value p=100 then the address location of variable p =2000;rvalue is the data value. it holds the value assigned by the user For example data u input in to variablep=100;

    3. What is a data type and mention the different data types Ans. Data type can be defined as the set of values which can be stored in a variable along with theoperations that can be performed on those values

    Data type means to identify the type of data and associated operations of handling it. Theclassification of data types include :

    1. Simple and fundamental data types2. Complex or derived data types3. User defined data types

    4. Explain the various data types. 5. What are modifiers and mention the different modifiers Ans. A modifier is used to alter the meaning of the base type so that it more precisely fits the needs ofvarious situations.Different type of modifiers are:

    Signed Unsigned Long Short

    6. What is an enumerated data type give an example. Ans.An enumeration is a user defined data type consisting of a set of named constants called enumerators.Enum is a keyword that assings values 0,1,2 automatic ally. This helps in providing an alternative meansfor creating symbolic constants example: enum choice{good, bad, excellent} // definition of enumerated type

    choicemychoice; // decalartion of variable type.

    Three Marks questions and answers1. Explainthe variables in detail Ans. variables are generally declared in the declaration section of the program and their values can bemanipulated during program execution.

    A variable is an object or element and it is allowed to change during the execution of the program.Variable represent name of the memory locations.

    The syntax of declaring a variable is: datatypevariable_name, Example inta,b,c;The syntax of initializing a variable is: datatypevariable_name= value, Example float a=6.00;

    There are two values associated with a variable lvalue and rvalue.lvalue is the location value.it holds memory address location For example if variable p is assigned

    with a value p=100 then the address location of variable p =2000;rvalue is the data value. it holds the value assigned by the user For example data u input in to variablep=100;

  • 8/10/2019 1stpu

    22/30

    invalid variables are : should not start with digit, should not have space etc

    2. Explain the data types and its classification Ans. Data type can be defined as the set of values which can be stored in a variable along with theoperations that can be performed on those values

    classification of data types include :1. Simple and fundamental data types2. Complex or derived data types3. User defined data types

    3. Explain the modifiers in detail Ans. A modifier is used to alter the meaning of the base type so that it more precisely fits the needs ofvarious situations.Different type of modifiers are:

    Signed Unsigned

    Long Short The modifiers singed, unsigned,long and short can be applied to integer base types. And also signed

    and unsigned can be applied to char, where as long can be applied to double.

    4. Explain enumerated data types with suitable examples Ans. .An enumeration is a user defined data type consisting of a set of named constants calledenumerators. Enum is a keyword that assings values 0,1,2 automatically. This helps in providing analternative means for creating symbolic constants example: enum choice{good, bad, excellent} // definition of enumerated type

    choicemychoice; // decalartion of variable type.

    C++ Data types

    Fundamental data types

    Int eg 100

    char eg 'c'

    float eg 99.99

    Derived data types

    arrays eg a[100]

    functions

    poniters eg int *ptr

    User defined Data types

    structure

    union

    class

    enumerated

  • 8/10/2019 1stpu

    23/30

    5. Give the C++ enum type conversion rules There is an implicit conversion from any enum type to int. suppose this type exists

    enumMyenumType{ALPHA,BETA,GAMMA}Then the following lines are legalint i=BETAint j=3+GAMMA

    On the other hand, C ++ does not supported an implicit conversion from int to an enum type. thistype conversion is always illegal

    Five Marks questions and answers1. Explain the variables in detail with suitable examplesAns. variables are generally declared in the declaration section of the program and their values can be

    manipulated during program execution.A variable is an object or element and it is allowed to change during the execution of the program.

    Variable represent name of the memory locations.The syntax of declaring a variable is: datatypevariable_name, Example inta,b,c;The syntax of initializing a variable is: datatypevariable_name= value, Example float a=6.00;

    There are two values associated with a variable lvalue and rvalue.lvalue is the location value.it holds memory address location For example if variable p is assigned

    with a value p=100 then the address location of variable p =2000;rvalue is the data value. it holds the value assigned by the user For example data u input in to variable

    p=100;invalid variables are : should not start with digit, should not have space etc

    2. What is a data type? Explain the classification of data types in detail. Ans. Data type can be defined as the set of values which can be stored in a variable along with theoperations that can be performed on those values

    Data type means to identify the type of data and associated operations of handling it. Theclassification of data types include :

    Simple and fundamental data types Complex or derived data types User defined data types

    1. Simple and fundamental data types include: Int.: integers are whole numbers without any fractional part. It include nos. such as 10, -10, 0 etc..it

    can be ve and +ve and range of storage is from -32768 to 32767, occupies two bytes of memoryspace. Possible operations can be performed are addition, subtraction, division, remainder.

    Char : its a character data type to store any character from basic character set. Its accepts singlecharacter at a time, it should be enclosed within single quote y it occupies 1 byte of memoryspace. Can store 256 characters.

    Float: this represent fractional part i.e real numbers such as, 10.3, 11.0000, -5.75 etc. it can alsohave positive and negative numbers. Can store -3.4e-38 to 3.4e38

    In derived we have arrays, functions, pointers which is derived from the fundamental datatypes

  • 8/10/2019 1stpu

    24/30

    User defined data type can be constructed from fundamental datatypes such structures union etc..

    3. Explain the user defined data type in detail. Ans.These data types are also constructed using simple or fundamental data types. Some of the user definedata types include: structure, union, class, and enumerated.

    We shall see the enumerated data type in detail.

    An enumeration is a user defined data type consisting of a set of named constants called enumerators.Enum is a ke yword that assings values 0,1,2 automatically. This helps in providing an alternative meansfor creating symbolic constants.

    The syntax for enum is as followsenum [tag] {enum list}; // for definition of enumerated typeenumtagdeclarator; for declaration variable of type tag

    example:enum choice{good, bad, excellent} // definition of enumerated typechoicemychoice; // decalartion of variable type.

    Chapter 9Input and Output Operators

    One Mark answer questions

    C++ Data types

    Fundamental data types

    Int eg 100

    char eg 'c'

    float eg 99.99

    Derived data types

    arrays eg a[100]

    functions

    poniters eg int *ptr

    User defined Data types

    structure

    union

    class

    enumerated

  • 8/10/2019 1stpu

    25/30

    1. Give the other name for cin()Ans. Stream Extraction (>>)

    2. Give the other name for cout()Ans. Stream insertion ( and cout) and extraction() on the cin stream. The operator must befollowed by the variable that will store the data that is going to be extracted from the stream.Example: int age;

    Cin>>age;

    2. Explain the output operator in C++Ans.output in C is done by using stream insertion operator( b>>c;

    cout

  • 8/10/2019 1stpu

    26/30

    Example: int age;Cin>>age;

    output in C++ is done by using stream insertion operator(

  • 8/10/2019 1stpu

    27/30

    Ans. #include#includeVoid main(){

    cout and cout) and extraction (a>>b>>c;

    cout

  • 8/10/2019 1stpu

    28/30

    1. What is compound statement?2. What is block?3. Name the types of control statements4. What are the three types of constructs?5. What is selection statement?6. What are the three different constructs7. What is the numerical equivalent of TRUE and FALSE?

    8. Which selection statement does C++ provide?9. What are the rules associated with IF statement?10. What is the purpose of else clause?11. What is two way branching statement?12. Correct the following code fragment

    If(x=1)K=100;

    ElseK=10;

    13. Consider the following program segmentX=10; y=5;

    If(x>y)w=15;W=25

    Cout

  • 8/10/2019 1stpu

    29/30

    35. Which header file must be included in the program for using the exit () function.

    Two marks question answers1. What is the purpose of if else statement ?2. What are the nested statements?3. Write syntax of if else stgatement?4. What is the puspose of switch statement?

    5. What are the case labels? What type of labels must be used in case labels?6. What will be the output of following code when i. A ii. C iii. D iv. F

    Cin>>choice;Switch(choice){

    Case A :cout

  • 8/10/2019 1stpu

    30/30