Knowee
Questions
Features
Study Tools

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):passclass 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 methodsOptionsOnly i) is TrueOnly ii) is TrueBoth i) and ii) are TrueNeither of i) or ii) is True

Question

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):passclass 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 methodsOptionsOnly i) is TrueOnly ii) is TrueBoth i) and ii) are TrueNeither of i) or ii) is True

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The correct answer is "Only i) is True".

In Python, a class that contains one or more abstract methods is called an abstract class. An abstract method is a method that has a declaration but does not have an implementation.

In the given code, Shape is an abstract class and it has two abstract methods: draw_shape and set_color.

The Circle class is a subclass of Shape and it only implements the draw_shape method. It does not implement the set_color method.

Therefore, according to the rules of Python, we cannot create an instance of the Circle class (i.e., we cannot instantiate Circle) because it does not provide the implementation for all the abstract methods of the superclass Shape.

So, statement i) is True.

However, the code will execute without any errors. Python does not enforce the implementation of abstract methods at compile time. The error will only occur at runtime when we try to instantiate the Circle class.

So, statement ii) is False.

This problem has been solved

Similar Questions

[True/False] In python classes,  the constructor __init__(self) is called implicitly when an object is instantiated of the class.

Which of the following is true about abstract classes in Python?  They can be instantiated directlyThey cannot contain concrete methodsThey can contain both abstract and concrete methodsThey must contain only abstract methods

What will be the output of the following Python code?class objects: def __init__(self): self.colour = None self._shape = "Circle"   def display(self, s): self._shape = sobj=objects()print(obj._objects_shape)The program runs fine because name mangling has been properly implementedError because the member shape is a protected memberError because the proper syntax for name mangling hasn’t been implementedError because the member shape is a private member

public abstract class Shape  {      private int x; private int y;      public abstract void draw();      public void setAnchor(int x, int y) {                  this.x = x;                  this.y = y;      }} Which two classes use the Shape class correctly? (Choose two.)Select one or more:a.public abstract class Circle extends Shape {       private int radius;}b.public class Circle extends Shape  {     private int radius;     public void draw();}c.public class Circle extends Shape   {     private int radius;     public void draw() {/* code here */}}d.public class Circle implements Shape  {        private int radius;}

The __________ parameter is a reference to the current instance of the class, and is used to access variables that belong to the class.__init__()selfboth self and __init__()None of the above

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.