0% found this document useful (0 votes)
13 views10 pages

Unit 1 QB

The document discusses key programming concepts, distinguishing between procedural and object-oriented programming, emphasizing data encapsulation and abstraction. It highlights the importance of these concepts in code maintenance, extensibility, and efficiency, while also providing examples of typecasting, variable scope, and operator precedence. Additionally, it demonstrates practical applications of these principles through C++ code snippets.

Uploaded by

molej65141
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)
13 views10 pages

Unit 1 QB

The document discusses key programming concepts, distinguishing between procedural and object-oriented programming, emphasizing data encapsulation and abstraction. It highlights the importance of these concepts in code maintenance, extensibility, and efficiency, while also providing examples of typecasting, variable scope, and operator precedence. Additionally, it demonstrates practical applications of these principles through C++ code snippets.

Uploaded by

molej65141
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/ 10

1.

Distinguish between procedural programming and object-oriented


programming. (4 M)

Procedural Programming:

● Focuses on procedures or routines (functions).


● Code is written as a sequence of instructions that manipulate data.
● Functions are separate from data, and there is no concept of encapsulation.
● Example: C, Fortran.

Object-Oriented Programming (OOP):

● Focuses on objects, which are instances of classes.


● Encapsulates both data and functions together within classes.
● Encourages the use of inheritance, polymorphism, and encapsulation.
● Example: C++, Java.

2. Relate data encapsulation in the context of object-oriented programming,


and why is it an important concept? (4 M)

Data Encapsulation is the bundling of data (variables) and methods (functions) that operate on
the data into a single unit or class. It restricts direct access to some of an object's components,
which can prevent accidental data modification and promotes modularity and maintainability.

Importance:

● Protects object data by restricting access to sensitive data.


● Promotes easier code maintenance.
● Encourages better design by defining clear interfaces.

3. Demonstrate the use of typecast operators in programming. (3 E)

Typecasting allows conversion between different data types. In C++, typecasting can be done
using explicit or implicit operators.

cpp
Copy code
#include <iostream>
using namespace std;
int main() {
double x = 5.75;
int y;

// Explicit typecasting (manual conversion)


y = (int)x; // Converting double to int
cout << "Value of y: " << y << endl;

return 0;
}

4. Explain the difference between a global variable and a local variable.


How can the scope resolution operator help differentiate them? (2 E)

● Global Variable: Declared outside of any function, accessible by any function in the
program.
● Local Variable: Declared within a function, accessible only within that function.

The Scope Resolution Operator (::) can be used to distinguish global variables from local
variables.

cpp
Copy code
#include <iostream>
using namespace std;

int x = 10; // Global variable

void func() {
int x = 5; // Local variable
cout << "Local x: " << x << endl;
cout << "Global x: " << ::x << endl; // Accessing global variable
}

int main() {
func();
return 0;
}
5. Analyze the challenges associated with code reuse in procedural
programming compared to Object-Oriented Programming (OOP) that uses
inheritance and polymorphism. (4 M)

● Procedural Programming:
○ Functions may need to be repeated in different parts of the program.
○ Reuse is limited to function calls and requires rewriting or copying code.
○ Maintenance becomes difficult as the program grows.
● Object-Oriented Programming (OOP):
○ Inheritance allows code reuse through class hierarchies.
○ Polymorphism allows for flexible method overrides and function calls without
changing the underlying code.
○ Easier to maintain and extend due to encapsulation and abstraction.

6. Provide an example where a variable is initialized at runtime and explain


the circumstances in which dynamic initialization is necessary. (3 E)

Dynamic initialization happens when the value of a variable is determined at runtime, typically
using user input or a runtime calculation.

cpp
Copy code
#include <iostream>
using namespace std;

int main() {
int n;
cout << "Enter a number: ";
cin >> n; // Dynamic initialization based on user input

int* arr = new int[n]; // Dynamically allocated array based on


runtime input
cout << "Array created with " << n << " elements." << endl;

delete[] arr; // Free allocated memory


return 0;
}
7. Provide examples where the scope resolution operator is essential to
distinguish between global and local variables or to access class-specific
members. (3 E)
cpp
Copy code
#include <iostream>
using namespace std;

int var = 5; // Global variable

class MyClass {
public:
int var = 10; // Member variable of MyClass
void show() {
cout << "Member var: " << var << endl; // Accessing member
variable
cout << "Global var: " << ::var << endl; // Accessing global
variable using scope resolution
}
};

int main() {
MyClass obj;
obj.show();
return 0;
}

8. Explain how an inline function is declared in C++. Which keyword is used


to define an inline function, and how does it affect the function's
execution? (2 E)

An inline function is defined using the inline keyword. It tells the compiler to insert the
function's code directly at the point of the function call, potentially reducing overhead from
function calls.

cpp
Copy code
#include <iostream>
using namespace std;

inline int square(int x) {


return x * x;
}

int main() {
cout << "Square of 5: " << square(5) << endl;
return 0;
}

9. Briefly explain what procedural programming is and how it differs from


Object-Oriented Programming. (2 E)

Procedural Programming:

● Focuses on procedures or functions that operate on data.


● Functions are written in a linear sequence, and data is passed between functions.

Object-Oriented Programming:

● Focuses on objects (instances of classes), which encapsulate both data and functions.
● Uses concepts like inheritance, polymorphism, and encapsulation.

10. Analyze a few applications of Object-Oriented Programming. (4 M)

Applications of OOP:

● Software Development: For creating modular and maintainable applications, e.g.,


desktop apps, games, enterprise software.
● Web Development: Frameworks like Django, Flask, and Ruby on Rails use OOP
principles.
● Simulation Systems: Used in simulations for natural or artificial systems, such as in
engineering or finance.
● Video Games: Game objects (characters, enemies, etc.) are modeled using OOP
principles.
11. Demonstrate the use of a member dereferencing operator. (3 E)
cpp
Copy code
#include <iostream>
using namespace std;

class MyClass {
public:
int value;
MyClass(int val) : value(val) {}
};

int main() {
MyClass* obj = new MyClass(10);
cout << "Value: " << obj->value << endl; // Dereferencing member
delete obj;
return 0;
}

12. Discuss operator precedence. (4 M)

Operator precedence determines the order in which operators are evaluated in an expression.
Operators with higher precedence are evaluated first. For example:

● * and / have higher precedence than + and -.


● Parentheses () have the highest precedence.

13. Interpolate the challenges associated with code reuse in procedural


programming, especially when compared to object-oriented programming.
(5 D)

In procedural programming, code reuse can be cumbersome because functions tend to be


isolated, and data is passed around as arguments. This often leads to duplication and difficulties
in maintaining consistency across the codebase.
In OOP, inheritance allows code to be reused by extending existing classes, and polymorphism
allows for flexible interactions between objects. Encapsulation further ensures that code is
modular and easy to maintain.

14. Show the primary benefits of data abstraction in software development,


particularly code maintenance and extensibility. (4 M)

Data Abstraction:

● Simplifies Complexity: Hides the implementation details and exposes only essential
features.
● Improves Maintainability: Modifications to internal details don't affect the external
interface.
● Increases Extensibility: New features can be added without disrupting existing
functionality.

15. Demonstrate how data encapsulation is utilized in object-oriented


programming. (3 E)
cpp
Copy code
#include <iostream>
using namespace std;

class Account {
private:
double balance; // Encapsulated data

public:
void deposit(double amount) {
balance += amount;
}

double getBalance() {
return balance;
}
};
int main() {
Account acc;
acc.deposit(1000);
cout << "Balance: " << acc.getBalance() << endl;
return 0;
}

16. Design and implement a C++ program that demonstrates the use of
global and local variables. (5 D)
cpp
Copy code
#include <iostream>
using namespace std;

int globalVar = 10; // Global variable

void demo() {
int localVar = 20; // Local variable
cout << "Local variable: " << localVar << endl;
cout << "Global variable: " << globalVar << endl;
}

int main() {
demo();
return 0;
}

17. Evaluate the significance of data abstraction as a fundamental software


design concept. Assess how it contributes to code maintainability,
extensibility, and the long-term efficiency of software projects. (4 M)

Data Abstraction helps in isolating the complexity of a system by allowing users to interact with
objects without understanding their internal workings. This leads to:

● Maintainability: Easier to maintain since changes to implementation details don’t affect


other parts of the system.
● Extensibility: New features can be added with minimal impact on existing code.
● Efficiency: Code is easier to understand and debug.

18. Demonstrate the role and functionality of the scope resolution operator
in programming. (3 E)
cpp
Copy code
#include <iostream>
using namespace std;

int var = 50;

int main() {
int var = 10;
cout << "Local var: " << var << endl;
cout << "Global var: " << ::var << endl; // Scope resolution to
access global variable
return 0;
}

19. Develop a C++ program demonstrating the interaction between function


overloading and default arguments. (5 D)
cpp
Copy code
#include <iostream>
using namespace std;

void display(int x, int y = 5) {


cout << "x: " << x << ", y: " << y << endl;
}

int main() {
display(10); // Uses default argument
display(10, 20); // No default argument
return 0;
}
20. Analyze the concepts of data encapsulation and data abstraction in
object-oriented programming. (5 D)

Data Encapsulation:

● Bundles data and methods into a single unit (class).


● Provides data protection by restricting direct access to class variables (using
private/protected access specifiers).

Data Abstraction:

● Hides unnecessary details and shows only essential features of an object.


● Helps in reducing complexity and increasing modularity.

Both concepts enhance code readability, maintainability, and extensibility.

You might also like