fraction.py:https://www.cse.msu.edu/~cse231/Online/Labs/Lab12/pre-lab_fraction.p
ID: 3851062 • Letter: F
Question
fraction.py:https://www.cse.msu.edu/~cse231/Online/Labs/Lab12/pre-lab_fraction.py
Question 1 (1 point)
Under what circumstances will the constructor (method “__init__”) create an object with 0 as the numerator and 1 as the denominator?
Question 1 options:
Save
Question 2 (1 point)
Question 2 options:
Save
Question 3 (1 point)
Referring to the Fraction class of fraction.py of Lab 12:
If I want to create a fraction 3/4, what statement do I use?
Question 3 options:
Save
Question 4 (1 point)
Referring to the Fraction class of fraction.py of Lab 12:
If I want to create an integer 5, what statement do I use?
Question 4 options:
Save
Question 5 (1 point)
Referring to the Fraction class of fraction.py of Lab 12:
Given:
A = fraction.Fraction( 1, 2 )
B = fraction.Fraction( 2, 3 )
What Fraction method is called when the expression A - B is evaluated?
Question 5 options:
Save
Question 6 (1 point)
Referring to the Fraction class of fraction.py of Lab 12:
Given:
A = fraction.Fraction( 1, 2 )
B = fraction.Fraction( 2, 3 )
What Fraction method is called when the expression A < B is evaluated?
Question 6 options:
fraction.py:https://www.cse.msu.edu/~cse231/Online/Labs/Lab12/pre-lab_fraction.py
fraction.py:https://www.cse.msu.edu/~cse231/Online/Labs/Lab12/pre-lab_fraction.py
Explanation / Answer
(1)
Fraction class contains a constructor with default values(numer=0, denom=1).
While creating object for Fraction class, if at all the Parameters are not passed these default values get assigned.
Option 1: A = fraction.Fraction( 4, 5 ); In this values 4, 5 are passing as input parameters. So default values are not assigned.
Hence this is not correct
Option 2: A = fraction.Fraction( 1, 0 ); In this values 1, 0 are passing as input parameters. So default values are not assigned.
Hence this is not correct
Option 3: A = fraction.Fraction( ); In this values no parameters are passed. So default values gets assigned.
Hence this is the correct option
Option 4: Is not a correct way of creating object.
Hence this is not correct
Hence Correct option is: (C) A = fraction.Fraction()
_____________________________________________________________________________________________________________________________________
(2)
In the Fraction class,
(i) method __repr__ returns the following string representation of the object:
Fraction: <numerator>/<denominator>
(ii) method __str_ returns the following formatted string:
<numerator>/<denominator>
Given that Numerator = 4, Denominator = 5
Hence
__repr__ displays
Fraction: 4/5
__str__ displays
4/5
Hence correct option is (B)
_____________________________________________________________________________________________________________________________________
(3)
In order to create a fraction 3/4, Fraction class object should be created by passing 3 for numerator and 4 for denominator.
Hence the correct way of creating an object is:
A = fraction.Fraction( 3, 4 )
Hence correct option is (D)
_____________________________________________________________________________________________________________________________________
(4)
In order to create an integer 5, Simply pass 5 as Numerator. As default value of denominator is 1, creates a fraction 5/1 which results in 5.
Object Creation: A = fraction.Fraction(5);
Value 5 gets assigned to Numerator( self.__numer ) and value 1 to denominator.
Hence correct option is: A = fraction.Fraction(5);
_____________________________________________________________________________________________________________________________________
(5)
Operation involved in the expression A-B is subtraction. As A and B are Fraction class objects, Overloaded method of subtraction (__sub__) gets called.
Hence correct option is __sub__
_____________________________________________________________________________________________________________________________________
(6)
Operation involved in the expression A<B is less than comparison. As A and B are Fraction class objects, Overloaded method of less than (__lt__) gets called.
Hence correct option is __lt__
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.