You are on page 1of 30

CHAPTER 06

OBJECT ORIENTED CONCEPTS


One Mark Answer Questions
1. Define Structure programming.
Ans. Structured programming, sometimes known as modular programming, is a subset of procedural
programming that enforces a logical structure on the program being written to make it more efficient and easier
to 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 as
encapsulation.
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.

13. What is message passing?


Ans.Message passing means a request for execution of procedure for an object. Therefore a message invokes a
procedure 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 Questions


1. Differentiate between structure programming and object oriented programming.
Ans. Structured programming, sometimes known as modular programming, is a subset of procedural
programming that enforces a logical structure on the program being written to make it more efficient and easier
to understand and modify.
OOP is a concept that combines both the data and functions that operate on that data into a single unit called
the 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 as
encapsulation. 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 another
class.
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 as
operator overloading. Using a single function name to perform different types of tasks is known as function
overloading.

Three Marks Answer Questions


1. 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-contained
Independent program segments.
Abstraction is an act which represents the essential features of an entity without including explanations or
any background details about it.

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.
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 operator
overloading. 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 call
at 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 call
at 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 a
procedure 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

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 operating
system. 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 using
OOPs 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.

Chapter 7
Introduction 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 not
allowed 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 a
digit.

12. What is an octal constant?


Ans. An octal constant is a constant which should always begin with 0 followed by a sequence of digits
in 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 are
represented 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 themselves
specify 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 logical
manipulations.

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 operations
containing 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. ==, >, <, >=, <=.
32. Mention the operators supported by logical operators.
Ans. &&, ||, !
33. Mention the operators supported by bitwise operators.
Ans. and(&), or(|), xor(^)
34. Give an example for assignment operator.
Ans. += example: c+= a is equivalent to c=c+a
35. Give an example of sizeof operator.
Ans. sizeof: operator returns the size of a variable. For example sizeof(a), where a is integer and will
return 2.
36. What are ternary operators?
Ans. Ternary operators are those operators that operate on three or more operands. And is also called
as conditional operator.
37. What is an expression?
Ans. An expression is a combination of opcode and operands.

38. What is operator precedence?


Ans. operator precedence determines the grouping of terms in an expression.
39. Convert the expression 2\/a2 + 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 a2 + b 2 +b2 = c 2 into equivalent C++ expression
Ans. 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 type
conversion.
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

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 with
a digit. We can specify integer constants in decimal, octal or hexadecimal form. They can specify signed
or unsigned types and long or short types. Example int a =1587; // decimal constant, int b =-148; // a
negative 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 digits
in 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 0
X followed by a sequence of digits in the range 0 through 9 and a or A through F are alphabets
represents 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 be
represented in two forms(i.e., fractional form and exponent form).

9. Explain character constant with suitable example.


Ans. characters constant are specified as single character enclosed in pair of single quotation marks for
example: 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 such
characters 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 () multiple
character constants are called as string constant and they are treated as an array of char. Ex: char
str*15+=c++ programming;
12. Explain unary operators with suitable example.
Ans.unary operations have only one operand they are evaluated before any other operations
containing 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 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
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 then
Operator
description
&&
Called as logical AND operator. If both the operands
arenon zero then the condition becomes true.
||
Called as logical OR operator. If both the operands
are non zero then the condition becomes true.
!
Called as Logical NOT operator. Use to reverses the
Logical state of its operand. If a condition is true
Then logical NOT operator will make it false.

Example
(a&&b) is false
(a||b)is true
!(a&&b) is true
!(a) is true. !(b) is false

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
0
0
1
0
1
1
1
1
1
1
0
1
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 type
conversion. Type conversions are of two types.
Implicit Conversion
Explicit Conversion
17. Give an example of simple C++ program.
Ans. #include<iostream.h>
void main()
{
Cout<< This is first C++ Program;
}
Sample Run: This is first C++ Program
Three Mark answer questions
1. Mention any four characteristics of C++
Object oriented programming
Portability
Brevity
Modular programming
2. Explain about character set and tokens in C++.
Ans. character set means the valid set of characters that a language recognizes. The character set
includes letter, digits or any other sign. The following are the character set in C++.
Digits
: 0-9
Alphabets
: a-z, A-Z ( Both upper and lower case)
Special characters
: +,-,{},(),!,@,#,$,%,^,*, blank space (these characters are building
blocks and they form the basic program elements)
Token: A token is a smallest individual unit in a program or lexical unit. C++ has following tokens
Identifiers
Keywords
Constants
Literals
Punctuators
Operators

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 examples
Ans. 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 specify signed
or unsigned types and long or short types. Example int a =1587; // decimal constant, int b =-148; // a
negative 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 the
compiler and are used for special purpose. For example tab key, carriage return, backspace such
characters 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 the
logical 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 is
called 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 get
multiplied 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 that
imply a different interpretation of the value, require an explicit conversion.
Example : short a =2000;

Int b;
b=(int)a; //c-like cast notation
b=int(a); // functional notation
Explicit conversion is performed by the user as his need.
9. Explain detail structure of C++ program.
Ans.//simple c++ program
#include<iostream> // header file included
using namespace std;
int main()
{
int a=10,b=34;
cout<<"simple c++ program \n"; // c++ statement
cout<<"hello world";
cout<<a<<b;
return 0; // returning no errors
}
The basic structure of c++ program mentioned above can be divided into following sections:

Documentation Section : This section comprises of comments. As the name suggests, this section is

used to improve the readability and understanding of the program.// (Double Slash)
represents comments in C++ program. Comments can be of single line or multiple lines. Double
Slash comments are used to represent single line comments. For multiple line comment, you can
begin with /* and end with */. For example :
In the above C++ program //simple c++ program represents single line comment.

Linking and Directives Section : The program written above begins with
#include<iostream>.<iostream> represents header file which includes the functionalities of
predefined functions. In linking section, the compiler in-built functions such as cout<<, cin>>etc are
linked with INCLUDE subdirectorys header file <iostream>. The #symbols tells about address to
or link to. Iostream is input/output stream which includes declarations of standard input-output
library in c++.

main() Section : This is the section in which the program coding is written. Basically, it acts as a
container for c++ program. The execution of the c++ program begins with main() function and it is
independent of the location of main() function in the program. main() is a function as represented
by parenthesis (). This is because it is a function declaration. The body of the main() function can
be found right after these parenthesis, the body is enclosed in braces , -.

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() function
only, unless they are declared globally outside the main() function. int a=10, b=34; in the above
program 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 program
has many executable statements like cout<<simple c++ program \n;
return 0; in the above program causes the function to finish and 0 represents that function has been
executed with zero errors. This is considered as most usual way to end a C++ program.
Finally the body of the main() function ends with -.
Global Declaration Section : There are certain programs which requires variables that can be used
in more than one function, so then the variables can be declared outside the main() function or
respective functions. Then those variables become accessible in any of the functions, Hence named
as Global Variables as their scope becomes global to the program.
User Defined Functions : There are certain functions that are called by calling statements from the
main() function. Every function includes local variable declaration section and executable statement
section similar to main program.

10. Explain mathematical and character library functions.


Mathematical functions
Sin(x)
: sine of an angle x (measured in radians)
Cos(x)
: cosine of an angle x(measured in radians)
Tan(x)
: tangent of an angle x(measured in radians)
Character functions
Islower(c) : it returns true if C is a lower case letter otherwise False
Isupper(c) : it returns true if C is a upper case letter otherwise False
11. Explain string and stdlib.h functions.
String functions:
Strrev(s) : it converts a string s into its reverse
Strupr(s) : it converts a string s into its upper case
Strlwr(s) : it converts a string s into its lower case
Stdlib functions:
atoi(s)
: it converts a string into numerical representation.
Itoa(n)
: it converts a number to a string
Five Marks answer question
1. Who developed C++? Explain the characteristics in detail
Ans. Bjarne Stroustrup developed C++, At Bell Labs, starting in 1979
Characteristics of c++ are:
Object-oriented programming:
The possibility to orientate programming to objects allows the programmer to design applications from
a point of view more like a communication between objects rather than on a structured sequence of

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 system
without 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 special
characters is preferred to key words, saving some effort to the programmer (and prolonging the life of
our keyboards!).
Modular programming:
An application's body in C++ can be made up of several source code files that are compiled separately
and then linked together. Saving time since it is not necessary to recompile the complete application
when 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 and
low-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 or
a-z or _underscore.
No special character is allowed except _(underscore) example: intstudent, where variable
student is an identifier

Keywords:Keyword is a predefined word and has special meaning to the complier. The programmer
is 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 by
themselves specify an operation that yields a value.
Types:!used along with = to indicate not equal to
; used to represent statement terminator.

Operators : An operator is a symbol that tells the compiler to perform specific mathematical or
logical 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 specify
signed 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 a
sequence of digits in the range 0 through 7. The digits 8 and 9 are errors in specifying an octal
constant. Example int a =0374; // octal constant.

hexadecimal constant when we specify a hexadecimal constant, we should always begin the
specification with 0x or 0
X followed by a sequence of digits in the range 0 through 9 and a or A through F are alphabets
represents 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(.) and
can contain exponents they are used to represent values that will have a fractional part and can be
represented 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 for
example: 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 the
compiler and are used for special purpose. For example tab key, carriage return, backspace such
characters 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:

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 operands
are non zero then the condition becomes true.
(a||b)is true
!
Called as Logical NOT operator. Use to reverses the
Logical state of its operand. If a condition is true
!(a&&b) is true
Then 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
0
0
1
0
1
1
1
1
1
1
0
1
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 examples
Ans. unary operations have only one operand they are evaluated before any other operations
containing 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 as
conditional operator.
Example : ? conditional operator. If condition is true expression in the TRUE is selected. Other
wiseexpression in the FALSE is 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 is
called 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 get
multiplied with 3*2 and then adds into 7.
8. Explain detail structure of C++ program with suitable programing example.
//simple c++ program
#include<iostream> // header file included
using namespace std;
int main()
{
int a=10,b=34;
cout<<"simple c++ program \n"; // c++ statement
cout<<"hello world";
cout<<a<<b;
return 0; // returning no errors
}
The basic structure of c++ program mentioned above can be divided into following sections:

Documentation Section : This section comprises of comments. As the name suggests, this section is

used to improve the readability and understanding of the program.// (Double Slash)
represents comments in C++ program. Comments can be of single line or multiple lines. Double
Slash comments are used to represent single line comments. For multiple line comment, you can
begin with /* and end with */. For example :
In the above C++ program //simple c++ program represents single line comment.

Linking and Directives Section : The program written above begins with
#include<iostream>.<iostream> represents header file which includes the functionalities of
predefined functions. In linking section, the compiler in-built functions such as cout<<, cin>>etc are
linked with INCLUDE subdirectorys header file <iostream>. The #symbols tells about address to
or link to. Iostream is input/output stream which includes declarations of standard input-output
library in c++.
main() Section : This is the section in which the program coding is written. Basically, it acts as a
container for c++ program. The execution of the c++ program begins with main() function and it is
independent of the location of main() function in the program. main() is a function as represented
by parenthesis (). This is because it is a function declaration. The body of the main() function can
be found right after these parenthesis, the body is enclosed in braces , -.
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() function
only, unless they are declared globally outside the main() function. int a=10, b=34; in the above
program 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 program
has many executable statements like cout<<simple c++ program \n;
return 0; in the above program causes the function to finish and 0 represents that function has been
executed with zero errors. This is considered as most usual way to end a C++ program.
Finally the body of the main() function ends with -.
Global Declaration Section : There are certain programs which requires variables that can be used
in more than one function, so then the variables can be declared outside the main() function or
respective functions. Then those variables become accessible in any of the functions, Hence named
as Global Variables as their scope becomes global to the program.
User Defined Functions : There are certain functions that are called by calling statements from the
main() function. Every function includes local variable declaration section and executable statement
section similar to main program.

9. Explain the various library functions.


Ans. C++ provides many built in functions that save the programming time. They include mathematical
functions, character functions, string function, console input output functions and some general
standard library function some of them are given below
Mathematical functions
Sin(x)
: sine of an angle x (measured in radians)
Cos(x)
: cosine of an angle x(measured in radians)
Tan(x)
: tangent of an angle x(measured in radians)

Character functions
Islower(c) : it returns true if C is a lower case letter otherwise False
Isupper(c) : it returns true if C is a upper case letter otherwise False

String functions:
Strrev(s) : it converts a string s into its reverse
Strupr(s) : it converts a string s into its upper case
Strlwr(s) : it converts a string s into its lower case

Stdlib functions:
atoi(s)
: it converts a string into numerical representation.
Itoa(n)
: it converts a number to a string

Chapter 8
Data types

One Mark questions and answers


1. 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 variable
Ans. datatypevariable_name= value
4. Differentiate lvalue and rvalue
Ans. 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 types
Ans. Those data types which are constructed using simple or fundamental data types. Such as arrays,
function, pointers
8. What are user defined datatypes
Ans. 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 answers


1. 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

Ans. 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;
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 the
operations that can be performed on those values
Data type means to identify the type of data and associated operations of handling it. The
classification of data types include :
1. Simple and fundamental data types
2. Complex or derived data types
3. 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 of
various 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 automatically. This helps in providing an alternative means
for creating symbolic constants
example: enum choice{good, bad, excellent} // definition of enumerated type
choicemychoice; // decalartion of variable type.

Three Marks questions and answers


1. Explainthe variables in detail
Ans. 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. 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 the
operations that can be performed on those values
classification of data types include :
1. Simple and fundamental data types
2. Complex or derived data types
3. User defined data types

C++ Data types

Fundamental data types

Derived data types

Int eg 100

arrays eg a[100]

char eg 'c'

functions

float eg 99.99

poniters eg int *ptr

User defined Data types

structure
union
class
enumerated

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 of
various 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 called
enumerators. Enum is a keyword that assings values 0,1,2 automatically. This helps in providing an
alternative means for creating symbolic constants
example: enum choice{good, bad, excellent} // definition of enumerated type
choicemychoice; // decalartion of variable type.

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 legal
int i=BETA
int j=3+GAMMA
On the other hand, C ++ does not supported an implicit conversion from int to an enum type. this
type conversion is always illegal

Five Marks questions and answers


1. Explain the variables in detail with suitable examples

Ans.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 the
operations that can be performed on those values
Data type means to identify the type of data and associated operations of handling it. The
classification 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 memory
space. 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 single
character at a time, it should be enclosed within single quote y it occupies 1 byte of memory
space. Can store 256 characters.
Float: this represent fractional part i.e real numbers such as, 10.3, 11.0000, -5.75 etc. it can also
have 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

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

C++ Data types

Fundamental data types

Derived data types

Int eg 100

arrays eg a[100]

char eg 'c'

functions

float eg 99.99

poniters eg int *ptr

User defined Data types

structure
union
class
enumerated

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 define
data 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 keyword that assings values 0,1,2 automatically. This helps in providing an alternative means
for creating symbolic constants.
The syntax for enum is as follows
enum [tag] {enum list}; // for definition of enumerated type
enumtagdeclarator; for declaration variable of type tag
example:enum choice{good, bad, excellent} // definition of enumerated type
choicemychoice; // decalartion of variable type.

Chapter 9
Input and Output Operators
One Mark answer questions

1. Give the other name for cin()


Ans. Stream Extraction (>>)
2. Give the other name for cout()
Ans. Stream insertion (<<)
3. What is cascading?
Ans. C++ supports insertion and extraction by using cin>> and cout<<i/o stream which accepts single input at a
time. If we want to insert and extract multiple input and output we can use multiple insertion(>>) and extraction
(<<) Operator this is known as cascading.
4. What are manipulators?
Ans. Manipulators are operator used along with the insertion operator to manipulate or modify the output.
Some of them are endl and setw.
5. Give the header file that holds setw() and endl.
Ans. iomanip.h
6. What is the purpose of setw()
Ans. causes the number that follows in the stream to be printed within a character wide field and the value is
right justified within the field.
Two Marks Answer questions
1. Explain the input operator in C++
Ans. Input in C++ is done by using stream extraction operator(>>) on the cin stream. The operator must be
followed 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(<<) on the cout stream. Cout stands for console
output. The operator must be followed by the series of statements that should be written in the double quotes
will insert the data that is going to be displayed on the screen or monitor
Example:
Cout<< Computer Science;
3. Explain the cascading of input operator in C++
Ans. C++ supports insertion and extraction by using cin>> and cout<<i/o stream which accepts single input at a time.
If we want to insert and extract multiple input and output we can use multiple insertion(>>) and extraction (<<)
Operator this is known as cascading.
For example :cin>>a>>b>>c;
cout<< computer science<<computer science<<endl;

Five Marks Answer Questions


1. Explain the input output operators in C++
Ans. Input in C++ is done by using stream extraction operator(>>) on the cin stream. The operator must be
followed by the variable that will store the data that is going to be extracted from the stream.

Example: int age;


Cin>>age;
Keyboard

cinn

>>

Variable

output in C++ is done by using stream insertion operator(<<) on the cout stream. Cout stands for console output.
The operator must be followed by the series of statements that should be written in the double quotes will insert
the data that is going to be displayed on the screen or monitor
Example:
Cout<< Computer Science;
Monitor

cout

<<

2. Write a C++ program to find the sum of two numbers


Ans.

#include<iostream.h>
#include<conio.h>
Void main()
{
inta,b,sum;
clrscr();
cout<< Enter two numbers:;
cin>>a>>b;
sum=a+b;
cout<< The sum is <<sum<<endl;
getch();
}
Output : Enter two numbers: 10 5
The sum is 15

3. Write a C++ program to convert Fahrenheit to Celsius


Ans.

#include<iostream.h>
#include<conio.h>
Void main()
{
float Fahrenheit, celcius;
clrscr();
cout<< Enter the value in Fahrenheit:;
cin>>Fahrenheit;
celsius =((5.0/9.0)*Fahrenheit-32.0);
cout<< Fahrenheit <<F=<<Celsius<<c <<endl;
getch();
}
Output : Enter the value in Fahrenheit:105
105F = 26.3333 c

4. Write a C++ program to demonstrate the cout

Variable

Ans.

#include<iostream.h>
#include<conio.h>
Void main()
{
cout<< c++ is an object oriented programming :;
cout<< I like computer science;
getch();
}

Output:c++ is an object oriented programming


I like computer science

5. Explain the cascading of input operator in C++ with suitable examples


Ans.C++ supports insertion and extraction by using cin>> and cout<<i/o stream which accepts single input at a time.
If we want to insert and extract multiple input and output we can use multiple insertion(>>) and extraction (<<)
Operator this is known as cascading.
For example :cin>>a>>b>>c;
cout<< computer science<<computer science<<endl;

Chapter 10
Control Statements
One marks questions

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

What is compound statement?


What is block?
Name the types of control statements
What are the three types of constructs?
What is selection statement?
What are the three different constructs
What is the numerical equivalent of TRUE and FALSE?
Which selection statement does C++ provide?
What are the rules associated with IF statement?
What is the purpose of else clause?
What is two way branching statement?
Correct the following code fragment
If(x=1)
K=100;
Else
K=10;
13. Consider the following program segment
X=10; y=5;
If(x>y)w=15;
W=25
Cout<<value of w is;
Cout<<w;
Find the output
14. State the use of nested if statement.
15. What is the purpose of switch statement?
16. How does the switch statement differ from if statement.
17. What type of value can be taken by case labels
18. What is the significance of default clause in a switch statement?
19. Define the term looping?
20. Name the iteration statement provided by C++?
21. When should we use while and do while loop?
22. What is pre-tested looping structure?
23. What is post tested looping structure?
24. Write a program fragment to display numbers 2,4,6,8,10.18,20 using while loop
25. Write a program fragment to display numbers 1,3,5,7,9.17,19 using do while loop.
26. Which structure is called as fixed execution looping statement?
27. When should we use for loop?
28. What is the output of the following code fragment
For(int i=1;i<10; i++)
Cout<<i;
29. Why does hellow not print even once
For(i=0;i>10;i++)
Cout<<hellow;
30. Write a for loop that display numbers from 50 to 61.
31. Name the jump statement provided by c++?
32. What is purpose of break statement?
33. Why is the continue statement used?
34. What is the function of exit()?

35. Which header file must be included in the program for using the exit () function.
Two marks question answers
1. 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<< GRADE A\n;
Case B :cout<< GRADE B\n;
Case C :cout<< GRADE C\n;
Case D :cout<< GRADE D\n;
Default cout<< GRADE F\n;
}
7. Explain the syntax of the while loop with a suitable example.
8. Explain working of do while loop with a example
9. Compare the working of while and do while loop.
10. Write a short program using while to print 1471040.
11. Write a program segment using do while to print all odd numbers from 1 to 24.
12. Write a short program to find largest of three numbers.
13. Write a short program to test the given number is even or odd.
14. Explain the syntax of for loop with a suitable example.
15. Compare break and continue statements.
16. Write the function of goto statement with general format.
Five marks questions
1. Explain the working of if statement and if else statement with suitable program fragments.
2. Explain if else if statement with general format and suitable example.
3. Explain the working of switch statement with an example.
4. Write general format of while loop with a suitable programming example.
5. Explain working of do while statement with an example.
6. Write a program to find the sum of the series 1+1/3!+1/5!+.1/n! using while loop.
7. Differentiate between while looping with do while looping structure.
8. Explain working of for looping structure with a programming example.
9. Write a program to find sum of digits of a given number using while loop.
10. Write a program to find GCD of two numbers using while loop.
11. Write a program to find the sum occurrence of a digid of a given number using do while.
12. Write a program to find all the integer divisors of a given number using for loop.
13. Write a program to find factorial of a given number using for loop.
14. Write a program to print Fibonacci series using for loop.
15. Write a program to find sum of the series s=1+x+x2+..+xn using for loop.

You might also like