Logo
Flashcard >CS196 Test 2
Add Flashcard
1/20

What's the output of this code?

class Car:
    wheels = 4
    def __init__(self, name, price):
        self.name = name
        self.price = price
    @staticmethod
    def go(cls):
        print(f'car drives on {cls.wheels} wheels')
    @staticmethod
    def stop():
        print(f"car stops")
    @classmethod
    def checkTires(cls):
        print(f'all {cls.wheels} wheels look good')
    @classmethod
    def cost(self):
        print(f"{self.name} is {self.price}")
    def buy(self):
        print(f"you buy {self.name} for {self.price}")

c=Car('Blazer EV','$38,000 after rebate')
c.checkTires()

all 4 wheels look good

no output

Error

all wheels look good

all 4 wheels look good

profile imgdavidmacago edited a year ago

What's the output of this code?

class Car:
    wheels = 4
    def __init__(self, name, price):
        self.name = name
        self.price = price
    @staticmethod
    def go(cls):
        print(f'car drives on {cls.wheels} wheels')
    @staticmethod
    def stop():
        print(f"car stops")
    @classmethod
    def checkTires(cls):
        print(f'all {cls.wheels} wheels look good')
    @classmethod
    def cost(self):
        print(f"{self.name} is {self.price}")
    def buy(self):
        print(f"you buy {self.name} for {self.price}")

c=Car('Blazer EV','$38,000 after rebate')
c.checkTires()

all 4 wheels look good

no output

Error

all wheels look good

all 4 wheels look good

What is the output of this code?

class Car:
    wheels = 4                 # class attribute
    def __init__(self, name):  # instance method
        self.name = name       # instance attribute
    @classmethod
    def foo(cls):               # class method
        print(f'car has {cls.wheels} wheels')
    def bar(self):               # class method
        print(f'car has {self.wheels} wheels')

c=Car('Jeep')
c.wheels=5
c.foo()
c.bar()

car has 4 wheels
car has 5 wheels

Error

car has 5 wheels
car has 4 wheels

car has 4 wheels
car has 4 wheels

car has 5 wheels
car has 5 wheels

car has 4 wheels
car has 5 wheels

What's the output of this code?

class Car:
    wheels = 4
    def __init__(self, name, price):
        self.name = name
        self.price = price
    @staticmethod
    def go(cls):
        print(f'car drives on {cls.wheels} wheels')
    @staticmethod
    def stop():
        print(f"car stops")
    @classmethod
    def checkTires(cls):
        print(f'all {cls.wheels} wheels look good')
    @classmethod
    def cost(self):
        print(f"{self.name} is {self.price}")
    def buy(self):
        print(f"you buy {self.name} for {self.price}")

c=Car('Blazer EV','$38,000 after rebate')
c.buy()

no output

Error

you buy Blazer EV for $38,000

you buy Blazer EV for $38,000 after rebate

you buy Blazer EV for $38,000 after rebate

class Chair:
    legs = 4
    def __init__(self, name):
        self.name = name

name and legs are class attributes

name and legs are instance attributes

legs is a class attribute, name is an instance attribute

legs is a instance attribute, name is an class attribute

legs is a class attribute, name is an instance attribute

What's the output of this code?

class Chair:
    legs = 4
    def __init__(self, name):
        self.name = name

c1 = Chair("dining room chair")
c2 = Chair("barber's stool")
c2.legs = 1

print(Chair.legs)
print(c1.legs)
print(c2.legs)

4
4
1

1
1
1
Error

1
4
1

4
4
4

4
4
1

What's the output of this code?

class Car:
    wheels = 4
    def __init__(self, name, price):
        self.name = name
        self.price = price
    @staticmethod
    def go(cls):
        print(f'car drives on {cls.wheels} wheels')
    @staticmethod
    def stop():
        print(f"car stops")
    @classmethod
    def checkTires(cls):
        print(f'all {cls.wheels} wheels look good')
    @classmethod
    def cost(self):
        print(f"{self.name} is {self.price}")
    def buy(self):
        print(f"you buy {self.name} for {self.price}")

c=Car('Blazer EV','$38,000 after rebate')
c.stop()

Error

no output

car stops

car stops

What's the output of this code?

class Chair:
    legs = 4
    def __init__(self, name):
        self.name = name

c = Chair("lazy boy")

print(Chair.name)

4

None

lazy boy

Error

Error

in Python objects and classes are synonymous with

variables and types

methods and attributes

variables and functions

literals and docstrings

variables and types

What is the output of this code?

'image description'


Text generated from the image above
1 class Dog:

2 '"'Class used to represent dogs.''' 3 def _init (self):

4 print('creating a dog')

Options

Class used to represent dogs. creating a dog

Class used to represent dogs.

creating a dog

no output

"no output" because the Dog class was never called/invoked

An object is ___________ of a class.

an instance

a method

a constant

an attribute

an instance

Functions and variables that belong to an object are called

variables and types

comments and docstrings

methods and attributes

variables and literals

methods and attributes

What is the output of this code?

'image description'


Text generated from the image above
1 class Dog:

2 def init (self,name):

3 print('creating a dog')

4 self.name = name

5 def sit(self):

[3 print(f'{self.name} sits") 7

8 dogl = Dog('Kobe')

9 dogl.sit()


Options

creating a dog
Kobe sits

no output

Kobe sits

creating a dog

creating a dog
Kobe sits

What is the output of this code?

'image description'


Text generated from the image above
1 class Dog:

2 def init (self,name):

3 print('creating a dog')

4 self.name = name

5 def sit(self):

[3 print(f'{self.name} sits") 7

8 dogl = Dog('Kobe')


no output

Kobe sits

creating a dog
Kobe sits

creating a dog

creating a dog

What is the output of this code?

'image description'


Text generated from the image above
1 class Point:

2 def _init (self, x=1, y=1): Bl self.x = x

4 self.y = y

=

6 pl = Point(2,2)

7 p2 = Point()

8

9 print( pl.x, pl.y, p2.X, p2.y )


2 2 1 1

Error

2 2 0 0

2 2

2 2 1 1

class A:
    def __init__(self,name):
        self.name = name
    def __str__(self):
        return f"hi, my name is {self.name}"

class B(A):
    def __init__(self,name):
        print('hello from D')

b = B('john')
print(b)

hello from D
Error

hello from D

hello from D
hi, my name is john

hi, my name is john

Error

hello from D
Error

class A:
    def __init__(self):
        print('hello from A')

class B(A):
    def __init__(self):
        super().__init__()
        print('hello from B')

class C(B):
    def __init__(self):
        super().__init__()
        print('hello from C')

c = C()

hello from A
hello from B
hello from C

no output

error

hello from C
hello from B
hello from A

hello from C

hello from A
hello from B
hello from C

class MySequence(list):
    def __str__(self):
        return "MySequence" + super().__str__()

m = MySequence([1,2,3])
m.append(4)
print( m )

MySequence(1, 2, 3, 4)

MySequence[1, 2, 3, 4]

error

MySequence

MySequence[1, 2, 3, 4]

class A:
    def __init__(self):
        print('hello from A')

class B(A):
    def __init__(self):
        print('hello from B')

class C(B):
    def __init__(self):
        print('hello from C')

c = C()

hello from A
hello from B
hello from C

no output

error

hello from C

hello from C
hello from B
hello from A

hello from C

class A:
    def __init__(self,name):
        self.name = name
    def __str__(self):
        return f"hi, my name is {self.name}"

class B(A):
    def __init__(self,name):
        super().__init__(name)
        print('hello from D')

b = B('john')
print(b)

hello from D
hi, my name is john

hi, my name is john

hello from D

Error

hello from D
Error

hello from D
hi, my name is john

class A:
    def foo(self):
        print('foo in A')

class B:
    def bar(self):
        print('bar in A')

class C(A,B):
    def foo(self):
        print('foo in C')

class D(C):
    def foobar(self):
        self.foo()
        self.bar()
        print('foobar')

d = D()
d.foobar()

foo in C
bar in A
foobar

foobar

foo in A
bar in A
foobar

foo in D
bar in D
foobar

foo in C
bar in A
foobar

Recommended Books

Reading books is a great way to learn. Here are some of the books we recommend.