Python/Python Mcq Set 19 Sample Test,Sample questions

Question:
 Is it possible to create a text file in python?

1. Yes

2.No

3.Machine dependent

4. All of the mentioned


Question:
 Is the following Python code valid?

class B(object):
  def first(self):
    print("First method called")
  def second():
    print("Second method called")
ob = B()
B.first(ob)

1.It isn’t as the object declaration isn’t right

2. It isn’t as there isn’t any __init__ method for initializing class members

3. Yes, this method of calling is called unbounded method call

4.Yes, this method of calling is called bounded method call


Question:
 What does print(Test.__name__) display (assuming Test is the name of the class)?

1. ()

2.Exception is thrown

3.Test

4.__main__


Question:
 What is getattr() used for?

1.To access the attribute of the object

2. To delete an attribute

3.To check if an attribute exists or not

4.To set an attribute


Question:
 What will be the output of the following Python code?

 class Demo:
    def __init__(self):
        pass
 
    def test(self):
        print(__name__)
 
obj = Demo()
obj.test()

1. Exception is thrown

2.__main__

3.Demo

4. test


Question:
 What will be the output of the following Python code?

>>> class demo():
	def __repr__(self):
		return '__repr__ built-in function called'
	def __str__(self):
		return '__str__ built-in function called'
>>> s=demo()
>>> print(s)

1. Error

2.Nothing is printed

3. __str__ called

4. __repr__ called


Question:
 What will be the output of the following Python code?

class fruits:
    def __init__(self, price):
        self.price = price
obj=fruits(50)
 
obj.quantity=10
obj.bags=2
 
print(obj.quantity+len(obj.__dict__))

1. 12

2. 52

3. 13

4.60


Question:
 What will be the output of the following Python code?

class Test:
    def __init__(self):
        self.x = 0
class Derived_Test(Test):
    def __init__(self):
        Test.__init__(self)
        self.y = 1
def main():
    b = Derived_Test()
    print(b.x,b.y)
main()

1.Error because class B inherits A but variable x isn’t inherited

2.0 0

3.0 1

4.Error, the syntax of the invoking method is wrong


Question:
 Which function overloads the == operator?

1.__eq__()

2.__equ__()

3. __isequal__()

4.none of the mentioned


Question:
How do you change the file position to an offset value from the start?

1. fp.seek(offset, 0)

2. fp.seek(offset, 1)

3.fp.seek(offset, 2)

4.none of the mentioned


Question:
How do you close a file object (fp)?

1.close(fp)

2.fclose(fp)

3.fp.close()

4. fp.__close__()


Question:
How do you delete a file?

1.del(fp)

2. fp.delete()

3.os.remove(‘file’)

4. os.delete(‘file’)


Question:
How do you get the current position within the file?

1. fp.seek()

2. fp.tell()

3.fp.loc

4. fp.pos


Question:
How do you get the name of a file from a file object (fp)?

1. fp.name

2. fp.file(name)

3.self.__name__(fp)

4.fp.__name__()


Question:
How do you rename a file?

1. fp.name = ‘new_name.txt’

2.os.rename(existing_name, new_name)

3.os.rename(fp, new_name)

4.os.set_name(existing_name, new_name)


Question:
Let A and B be objects of class Foo. Which functions are called when print(A + B) is executed?

1.__add__(), __str__()

2. __str__(), __add__()

3.__sum__(), __str__()

4.__str__(), __sum__()


Question:
Suppose B is a subclass of A, to invoke the __init__ method in A from B, what is the line of code you should write?

1. A.__init__(self)

2.B.__init__(self)

3.A.__init__(B)

4.B.__init__(A)


Question:
The assignment of more than one function to a particular operator is _____

1.Operator over-assignment

2.Operator overriding

3.Operator overloading

4. Operator instance


Question:
What are the methods which begin and end with two underscore characters called?

1.Special methods

2.In-built methods

3.User-defined methods

4.Additional methods


Question:
What happens if no arguments are passed to the seek function?

1. file position is set to the start of file

2.file position is set to the end of file

3.file position remains unchanged

4. error


Question:
What is delattr(obj,name) used for?

1. To print deleted attribute

2.To delete an attribute

3.To check if an attribute is deleted or not

4.To set an attribute


Question:
What is hasattr(obj,name) used for?

1.To access the attribute of the object

2.To delete an attribute

3. To check if an attribute exists or not

4. To set an attribute


Question:
What is Instantiation in terms of OOP terminology?

1. Deleting an instance of class

2.Modifying an instance of class

3.Copying an instance of class

4.Creating an instance of class


Question:
What is setattr() used for?

1. To access the attribute of the object

2.To set an attribute

3.To check if an attribute exists or not

4.To delete an attribute


Question:
What is the difference between r+ and w+ modes?

1.no difference

2.in r+ the pointer is initially placed at the beginning of the file and the pointer is at the end for w+

3.in w+ the pointer is initially placed at the beginning of the file and the pointer is at the end for r+

4.depends on the operating system


Question:
What will be the output of the following Python code?

 class test:
     def __init__(self,a):
         self.a=a
 
     def display(self):
         print(self.a)
obj=test()
obj.display()

1.Runs normally, doesn’t display anything

2.Displays 0, which is the automatic default value

3.Error as one argument is required while creating the object

4.Error as display function requires additional argument


Question:
What will be the output of the following Python code?

>>> class demo():
	def __repr__(self):
		return '__repr__ built-in function called'
	def __str__(self):
		return '__str__  built-in function called'
>>> s=demo()
>>> print(s)

1. __str__ called

2.__repr__ called

3.Error

4.Nothing is printed


Question:
What will be the output of the following Python code?

class A():
    def disp(self):
        print("A disp()")
class B(A):
    pass
obj = B()
obj.disp()

1.invalid syntax for inheritance

2.Error because when object is created, argument must be passed

3.Nothing is printed

4.A disp()


Question:
What will be the output of the following Python code?

class change:
    def __init__(self, x, y, z):
        self.a = x + y + z
 
x = change(1,2,3)
y = getattr(x, 'a')
setattr(x, 'a', y+1)
print(x.a)

1. 6

2. 7

3. Error

4. 0


Question:
What will be the output of the following Python code?

class stud:
   ‘Base class for all students’
   def __init__(self, roll_no, grade):
      self.roll_no = roll_no
      self.grade = grade
   def display (self):
      print("Roll no : ", self.roll_no,  ", Grade: ", self.grade)
print(student.__doc__)

1.Exception is thrown

2.__main__

3.Nothing is displayed

4.Base class for all students


Question:
What will be the output of the following Python code?

class stud:
   def __init__(self, roll_no, grade):
      self.roll_no = roll_no
      self.grade = grade
   def display (self):
      print("Roll no : ", self.roll_no,  ", Grade: ", self.grade)
stud1 = stud(34, 'S')
stud1.age=7
print(hasattr(stud1, 'age'))

1.Error as age isn’t defined

2. True

3. False

4.7


Question:
What will be the output of the following Python code?

class test:
    def __init__(self):
        self.variable = 'Old'
        self.Change(self.variable)
    def Change(self, var):
        var = 'New'
obj=test()
print(obj.variable)

1.Error because function change can’t be called in the __init__ function

2. ‘New’ is printed

3.‘Old’ is printed

4.Nothing is printed


Question:
What will be the output of the following Python code?
class test:
     def __init__(self,a="Hello World"):
         self.a=a
 
     def display(self):
         print(self.a)
obj=test()
obj.display()

1. The program has an error because constructor can’t have default arguments

2.Nothing is displayed

3. “Hello World” is displayed

4.The program has an error display function doesn’t have parameters


Question:
What will be the output of the following Python code?
class Test:
    def __init__(self):
        self.x = 0
class Derived_Test(Test):
    def __init__(self):
        self.y = 1
def main():
    b = Derived_Test()
    print(b.x,b.y)
main()

1.0 1

2. 0 0

3.Error because class B inherits A but variable x isn’t inherited

4.Error because when object is created, argument must be passed like Derived_Test(1)


Question:
Which function is called when the following Python code is executed?

f = foo()
format(f)

1. format()

2. __format__()

3.str()

4.__str__()


Question:
Which function is used to close a file in python?

1.Close()

2.Stop()

3.End()

4.Closefile()


Question:
Which function overloads the >> operator?

1.__more__()

2. __gt__()

3.__ge__()

4.none of the mentioned


Question:
Which function overloads the + operator?

1. __add__()

2.__plus__()

3. __sum__()

4.none of the mentioned


Question:
Which function overloads the // operator?

1. __div__()

2._ceildiv__()

3. __floordiv__()

4.__truediv__()


Question:
Which of the following are the modes of both writing and reading in binary format in file?

1. wb+

2.w

3.wb

4.w+


Question:
Which of the following best describes inheritance?

1.Ability of a class to derive members of another class as a part of its own definition

2.Means of bundling instance variables and methods in order to restrict access to certain class members

3.Focuses on variables and passing of variables to functions

4.Allows for implementation of elegant software that is well designed and easily modified


Question:
Which of the following is not a class method?

1.Non-static

2. Static

3.Bounded

4.unbounded solution


Question:
Which of the following is not a valid attribute of a file object (fp)?

1. fp.name

2. fp.closed

3.fp.mode

4.fp.size


Question:
Which of the following is not a valid mode to open a file?

1.ab

2. rw

3.r+

4. w+


Question:
Which of the following statements is wrong about inheritance?

1. Protected members of a class can be inherited

2.The inheriting class is called a subclass

3.Private members of a class can be inherited and accessed

4.Inheritance is one of the features of OOP


Question:
Which operator is overloaded by the __or__() function?

1.||

2. |

3.//

4./


Question:
Which operator is overloaded by __invert__()?

1. !

2. ~

3.^

4.–


Question:
Which operator is overloaded by __lg__()?

1.<

2. >

3. !=

4.none of the mentioned


Question:
____ is used to create an object.

1.class

2.constructor

3.User-defined functions

4. In-built functions


Question:
_____ represents an entity in the real world with its identity and behaviour.

1. A method

2.An object

3. A class

4. An operator


More MCQS

  1. Python MCQS - Function
  2. Python MCQS - GUI in python
  3. Python MCQS - Operators
  4. Python MCQS - Data type in python
  5. Python MCQS - loops in python
  6. Python MCQS - Numpy
  7. Python MCQS - sqlite3
  8. Python MCQS - Library
  9. Python MCQS - Pandas
  10. Python MCQs
  11. Dictionary Python MCQ set 1
  12. Dictionary Python MCQ set 2
  13. MCQ For Python Fundamentals
  14. MCQ Introduction to Python Section 1
  15. MCQ Introduction to Python Section 2
  16. MCQ Introduction to Python Section 3
  17. MCQ on Flow of Control in Python Set 1
  18. MCQ on Flow of Control in Python Set 2
  19. MCQ on Python String Set 1
  20. File Handling in Python section 1
  21. File Handling in Python section 2
  22. Python Functions MCQS Set 1
  23. Python Functions MCQS Set 2
  24. MCQ on List in Python
  25. Pandas MCQ Questions Set 1
  26. Pandas MCQ Questions Set 2
  27. Tuple MCQ in Python
  28. Python dataframe MCQ
  29. Python Mcq Set 1
  30. Python Mcq Set 2
  31. Python Mcq Set 3
  32. Python Mcq Set 4
  33. Python Mcq Set 5
  34. Python Mcq Set 6
  35. Python Mcq Set 7
  36. Python Mcq Set 8
  37. Python Mcq Set 9
  38. Python Mcq Set 10
  39. Python Mcq Set 11
  40. Python Mcq Set 12
  41. Python Mcq Set 13
  42. Python Mcq Set 14
  43. Python Mcq Set 15
  44. Python Mcq Set 16
  45. Python Mcq Set 17
  46. Python Mcq Set 18
  47. Python Mcq Set 19
  48. Python Mcq Set 20
  49. Python Mcq Set 21
  50. Python MCQ
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!