Computer Science : Scoping
Evaluation
Part – I
1. Which of the following refers to the visibility of variablesin one part of a program to another part of the same program.
(A) Scope
(B) Memory
(C) Address
(D) Accessibility
2. The process of binding a variable name with an object is called
(A) Scope
(B) Mapping
(C) late binding
(D) early binding
3. Which of the following is used in programming languages to map the variable and object?
(A) ::
(B) :=
(C) =
(D) ==
4. Containers for mapping names of variables to objects is called
(A) Scope
(B) Mapping
(C) Binding
(D) Namespaces
5. Which scope refers to variables defined in current function?
(A) Local Scope
(B) Global scope
(C) Module scope
(D) Function Scope
6. The process of subdividing a computer program into separate sub-programs is called
(A) Procedural Programming
(B) Modular programming
(C)Event Driven Programming
(D) Object oriented Programming
7. Which of the following security technique that regulates who canuse resources in a computing environment?
(A) Password
(B)Authentication
(C) Access control
(D) Certification
8. Which of the following members of a class can be handled only from within the class?
(A) Public members
(B)Protected members
(C) Secured members
(D) Private members
9. Which members are accessible from outside the class?
(A) Public members
(B)Protected members
(C) Secured members
(D) Private members
10. The members that are accessible from within the class and are also available to its sub- classes is called
(A) Public members
(B)Protected members
(C) Secured members
(D) Private members
Part – II
1. Mention the characteristics of Interface.
Ans. Scope refers to the visibility of variables, parameters and
functions in one part of a program to another part of the same program.
2. Why strlen is called pure function?
Ans. Scobe should be used for a variable because every part of the
program can access the variable.
3. What is the side effect of impure function. Give example.
Ans. The process of binding a variable name with an object is called
mappings. = (equal to sign) is used in programming languages to map the
variable and object.
4. Differentiate pure and impure function.
Ans. Namespaces are containers for mapping names of variables to
objects.
5. Wha happens if you modify a variable outside the function? Give an example.
Ans. Python prescribes a convention of prefixing the name of the
variable/method with single or double underscore to emulate the behaviour of
protected and private access specifiers.
Part – III
1. Define Local scope with an example.
Ans. (i) Local scope refers to variables defined in current
function. Always, a function will first look up for a variable name in its
local scope.
(ii) Only if it does not find it there, the outer scopes are
checked.
(iii) Look at this example :
(iv) On execution of the above code the variable a displays the
value 7, because it is defined and available in the local scope.
2. Define Global scope with an example.
Ans. (i) A variable which is declared outside of all the functions
in a program is known as Global variable.
(ii) This means, global variable can be accessed inside or
outside of all the functions in a program. Consider the following example
(iii) On execution of the above code the variable a which is
defined inside the function displays the value 7 for the function call Disp()
and then it displays 10, because a is defined in global scope.
3. Define Enclosed scope with an example.
Ans. (i) All programming languages permit functions to be nested. A
function (method) within another function is called nested function.
(ii) A variable which is declared inside a function which
contains another function definition with in it, the inner function can also
access the variable of the outer function. This scope is called enclosed scope.
(iii) When a compiler or interpreter search for a variable in a
program, it first searches Local, and then searches Enclosing scopes. Consider
the following example
4. Why access control is required?
Ans. (i) Access control is a security technique that regulates who
or what can view or use resources in a computing environment.
(ii) It is a fundamental concept in security that minimizes risk
to the object.
5. Identify the scope of the variables in the following pseudo code and write its output
color:= Red
mycolor():
b:=Blue
myfavcolor():
g:=Green
printcolor, b, g
myfavcolor()
printcolor, b
mycolor()
print color
Ans. color : =Red - global scope
b:=Blue - local scope
g:=Green - Enclosed scope
Part – IV
1. Explain the types of scopes for variable or LEGB rule with example.
Ans. Types of Variable Scope :
There are 4 types of Variable Scope, let's discuss them one by
one:
Local Scope:
(i) Local scope refers to variables defined in current function.
Always, a function will first look up for a variable name in its local scope.
Only if it does not find it there, the outer scopes are checked.
Look at this example
(ii) On execution of the above code the variable a displays the
value 7, because it is defined and available in the local scope.
Global Scope:
(i) A variable which is declared outside of all the functions in
a program is known as global variable.
(ii) This means, global variable can be accessed inside or outside of all the functions in a program. Consider the following example
(iii) On execution of the above code the variable 'a' which is
defined inside the function displays the value 7 for the function call DispO
and then it displays 10, because a is defined in global scope.
Enclosed Scope:
(i) All programming languages permit functions to be nested. A
function (method) with in another function is called nested function.
(ii) A variable which is declared inside a function which
contains another function definition with in it, the inner function can also
access the variable of the outer function. This scope is called enclosed scope.
(iii) When a compiler or interpreter search for a variable in a
program, it first search Local, and then search Enclosing scopes. Consider the
following example
(iv) In the above example Displ() is defined with in Disp(). The
variable a’ defined in Disp() can be even used by Displ () because it is also a
member of Disp().
Built-in Scope:
(i) Finally, we discuss about the widest scope. The built-in
scope has all the names that are pre-loaded into the program scope when we
start the compiler or interpreter.
(ii) Any variable or module which is defined in the library functions of a programming language has Built-in or module scope. They are loaded as soon as the library files are imported to the program.
LEGB rule :
(i) Scope also defines the order in which variables have to be
mapped to the object in order to obtain the value.
(ii) Let us take a simple example as shown below:
1. x:= 'outer x variable'
2. display():
3. x:= 'inner x variable'
4. print x
5. display()
(iii) When the above statements are executed the statement (4)
and (5) display the result as
Output:
outer x variable
inner x variable
(i) Above statements give different outputs because the same
variable name 'x' resides in different scopes, one inside the function
display() and the other in the upper level.
The value ‘outer x
variable’ is printed when x is referenced outside the function definition.
(ii) Whereas when display() gets executed, ‘inner x variable’ is printed which is the x value inside the
function definition. From the above example, we can guess that there is a rule
followed, in order to decide from which scope a variable has to be picked.
(iii) The LEGB rule
is used to decide the order in which the scopes are to be searched for scope
resolution. The scopes are listed below in terms of hierarchy (highest to
lowest).
Local(L) : Defined inside function/ class
Enclosed(E) : Defined inside enclosing functions (Nested
function concept)
Global(G) : Defined at the uppermost level
Built-in(B) : Reserved names in built- in functions (modules)
2. Write any Five Characteristics of Modules.
Ans. The following are the desirable characteristics of a module.
(i) Modules contain instructions, processing logic, and data.
(ii) Modules can be separately compiled and stored in a library.
(iii) Modules can be included in a program.
(iv) Module segments can be used by invoking a name and some
parameters.
(v) Module segments can be used by other modules.
3. Write any five benefits in using modular programming.
Ans. (i) Less code to be
written.
(ii) A single procedure can be developed for reuse, eliminating
the need to retype the code many times.
(iii) Programs can be
designed more easily because a small team deals with only a small part of the
entire code.
(iv) Modular programming allows many programmers to collaborate
on the same application.
(v) The code is stored across multiple files.
(vi) Code is short, simple and easy to understand.
(vii) Errors can easily be identified, as they are localized to
a subroutine or function.
(viii) The same code can be used in many applications.
(ix) The scoping of variables can easily be controlled.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.