Java Slips Solutions PDF
Java Slips Solutions PDF
Calculate and
// as well as area of a rectangle by using inline function. SLIP NO 12
#include <iostream>
using namespace std;
inline void calculate (double length, double width, double& area, double& perimeter)
area = length * width;
perimeter = 2 * (length + width);
int main()
~Array() { I
delete[] arrs
void displayMedian() {
sort(arr, arr + size);
if (size % 2 == 0) {
int mid = size / 2j
cout <« "Median of the array is:"<< (arr[midT + arr[mid - 1]) /
2.0 << endl;
else {
int mid = size / 2;
cout << "Median of the array is:"<< arr[mid] << endl;
}
void readArray() {
cout << "Enter << size <<elements of the array:<< endl;
for (int i = 0; issizes i+)
cin >> arr[i];
int main() {
int nj
cout << "Enter the size of the array:
cin >> n
Array a(n)
a.readArray() ;
a.displayMedian()
retun O;
File Edit View
I| Q1.Write a C++ program to implement a class student to overload following functions as follows:
1/ a. int maximum(int, int) - returns the maximum score of two students
// b. int maximum(int *a, int arraylength) - returns the maximum score from an array a' SLIP NO 13
#include <iostream>
using namespace std;
class student {
public :
int maximum(int a, int b) {
return a > b ?a : b;
}
int maximum (int *a, int arraylength) {
int maxscore = a[0];
for(int i-1; i<arraylength; i++) {
if(a[i] > maxscore) {
maxscore = a[i];
}
return maxscore;
}B
int main() {
student s;
int scorel = 80, score2 90;
int scores [] = {85, 92, 88, 90, 87};
int num_scores = sizeof (scores) /sizeof (scores [0]);
cout << "Maximum score of two students: " << s.maximum(scorel, score2) << endl;
cout << "Maximum score from an array: << S.maximum(scores, num_scores) << endl;
return O;
class Distance which contains dat
// Q2.Write a Ct+ program to create a
#include <iostream> SLIP NO 13
using namespace std;
class Distance {
private:
int kilometer;
int meter;
public:
Distance() {
kilometer = 0;
meter = 0;
}
bool operator>(Distance d2) {
int total meter1 = kilometer * 1000 + meter;
int total meter2 = d2.kilometer * 1000 + d2. meter;
return total meter1 > total meter2;
}
private:
void normalize distance() {
if(meter = 1000) {
int extra kilometer = meter / 1000;
meter %= 100;
kilometer += extra kilometer;
}
int main() {
Distance d1 d25
cout << "Enter distance 1:" << endl;
d1.accept distance()s
cout << "Enter dístance 2:" << endl;
d2.accept distance()5
cout << endl;
cout << "Distance 1:" << end;
d1.display_distance() ;
cout < "Distance 2:" << endl;
d2.display_distance();
cout <« endl;
if (d1 > d2)
cout << "Distance 1 is greater than distance 2" << endl;
} else {
cout << "Distance 2 is greater than distance 1" << endl
return 0;
File tdit View
int main() {
int x = 10, y = 20;
cout << "Before swap: X = <<<x X << , y = << y << endl;
Swap(x, y);
cout << "After swap: x = << X << " << y << endl;
return 0;
}
I/ or 02. Create a class Fraction that contains two data members as
numerator and denominator
I/Write a C+t program to overload following operators
1/a. Unary (pre and post both)
I/b. <« and >> verload as friend functions//not understand SLIP NO..14
#include <iostream>
using namespace std;
class Fraction {
private:
int numerator, denominator;
public:
Fraction(int n = 0, int d = 1) : numerator (n), denominator(d) }
friend istream& operator>> (istream& in, Fraction& f);
friend ostream& operator<<(ostream& out, const Fraction& f);
Fraction operator++(); I/ pre-increment
Fraction operator++(int); // post-increment
}
istream& operator>>(istream& in, Fraction& f) {
in >> f.numerator >> f.denominator:
return in;
int main() {
Fraction fi(3, 4), f2;
cout << "Enter fraction
cin >> f2; (numerator/denominator): ";
cout << "f1 = <« f1 << endl;
cOut << "f2 = << f2 << endl;
+fl;
cout << "After pre-increment, f1 = " << f1 << endl;
f2++;
cout << "After post-increment, f2 " << f2 << endl;
return 0;
que_n que_nO_5 que_ que_noO_| que_n( que_nO_2 que_n que_iO
File Edit View
1/ Q1.Write a C++ program to check minimum and maximum of two integer number (use inline
1/ function and conditional operator) SLIP NO 15
#include <iostream>
using namespace std;
inline int max(int a, int b) {
return (a > b) ?a: b;
int main() {
int num1, num2;
cout << "Enter two integer numbers: ";:
cin >> num1 >> num2;
cout << "Maximum of " << num1 << " and " < num2 <« " is << max(num1, num2) << endl;
cout <« "Minimum of << num1 << " and " << num2 << is << min(num1, num2) << endl;
return 0;
}
or 02. Write C++ progran to create a
I/ Designation and Salary. class Empluyee containing data menbers Emp
ro, Emp Nane.
Il Constructor. Also Nite Create and initialize the objects using default, paraneterized and CoN
meber function to calculate Income tax of the
I/ of salary. eployee which is 2%
#include <iostream)
#include (string>
Using anespace std;
class Enployee {
private:
int Emp no;
string Errp_Name ;
string Designation;
double Salary;
public:
I/ Default Constructor
Employee() {
Emp no= 0;
Erp_Name =
Designation =
Salary = ;
I/ Paraneterized Constructor
Eployee(int enpNo, string empame, string designation, double salary) (
Brp no =empNo;
Ep Name = erpName;
Designation = designation;
Salary = salaryS
}
// copy Constructor
Employee(const Enployee& e) {
Enp_no = e.Brp_ no;
Enp Name = e.Emp Name
Designation = e.Designation;
Salary = e.Salary;
int main() {
1/ Initializing objects using different constructors
Employee elj
Enployee e2(191, "John Doe", "Aarnager", 5O000);
Enployee e3 = e2;
I/ Displaying Enployee details
cout << "Enployee 1 details:" <% endl;
el,displayEsployee();
cout <K endi;
I| Q1. Write a C++ program to create a class Number which contains two integer data members. Create
I/ and initialize the object by using default constructor, parameterized constructor. Write a
I| member function to display maximum from given two numbers for all objects.
#include<iostream>
using namespace std; SLIP NO 16
class Number {
private:
int num1, num2;
public:
Number() {
numl = 0;
num2 : 0;
void displayMax() {
cout << "The maximum of " << num1 << " and " << num2 << " is " << ((num1 > num2)? num1 : num2) <« endl;
};
int main() {
Number objl, obj2 (8, 5), obj3(7, 2);
obj1. displayMax (0;
obj2.displayMax();
obj3.displayMax ();
return e;
}
I| or Q2. Create a class Time containing members as: 16 SLIP
1| - hours
|| - mínutes
1|- seconds
1/ Write a C+t program for
overloading
I/ also write a member function operators >> and << to accept and display a Time
to display tine in total seconds.//not understand logic
#include <iostream>
public:
Fraction(int n, int d) {
numerator = n;
denominator = d;
Fraction operator++() {
numerator += denominator;
return *this;
Fraction operator--() {
numerator -= denominator;
return *this;
void print() {
cout << numerator <<" << denominator << endl;
}
int main() {
Fraction f1(1, 2)
Fraction f2(3, 4);
Fraction f3 ++fl;
Fraction f4 =--f2;
Fraction f5 = f1 * f2;
cout << "f1 after
increment:"
f1.print();
cout << "f2 after decrement: ";
f2.print()
cout K "f1*F2
f5-print();
return 0;
// Q1.Write a C++ program to calculate following series:
I/ (1) + (1+2) + (1+2+3) + (1+2+3+4) +
1/ +(1+2+3+4+...+n)
#include <iostream> SLIP NO 18
using namespace std;
int main() {
int n
cout <« "Enter the value of n: ";
cin >> n;
int sum = Ø;
for (int i = 1; i <= n; i++) {
int inner sum = 0;
for (int j = 1; j <= i; j++) {
inner sum += j;
}
sum += inner_ sum;
}
cout << "Sum of the series is: " << sum << endl3
return 0;
and
I/ 02. Write a C++ program to read student information such as rollno, name
I/ students. Write the student information using file handling
#include <iostream> SLIP NON 18
#include <fstream>
using namespace std;
struct Student {
int roll_no;
string name;
float percentage;
int main() {
int n;
cout << "Enter the number of students:
cin >> n
Student students[n];
for (int i = 0; i < n; it+) {
cout << "Enter the information for student" << i+1 << ":\n";
cout << "Roll No: ";
cin >> students [i] . roll_no;
cout << "Name:
cin.ignore () ;
getline(cin, students[i] .name);
cout << "Percentage : ";
cin >> students [i].percentage;
file.close() ;
cout << "Student information written to file successfully. \n"
return 0;
que_n que_no_2 que_n que_n que_no_2 que_no_3 que_n que_no_2 que
File Edit View
int main() {
int num;
cout << "Enter a number:
cin >> num;
return e;
}
1/ Q2. Design a two base classes Employee (Name, Designation) and Project (Pr
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
SLIP NO 19
class Employee {
protected:
string name;
string designation;
public:
Employee() )
Employee (string n, string d) {
name = n;
designation = d;
void input() {
cout << "Enter Employee Name:
cin >> names
cout << "Enter Employee Designation: ";
cin >> designation;
void display() {
cout << "Employee Name: << name << endl;
cout << "Employee Designation: << designation << endl;
class Project {
protected:
int project_id;
string title;
public:
Project() (}
Project (int id, string t) {
project_id = id;
title = t;
void input () {
cout << "Enter Project ID: ";
cin >> project_id;
cout << "Enter Project Title : ";
cin >> title;
u1.Write a C++ progra to sort integer and
float array elaents in ascending oner by usir
I| fuxtion overloating SLIP ND 29
#include <iostreaD
using namespace std;
int nain() {
int arr1[] = {5, 2, 7, 1, 8};
float arr2[] = {3.5, 2.0, 7.8, 1.2, 6.4};
int n1 = sizeof (arr1) /
int n2 = sizeof(arr2) / sizeof(arr1[0]) ;
sizeof(arr2[0]);
sortArray(arr1, n1);
sortArray(arr2, n2);
cout << "Sorted Integer
pritArray(arr1, n1); Array:"
cout << "Sorted Float
printArray(arr2, n2); Array:M;
return 0;
"Sample.txt" file. Store a l t
I| or 02. Write a C++ program to read the contents of a
#include <iostream> SLIP NO 20
#include <fstream>
#include <ctype.h>
using namespace std;
int main() {
ifstream fin;
fin.open("Sample.txt") ;
else if (isdigit(ch)) {
fout digit << chË
if (isalpha(ch)) {
if (islower (ch)) {
ch = toupper(ch);
else {
ch = tolower(ch);
fin.close();
fout_ upper.close()5
fout lower.close();
fout digit.close();
fout convert.close()
return 0;
}