2021 Solution
2021 Solution
Somrat Rony
1. a) What is Object-Oriented Programming (OOP)? Write down its benefits for
programming.
• Encapsulation: Bundling the data (attributes) and methods (functions) that operate on
the data within one unit (object).
• Inheritance: Creating new classes (subclasses) from existing ones (superclasses) to reuse
code and share common functionality.
• Polymorphism: Objects can be treated as instances of their parent class, and methods can
behave differently based on the object that calls them.
• Abstraction: Hiding the internal complexity and showing only the relevant information
to the outside world.
Benefits:
• Code Reusability: Through inheritance, existing code can be reused in new applications.
• Modularity: Code is organized into objects, making it easier to manage and debug.
• Scalability: New features and functionalities can be added easily.
• Maintainability: Code is easier to modify and maintain.
• Security: Encapsulation helps in hiding data and protecting it from unauthorized access.
1. Game Development: OOP is used extensively in game engines (e.g., Unity, Unreal
Engine) to model characters, environments, and behaviors.
2. Graphical User Interfaces (GUIs): Applications like web browsers and desktop apps
use OOP to handle windows, buttons, menus, etc.
3. Mobile Applications: Most mobile app frameworks (like Android with Java/Kotlin, and
iOS with Swift) are based on OOP principles.
4. Real-Time Systems: OOP is used in systems like flight simulators and medical devices
for managing complex operations.
5. Web Development: Frameworks like Django (Python) and Laravel (PHP) apply OOP for
structuring web applications.
1. MATLAB
2. Simulink
3. ANSYS
4. COMSOL Multiphysics
5. Autodesk Simulation
In C programming, variables can be of different types depending on the data they hold:
1. Integer constant:
o Example: int x = 100; (100 is an integer constant)
2. Floating-point constant:
o Example: float y = 12.34; (12.34 is a floating-point constant)
3. Character constant:
o Example: char ch = 'A'; ('A' is a character constant)
4. String literal:
o Example: char str[] = "Hello"; ("Hello" is a string literal)
C.
3. a) Seven types of expressions:
• If statement: The if statement evaluates a condition and executes a block of code if the
condition is true.
o Example:
if (x > 0) {
printf("x is positive");
Switch statement: The switch statement selects one of many blocks of code to be executed
based on the value of a variable.
• Example:
switch (x) {
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
default:
printf("Other number");
}
While statement: The while loop keeps executing a block of code as long as the condition is
true.
• Example:
printf("%d", x);
x++;
case value1:
break;
case value2:
break;
default:
The switch statement evaluates an expression and compares its value against a list of cases.
When a match is found, the code block associated with that case is executed. If no case matches,
the default block (if present) is executed. The break statement ensures that only the matching
case is executed and then exits the switch.
4. c) If the break statement is not used...
True. If the break statement is not used after a matching case, the program continues to execute
all the subsequent case statements, regardless of whether they match or not. This is called fall-
through.
int x = 2;
switch (x) {
case 1:
printf("One");
case 2:
printf("Two");
case 3:
printf("Three");
#include <stdio.h>
int main() {
char ch;
scanf("%c", &ch);
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
break;
default:
return 0;
}
5.a
Answer: In situations where we need to execute a block of code repeatedly, we use loops in C, such as
the for, while, and do-while loops. These allow us to repeat the execution of a block of code based
on a condition, making our programs more efficient and reducing code redundancy. This is essential in
scenarios where repetitive tasks are required, such as iterating over data structures, performing
calculations multiple times, or generating a series of outputs.
(b) For Loop: Multiplication Table
Here’s the C program to print the multiplication table for a given number:
#include <stdio.h>
int main() {
int num, i;
scanf("%d", &num);
return 0;
Loops
#include <stdio.h>
int main() {
if(num % i == 0) {
break;
if(isPrime == 1) {
return 0;
}
(d) While Loop: Print Even Numbers Between 1 and n
#include <stdio.h>
int main() {
int i = 2, upperRange;
scanf("%d", &upperRange);
return 0;
}
6. a) General form of a C++ class with public, private variables, and a constructor and function
#include <iostream>
class MyClass {
private:
// Private variables
int privateVar;
public:
// Public variables
int publicVar;
// Constructor
MyClass(int pVar, int pubVar) {
privateVar = pVar;
publicVar = pubVar;
// Member function
void display() {
};
int main() {
obj.display();
return 0;
7. b) Program to accept an integer and output the factorial using C++ style I/O
#include <iostream>
private:
int number;
public:
Factorial(int num) {
number = num;
int calculateFactorial() {
int fact = 1;
fact *= i;
return fact;
void displayFactorial() {
cout << "Factorial of " << number << " is " << calculateFactorial() << endl;
};
int main() {
int num;
Factorial fact(num);
fact.displayFactorial();
return 0;
7. c) Program to accept an integer and output the square root using C++ style I/O
#include <iostream>
class SquareRoot {
private:
int number;
public:
SquareRoot(int num) {
number = num;
}
// Function to calculate square root
double calculateSquareRoot() {
return sqrt(number);
void displaySquareRoot() {
cout << "Square root of " << number << " is " << calculateSquareRoot() << endl;
};
int main() {
int num;
SquareRoot sqRoot(num);
sqRoot.displaySquareRoot();
return 0;
}
8. C++ Program for Evaluating a Function Using Conditional Operators
#include <iostream>
int main() {
double x, y;
cin >> x;
return 0;
}
#include <stdio.h>
#include <math.h>
double f(double x) {
return;
double c;
c = (a + b) / 2;
if (f(c) == 0.0) {
break;
b = c;
} else {
a = c;
int main() {
double a, b, tol;
scanf("%lf", &tol);
bisection(a, b, tol);
return 0;
}
9. b) Newton-Raphson Method for Locating Real Roots
#include <stdio.h>
#include <math.h>
double f(double x) {
double f_prime(double x) {
double x1;
int iterations = 0;
while (1) {
break;
}
x0 = x1;
int main() {
scanf("%lf", &x0);
scanf("%lf", &tol);
newton_raphson(x0, tol);
return 0;
}
8. c) Gauss-Jordan Elimination Method
1. x+2y+3z=9x + 2y + 3z = 9x+2y+3z=9
2. 2x+3y+1z=82x + 3y + 1z = 82x+3y+1z=8
3. 3x+1y+2z=73x + 1y + 2z = 73x+1y+2z=7
[123923183127]\begin{bmatrix} 1 & 2 & 3 & 9 \\ 2 & 3 & 1 & 8 \\ 3 & 1 & 2 & 7
\end{bmatrix}
Solution: