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

Advance Programming

Uploaded by

waqarkhan03109
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Advance Programming

Uploaded by

waqarkhan03109
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Advance programming

Question no 1: Write short definitions of FOUR oops principles of python?

Answer:
1 Encapsulation:

 Encapsulation means keeping all the important data and methods (functions) inside a class and
hiding them from the outside world. This way, the internal workings of the class can change
without affecting the rest of your code. Think of it like a capsule where everything inside is
protected and only certain parts are accessible.

2 Abstraction:

 Abstraction is about simplifying complex systems by showing only the necessary details and
hiding the complexities. For example, when you use a car, you just need to know how to drive it,
not how the engine works. In programming, abstraction allows you to use objects without
needing to understand all the internal details.

3 Inheritance:

 Inheritance allows a new class (called a child or subclass) to take on properties and methods
from an existing class (called a parent or superclass). This way, you can create a general class
and then create more specific classes that reuse the general class's code, reducing redundancy
and promoting code reuse.

4 Polymorphism:

 Polymorphism allows different classes to be treated as instances of the same class through a
common interface. For example, if you have a general class called Animal and subclasses like
Dog and Cat, you can use the same method names for different behaviors. This makes your code
more flexible and easier to extend.

Question no 2: What are the difference between functions and methods in python?

Answer:
Functions
 Definition: A function is a block of reusable code that performs a specific task. It is defined using
the def keyword.
 Scope: Functions can be defined at the module level or inside other functions (nested functions).

 Usage: They can be called independently, without being tied to any object.

Code:
def add(a, b):

return a + b

result = add(3, 4)

Output:
result is 7

Methods
 Definition: A method is similar to a function but is associated with an object. Methods
are functions defined within a class.
 Scope: Methods belong to the class they are defined in and operate on instances of that
class.
 Usage: They are called on objects (instances of classes) and can access and modify the
object's state.

Code:
class Calculator:
def add(self, a, b):
return a + b

calc = Calculator()
result = calc.add(3, 4)
Output:
result is 7

Question no 3: Python doesn’t have a concept of private attributes, but how we can define a
private attribute in python also write its program?
Answer:
In Python, you can define private attributes by prefixing their names with double underscores ( __).

This makes them harder to access from outside the class.

Code:
class Counter:
def increment(self):
self.__current += 1

counter = Counter() this will give you an error


print(counter.__current)

However, you can access the __current attribute as _Counter___current


like this:

counter = Counter() this is the correct approach


print(counter._Counter__current)

Question no 4: Why we use break and continue statements in loops and write its program and display
its output?

Answer:
Break Statement

The break statement is used to exit a loop prematurely. When break is encountered inside a loop, the
loop terminates immediately, and the control flow of the program continues with the statement
immediately following the loop.

Code:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for number in numbers:

if number == 5:
print("Number 5 found, exiting the loop.")

break

print(number)

output:
1
2
3
4
Number 5 found, exiting the loop.

Continue Statement

The continue statement is used to skip the rest of the code inside the current loop iteration and move to
the next iteration. When continue is encountered, the loop does not terminate but continues with the
next iteration.

Code:
for number in range(1, 11):

if number % 2 == 0:

continue

print(number)

output:
1
3
5
7
9

You might also like