This article covers the InfyTQ Final Round MCQs in Python. Post the initial Qualifying Round, the Final Round in InfyTQ is what determines whether candidates get the certificate of Infosys Certified Software Programmer and also potentially land a job offer in Infosys. What's covered in this article is solutions in Python to the InfyTQ Questions (MCQs), which are part of the official sample paper.
Given below are the InfyTQ Questions (Python MCQs) that were covered in the Final Round.
Consider the following Python code snippet.
class ClassA: __param1=200 def __init__(self): self.__param1 = 100 def method1(self): #Line 1_________ @staticmethod def method2( ): #Line 2_________ obj1=ClassA( ) obj1.method1() #Line 3________________
Note: Line numbers are for reference only.
Fill in the blanks at Line1, Line2 and Line3 to get the output 302.
Line 1:ClassA._param1=ClassA._param1+(self._param1 + 2)
Answer: C and E
A. You cannot have a self.method2() outside the class which would throw an error
B. Option B would give 402
C. Would give 302
D. Would throw an error because method2() in Line3 does not have any reference
E. Would also give 302
Which of the following statements can replace Line1 and Line2 in the below Python code to get output as ‘kcjh’?
def func1(st1): stack1=Stack(len(st1)) result="" for ch in st1: if(ch!="a" and ch!="e" and ch!="i" and ch!="o" and ch!="u"): #Line1 if(ch == "#"): #line2 while(not stack1.is_empty()): result+=stack1.pop( ) print ( result ) st="hi##jack" func1 ( st )
Assumption: Stack class, with the necessary methods, is available.
A: Line 1: stack1.pop() Line2: stack1.push(ch)
B: Line 1: stack1.push(ch) Line2: stack1.push(ch)
C: Line 1: stack1.push(ch) Line2: stack1.pop()
D: Line 1: stack1.pop() Line2: stack1.pop()
If we are supposed to get the desired output we are supposed to do the following:
This can be only done when we follow the coding lines given in option C.
Alister is a Python developer who has written the below code. He desires the output to be 123 Volvo, but, he is unable to proceed due to an error.
class Car: def __init__( self,car_id,car_name): self.__car_id=car_id #Line 1 self.car_name=car_name #Line 2 car1=Car(123, "Volvo") #Line 3 print(car1.car_id(),car1.car_name) #Line 4
Choose two options from below which he should implement to get the desired output?
Note: Line numbers are for reference only.
A: Add a method to the Car class as follows: def get_car_id(self):return car_id
B: Add a method to the Car class as follows: def get_car_id(self): return self._car_id
C: Change Line 4to : print(car1.get_car_id(),car1.car_name)
D: Change Line 4 to : print(car1._car_id,car1.car_name)
Answer: B and C
As the variable __car_id is a private variable, we would have to use a separate method to use the variable outside the class. The same has to be used in the print statement.
Consider the following Python function for a vehicle management system. Objective of the code is to record details of vehicles-
def vehicle_record_no,mileage,veh_type,purpose,kickstart,no_of_gears,miles_run ) : record=”” if(veh_type == “TwoWheeler”) : record=reg_no+”:TwoWheeler:”+ str(mileage)+”:”+kickstart elif(veh_type == “FourWheeler”) : record=reg_no+”:FourWheeler:”+str(mileage)+”:”+no_of_gears if(purpose == “Commercial”) : record=record+”:Commercial FourWheeler:”+miles_run return record record=vehicle_record(“KA09Z7211”,15,”FourWheeler”,”Commercial”,”No”,”4”,”89000”)
What will be the optimal class structure if this was to be re-written in Object oriented programming?
A: 4 independent classes: Vehicle, TwoWheeler, FourWheeler, CommericalFourWheeler
B: 3classeswith inheritance: TwoWheeler and FourWheeler inherit from Vehicle
C: 4 classes with inheritance: TwoWheeler, FourWheeler and CommericalFourWheeler which inherit from Vehicle
D: 4 classes with inheritance: TwoWheeler and FourWheeler inherit from Vehicle; CommericalFourWheeler inherits from FourWheeler
The main objective of object-oriented programming is code reusability and in the above case it can be done by segregation the parameters into parent-child class pairs. The optimum design would be as given below:
So in this segregation the common features of vehicle class can be used by all its child classes and likewise the common features of four wheeler class can be used by the commercial class.
This arrangement is suggested in option D
Consider the Hashing methods given below:
i) h(key) = key%10
ii) h(key) = key%25
iii) h(key) = key%50
Which of the hashing methods would NOT lead to collision when the following values are to be stored in the hash table?
80, 20, 35, 45, 25, 90
A: Only iii)
B: Both ii) and iii)
C: Both i) and iii)
D: All i), ii) and iii)
Only when the numbers are divided by 50 we would get different remainders for all the respective numbers. Remaining numbers would at least give one duplicate remainder.
Number 14 needs to be searched using BINARY SEARCH in the following sorted list of numbers:
1, 3, 7, 9, 14, 19, 45
How many comparisons will be required to conclude that the number 14 is found at 5th position?
Note: We have used integer division for finding the middle element and the index starts with 0(zero)
1st iteration: low= 0
mid= (0+6)//2 = 3
a<key, which means 9<14
2nd iteration: low = mid+1 = 4
high = 6
mid = (4+6)//2 = 5
a>key, which means 19>14
3rd iteration: low = 4
high = mid - 1 = 4
mid = (4+4)//2 = 4
a==key, which means 14==14.
What will be the output of the below code?
class Sample: def __init__(self,val): self.val=val def method1(self): return self.val//2 class Parent: counter=0 def __init__(self, val1): Parent.counter+=1 self.num1=val1 def method2 (self): self.num1*=3 return self.num1 class Child(Parent): variable=100 def __init__(self,val1, val2, obj): Parent.counter-=1 super().__init__(val1) self.num2=val2+Child.variable self.obj_x=obj def method1 (self): return (self.obj_x.method1()+self.num1) obj1=Sample(20) obj2=Child(15,20, obj1) print(obj2.method1()+obj2.method2() +Parent.counter)
The combined sum of obj2.method1()+obj2.method2()+Parent.counter would be (25+45+0) so the sum would be 70.
Consider the below code:
my_queue1 = Queue(3) my_queue2 = Queue(3) for index in range(0, 3): my_queue1.enqueue(index *3) for index in range(0, 3): if(index == 2): break my_queue2.enqueue(my_queue1.dequeue ( ) ) my_queue2.enqueue(12)
Assumption: Queue class, with the necessary methods, is available
What is the status of my_queue 1 and my queue2 after the execution of above code?
In the options, consider the elements of the queue from Front to Rear
A: my_queue 1: 6, my_queue2: 0,3
B: my_queue 1: 6, my queue2: 0, 3, 12
C: my queue1: Queue is empty, my_queue2: 0, 3, 12
D: my_queue1: 0,3,6, my_queue2:0, 3, 12
Here the queue1 is being filled as 0, 3, 6 (front to rear). When this is dequeued the order would be 0 first, then 3 and finally 6. So the 1st dequeue would give us 0, so now queue2 becomes 0 (front to rear). Next dequeue of queue1 would give us 3, which would be the 2nd enqueue for queue2. So now queue2 would be 0, 3 (front to rear). The dequeue of queue1 would stop here as the index reaches 2. Final enqueue of queue2 would happen with a 12. So finally queue2 becomes 0, 3, 12 (front to rear).
What will be the output of the code below?
class ClassA: def __init__(self,val1): self.value=val1 def method_a(self): return 10+self.value class ClassB: def __init__(self,val2): self.num=val2 def method_b(self,obj): return obj.method_a()+self.num obj1=ClassA(20) obj2=ClassB(30) print(obj2.method_b(obj1))
obj.method_a() would return a value 10+self.value where self.value() is 20 resulting return would be 30. self.num is already stored as 30. Therefore the final output from obj2.method_b(obj_1) is 30+30 that is 60.
Consider the python code below.
Assume that necessary imports have been done.
class Shape (metaclass=ABCMeta): def __init__(self): print("I am in init") @abstractmethod def draw_shape(self): pass @abstractmethod def set_color(self): pass class Circle(Shape): def draw_shape(self): print("Draw Circle")
Which of the following statement(s) is/are TRUE?
i) Class Circle cannot be instantiated as it does not implement set_color() method
ii) The above code will not execute because class Shape has two abstract methods
A: Only i) is True
B: Only ii) is True
C: Both i) and ii) are True
D: Neither of i) or ii) is True
The abstract base class may have more than one abstract method.
Here class Shape has more than one abstract method. It is totally possible for a base class to have more than one abstract method but all these methods have to be implemented by the child class or a TypeError would occur.
Statement 1 says that the child class Circle cannot be instantiated which is true, as it does not implement all the abstract methods of the base class.
Statement 2 says the code will not compile because of the base class having more than one abstract method which is false.
If you want to check out the video explanation of these questions, please watch the video below.