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 when code looks ugly

  what to do when a programmer dies

  None of the above

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

 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 ____________________?

  restricted access

  perfectly legal to be

  protected access (e.g. entry is limited)

  illegal to be found

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

  None of the above

  Protected

  Public

  Private

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

  None of the above

  Protected

  Public

  Private

 5. _______ Attributes can and should be freely used.

  Protected

  None of the above

  Public

  Private

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

  None of the above

  Public

  Protected

  Private

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

  Public

  Protected

  None of the above

  Private

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

  FALSE

  TRUE

 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

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

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

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

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

 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