
Understanding Variable Scope in Swift
Authored by Iman Meg
Computers
9th Grade
Used 6+ times

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

Continue with Google

Continue with Email

Continue with Classlink

Continue with Clever
or continue with

Microsoft
%20(1).png)
Apple
Others
Already have an account?
Similar Resources on Wayground
10 questions
Programming Constructs
Quiz
•
5th - 11th Grade
11 questions
Threats to Data
Quiz
•
9th - 12th Grade
11 questions
HEALTH AND SAFETY 9
Quiz
•
7th - 9th Grade
10 questions
Basic Parts of Speech EPS
Quiz
•
9th - 10th Grade
15 questions
Quiz 1
Quiz
•
1st - 12th Grade
15 questions
visual basic 6.0 LDA
Quiz
•
9th Grade
14 questions
LA5.C5: Branching and Subroutines
Quiz
•
9th Grade - University
13 questions
Guess the Retro Computer 1
Quiz
•
KG - Professional Dev...
Popular Resources on Wayground
7 questions
History of Valentine's Day
Interactive video
•
4th Grade
15 questions
Fractions on a Number Line
Quiz
•
3rd Grade
20 questions
Equivalent Fractions
Quiz
•
3rd Grade
25 questions
Multiplication Facts
Quiz
•
5th Grade
22 questions
fractions
Quiz
•
3rd Grade
15 questions
Valentine's Day Trivia
Quiz
•
3rd Grade
20 questions
Main Idea and Details
Quiz
•
5th Grade
20 questions
Context Clues
Quiz
•
6th Grade
Discover more resources for Computers
10 questions
Exploring Valentine's Day with Charlie Brown
Interactive video
•
6th - 10th Grade
18 questions
Valentines Day Trivia
Quiz
•
3rd Grade - University
20 questions
El Verbo IR Practice
Quiz
•
9th Grade
10 questions
Valentine's Day History and Traditions
Interactive video
•
6th - 10th Grade
20 questions
Graphing Inequalities on a Number Line
Quiz
•
6th - 9th Grade
20 questions
Exponent Properties
Quiz
•
9th Grade
21 questions
Presidents Day Trivia
Quiz
•
6th - 12th Grade
10 questions
Valentine's Day: History and Modern Celebration
Interactive video
•
9th - 12th Grade