Python Practice OOP Loops

In this class, We discuss Python Practice OOP Loops.

For Complete YouTube Video: Click Here

These examples will help the reader to understand the concepts of python easily.

Take the complete placement training course. The reader can easily crack any campus placement exam.

Try to solve the questions on your own and then check for a solution.

Q1)

1) class A which had a instance variable that should be accessible outside the class A
2) class B which had a class variable that should not be accessed out side class b
Which of the following are correct?

a) instance variable should be made private and there should not be any getter method
b) instance variable should be made private and the should be a getter method
c) class variable should be public
d) class variable should be private and there should not be any getter method

In the above example. 

The first statement states outside the class, the instance variable can be accessed.

The instance variable can be public, or we can make it private and write a getter method.

The getter methods are written to access the private variables.

The second statement states, The class variable should not be accessed outside the class.

Option b,d is the correct answer.

Q2)

What is the output displayed by the below code?

def modifylist(inlist):
    outlist=[]
    templist=list(inlist)#copy elements of list
    num=len(inlist)
    for i in inlist:
        if (i>=num):
            outlist.append(i)
        else:
            templist.remove(i)
            num-=1
    finallist=outlist+templist
    print(finallist)
inlist=[7,5,4,6,1,2]
modifylist(inlist)

In the above program, we copy the elements in the list to the temp list and modify the temp list.

The for loop in the program checks the number of the list greater than the length of the list then the number is appended to the out list.

Otherwise, remove an element from the temp list.

The temp list is appended to our list in the last.

Given below is the output displayed by the program.

[7, 6, 7, 6]