0% found this document useful (0 votes)
765 views

Computer Science (Part - I) Practical File Answer Sheet

This document appears to be a practical file answer sheet for a Computer Science exam containing programming problems and solutions in C++. It includes 7 programming problems and their solutions demonstrating concepts like data swapping, linear search, binary search, constructors/destructors, operator overloading, inheritance. For each problem, the document provides the problem statement, sample code to write, and a note to write the output on the printed sheet.

Uploaded by

Sarthak Lamba
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
765 views

Computer Science (Part - I) Practical File Answer Sheet

This document appears to be a practical file answer sheet for a Computer Science exam containing programming problems and solutions in C++. It includes 7 programming problems and their solutions demonstrating concepts like data swapping, linear search, binary search, constructors/destructors, operator overloading, inheritance. For each problem, the document provides the problem statement, sample code to write, and a note to write the output on the printed sheet.

Uploaded by

Sarthak Lamba
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Page 1 of 17

STD. 12th : Computer Science (Part - I)


(C++, HTML practical’s)
Practical file Answer sheet:
****************************************************
* Note: C++ code may be vary depends on compiler(IDE) - standard
library/header files but basic logic / code Implementation will not affect
/* 1. Swapping call by value */
 Write a program in C++ that exchange data (Call by Value) using SWAP
function i.e. void swap (int, int) to interchange the given two numbers.
 Enter the program and verify proper execution of the same on the computer.
The output must list the given numbers before as well as after swapping.
Note: Write the below program code on proper printout of practical file sheet pdf )
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
cout<<"Enter 1st Number: ";
cin>>num1;
cout<<"Enter 2nd Number: ";
cin>>num2;

//displaying numbers before swapping


cout<<"Before Swapping: First Number: "<<num1<<" Second Number:
"<<num2;

// swapping function call


swap(num1, num2);

cout<<" \nAfter Swapping: First Number: "<<num1 <<" Second Number:


"<<num2;
return 0;
}

void swap(int num1,int num2){


int temp;
temp=num1;
num1=num2;
num2=temp;
}
OUTPUT :- (attach extra blank pages also and write output as per program )
(Write output here/ after the program written on your sheet )
Page 2 of 17

/* 2. Swapping call by Reference */


 Write a program in C++ that exchange data (Call by Reference)
using SWAP function that is void swap (int*, int*) to
interchange the given two numbers.
 Enter the program and verify proper execution of the same on the
computer.
 The output must list the given numbers before as well as after
swapping.
(Note: Write the below program code on proper printout of practical file sheet pdf)
#include<iostream>
#include<conio.h>
using namespace std;

void swap(int *p1, int *p2)


{
int temp;
temp= *p1;
*p1= *p2;
*p2= temp;
}

int main()
{
//clrscr();
int a, b;
//void swap(int &,int &);
cout<<"Enter two values:";
cin>>a>>b;
cout<<"\n Befor swapping: a= "<<a <<" b= "<<b;
swap(&a, &b);
cout<<"\n After swapping: a= "<<a <<" b= "<<b;
getch();
//return 0;
}

OUTPUT :-
( Write output….. )
Page 3 of 17

/* 3. Linear Search */
 Write a program in C++, that first initializes an array of given 10 integer
numbers. The program must verify whether a given element belongs to this
array or not, using LINEAR SEARCH technique.
 The element (to be search) is to be entered at the time of execution. If the
number is found, the program should print: “The Number is Found”
otherwise, it should print: “The number is not Found”.
 Enter the program and verify proper execution of the same on the computer.
Note: Write the below program code on proper printout of practical file sheet pdf)

#include<iostream>
using namespace std;

int main()
{
int a[20],n,x,i,flag=0;
cout<<"How many elements?";
cin>>n;
cout<<"\nEnter elements of the array\n";

for(i=0;i<n;++i)
cin>>a[i];
cout<<"\nEnter element to search:";
cin>>x;
for(i=0;i<n;++i)
{
if(a[i]==x)
{
flag=1;
break;
}
}
if(flag)
cout<<"\nElement is found at position "<<i+1;
else
cout<<"\nElement not found";
return 0;
}
Page 4 of 17

/* 4. Binary Search */
 Write a program in C++, that first initializes an array of
given 10 integer numbers. The program must verify
whether a given element belongs to this array or not, using
BINARY SEARCH technique.
 The element (to be search) is to be entered at the time of
execution. If the number is found, the program should
print: “The Number is Found” otherwise, it should print:
“The number is not Found”.
 Enter the program and verify proper execution of the same
on the computer.
Note: Write the below program code on proper printout of practical file sheet pdf)

#include<iostream>
using namespace std;
int main()
{
int search(int [],int,int);
int n,i,a[100],e,res;
cout<<"How Many Elements:";
cin>>n;
cout<<"\n Enter Elements of Array in Ascending order\n";
for(i=0;i<n;++i)
{
cin>>a[i];
}
cout<<"\nEnter element to search:";
cin>>e;
res=search(a,n,e);
if(res!=-1)
cout<<"\nElement found at position "<<res+1;
else
Page 5 of 17

cout<<"\nElement is not found....!!!";


return 0;
}

int search(int a[], int n, int e)


{
int f,l,m;
f=0;
l=n-1;

while(f<=l)
{
m=(f+l)/2;
if(e==a[m])
return(m);
else
if(e>a[m])
f=m+1;
else
l=m-1;
}
return -1;
}
Output:
(attach extra blank pages also and write output as per program requirement )
Page 6 of 17

/* 5. Constructor & destructor */


Write a C++ Program using OOP concept to create Line class
with constructor & destructor. The program must print the
message “object is being created” during initialization. The
program must display “object is being deleted” message when
the destructor is called for the object when it reached the end of
it’s scope.
Note: Write the below program code on proper printout of practical file sheet pdf)

#include <iostream>
using namespace std;
class Line {
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor declaration
~Line(); // This is the destructor: declaration
private:
double length;
};

// Member functions definitions including constructor


Line::Line(void) {
cout << "Object is being created" << endl;
}

Line::~Line(void) {
cout << "Object is being deleted" << endl;
}

void Line::setLength( double len ) {


length = len;
}
Page 7 of 17

double Line::getLength( void ) {


return length;
}

// Main function for the program

int main() {

Line line;
// set line length
line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;
return 0;
}

Output:
(attach extra blank pages also and write output as per program requirement )
Page 8 of 17

/* 6. Binary Operator overloading */


Write a program in C++ for addition/subtraction of two complex
number using operator overloading. The program should print
the given complex number and their addition/substraction.
Note: Write the below program code on proper printout of practical file sheet pdf)

#include<iostream>
#include<conio.h>
using namespace std;

class complex {
int a, b;
public:

void getvalue() {
cout << "Enter the value of Complex Numbers a,b:";
cin >> a>>b;
}

complex operator+(complex ob) {


complex t;
t.a = a + ob.a;
t.b = b + ob.b;
return (t);
}

complex operator-(complex ob) {


complex t;
t.a = a - ob.a;
t.b = b - ob.b;
return (t);
}
Page 9 of 17

void display() {
cout << a << "+" << b << "i" << "\n";
}

};

int main() {
// clrscr();
complex obj1, obj2, result, result1;

obj1.getvalue();
obj2.getvalue();

result = obj1 + obj2;


result1 = obj1 - obj2;

cout << "Input Values:\n";


obj1.display();
obj2.display();

cout << "Result:";


result.display();
result1.display();
return 0;

// getch();
}
Output:
(attach extra blank pages also and write output as per program requirement )
Page 10 of 17

/* 7. Single inheritance / Implementing Inheritance */


Write a C++ program, using OOP to demonstrating the
implementation of single inheritance, having two classes.
Enter the program and verify the proper execution of the same
on the computer.
Note: Write the below program code on proper printout of practical file sheet pdf)

#include<iostream>
#include<conio.h>
using namespace std;

class emp {
public:
int eno;
char name[20], des[20];

void get() {
cout << "Enter the employee number:";
cin>>eno;
cout << "Enter the employee name:";
cin>>name;
cout << "Enter the designation:";
cin>>des;
}
};

class salary : public emp {


float bp, hra, da, pf, np;
public:
void get1() {
cout << "Enter the basic pay:";
cin>>bp;
cout << "Enter the Humen Resource Allowance:";
cin>>hra;
Page 11 of 17

cout << "Enter the Dearness Allowance :";


cin>>da;
cout << "Enter the Profitablity Fund:";
cin>>pf;
}

void calculate() {
np = bp + hra + da - pf;
}
void display() {
cout << eno << "\t" << name << "\t" << des << "\t" << bp
<< "\t" << hra << "\t" << da << "\t" << pf << "\t" << np << "\n";
}
};

int main() {
int i, n;
char ch;
salary s[10];
cout << "Enter the number of employee:";
cin>>n;
for (i = 0; i < n; i++) {
s[i].get();
s[i].get1();
s[i].calculate();
}
cout << "\ne_no \t e_name\t des \t bp \t hra \t da \t pf \t np \n";
for (i = 0; i < n; i++) {
s[i].display();
}
getch();
return 1;
}
Page 12 of 17

Practical No. 8 : /* Study of Virtual Function */


Note: Write the below program code on proper printout of practical file sheet pdf)

#include<iostream>
#include<conio.h>
using namespace std;

class Person{
public: virtual void print(){
cout<<"\n The Name of person is BOB";
}
};

class Student: public Person{


public:
void print(){
cout<<"\n The Name of Student is TOM";
}
};

int main()
{
//clrscr();
Person *p;
Person p1;
p=&p1;
p->print();

Student s1;
p= &s1;
p->print();
getch();
}
Page 13 of 17

Practical No. 9 : /* File Handling in C++ */


Note: Write the below program code on proper printout of practical file sheet pdf)
#include<iostream>
#include<fstream>
#include<conio.h>
using namespace std;

int main(){
ifstream fcountry;
ifstream fcap;
char fnm1[20], fnm2[20], str1[20], str2[20];
//clrscr();
cout<<"\n enter country file name- ";
cin>>fnm1;
fcountry.open(fnm1) ;

if(!fcountry)
cout<<"\n Country file not found";
else{
cout<<"\n Enter capitl filename-";
cin>>fnm2;
fcap.open(fnm2);
if(!fcap)
cout<<"\n Capital file not found";
else{
Page 14 of 17

while(!fcountry.eof())
{
fcountry.getline(str1,20,'\n');
cout<<str1<<"\t";
fcap.getline(str2,20,'\n');
cout<<str2<<endl;
}
}
}

fcap.close();
fcountry.close();
getch();

}
Page 15 of 17

Practical 10 ( Refer recorded practical ‘link)

Create a simple HTML page on the following topics:


Computer Manufacturer or Software Development
Company.
 The page must consist of a scrolling marquee displaying an
appropriate Message.
 The page must include a table with at least 5 rows and 3
columns having merged cells at least at 1 place.
 The page must also display an image, such that when the
same is viewed through a browser and the mouse is placed
(hovered) on the Image, an appropriate text message should
be displayed.
 The image itself should also act as a hyper-link to another
page.
(Write HTML code as per file name/pages)
Page 16 of 17

Practical 11. (Refer Recording-self practice program)


Create a simple HTML page on the following topics:
College Profile-website:
 The page must consist of at least 3 paragraphs of text.
 The page must have an appropriate title, background color
or background image, and hyper-links to other pages(2/3
pages).The paragraphs must have text consisting of
different colors and styles in terms of alignment and Font
Size.
 Save the file and view the same using any HTML Browser.
Verify functioning of the Hyper-links.
Page 17 of 17

Practical 12. (VB - Script program)


Write a simple VB scripts to do the following task when executed:
i) Collect information of name, date of Birth.
ii) Using the date of birth, calculate the age.
Incorporate this script in an HTML page and view the page using a
browser offline.
Note: Write the below program code on proper printout of practical file sheet pdf)

You might also like