Preview

12 - Public and Private attributes

 1. In the design of object-oriented programming languages, it is important to consider how to protect the data which should be private as well as _______________

  what to do if trespassing, i.e. accessing or changing private data, occurs

  what to do when a programmer dies

  what to do when code looks ugly

  None of the above

 2. Consider the following analogy and fill in the blanks.
1.Some owners allow a restricted access to their property. 
An unauthorised person may not enter.

2. Joggers or hikers may find signs like 
"Enter at your own risk". 

3. A third kind of property might be public property 
like streets or parks, where it is ____________________?

  illegal to be found

  perfectly legal to be

  restricted access

  protected access (e.g. entry is limited)

 3. ______attributes should only be used by the owner, i.e. inside of the class definition itself.

  Private

  Protected

  None of the above

  Public

 4. ______ (restricted) Attributes may be used, but at your own risk. Essentially, this means that they should only be used under certain conditions.

  Protected

  Public

  None of the above

  Private

 5. _______ Attributes can and should be freely used.

  None of the above

  Protected

  Private

  Public

 6. Prefix an attribute name with a leading underscore "_". This marks the attribute as

  Private

  Public

  Protected

  None of the above

 7. Prefix an attribute name with two leading underscores "__". The attribute is now inaccessible and invisible i.e. ______

  None of the above

  Private

  Public

  Protected

 8. Python doesn’t really enforce encapsulation strongly like Java. Rather it does something called name mangling

  TRUE

  FALSE

 9. The following example is given in VB.Net. Analyse it and decide which of the following statements is correct.
    Public Class Book
        Private strTitle As String

        Public Property Title()
            Get
                Return strTitle
            End Get
            Set(ByVal Value)
                strTitle = Value
            End Set
        End Property
    End Class

  In this example there are no private attributes as a Class isn't allowed this capability

  Here, the 'Return Str' will cause an error, because the 'Title' is a private attribute

  In this example there are two private methods: 'Get' and 'End Set'

  Here, the member variable strTitle is declared as private inside a class Book

 10. In this example the members (e..g age) cannot be accessed in the same class but they are available to all other classes.
Public Class Class1
        Protected age As Integer

        '... other code
    End Class

    Public Class Class2
        Inherits Class1

        Public Sub SomeMethod()
            age = 99 'OK
        End Sub
    End Class

    Public Class Class3
        Public Sub SomeMethod()
            Dim x As New Class1()
            x.age = 99 'ERROR
        End Sub
    End Class

  TRUE

  FALSE