Beruflich Dokumente
Kultur Dokumente
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.
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.
Example
(a&&b) is false
(a||b)is true
!(a&&b) is true
!(a) is true. !(b) is false
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.
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
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;
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:
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
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.
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
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.
invalid variables are : should not start with digit, should not have space etc
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
Int eg 100
arrays eg a[100]
char eg 'c'
functions
float eg 99.99
structure
union
class
enumerated
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.
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
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..
Int eg 100
arrays eg a[100]
char eg 'c'
functions
float eg 99.99
structure
union
class
enumerated
Chapter 9
Input and Output Operators
One Mark answer questions
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
<<
#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
#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
Variable
Ans.
#include<iostream.h>
#include<conio.h>
Void main()
{
cout<< c++ is an object oriented programming :;
cout<< I like computer science;
getch();
}
Chapter 10
Control Statements
One marks questions
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
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.