
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
14 questions
Computing figures
Quiz
•
4th - 12th Grade
15 questions
CIW
Quiz
•
9th - 12th Grade
10 questions
Word basics
Quiz
•
8th - 12th Grade
10 questions
Online Databases vs. Search Engines
Quiz
•
6th - 10th Grade
15 questions
Systems Architecture
Quiz
•
KG - 9th Grade
10 questions
Microsoft Publisher
Quiz
•
7th - 10th Grade
10 questions
Logic Gates
Quiz
•
9th - 12th Grade
10 questions
AI and Python: Ch.1 Revision
Quiz
•
9th Grade
Popular Resources on Wayground
15 questions
Fractions on a Number Line
Quiz
•
3rd Grade
20 questions
Equivalent Fractions
Quiz
•
3rd Grade
25 questions
Multiplication Facts
Quiz
•
5th Grade
54 questions
Analyzing Line Graphs & Tables
Quiz
•
4th Grade
22 questions
fractions
Quiz
•
3rd Grade
20 questions
Main Idea and Details
Quiz
•
5th Grade
20 questions
Context Clues
Quiz
•
6th Grade
15 questions
Equivalent Fractions
Quiz
•
4th Grade