Sunteți pe pagina 1din 32

CBSE class XII Computer Science Imp. Ques. & Ans.

2008

Q. 1. Distinguish between an object and a class.


Ans: An object is an identifiable entity with some characteristics and behaviour. A class is a
group of objects that share common properties and relationships.
Q. 2. What is polymorphism? Give an example in C++ to show its implementation
in C++.
Ans: Polymorphism is the property by which the same message can be processed in more than
one form. This is of 2 types:
a.

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.

Q. 3. How does a pointer variable differ from a simple variable?


Ans: A simple variable stores a data value whereas a pointer variable stores a memory address
of another variable.
Q. 4. Name the header file to be included for the use of following built-in functions:
1. frexp( )
2. tooupper( )
3. getc( )
4. strcat( )
5. setw( )
Ans:

i.

math.h

ii.

ctype.h

iii.

stdio.h

iv.

string.h

v.

iomanip.h

Q. 5. Find the syntax errors:


# include(iostream.h)
void main( )
{
int X, Y;
cin>>X;
for(Y=0;Y<10,Y++)
if X = = Y
cout<
else
cout<
}
Errors:
File iostream.h should be in angled brackets < > i.e. #include
The condition and updation segments should be separated by a semicolon(;) i.e.
For(Y=0;Y<10;y++)
The condition should be enclosed in braces as shown below: i.e. if(X= = Y)
Statement should be terminated by ; i.e. cout<<Y;< p>
Q. 6. Will the following program execute successfully? If not, state the reason(s):

# 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< }

Ans: Output is:


20 23
10 23
11 11
Q. 8. Write a function in C++ to find the sum of the following series:
1+2+3+4+5+6+7+. upto n terms.
Ans:
#include
#include
int sum_series(int n);
void main( )
{
clrscr( );
int t = 0, sum = 0;
cout<<How many terms?;
cin>>t;
sum = sum_series(t);
cout<<\n The sum of the series 1+2+3+4+....upto<<T<<IS<
}
int sum_series(int n)
{
int s = 0;
for(int i =1;i<=n;i++)
s+=i;
return s;
}
Q. 9. Write a C++ function SUMFUN( ) having two parameters X of type double and
n of type integer with a result type as double to find the sum of the series given
below:

X + X2/3!+ X3/5!+.+ Xn/(2n-1)!


Ans:
double SUMFUN(double X, int N)
{
int i,j;
double sum = 0; fact;
for(i=1;i<=n;++i)
{
fact = 1;
for(j=1;j<=2*i-1;++j)
fact = fact * j;
sum = sum + pow(x,i)/fact;
}
return sum;
}
Chapter 2 Structures
Q. 10. Find the output of the following programs:
# include
struct point
{
int X,Y;
};
void show(point p)
{
cout<<P.X<<:<<P.Y<
}
void main( )
{
point u = {20, 10}, v, w;
v = u;

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.

Chapter 4 Classes and Objects


Q. 14. Describe the methods of accessing data members and member functions of a
class in the following cases:
i.

Inside the main program

ii.

Inside a member function of the same class

iii.

Inside a member function of another class.

Ans:
i.

The public members can be accessed using the syntax


Objectname.public membername;

ii.

All the members of the class are accessed as ordinary


members. No class name or object name is required here.

iii.

Here only the public members are accessed using the objects
of the same type i.e. Objectname.membername.

Q. 15. Define a class student with the following specifications:


Private members:
rollno integer
name character array of size 20
class_st character array of size 8
marks integer array of size 5
percentage float
calculate that calculates overall percentage marks and returns the percentage.
Public Members:
readmarks reads marks and invokes the calculate function
displaymarks prints the data.
Ans:
class student
{
int roll_no;
char name[20];
char class_st[8];
int marks[5];
float percentage;
float calculate;
public:
void readmarks( );
void displaydata( );
};
float student::calculate( )

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

Function 1 is referred to as constructor and it gets invoked


when an object gets created.

ii.

Function 3 is referred to as Destructor and it gets invoked


when an object goes out of scope.i.e. when its scope is over.

Q. 18. Define the following:


1. Default Constructor
2. Copy Constructor
Ans:
1. Default Constructor: A constructor that accepts no parameter
is called default constructor.
2. Copy Constructor: A constructor that is invoked when an
object is defined and initialized with another object. It takes
the following form:
classname(classname &);
Q. 19. Distinguish between the following two statements:
time T1(13, 10, 25); //statement 1
time T1 = time(13,10,25); //statement 2
Ans: The first statement creates the object T1 by calling the time constructor implicitly. On the
other hand, the second statement creates the object T1 by calling the time constructor
explicitly.
Q. 20. Predict the output for the following code:
#include
class student_rec
{

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.

Name the members, which can be accessed from the


member functions of class physicsbook.

ii.

Name the members, which can be accessed by an


object of class textbook.

iii.

Name the members, which can be accessed by an


object of class physicsbook.

iv.

What will be the size of an object (in bytes) of class


physicsbook.

Ans:
i.

Data Members: standard, topic


Member Functions: readtextbook( ), showtextbook( ),
readphysicsbook( ); showphysicsbook( ).

ii.

Member Functions: readtextbook( ), showtextbook( ).

iii.

Member Functions: readtextbook( ), showtextbook( ),


readphysicsbook( ), showphysicsbook( ).

iv.

68 bytes.

Chapter 7 Data File Handling


Q. 23. Write a user defined function in C++ to read the content from a text file
STORY.TXT, count and display the number of alphabets present in it.
Ans:
#include
void main( )
{
ifstream fin(STORY.TXT);
char ch;

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( ).

Q. 27. Write a program that displays the size of a file in bytes:


Ans:
#include
#include
#include
void main( )
{
clrscr( );
char filename[13];
cout<<Enter filename:\n;
cin.getline(filename, 13);
ifstream infile(filename);
if(!infile)
{
cout<<Cannot open <<FILENAME<<
exit(-1);
}
int no_bytes = 0;
char ch;
while(cin.get(ch))
no_bytes++;
cout<<File size is< no_bytes<< bytes;
getch( );
}
Chapter 8 Pointers
Q. 28. Distinguish between:
int * ptr = new int(5);
int *ptr = new int[5];

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 ++

Cannot modify const object

ptr = &j

Cannot modify const object


j = *ptr;

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

Q. 38. Transform the following expressions to infix form:


1. + - ABC
2. + A BC
Ans:
1. +(A B)C = (A B) + C
2. + A(B C) = A + (B C)
Q. 39. Evaluate the following postfix expression using a stack and show the
contents of the stack after execution of each operation
5, 6, 9, +, 80, 5, *, -, /
Ans: Scan from left to right

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

The result is: -1/77


Q. 40. Give the necessary declaration of a linked implemented stack containing
integer type numbers; also write a user defined function in C++ to pop a number
from this stack.
Ans:
struct stack
{
int info;
stack *next;
}*top, *ptr;
void pop( )
{
if(!top)
{
cout<<underflow \n;
exit(1);
}
else
{
cout<info;

ptr = top;
top = top -> next;
ptr -> next = null;
delete ptr;
}
}

Chapter 11 Database Concepts


Q. 41. What is a relation? What is the difference between a tuple and an attribute?
Ans: A relation is a table having atomic values, unique rows and unordered rows and columns.
A row in a relation is known as tuple whereas a column of a table is known as an attribute.
Q. 42. Define the following terms:
i.
ii.

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.

Q. 44. Given the following tables for a database LIBRARY:


Table: Books

Book_Id
Book_Name

Author_Name

Publisher Pric
s
e

Type

Qty

F001

The tears

William Hopkins First Publ

750

Fiction

10

F002

Thunderbolts

Anna Roberts

First Publ

700

Fiction

T001

My First C++

Brian & Brooke

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

Write SQL queries for (a) to (f):


a. To show Book name, Author Name, and Price of
books of EPB publishers.
b. To list the names of books of fiction type.
c. To display the names and price of the books in
descending order of their price.
d. To increase the price of al books of first publ by 50.
e. To display the Book_Id, Book_Name, and
Quantity_Issued for all books which have been
issued.
f. To insert a new row in the table Issued having the
following data: F002, 4.
g. Give the output of the following queries based on the
above tables:
1. SELECT COUNT (DISTINCT Publishers) FROM
Books.
2. SELECT SUM (Price) FROM Books WHERE
Quantity>5
3. SELECT Book_Name, Author_Name FROM Books
WHERE Price<500
4. SELECT COUNT (*) FROM Books.

Ans:
i.

SELECT Book_Name, Author_Name, Price


FROM Books WHERE Publishers = EPB;

ii.

SELECT Book_Name FROM Books WHERE Type = Fiction;

iii.

SELECT Book_Name, Price FROM Books ORDER BY Price


DESC;

iv.

UPDATE Book SET Price = Price + 50 WHERE Publishers =


First Publ;

v.

SELECT Books.Book_Id, Book_Name, Quantity_Issued


FROM Books, Issued WHERE Books.Book_Id =
Issued.Book_Id;

vi.
vii.

INSERT INTO issued VALUES(F002, 4);


i) 3
ii) 1350
iii) My First C++ Brian & Brooke
C++ Brainworks A.W.Rossaine
Fast Cook Lata Kapoor
iv) 5

Chapter 13 Boolean Algebra


Q. 45. State the principle of duality in Boolean Algebra and give the dual of the
Boolean expression (X + Y) . (X + Z) . (Y + Z)
Ans: Principle of Duality states that from every Boolean relation, another boolean relation can
be derived by
1. Changing each OR sign to AND and vice versa
2. Replacing each 1 by 0 and vice versa.

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

Quad 1 = ac , Quad 2 = ab , Pair 1 is abd, Pair 2 is bcd , Pair 3 is acd


The simplified form is: ac + ab + abd + bcd + acd

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.

Network File Server

v.

File Transfer Protocol.

S-ar putea să vă placă și