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

2021 Solution

The document discusses Object-Oriented Programming (OOP), its benefits, and applications, including game development and web development. It also covers various programming concepts in C, such as variable types, expressions, control statements, loops, and examples of C++ classes. Additionally, it presents numerical methods for root finding, such as the bisection method and Newton-Raphson method, along with a brief mention of the Gauss-Jordan elimination method.

Uploaded by

tazicety1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

2021 Solution

The document discusses Object-Oriented Programming (OOP), its benefits, and applications, including game development and web development. It also covers various programming concepts in C, such as variable types, expressions, control statements, loops, and examples of C++ classes. Additionally, it presents numerical methods for root finding, such as the bisection method and Newton-Raphson method, along with a brief mention of the Gauss-Jordan elimination method.

Uploaded by

tazicety1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

2021 YEAR QUESTION SOLUTION

Somrat Rony
1. a) What is Object-Oriented Programming (OOP)? Write down its benefits for
programming.

Object-Oriented Programming (OOP) is a programming paradigm that organizes software


design around data, or objects, rather than functions and logic. An object can be defined as a data
field that has unique attributes and behavior. The key concepts of OOP are:

• 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. b) Five applications of OOP:

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. c) Five names of simulation software that help with numerical solutions:

1. MATLAB
2. Simulink
3. ANSYS
4. COMSOL Multiphysics
5. Autodesk Simulation

2. a) Describe the different types of variables used in C programs.

In C programming, variables can be of different types depending on the data they hold:

1. int: Used to store integer values (whole numbers) without decimals.


o Example: int age = 25;
2. float: Used for floating-point numbers (numbers with decimal points).
o Example: float temperature = 36.5;
3. char: Stores single characters.
o Example: char grade = 'A';
4. double: Similar to float but allows for more precision and can store larger floating-
point numbers.
o Example: double pi = 3.14159;
5. void: Used when a function does not return a value, and void* is used for pointers with
no specific data type.
6. Pointer variables: Store memory addresses of other variables.
o Example: int* ptr = &age;

2. b) Examples of different types of literals (constants):

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:

1. Constant expressions: These involve constant values that cannot be altered.


o Example: int x = 5 + 10; (5 + 10 is a constant expression).
2. Arithmetic expressions: Expressions that involve arithmetic operators like +, -, *, /, etc.
o Example: int result = 5 * 2;.
3. Relational expressions: These expressions compare two values and return a Boolean
result (true or false).
o Example: x > 10.
4. Logical expressions: Combine two or more relational expressions using logical operators
like && (AND), || (OR), ! (NOT).
o Example: x > 10 && y < 5.
5. Assignment expressions: Used to assign a value to a variable.
o Example: int a = 5;.
6. Conditional expressions: Also known as ternary operators, these are a compact form of
if-else statements.
o Example: int max = (x > y) ? x : y;.
7. Function call expressions: These are expressions that call a function and return a result.
o Example: int sum = add(5, 10); (where add is a function).
3. b) Explanation of logical statements:

• 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:

while (x < 10) {

printf("%d", x);

x++;

5. a) Basic syntax of switch...case:


switch (expression) {

case value1:

// Code to execute if expression matches value1

break;

case value2:

// Code to execute if expression matches value2

break;

default:

// Code to execute if expression doesn’t match any case

4. b) Operation of the switch statement:

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");

4. d) C program to determine if a character is a vowel using


switch case:

#include <stdio.h>

int main() {

char ch;

printf("Enter a character: ");

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':

printf("%c is a vowel.\n", ch);

break;

default:

printf("%c is not a vowel.\n", ch);

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;

printf("Enter a number: ");

scanf("%d", &num);

for(i = 1; i <= 5; i++) {

printf("%d * %d = %d\n", num, i, num * i);

return 0;

(c) Prime Numbers Between 2 and 100 Using Nested

Loops
#include <stdio.h>

int main() {

int num, i, isPrime;

printf("Prime numbers between 2 and 100 are:\n");

for(num = 2; num <= 100; num++) {

isPrime = 1; // Assume number is prime

// Check if num is divisible by any number from 2 to sqrt(num)

for(i = 2; i <= num / 2; i++) {

if(num % i == 0) {

isPrime = 0; // Not a prime number

break;

if(isPrime == 1) {

printf("%d ", num);

return 0;

}
(d) While Loop: Print Even Numbers Between 1 and n
#include <stdio.h>

int main() {

int i = 2, upperRange;

printf("Enter upper range: ");

scanf("%d", &upperRange);

printf("Even numbers between 1 and %d are:\n", upperRange);

while(i <= upperRange) {

printf("%d ", i);

i += 2; // Increment by 2 to get the next even number

return 0;

}
6. a) General form of a C++ class with public, private variables, and a constructor and function

#include <iostream>

using namespace std;

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

cout << "Private Variable: " << privateVar << endl;

cout << "Public Variable: " << publicVar << endl;

};

int main() {

// Creating an object of MyClass

MyClass obj(10, 20);

// Accessing public function

obj.display();

return 0;

7. b) Program to accept an integer and output the factorial using C++ style I/O

#include <iostream>

using namespace std;


class Factorial {

private:

int number;

public:

// Constructor to initialize the number

Factorial(int num) {

number = num;

// Function to calculate factorial

int calculateFactorial() {

int fact = 1;

for (int i = 1; i <= number; i++) {

fact *= i;

return fact;

// Function to display factorial

void displayFactorial() {

cout << "Factorial of " << number << " is " << calculateFactorial() << endl;

};
int main() {

int num;

cout << "Enter a number: ";

cin >> num;

// Creating object of Factorial class

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>

#include <cmath> // For sqrt function

using namespace std;

class SquareRoot {

private:

int number;

public:

// Constructor to initialize the number

SquareRoot(int num) {

number = num;

}
// Function to calculate square root

double calculateSquareRoot() {

return sqrt(number);

// Function to display square root

void displaySquareRoot() {

cout << "Square root of " << number << " is " << calculateSquareRoot() << endl;

};

int main() {

int num;

cout << "Enter a number: ";

cin >> num;

// Creating object of SquareRoot class

SquareRoot sqRoot(num);

sqRoot.displaySquareRoot();

return 0;

}
8. C++ Program for Evaluating a Function Using Conditional Operators

#include <iostream>

#include <cmath> // For abs and sin functions

using namespace std;

int main() {

double x, y;

// Reading the value of x

cout << "Enter the value of x: ";

cin >> x;

// Using conditional operators to evaluate y

y = (x < -1) ? (abs(x) + abs(x - 1)) : (sin(abs(x + 1)));

// Displaying the result

cout << "The value of y is: " << y << endl;

return 0;

}
#include <stdio.h>

#include <math.h>

// Define the function for which we want to find the root

double f(double x) {

return x * x * x - 4 * x - 9; // Example: f(x) = x^3 - 4x - 9

void bisection(double a, double b, double tol) {

if (f(a) * f(b) >= 0) {

printf("Incorrect interval [a, b]\n");

return;

double c;

while ((b - a) >= tol) {

// Find the midpoint

c = (a + b) / 2;

// Check if midpoint is root

if (f(c) == 0.0) {

break;

// Decide which side to take


else if (f(c) * f(a) < 0) {

b = c;

} else {

a = c;

printf("Current mid-point: %.5lf\n", c);

printf("The root is: %.5lf\n", c);

int main() {

double a, b, tol;

printf("Enter the values of a and b (initial guesses): ");

scanf("%lf %lf", &a, &b);

printf("Enter the tolerance: ");

scanf("%lf", &tol);

bisection(a, b, tol);

return 0;

}
9. b) Newton-Raphson Method for Locating Real Roots

#include <stdio.h>

#include <math.h>

// Function for which we want to find the root

double f(double x) {

return x * x * x - 4 * x - 9; // Example: f(x) = x^3 - 4x - 9

// Derivative of the function

double f_prime(double x) {

return 3 * x * x - 4; // Derivative: f'(x) = 3x^2 - 4

void newton_raphson(double x0, double tol) {

double x1;

int iterations = 0;

while (1) {

x1 = x0 - f(x0) / f_prime(x0); // Newton-Raphson formula

printf("Iteration %d: x = %.5lf\n", iterations++, x1);

if (fabs(x1 - x0) < tol) {

break;
}

x0 = x1;

printf("The root is: %.5lf\n", x1);

int main() {

double x0, tol;

printf("Enter initial guess: ");

scanf("%lf", &x0);

printf("Enter tolerance: ");

scanf("%lf", &tol);

newton_raphson(x0, tol);

return 0;

}
8. c) Gauss-Jordan Elimination Method

Example System of Equations:

Let's assume the following system of equations:

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

Using Gauss-Jordan Elimination:

The augmented matrix representation of the system is:

[123923183127]\begin{bmatrix} 1 & 2 & 3 & 9 \\ 2 & 3 & 1 & 8 \\ 3 & 1 & 2 & 7

\end{bmatrix}

Expected Output from the Program:

Solution:

The value of variable 1 is: 1.00000

The value of variable 2 is: 2.00000

The value of variable 3 is: 3.00000

You might also like