CS - Classes

Question 1

1 / 1 point

What is Instantiation in terms of OOP terminology?

Question options:

Unselected

Deleting an instance of class

Unselected

Modifying an instance of class

Unselected

Copying an instance of class

Selected

Creating an instance of class

Question 2

1 / 1 point

A class can be used to group related variables together.

Question options:

Selected

True

Unselected

False

Question 3

1 / 1 point

_____ is used to create an object?

Question options:

Unselected

class

Selected

constructor

Unselected

User-defined functions

Unselected

In-built functions

Question 4

2 / 2 points

Find the correct matching explanation for each term

4

A method parameter that refers to the class instance.

2

A constructor method that initializes a class instance.

3

A group of related variables and functions.

1

A name following a "." symbol.

5

An instantiation of a class.

1.

attribute

2.

init

3.

class

4.

self

5.

instance

Question 5

2 / 2 points

Find the correct matching explanation for each class or object term

3

A variable shared with all instances of a class.

2

A variable that exists in a single instance.

4

A factory for creating new class instances.

5

Represents a single instance of a class.

1

Functions that are also class attributes.

1.

Instance methods

2.

Instance attribute

3.

Class attribute

4.

Class object

5.

Instance object

Question 6

1 / 1 point

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()

Question options:

Unselected

The program has an error because constructor can't have default arguments

Unselected

The program has an error display function doesn't have parameters

Unselected

Nothing is displayed

Selected

"Hello World" is displayed

Question 7

1 / 1 point

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()

Question options:

Unselected

Runs normally, doesn't display anything

Unselected

Displays 0, which is the automatic default value

Selected

Error as one argument is required while creating the object

Unselected

Error as display function requires additional argument

Hide question 7 feedback

Feedback

The init special method has another argument a other than self, during object creation, one argument is required.

For example: obj=test("Hello World")

Question 8

1 / 1 point

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)

Question options:

Unselected

Error because function change can't be called in the init function

Unselected

'New' is printed

Selected

'Old' is printed

Unselected

Nothing is printed

Question 9

0 / 1 point

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__ ) )

Question options:

Unselected

12

Unselected

52

Unselected

13

Selected

60

Question 10

1 / 1 point

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( )

Question options:

Unselected

Exception is thrown

Unselected

Demo

Unselected

name

Selected

main

Question 11

1 / 1 point

 What will be the output of the following Python code?

def add(c,k):

    c.test=c.test+1

    k=k+1


class A:

    def init(self):

        self.test = 0


def main():

    Count=A()

    k=0

    for i in range(0,25):

        add( Count, k)

    print("Count.test=", Count.test)

    print("k =", k)

   

main( )

Question options:

Unselected

Error happened in the above code. 

Unselected

Count.test=25
k=25

Selected

Count.test=25
k=0

Unselected

Count.test=0
k=0

Hide question 11 feedback

Feedback

test is a member of the class while k isn't. Hence test keeps getting incremented 25 time while k remains 0.

Question 12

1 / 1 point

To fix the error in the following Python code, which answer is correct? 

class Person:

    def init(self, the_last, the_first):

        last = the_last

        first = the_first

    def str(self):

        return( f"{last}  {first} " )

p1 = Person( )

print( p1 )

Question options:

Unselected

class Person:

    def init(self, last, first):

        last = last

        first = first

    def str(self):

        return( f"{last}  {first} " )

p1 = Person( )

print( p1 )

Unselected

class Person:

    def init(self, the_last, the_first):

        self.last = the_last

        self.first = the_first

    def str(self):

        return(f"{self.last}  {self.first} " )

p1 = Person( )

print(p1)

Unselected

class Person:

    def init(self, the_last, the_first):

        self.last = the_last

        self.first = the_first

    def str(self):

        return(f"{the_last}  { the_first} " )

p1 = Person("AB","CD")

print(p1)

Selected

class Person:

    def init(self, the_last, the_first):

        self.last = the_last

        self.first = the_first

    def str(self):

        return(f"{self.last}  {self.first} " )

p1 = Person("AB","CD")

print(p1)

Question 13

1 / 1 point

Which code can output "we will go to mountain" ?

Question options:

Unselected

class Foo:

    a = "mountain"

    def init(self, a):

        self.a = a

    def someday(self):

        return "we will go to " + a


f1 = Foo("Hi, Jack")

print(f1.someday())

Unselected

class Foo:

    a = "mountain"

    def init(self, a):

        self.a = a

    def someday(self):

        return "we will go to " + self.a


f1 = Foo("Hi, Jack")

print(f1.someday())

Selected

class Foo:

    a = "mountain"

    def init(self, a):

        self.a = a

    def someday(self):

        return "we will go to " + Foo.a


f1 = Foo("Hi, Jack")

print(f1.someday())

Unselected

class Foo:

    a = "mountain"

    def init(self, a):

        self.a = a

    def someday(self):

        return "we will go to " + self.a


f1 = Foo( )

print(f1.someday())