CS 1 TPS Computer Science PDF Guide
CS 1 TPS Computer Science PDF Guide
com
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
Thank You for Visiting
[Link]
Welcome to [Link]
Chapter 3> [Link]
Probable marks:41
Syllabus
Scope of the
Review of C++
references,
strings
pointers,
Arrays, programming
oriented
rinciple of
object
Classes and objects
Constructors and destructors
conversions
overloading
and type
Operator
Inheritance
and polymorphism
Virtual functions
Working with files
REVIEW OF C++
(March 2003,13
Q.1 What is C++? What are the advantages of C++ ?
Ans.
C+t is an object oriented programming language. Initially C++ was named as t witt
classes". C++ Was developed by Bjarne Stroustrup at AT & T Bell Laboratories, USA
the early eighties.
The advantages of C++ over C are :
(i)
) C++ is an incremented version of C. It is
a
r a m s can s
procedure.
3 Large programs are divided into 3 Programs aré divided into entities
smaller programs known as functions. known as objects.
4 Data move openly around the system| 4 Data is hidden and cannot be accessed
from by external functions.
in 5 Follows bottom-up approach in
Employs top-dowm approach
program design. program design.
and Object Oriented
Q.3 What do you mean by Object Based Programming Language
Programming Language ? State the relationship between theselanguages.
(Oct. 2008,March 2010, 3)
Ans
Object Based Programming Language:
with objects are said to be object based
1) Language that supports programming
programming languages.
2) lt is a style of programming that primarily supports
encapsulation & object or identity.
3) Major features are
a) Data encapsulation
mechanism
b) Data hiding & access
C+ data types
int
Char Float double
2) C++ allows user to
create new abstract data types, which
data type. These are can behave like uit-in
called user-defined any D
and enumeration. data types. These
3)
include structure, unio
class
C++
provides three built-in data
4) Integral includes integer and types which are integral, void and
double. character (string) while floating
5) In floating type includes
includes float
i
addition to these data types, C++
which are referred as provides user with arrays,
derived data types. functions and
inter
Science I
TPS Computer
-
3-4 [Link] C+
Q.6 Enlist the basic data types used in C++ with size of data in terms of bytes for each.
March 2002, 2006, October 2006)
OR Enlist different built in data types in C++ with their sizes.
(Oct. 2009
Ans.: There are three main basic built-in data types used in C++ viz. integral type, voia ana
floating type.
Built-in type
or
Basic data type
unsigned int
2 0 to 65535
4 3.4 x 10-38 to 3.4 x 10-38
float
1.7x 10308
8 1.7x 10-308 to
6 double
3.4 x 10-4932
7 long double 10 3.4 x 10-4932 to
3-5
TPS Computer Science -I
[Link]
another block.
program may contain one block withir
3) When a variable is declared in
program, scope extends from the point of declaration tili
the end of the block in which it is
defined.
4) The same variable name can be used to
have different meaning in different blocks.
5) Consider the following
segment
of
program.
*****
int x =10;
*****
int x =
1; Block 2 Block 1
****
Here Block 2 is
hides the contained in Block 1. Note
declaration of the same that declaration of a variable in an è rbloo
variable in an outer in
block.
TPS Computer
Science -I [Link]
3-6 C+
Scope resolution operator is used to uncover a hidden
6)
It takes the form variable.
:variable namee
e.g
intx= 10;
int x =
1;
cout << "Local x is" << x;
int num = 20
void main ()
variable
intnum 10;// local
=
char ptr
ptr = new char [len + 1];
strcpy (ptr, str);
cout <<"ptr =" << ptr;
delete ptr;
In above example, the new operator returns a pointer that point to a memory section
large enough to hold the string str plus an extra byte for null character. Thern after use of
memory delete operator released memory.
Q. 10 What are the different selection (conditional) statements in C++? Give syntax for
each.
Ans.
The program has to be able to evaluate conditions and select alternative path in
programn.
In C++, there are two ways in which selection may be made:
1) The if statement 2) The switch statement
1) The if statement:
The if statement has two forms:
i) Simple if statement ii) if else statement
i) Simple if statement
Syntax if (condition)
action 1;
action 2;
1) Depending the
on
condition value,
direction or another. program execution proceeds
2) If the condition is
true, then action I will be
done.
Science II [Link] C+
s
T P S
Computer
3-8
Syntax if (condition)
action 1;
else
action 2
action 3; will
otherwise action 2
If the condition is true, then and then only action 1 will be done,
be done.
The switch statement:
2)
1) This is a multiple branching statement.
only one module out of several. If no
case 1:
action 1;
break;
case2:
action 2;
break;
default
action x
[Link]
CH
3-9
TPS Computer Science - I
each.
Give syntax for
structures in C++ ?
Q.11 What are the different looping
Ans.: inC++:
structures
Following are the differentlooping
3) Do-while loop
2) While loop
1) For loop
1) The for loop: when action is
to be repeate
entry-controlled loop.
It is used
The for loop is an
predetermined number of times.
Syntax: increment / decrement expression)
for (initial expression; test expression;
action;
***°
where
starts.
(a) Initial expression is executed only once, when the loop
(6) Test-expression evaluated each time through the loop, before the body of the loop-
b)
is executed.
(c) Increment/ Decrement expression changes the value of the loop variable at the
end of the loop.
2) The while loop:
The while loop is an entry-controlled loop and it repeats the action until the condition
becomes false. When condition is false, that time loop is terminated.
action 1;
action 2;
3) The do-while loop:
The do-while loop is an exit-control loop used to carry out
Syntax conditional looping.
do
action 1;
while (condition);
action 2;
In do-while, condition is not tested
even if until the body of the
the condition is false the loop has been executed
after the first loop is executed at
iteration, the loop is terminated. least once. If the
o1
conditiOT is
Q.12
What is function prototyping? [Link]
Ans. :
3)9However, it makes program to take up more memory, because the statements that
define inline function are reproduced at each point where the function is called.
4) "An inline function is a function that is expanded inline when it is invoked". ie. the
Compiler replaces function call with the corresponding function code.
5)
Theinline functions are defined as follows:
inline function header
function body
[Link]
3-11
TPS Computer Science - I
return (a*b);
The above
prototype declares default value 3.14 to the argument Pi. A
function call like - subsequent
A =area(7); //one argument
missing
passes the value 7 tor and lets the function
use default
The call A area (7, 2.5)
=
value 3.14 for' Pi
passes an explicit value 2.5 to. Pi.
4 Only trailing arguments have
default value cannot provide to an default values.
can
i.e. add defaults
argument in the middle of
from right to lenft. A
5) Advantages of using default arguments: list.
i These are useful in
situations, where some
ii It
provides better flexibility to arguments have same values.
arguments that are meaningful to programmers by allowing to use rticula
single T
Ans.
overloading with example. 2000
orfunction
refers the use
to
of same
a
polymorphism. polymorphism.
TPs Computer Science I
-
3-12
[Link] C+
3 Using the concept of function
name but with different overloading, create
argument lists. a family of functions with one function
4) The function would
call.
pertorm different operation.
depending on argument list in functionn
5) The correct function to be
of the arguments and not invoked is determined by checking the number
on the and the type
function type.
6) eg #include siostream.h>
int area (int
s);
int area (int 1, int //prototype declaration
b) //for overloading area()
main ()
return (1b);
n above example the function area( ) is overloaded. The first function is used to
calculate area of square. It has one integer parameter.
The second function is used to calculate area of rectangle. It has two integer parameters.
7) When a function is called, the compiler first matches the prototype having same number
and types of arguments and then calls appropriate function for execution. A best match
must be unique.
Q.16 Explain the structure of a general C++ program. March 2019
Ans.
1) A typical C++ program contains 4 sections as shown in following figure These sections
may be placed in different code files and then compiled independently or jointly.
Include files
Class declaration
Class functions definitions
3-13
interface from the
TPS Computer Science - I
the
abstract of the
to separate
approach enables the prograr
4) This
implementation details.
that uses the class is placed in third
file, which
chudes the
inchsd
int a, b, c, max;
cout<"Enter three numbers" <<endl;
cin>>a>>b>>c;
if (b>c)
max = b;}
else
max c
if (a>max)
max a;}
cout<<"The larger number is:-";
cout<<max;
Ans.: #include<iostream.h>
void main()
int f0, f1, f, n;
f0 = 0;
f1 =1;
clrscr();
cout<<"Fibbonacci series \n";
cout<"\n" <<f0<<"\n"<<f1;
for (n=1;n<=13; n++)
f f0+ f1;
cout<"\n"<<f;
f0 = f1;
f1 =f;
Science -
I
TPS Computer 3-14 [Link] C+
fact 1;
for (i = 1; i<=n; i++)
fact facti;
coutecendlk<n«<"\t"<cfact;
Q. 20 Write a C++ program to find factorial of a natural number input during program
execution. March 2004, 08, 17, Oct. 2002,04,12)
number
Ans. //Program to find factorial of a
#include<iostream.h>
#include<conio.h>
void main ()
fact f a c t * i;
#include<conio.h>
TPS Computer Science - I 3-15
[Link]
void main()
d=dn%10;
temp-(temp*10)+d;
dn=dn/10;
if (n==temp)
else
temp=0;
while (dn!=0)
d=dn%10;
[Link]
Sciernce -
I C+
TPS
Computer 3-16
temp=temp+(d*d*d);
dn=dn/10;
if(n==temP)
else
n terms
Ans.
//C++ program to print given pattern
#include<iostream.h>
#includecconio.h>
void main()
int i, j, n
clrscr();
cout<<"Enter a number";
cin>>n;
cout<<endl;
for (i=1; i<=n; it+)
for =1;j<=i:j++)
cout<<i<<"\t";
cout<<endl;
numbers
Q. 24 Write a program in C++ to printthe
1 0 1 0 1
1 0 1 0
1 0 1
10
1
Ans.
//C++ program to print given pattern
#includeiostream.h>
#include<conio.h>
void main( )
clrscr( );
int i, j;
for (i=5; i>=1;i--)
cout<j%2<<"\";
cout<<endl;
Q.25 Write a
program to
perform
arithmetic calculations such
multiplication division, depending
or
on choice
as
addition, subtractio
Ans. using switch statement.
//C++ program to
generate simple calculator
#include<iostream.h>
#include<conio.h>
void main()
float a, b, result;
int ch;
clrscr);
cout<"Enter two numbers";
cin>>a>>b;
cout<<"\nl-addition \n 2-subtraction
cout"Enter Your Choice:"; \n
cin>>ch; 3-multiplication \n 4-division
switch (ch)
[Link]
Science -
I [Link] C+
Computer 3-18
TPS
case 1:
result=a+b;
cout<<"Sum is""<<result;
break;
case 2:
result=a-b;
cout<<"Difference is"<<result;
break;
case 3:
result=a*b;
cout<"Product is"<<result;
break;
case 4:
result=a/b;
couts<"Division is"<<result;
break;
default
cout<<"invalid choice";
break;
recursive function."
addition of first n numbers
//Program to calculate
#include<iostream.h>
#include<conio.h>
int add (int);
void main()
int n, sum;
cout<<"Enter a number \n";
cin>>n;
sum=add(n);
of
first"<<n<<"numbers is"<<sum;
cout<<"Addition
addition
//function to calculate
int add (int x)
int S = 0;
if (xl=0)
[Link]
3-19
TPS Computer Science - I
S=xtadd(x-1);
return(S);
cylinder and rectangula
of cube, r bo-
C++ to calculate volume
27 Write a program in
overloading.
function
depending on choice by using
Ans.: //Program using function overloading
#include<conio.h>
#includesiostream.h>
int volume (int s);
float volume (float r, float h);
int volume (int 1, int b, int h};
void main()
int ch;
do
clrscr();
cout<<"\nl:Volume of cube";
cout"n2:Volume of cylinder";
out<<"\n3:Volume of rectangular box";
cout<<"\n4:Quit";
cout<<"\n\n Enter Your Choice";
cin>>ch;
switch (ch)
case 1:
cout"Volume of
cout<<volume rectangular
(3,5, 7); box is";
break;
Science J [Link]
TPS Computer
-
3-20 C+
case 4: break;
default:
cout<<"Invalid choice";
cout<<"Reenter your choice";
while (ch!=4);
return (s*s*s);
return (3.14*r*r*h);
return (1'bh);
and Strings
Arrays, Pointers, References
onto a function.
What is ? Explain how array can be passed
Q.28 an array
Ans.
objects, which are stored in consecutive
"An array is collection of identical data
1) a
variable name."
locations under common
memory
multidimensional.
dimensional o r
2) Arrays may be o n e
declaration of one-dimensional arrayis
3 The general form for
data-type array-name lexpression];
TPS
onto a
function.
function
claration. No
declaration. No
subs
entire array
C t + allows to pass the for the
5) be used a s .a n
argument
An array name
can
function using arrays.
a
to invoke
bracketsare required array
(float bll, int c);
one 1s a n
e.g. float
rev
parameters,
out ofwhich
declares a function rev, with
two
March 11,19, July1 ,1
his of using pointerS.
the advantages
Q. 29 What are pointers ? Give
variable."
Ans. address of other
holds the memory
function
the main
to
copied to the function.
the
calling functionfunction,
a
control will be transie transferred
and the value of actual argumen
(b) Within function the actual
value may be
altered or
changed.
TPS Computer Science I [Link] C++
-
3-22
()
(c) When the control is transferred back from function to the program,
are not transferred back. This type of passing formal argument to altered vauesis
a function
called as call by value
(d) For example:
main ()
void furnct (int X, int Y);
v o i d f u n c t ( i n t * X, i n t * Y);
Q.31 Explain how the memory address of a variable can beaccessed in C++
(March2004, 07,14;Oct.2004,12)
Ans.
1) Computer uses memory forstoring the values of variables and the memory is a
sequential collection of storage cell. Each cell has a number called address of the cell.
2) In C++, if declare a variable, then it gets associated with certain location where the value
of the variable is stored.
[Link]
3-23
TPS Computer Science - I
30Value atlocation
7940 Location number (address) the value 30.
location to store
LOmputer has selected
7940 memory
"&t operator is used. m
of a particular variable
4) lo access the memory address
returns the memory address
of its operand.
operator
For example: a = &p; location add.
the
assigns the memory address of variable p to the a.
This address is
ddress
variable. The operator '& is "the address
of" operator
as that contains
the address. The Doin
The variable 'a' is declared as pointer variable pointe
variable declared in C++ as,
i n t * a;
What is call by reference ? What is the advantage of call by reference over call b
Q.32
value? (Mar. 2014
Ans.
A function can be called by two methods :
) Call by value
(i) Call by reference
1) When a function call passes arguments by value (call by value) the called funchar
creates a new set of variables and copies the values of
arguments into them.
2) The function does not have access to the actual
variables in the calling program and u
only work on the copies of values.
3) Provision of reference variables in C++
reference.
permits to pass parameters to the functiol
4)
4) When pass arguments by reterence
function become alises to the actual(callby reference) the formal arguments in tne calle
when the function is working with arguments
in the calling that
its
function. This mean n the
original data. own arguments, it is actually working
5) The mechanism of call
by value is good, if the function does not need to
of the original variables in the
Values
alter tue
6) But, if a situation to
calling program.
sort compare two
change the values of variables in the b u b b h
situation, the
variables of calling program, whichfunction
is not
should be able to
interchange the be done
the call by reference method is
used. possible by call by value. But it
But it can can
I [Link]
TPS 3-24 C+
void main()
int a, b;
cin>>a>>b;
swap (&a,&«b); //call by reference
cout<<"a="<<a;
cout"b="<<b;
void swap (int"a, int"b) //function definition
int t;
t=*a;/assign the value at address a tot.
*a=*b;//put the value at b into a.
*b=t;//put the value at t intob.
For example
int main ()
charS, []="ABCD"
char S2 []="XYZ"
cout << "Before strcpy (S, S) \n";
cout << " \ t S; = [ " < < S1 < < " ] , l e n g t h = " < < s t r e l n (S,) < < e n d l ;
cout " \ t S2 = ["<< S, «<"], length = " < < s t r l n (S,) < < e n d l ;
S= [XYZ]; length = 3
After strepy (S1, S2)
S =
[XYZ], length = 3
S=XYZ], length = 3
3-25
TPS Computer Science - I
[Link]
Example:
Char "S, = "ABCDE"
Char S2 ="
If (strcmp (S, S3) <0)
Cout< S << "<" <<S, << endl;
else
cin >> a ;
temp =aijl;
alj] =alj+1]
alj+1] = temp;
cout<<"Ascendingorder is:\n";
Thank you for visiting
[Link]
Visit [Link] for all Chapters
Visit [Link] for all Chapters
Visit [Link] for all Chapters
Visit [Link] for all Chapters
Visit [Link] for all Chapters
Visit [Link] for all Chapters
Visit [Link] for all Chapters
Visit [Link] for all Chapters
Visit [Link] for all Chapters
Visit [Link] for all Chapters
Visit [Link] for all Chapters
Visit [Link] for all Chapters
Visit [Link] for all Chapters
Visit [Link] for all Chapters
Visit [Link] for all Chapters
Visit [Link] for all Chapters
Visit [Link] for all Chapters
Visit [Link] for all Chapters
Visit [Link] for all Chapters
Visit [Link] for all Chapters
Visit [Link] for all Chapters
Visit [Link] for all Chapters
Visit [Link] for all Chapters
Visit [Link] for all Chapters
Visit [Link] for all Chapters
Visit [Link] for all Chapters
Visit [Link] for all Chapters