Skip to content

Commit 8f677e0

Browse files
committed
OOPs PYQs practice
1 parent 3b5ff17 commit 8f677e0

File tree

12 files changed

+304
-0
lines changed

12 files changed

+304
-0
lines changed

practice/p1.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
void updateByReference(int &x)
5+
{
6+
cout << &x << " : " << x << endl;
7+
x += 10;
8+
}
9+
10+
void updateByAddress(int *x)
11+
{
12+
cout << x << " : " << *x << endl;
13+
*x += 10;
14+
}
15+
16+
int main()
17+
{
18+
int a = 5;
19+
updateByReference(a);
20+
cout << "a after updateByReference: " << a << endl;
21+
updateByAddress(&a);
22+
cout << "a after updateByAddress: " << a << endl;
23+
return 0;
24+
}

practice/p10.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
//package practice;
3+
import java.util.*;
4+
5+
public class p10 {
6+
void sort(int[] args) {
7+
int i, j, temp;
8+
for (i = 0; i < args.length - 1; i++) {
9+
for (j = 0; j < args.length - i - 1; j++) {
10+
if (args[j] > args[j + 1]) {
11+
temp = args[j];
12+
args[j] = args[j + 1];
13+
args[j + 1] = temp;
14+
}
15+
}
16+
}
17+
}
18+
19+
void display(int[] args) {
20+
int i;
21+
for (i = 0; i < args.length; i++) {
22+
System.out.print(args[i] + " ");
23+
}
24+
System.out.println();
25+
}
26+
27+
public static void main(String[] args) {
28+
p10 ob = new p10();
29+
int i;
30+
int[] arr = new int[5];
31+
for (i = 0; i < 5; i++) {
32+
arr[i] = Integer.valueOf(args[i]);
33+
}
34+
35+
ob.display(arr);
36+
ob.sort(arr);
37+
System.out.println("Sorted args:");
38+
ob.display(arr);
39+
}
40+
}

practice/p11.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Test
5+
{
6+
public:
7+
int i;
8+
9+
Test(int idx)
10+
{
11+
i = idx;
12+
cout << "Object Created: " << i << endl;
13+
}
14+
15+
~Test()
16+
{
17+
cout << "Object Destroyed: " << i << endl;
18+
}
19+
};
20+
21+
int main()
22+
{
23+
Test t1(1);
24+
Test t2(2);
25+
return 0;
26+
}

practice/p12.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Department {
2+
Department() {
3+
System.out.println("CSE");
4+
}
5+
6+
Department(String name) {
7+
this();
8+
System.out.println("CSE: String Constructor" + name);
9+
}
10+
}
11+
12+
public class p12 extends Department {
13+
p12() {
14+
System.out.println("Third year ");
15+
}
16+
17+
p12(String name) {
18+
this();
19+
System.out.println("Third year: String Constructor" + name);
20+
}
21+
22+
public static void main(String[] args) {
23+
new p12("Section A+B+C");
24+
}
25+
}

practice/p2.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
void printVal(void *ptr)
5+
{
6+
cout << "Value = " << *(int *)ptr << endl;
7+
}
8+
9+
int main()
10+
{
11+
int a = 7;
12+
printVal((int *)&a);
13+
return 0;
14+
}

practice/p3.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class test
5+
{
6+
public:
7+
test() { cout << "Cons" << endl; }
8+
~test() { cout << "Dest" << endl; }
9+
};
10+
11+
int main()
12+
{
13+
for (int i = 0; i < 2; i++)
14+
test obj;
15+
return 0;
16+
}

practice/p4.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class MyClass
5+
{
6+
private:
7+
static int count; // static variable to track number of objects
8+
9+
public:
10+
// Constructor
11+
MyClass()
12+
{
13+
count++;
14+
cout << "Object created. Total alive: " << count << endl;
15+
}
16+
17+
// Destructor
18+
~MyClass()
19+
{
20+
count--;
21+
cout << "Object destroyed. Total alive: " << count << endl;
22+
}
23+
24+
// Static function to access count
25+
static int getCount()
26+
{
27+
return count;
28+
}
29+
};
30+
31+
// Initialize static variable outside the class
32+
int MyClass::count = 0;
33+
34+
int main()
35+
{
36+
MyClass obj1;
37+
{
38+
MyClass obj2, obj3;
39+
cout << "Currently alive: " << MyClass::getCount() << endl;
40+
} // obj2 and obj3 go out of scope here
41+
42+
cout << "Currently alive after block: " << MyClass::getCount() << endl;
43+
44+
return 0;
45+
}

practice/p5.java

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class p5 {
2+
public static void main(String[] args) {
3+
int i;
4+
System.out.println("Command Line Args:");
5+
for (i = 0; i < args.length; i++) {
6+
System.out.print(args[i] + " ");
7+
}
8+
9+
System.out.println();
10+
}
11+
}

practice/p6.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//package practice;
2+
3+
public class p6 {
4+
double getArea(int r) {
5+
return (22.0 / 7.0) * r * r;
6+
}
7+
8+
double getArea(int l, int b) {
9+
return l * b;
10+
}
11+
12+
public static void main(String[] args) {
13+
p6 ob = new p6();
14+
System.out.println("Area of Circle = " + ob.getArea(7));
15+
System.out.println("Area of Rectangle = " + ob.getArea(3, 4));
16+
}
17+
}

practice/p7.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Animal {
5+
public:
6+
virtual void sound() { // virtual method
7+
cout << "Animal makes a sound" << endl;
8+
}
9+
};
10+
11+
class Animal2 {
12+
public:
13+
void sound() { // non-virtual method
14+
cout << "Animal makes a sound in 2" << endl;
15+
}
16+
};
17+
18+
class Dog : public Animal {
19+
public:
20+
void sound() override {
21+
cout << "Dog barks" << endl;
22+
}
23+
};
24+
25+
class Dog2 : public Animal2 {
26+
public:
27+
void sound() {
28+
cout << "Dog barks 2" << endl;
29+
}
30+
};
31+
32+
int main() {
33+
Animal* a;
34+
Animal2* b;
35+
Dog d;
36+
Dog2 d2;
37+
a = &d;
38+
a->sound(); // Resolved at runtime
39+
b = &d2;
40+
b->sound();
41+
return 0;
42+
}

practice/p8.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main() {
5+
void *ptr;
6+
int x = 6;
7+
ptr = &x;
8+
9+
// Print int
10+
cout << "Value of x: " << *((int *)ptr) << endl;
11+
12+
// Reassign ptr to a char array
13+
char str[] = "Hello, world!";
14+
ptr = str; // now ptr points to char array
15+
16+
// Print char array
17+
cout << "Character array: " << (char *)ptr << endl;
18+
19+
return 0;
20+
}

practice/p9.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class test
5+
{
6+
int var;
7+
8+
public:
9+
test()
10+
{
11+
var = 0;
12+
cout << var << endl;
13+
}
14+
test(int a)
15+
{
16+
var = a;
17+
cout << var << endl;
18+
}
19+
};
20+
21+
int main()
22+
{
23+
test obj;
24+
}

0 commit comments

Comments
 (0)