Search Header Logo

Understanding Variable Scope in Swift

Authored by Iman Meg

Computers

9th Grade

Used 6+ times

Understanding Variable Scope in Swift
AI

AI Actions

Add similar questions

Adjust reading levels

Convert to real-world scenario

Translate activity

More...

    Content View

    Student View

10 questions

Show all answers

1.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Consider the following Swift code snippet: ```swift func exampleFunction() { let localVariable = 10 print(localVariable) } ``` What is the scope of `localVariable` in the above code?

The entire program.

The function `exampleFunction`.

The file in which it is declared.

The class in which it is declared.

2.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Consider the following Swift code snippet: ```swift var globalVariable = 5 func anotherFunction() { print(globalVariable) } ``` What is the scope of `globalVariable` in the above code?

The function `anotherFunction`.

The block in which it is declared.

The entire program.

The class in which it is declared.

3.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What happens if a local variable in Swift has the same name as a global variable? ```swift var number = 20 func calculate() { let number = 10 print(number) } ```

The global variable is overridden by the local variable within the local scope.

The program will not compile.

The local variable is ignored.

The global variable is deleted.

4.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Which of the following is true about the lifecycle of a local variable in Swift? ```swift func temporaryFunction() { let tempVar = 100 // some code } ```

It persists for the lifetime of the program.

It is destroyed once the function or block in which it is declared is exited.

It is automatically initialized to zero.

It can be accessed from any part of the program.

5.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Which of the following is true about global variables in Swift? ```swift var globalCount = 0 func increment() { globalCount += 1 } ```

They are only accessible within the function they are declared in.

They are initialized to zero by default.

They persist for the lifetime of the program.

They cannot be changed once declared.

6.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

In Swift, where can you declare a global variable? ```swift var globalValue = 50 ```

Inside a function.

Inside a class method.

Outside of any function or class.

Inside a loop.

7.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Which of the following is a benefit of using local variables? ```swift func compute() { let localValue = 25 // some operations } ```

They can be accessed from anywhere in the program.

They help in reducing memory usage by being temporary.

They are automatically synchronized across threads.

They are easier to debug than global variables.

Access all questions and much more by creating a free account

Create resources

Host any resource

Get auto-graded reports

Google

Continue with Google

Email

Continue with Email

Classlink

Continue with Classlink

Clever

Continue with Clever

or continue with

Microsoft

Microsoft

Apple

Apple

Others

Others

Already have an account?