Documente Academic
Documente Profesional
Documente Cultură
2008
Function Overloading: Same function name can be used for more than one purpose.
b.
Operator Overloading: Same operator can be used for more than one purpose.
i.
math.h
ii.
ctype.h
iii.
stdio.h
iv.
string.h
v.
iomanip.h
# include
void main( )
{
int x, sum = 0;
cin<
for(x=1,x<100,x+=2)
if x%2 = = 0
sum += x;
cout>>SUM=>>SUM;
}
Ans: #include
Wrong operator with cin. It should be cin>>n;
In place of if x%2 = = 0, it should be if(x%2 = = 0)
Wrong operator with cout. It should be cout<<SUM=<<SUM;< p>
Q. 7. What will be the output:
int fun(int &x, int y=10)
{
if(x%y = = 0) return ++x; else return y--;
}
void main( )
{
int p=20, q=23;
q = fun(p,q);
cout<<P<<
<<q< p = fun(q);
cout<<P<<
<<q< q = fun(p);
cout<<P<<
<<q< }
v.x += 20;
w = y;
u.y += 10;
u.x += 5;
w.x -= 5;
show(u);
show(v);
show(w);
}
Ans: Output is:
25:20
40:10
35:10
Q .11. Rewrite the corrected code for the following program. Underline each
correction.
#include
structure club
{
int mem number;
char memname[20];
char memtype[] = HELLO;
};
void main( )
{
club p1, p2;
cin<<Member Number:;
cin>>memnumber.p1;
cout<<Member Name:;
cin>>p1.membername;
p1.memtype = WORLD;
p2 = p1;
cin<<Member Number:<
cin<<Member Name:<
cin<<Member Number;<
}
Ans:
#include
#include
struct club
{
int memnumber;
char memname[20];
char memtype[6];
};
void main( )
{
club p1, p2;
cout<<Member Number:;
cin>>p1.memnumber;
cout<<Member Name:;
cin>>p1.memname;
strcpy(p1.memtype ,WORLD);
p2 = p1;
cout<<Member Number:<
cout<<Member Name:<
cout<<Member Type:<
}
Chapter 3 Object Oriented Programming
Q. 12. What do you understand by function overloading? Give an example
illustrating its use in a C++ program.
Ans: A function name having several definitions that are differentiable by the number or types
of their arguments is known as function overloading. For example;
float area(float a)
{
return a * a;
}
float area(float a, float b)
{
return a * b;
}
Q. 13. Enlist some advantages of OOP.
a. Reusability of code
b. Ease of comprehension
c. Ease of fabrication and maintenance
d. Easy redesign and extension.
ii.
iii.
Ans:
i.
ii.
iii.
Here only the public members are accessed using the objects
of the same type i.e. Objectname.membername.
{
float p = 0;
int tot = 0;
for(int i =0; i<5; i++)
tot += marks[i];
p = tot/5.0;
return p;
}
void student::readmarks( )
{
cout<<Enter roll number;
cin>>roll_no;
cout<<Enter name:;
gets(name);
cout<<Enter class:;
gets(class_st);
cout<<Enter marks in 5 subjects:;
for(int i =0;i<5;i++)
cin>>marks[i];
}
void student::displaydata( )
{
cout<<Roll number:<<ROLL_NO<
cout<<Name:<<NAME<
cout<<Class:<<CLASS_ST<
cout<<Marks in 5 subjects:;
for(int i=0;i<5;i++)
cout<<MARKS[I]<<
cout<
percentage = calculate( );
cout<<Percentage=<<PERCENTAGE<<%;
}
Q. 16. What are the advantages and disadvantages of inline functions?
Ans: The main advantage of inline functions is that they save on the overheads of a function call
as the function is not invoked, rather its code is replaced in the program.
The major disadvantage of inline functions is that with more function calls, more memory is
wasted as for every function call, the same function code is inserted in the program. Repeated
occurrences of same function code waste memory space.
Chapter 5 Constructors and Destructors
Q .17. Given the following C++ code, answer the questions i and ii:
class readbook
{
public:
readbook( ) //Function1
{
cout<<Open the Book<
}
void readchapter( ) //Function 2
{
cout<<Reading chapter one<
}
~readbook( ) //Function 3
{
cout<<Close the book<
}
};
1. In OOP, what is Function 1 referred as and when does
it get invoked/called?
2. In OOP, what is Function 3 referred as and when does
it get invoked/called?
Ans:
i.
ii.
int m1,m2,m3;
float percentage;
public:
student_rec( )
{
m1 = m2 = m3 = 0;
percentage = 0.0;
}
void calc_perc(int x, int y, int z)
{
m1 = x; m2 = y; m3 = z;
percentage = (m1+ m2 + m3)/3.0;
display_rec( );
}
void display_rec( )
{
cout<<ENDL<<PERCENTAGE=<<PERCENTAGE<<%;
}
};
void main( )
{
student_rec s1;
s1.display_perc( );
s1.calc_perc( );
s1.display_perc( );
}
Ans: Percentage = 0%, Percentage = 35%
Chapter 6 Inheritance
Q. 21. What is Containership? How does it differ from Inheritance?
Ans: When a class contains objects of another class as its members, this kind of relationship is
called containership or nesting. Inheritance lets you create or define a specialized object of a
class that shares the properties of the class and at the same time adds new features to it. But
containership is just a way to define an object which itself is collection of objects of other classes.
Q .22. Given the following class definitions answer the questions that follow:
class book
{
char title[20];
char author[20];
int no_of_pages;
public:
void read( );
void show( );
};
class textbook:private book
{
int no_of_chapters, no_of_assignments;
protected:
int standard;
public:
void readtextbook( );
void showtextbook( );
};
class physicsbook:public textbook
{
char topic[20];
public:
void readphysicsbook( );
void showphysicsbook( );
};
i.
ii.
iii.
iv.
Ans:
i.
ii.
iii.
iv.
68 bytes.
int count = 0;
if(!fin)
{
cout<<\n File does not exist;
return;
}
while(1)
{
fin.get(ch);
if(ch = = EOF)
break;
if((ch>=A && ch<=Z)||(ch>=a&& ch<=z))
count++;
}
cout<<\nNumber of alphabets are:<
fin.close( );
}
Q. 24. Name two member functions common to the classes ifstream and ofstream.
Ans: open ( ), close ( ).
Q. 25. Assuming the class Computer as follows:
class computer
{
char chiptype[10];
int speed;
public:
void getdata( )
{
gets(chiptype);
cin>>speed;
}
void showdata( )
{
cout<<Chip<<CHIPTYPE<<SPEED=<
}
};
Write a function readfile( ) to read all the records present in an already existing
binary file SHIP.DAT and display them on the screen, also count the number of
records present in the file.
Ans:
void readfile( )
{
ifstream fin;
fin.open(SHIP.DAT,ios::in|ios::binary);
computer c;
while(!fin.eof( ))
{
fin.read((char*)&c, sizeof(c));
count++;
c.showdata( );
}
cout<<Total Number of Records are<
}
Q. 26. Differentiate between write and put functions of ostream class.
Ans: The prototypes of put ( ) and write ( ) are:
ostream & put (char ch);
ostream & write ((char *) & buf, int sizeof(buf));
The put ( ) writes the value of ch(one character) to the stream and returns a reference to the
stream. The write ( ) function writes sizeof (buf) bytes to the associated stream from the
buffer pointed to by buf. The data written to a file using write( ) can only be read accurately
using read( ).
Ans:
The first statement allocates memory of one integer to ptr and initializes it with value 5. The
second statement allocates memory of 5 contiguous integers (i.e. an array of 5 integers) and
stores beginning address in pointer ptr.
Q. 29. What will be the output of the following code fragment:
#include
#include
void main( )
{
clrscr( );
int a[ ] = {3, 5, 6, 7};
int *p, **q, ***r, *s, *t, **ss;
p = a;
s = p + 1;
q = &s;
t = (*q + 1);
ss = &t;
r = &ss;
cout<<*p<<\t<<**q<<\t<<***r<
}
Ans: 3, 5, 6.
Q. 30. Identify syntax errors, if any, in the following program. Also give reason for
errors.
void main( )
{
const int i = 20;
const int * const ptr = &i;
*ptr ++;
int j = 15;
ptr = &j;
}
Ans:
Erroneous statements
Errors and corrections
*ptr ++
ptr = &j
Chapter 9 Array
Q. 31. How is computer memory allotted for a 2D array?
Ans: For two-dimensional array, the computer memory is allocated either in row-major form
or in column-major form.
Row Major form stores the 2-D array row wise i.e., firstly the first row is stored, then the second
row, then third row, and so forth.
Column Major form stores the 2-D array column wise i.e. firstly the first column, then the
second column, then third and so forth. The default form is Row-Major.
Q. 32. Binary search is to be used on the following sorted array to search for 30
and 60.
Array
Index:
10
Value:
11
22
30
33
40
44
55
60
66
70
Give the index of the element that would be compared with at every step. Repeat
the process replacing 30 by 60.
Ans:
For 30
Step Index Result
1. 4 Unsuccessful
2. 1 Unsuccessful
3. 2 Successful
For 60
Step Index Result
1. 4 Unsuccessful
2. 7 Successful.
Q. 33. Consider the single dimensional array AAA [45] having base address 300
and 4 bytes is the size of each element of the array. Find the address of AAA [10],
AAA [25] and AAA [40].
Ans:
lb=0, b=300, s=4 bytes.
AAA [I] = b + (l lb)*s
AAA [10] = 300 + (10-0)*4 = 340
AAA [25] = 300 + (25-0)*4 = 400
AAA [40] = 300 + (40-0)*4 = 460.
The addresses of AAA [10], AAA [25], AAA [40] are 340, 400 and 460 respectively.
Q .34. Given two dimensional array A[10][20], base address of A being 100 and
width of each element is 4 bytes, find the location of A[8][15] when the array is
stored as a) column wise b) Row wise.
Ans:
1. Column wise
Address[i][j] = B + w[n(J-0)+(I-0)]
Address A[8][15] = 100 + 4[10(15-0) + (8-0)] = 100 + 4[150 + 8] = 100 + 632 = 732.
2. Row wise
Address[i,j] = B + w[n(I-0) + (J-0)]
Address A[8][15]=100 + 4[20(8 0) + (15 0)]= 100 + 4[160 + 15]= 100 + 700 = 800
Q. 35. Write a C++ function to find and display the sum of each row and each
column of a 2 dimensional array of type float. Use the array and its size as
parameters with float as the return type.
Ans:
void rowcolsum(float a[10][10], int r, int c)
{
int i,j;
float rs[10], cs[10];
for(i=0;i
{
rs[i] = 0;
for(j=0;j
rs[i] = rs[i] + a[i][j];
}
for(j=0;j
{
cs[j] = 0;
for(i=0;i
cs[j] = cs[j] + a[i][j];
}
//Display 2 D array with row sum and column sum
for(i=0;i
{
for(j=0;j
cout<<A[I][J]<<
cout<<RS[I]<
}
for(j=0;j
cout<<CS[J]<<
cout<
}
Chapter 10 Linked Lists, Stacks and Queues
Q. 36. Differentiate between a FIFO list and LIFO list.
Ans: A FIFO (First In First Out) list processes its elements in first come first served basis.
A LIFO (Last In First Out) list is processed in LIFO manner i.e., elements that entered last are
removed first of all.
Q. 37. Transform the following expression to prefix and postfix form:
(A + B * C D)/E * F
Ans:
i) Postfix form
(A + B * C D)/E * F = ((A + (B * C)) D)/ (E * F)
= ((A + (BC*)) D)/ (EF *)
= ((ABC * +) D)/ (EF *)
= ((ABC * + D ))/EF*
= ABC * + D EF */
ii) Prefix form
(A + B * C D)/E * F = ((A + (B * C)) D)/ (E * F)
= ((A + (*BC)) D)/(*EF)
= ((+A * BC) D)/(*EF)
= (- + A * BCD)/(*EF)
= / - + A * BCD * EF
Operator Scanned
Stack Content
5, 6
5, 6, 9
5, 15
80
5, 15, 80
5, 15, 80, 5
5, 15, 400
5, -385
-1/77
ptr = top;
top = top -> next;
ptr -> next = null;
delete ptr;
}
}
Degree
Cardinality
Ans:
1. Degree: The number of attributes (columns) in a relation
determine the degree of a relation.
2. Cardinality: The number of tuples (rows) in a relation is called
the cardinality of the relation.
Chapter 12 Structured Query Language
Q. 43. What are DDL and DML?
Ans: The DDL provides statements for the creation and deletion of tables and indexes.
The DML provides statements to enter, update, delete data and perform complex queries on
these tables.
Book_Id
Book_Name
Author_Name
Publisher Pric
s
e
Type
Qty
F001
The tears
750
Fiction
10
F002
Thunderbolts
Anna Roberts
First Publ
700
Fiction
T001
My First C++
EPB
250
Text
10
T002
C++
Brainworks
A.W.Rossaine
TDH
325
Text
C001
Fast Cook
Lata Kapoor
EPB
350
Cookery 8
Table: Issued
Book_Id
Quantity _Issued
F001
T001
C001
Ans:
i.
ii.
iii.
iv.
v.
vi.
vii.
The new derived relation is known as the dual of the original relation.
Dual of (X + Y) . (X + Z) . (Y + Z) is X.Y + X.Z + Y.Z
Q. 46. Seven inverters are cascaded one after another. What is the output if the
input is 1?
Ans: 0.
Q. 47. Why are NAND and NOR gates called Universal Gates?
Ans: NAND and NOR gates are less expensive and easier to design. Also other functions (NOT,
AND, OR) can easily be implemented using NAND/NOR gates.
Q. 48. Obtain a simplified form for the following Boolean Expression using
Karnaughs Map:
F(a, b, c, d) = (0, 1, 2, 4, 5, 7, 8, 9, 10, 11, 14).
Ans:
1
1
Q. 49. State and prove Associative Law with the help of Truth Table.
Chapter 14 Communication and Network Concepts
Q .50. What is meant by Network Topology?
Ans: The pattern of interconnection of nodes in a network is called the network topology. Major
topologies are:
a. Star
b. Bus
c. Ring or Circular
d. Tree
e. Graph
Q. 51. What are Bridges? How do they differ from Repeaters?
Ans: Bridge is a device that links two networks together. Bridges differ from repeaters in their
capability of deciding whether a particular message is to be communicated on the other side or
not where as a repeater just amplifies the signal and pass it to other side.
Q. 52. Give the full form for the following:
a. MODEM
b. FM
c. AM
d. NFS
e. FTP
Ans:
i.
Modulator Demodulator
ii.
Frequency Modulation
iii.
Amplitude Modulation
iv.
v.