Lecture 5 Flashcards

1
Q

What is the scope of a variable?

A

The scope of a variable is the range of statements in which the variable is visible.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When is a variable visible in a statement?

A

A variable is visible if it can be referenced in that statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the types of variables based on scope?

A

Local and non-local variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is static scoping?

A

A method of linking names to non-local variables so that the variable’s scope can be determined before the program runs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which languages support nested static scopes?

A

Ada
JavaScript
Common LISP
Scheme
Fortran 2003+
F
Python.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which languages do not support nested static scopes?

A

C-based languages.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How is a reference to a variable found in a static-scoped language?

A

The search starts in the subprogram where the variable is referenced. If not found, it continues in the subprogram’s static parent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can hidden variables be accessed in some languages?

A

Yes, for example, in Ada.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a hidden variable in static scope?

A

A variable in an outer scope that is hidden by another variable with the same name in an inner scope.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How are variables typically defined in blocks?

A

As static dynamic variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are blocks in programming?

A

Sections of code with their own local variables whose scope is minimized.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which languages do not allow block scope inside functions?

A

Java and C#

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Where must data declarations appear in C89?

A

At the beginning of the function, except in nested blocks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which languages allow variable declarations anywhere in the function?

A

C99, C++, Java, JavaScript, C#.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a global variable?

A

A variable defined outside all functions, potentially visible to all functions in the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How are global variables managed in C and C++?

A

Through declarations and definitions, with memory allocation only occurring at the definition.

15
Q

What is a drawback of static scoping?

A

It often allows more access to variables and subprograms than necessary, and software is highly dynamic.

16
Q

What is a merit of dynamic scoping?

A

Parameters passed from one subprogram to another can be variables defined in the caller, and none of these needs to be explicitly passed.

17
Q

What is the referencing environment of a statement?

A

The collection of all variables visible to the statement.

18
Q

How is the referencing environment determined in a static scoped language?

A

By the local scope plus all ancestor scopes.

19
Q

How is the referencing environment determined in a dynamic scoped language?

A

By the local variables plus variables of all currently active subprograms.

20
Q

What is a named constant?

A

A variable that is bound to a value only once.

21
Q

Why are named constants useful?

A

They aid readability and program reliability.

22
Q

Give an example of a named constant in Java.

A

final int len = 100;

23
Q
A