Join Regular Classroom : Visit ClassroomTech

Infytq Python Solve

def display_cat_details(color,name,pet_name=None):
    print("Color:")
    print(color)
    print("Name:")
    print(name)
    if pet_name !=None:
        print(pet_name)

Which of the following functions call will execute successfully?
Choose two correct options
a. display_cat_details(“Black”,”Roger”,”Snow”)
b. display_cat_details(“white”)
c. display_cat_details(“Grey”, pet_name=”Grey)
d. display_cat_details(“Brown”,”Nelly”)

Answer

a, d

Explanation

In this function, there are three parameters among them the last parameter is optional. In option b only one parameter is passed so it will not be a proper call. And in option c 2nd parameter which is necessary is not been passed so the wrong function call and a and d are proper function calls.



class Trainee:
    def __init__(self):
        self.id=1000
        self.name="Tom"
        self.__salary=1000
    def view_details(self):
        print("Trainee "+self.name+"has salary of " + str(self.__salary))
trainee=Trainee()
trainee.view_details()

Choose the Incorrect answer from the below options
a. view_details method has complete access to all attributes of class trainee
b. id of the trainee object can be changed outside the class trainee even if there is no setter for it.
c. salary of the trainee object can be displayed outside the class trainee using trainee.__salary
d. name of the trainee object can be displayed outside the class trainee without calling the view_details method

Answer

c

Explanation

self.__variablename is the syntax for private variable and so salary is a private variable hence it can not be accessed outside of the class


What will be the output of this code?

class Glove:
    def __init__(self,color):
        self.__color=color
    def get_color(self):
        return self.__color
    def set_color(self,color):
        self.__color=color
class Minion:
    def __init__(self,glove):
        self.__glove=glove
        self.color="Pink"
    def get_glove(self):
        return self.__glove
violet_glove=Glove("Violet")
yellow_glove=Glove("Yellow")
bob=Minion(yellow_glove)
special_glove=violet_glove
yellow_glove.set_color(bob.color)
print(bob.get_glove().get_color())

a violet
b Pink
c Yellow
d Attribute Error:

Answer

b

Explanation

Yellow_globe color is changed with bob color which is pink so yellow_globe color becomes pink. Then in last line bob.get_glove() is actually the yellow_glove and yellow_glove.get_color() is pink now so pink is answer.


class Car:
    __counter=100
    types=['SUV','Hatchback','Coupe']
    def __init__(self,model,doors):
        self.model=model
        self.doors=doors
        self.color=None
car1=Car("Ford",4)
car2=Car("Porsche",2)

How many static variables, local variables, and instance variables are present in the given code snippet?
a. static=2 Local=2 instance =3
b. static=2 local 3 instance 3
c. static =1 local=3 instance=2
d. static=1 local=2 instance =2

Answer

a

Explanation

__counter and types are two static variable and model and doors are two local variable and model doors and color are three instance variable.


Consider the hash function below:
h(key) = key%7
Which values may have collision if any, when the following values are stored in the hash table
80 15 25 14 12 20 35 49
a.14 35 25
b. 14 35 49 80 25
c. No collision
d. 14 35 49

Answer

d

Explanation

In option d if we apply the hash function then output will be 0 for all the three numbers and hence it will collision.


Tom and peter have joined a university which offers two types of subjects- mandatory and optional. Each mandatory subject has an educator, grade, and classroom. Each optional subject has duration and grade. Every subject has an id and if the above scenario is to be implemented using an object-oriented approach, identify the most suitable implementation from the above options.

a 3 independent classes : Subject, MandatorySubject, OptionalSubject
b 3 classes: Subject as the parent class. MandatorySubject and OptionalSubject as child classes of subject
c 3 classes : Subject, MandatorySubject ,OptionalSubject . Subject aggregates mandatory subject and OptionalSubject.
d 3 classes: Subject as the parent class . OptionalSubject as the child class of subject . MandatorySubject as the child class of OptionalSubject.

Answer

b


What will be the Output of the below code?

class AdditionQuiz:
    def __init__(self,num1,num2):
        self.score=0
        self.num1=num1
        self.num2=num2
    def evaluate_quiz(self,answer):
        if self.num1+self.num2==answer:
            self.score+=1
            return "Good job"
        return "Try Again"
    def return_score(self):
        return self.score
class MultiplicationQuiz(AdditionQuiz):
    def evaluate_quiz(self,answer):
        if self.num1+self.num2==answer:
            self.score+=2
            return " Well done"
        elif answer%self.num1==0 or answer%self.num2==0:
            self.score+=1
            return " Better lucjk next time"
        return "can do better"
add_quiz_obj=AdditionQuiz(14,2)
mul_quiz_obj= MultiplicationQuiz(2,7)
print(mul_quiz_obj.evaluate_quiz(16))
print("score:", mul_quiz_obj.return_score())

a. Try again
score: 0
b. Good job
Can do better
Score: 1
C. Better luck next time
Score: 1
d. Can do better
AttributeError: ‘MultiplicationQuiz’ object has no attribute

Answer

c

Explanation

In driver code, the second-line MultiplicationQuiz object has been made with value 2,7 and the second line evaluate_quiz is called with input 16. As 2+7=9 ins not equals to 16 and in second else if statement 16%2=0 so score will become 1 and Better Luck next time will be printed.


def dict_items(dict1):
    global dict2
    for key in dict1.keys():
        dict2[key+1]=dict1[key]+key
        dict1[key]=dict2[key+1]
dict2={}
dict_items({1:1,2:20,3:32,4:43,5:54})

a. dict1-{1:2,2:22,3:35,4:47,5:59}
dict2-{1:2,2:22,3:35,4:47,5:59}
b. dict1-{1:2,2:22,3:35,4:47,5:59}
dict2-{2:2,3:22,4:35,5:47,6:59}
c. dict1-{1:1,2:1,3:22,4:35,5:47,6:59}
dict2-{2:2,3:22,4:35,5:47,6:59}
d. dict1-{1:2,2:22,3:35,4:47,5:59}
dict2-{}

Answer

b

Explanation

The input dictionary to the function is 1:1, 2:20,3:32,…

For every time the loop executes the dict2 keys will start from 2 and the value will be the value of key-1 of the initial dictionary+key value means.  dict2[2]=dict1[1]+1 so it becomes 2:2 in next iteration dict2[3]=dict1[2]+2so it becomes 3:22 and next line the value of dict1 updates as the same of dict2 but key is less than 1 so the output will be dict1 will start from key value 1 and dict2 will start from key value 2 but they will content same value accordingly means what will be the value of dict1[key] will be  same value in dict2[key+1]


What will be the output of the code?

def dict_items(dict1):
    global dict2
    for key in dict1.keys():
        dict2[key+1]=dict1[key]
        dict1[key]=dict2[key+1]+key
dict2={}
dict_items({1:10,2:20,3:30,4:40,5:50})
print(dict2)

a. dict1 – {1:11 , 2:22 , 3:33 , 4:44 , 5: 55}
dict2 – {1:11 , 2:22 , 3:33 , 4:44 , 5: 55}
b. dict1 — {1:11 , 2:22 , 3:33 , 4:44 , 5: 55}
dict2 — {2:11 , 3:22 , 4:33 , 5:44 , 6: 55}
c. dict1 – {1:11 , 2:22 , 3:33 , 4:44 , 5: 55}
dict2 – {2:10 , 3:20 , 4:30 , 5:40 , 6: 50}
d. dict1 – {1: 11 , 2 : 11, 3: 22, 4 : 33 , 5 : 44 , 6 : 55}
dict2 – {2 :10, 3: 21, 4:30 ,5: 41, 6:50}

Answer

c

Explanation

Let’s try to understand the output of the dict2 first dict2 value is the same in all position like dict1 only the key is increased by 1 so the output is 2: 10, 3:20, 4:30 like this and dict1 value will be updated what will be the previous value will be added with the key-value by keeping all keys same as previous so 1:10 will become 1:10+1= 1:11 like this 2: 20 will become 2:20+2=2:22 like this.