Home [Python] Namespace & OOP Quiz
Post
Cancel

[Python] Namespace & OOP Quiz

Quiz on Namespace/OOP

I intentionally disincluded the outputs. You should type the codes out on your own to fully understand. The way you answer each question is to write whatever that would happen when you run the code unless explicitly asked.

For example,

  1. If an exception occurs, you can answer “xxx exception occurs because ..”
  2. The code will print “hello world” because..
  3. It will print nothing because..

Since some of the questions might be very tricky if you don’t comprehensively understand the subjects, I recommend going over my other posts about OOP and Namespace for better understanding.

The namespace of each code block is assumed to be global or module namespace. In other words, __name__ == "__main__"

Class object & Instantiation

Q0

1
2
3
4
class Person:
    pass

p1 = Person()

Q1

1
2
3
4
5
class Person:
    def __init__(self):
        return "hello world"

p1 = Person()

Q2

1
2
3
4
5
class Person:
    def __init__():
        return "hello world"

p1 = Person()

Q3

1
2
3
4
5
class Person:
    def __init__():
        print("Instantiation successful!")

p1 = Person()

Q4

1
2
3
4
5
class Person:
    def __init__():
        print("Instantiation successful!")

p1 = Person(self)

Q5

1
2
3
4
5
class Person:
    def __init__(self):
        print("Instantiation successful!")

p1 = Person(self)

Q6

1
2
3
4
5
class Person:
    def __init__(self):
        print("Instantiation successful!")

p1 = Person()

Q7

1
2
3
4
5
6
7
8
9
loc = None

class Person:
    def __init__(self):
        global loc
        loc = self

p1 = Person()
print(p1 == loc)

Q8

1
2
3
4
class Person:
    "hello world!"

p1 = Person()

Q9

How to access the “hello world” from outside the class scope?

1
2
3
4
class Person:
    "hello world!"

(Write your code)

Q10

How to see all the attributes of the Person class object below?

1
2
3
4
5
6
7
8
9
10
class Person:
    a = 10
    b = 20
    c = 30
    def __init__(self):
        pass
    def greeting(self):
        print("hello!")

(Write your code)

Q11

1
2
a = lambda x: x + 1
print(type(a) == a.__class__)

Q12

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Person:
    class_name = "Jason"

    def __init__(self, inst_name):
        self.inst_name = inst_name

    def f(self):
        print("hello")

p1 = Person("bob")
p2 = Person("john")

print(p1.inst_name)
print(p2.inst_name)
print(Person.class_name)
print(p1.class_name)
print(p2.class_name)
print(id(p1.class_name) == id(p2.class_name))
print(id(p1.class_name) is id(p2.class_name))
print(id(p1.inst_name) == id(p2.inst_name))
print(p1.inst_name == p2.inst_name)
print(p1.class_name == p2.class_name)

Q13

1
2
3
4
5
6
7
8
9
10
11
12
class Person:
    def __init__(self):
        pass
    def f(self):
        print("hello")

p1 = Person()
p2 = Person()

print(Person.f.__class__)
print(p1.f.__class__)
print(p2.f.__class__)

Namespace and Scope

Q14

1
2
3
4
5
6
7
class Person:
    a = 10
    def __init__(self):
        pass

print(globals()['Person'] == Person)
print(locals()['Person'] == Person)

Q15

Why do people use the code below?

1
2
if __name__ == '__main__':
    print("hello world")

Q16

Name three namespaces. Also state the relationships between them.

Q17

1
2
3
print(globals() == locals())
print(globals() == vars())
print(locals() == vars())

Q18

Print all the built-in functions

Q19

1
print(globals()['__name__'])
1
2
globals()['__name__'] = '__modified__'
print(globals()['__name__'])

Q20

1
2
3
4
5
6
7
8
9
def outer():
    def inner():
        x = 1
        print(locals())
        locals()['x'] = 10
        print(locals())
    inner()

outer()

Q21

1
2
3
4
5
6
7
8
9
10
11
12
13
class Person:
    __var1_ = "hello" # at least 2 leading _ and at most 1 training _
    __var2 = "bye" # at least 2 leading _ and at most 1 training _
    ___var3 = "what's up" # at least 2 leading _ and at most 1 training _
    _nonprivate1 = "public1" # only one leading _ : not private (no name-mangling)
    __nonprivate2__ = "public2" # two training _ : not private (no name-mangling)

p = Person()
print(Person._Person__var1_)
print(Person._Person__var2)
print(Person._Person___var3)
print(Person._nonprivate1) # at least two leading underscores required but found ONLY ONE.""
print(Person.__nonprivate2__) # at most one trailing underscore required but found TWO
This post is licensed under CC BY 4.0 by the author.
Trending Tags