
Understanding Variable Scope in Swift

Quiz
•
Computers
•
9th Grade
•
Easy
Iman Meg
Used 6+ times
FREE Resource
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.
Create a free account and access millions of resources
Similar Resources on Wayground
12 questions
HIGHER Python Subroutines Quiz

Quiz
•
9th Grade
10 questions
Function with parameters in swift playground - 1

Quiz
•
9th Grade
10 questions
Python Code Review

Quiz
•
9th Grade
15 questions
Python Basics Quiz

Quiz
•
6th - 10th Grade
12 questions
Python Programming Basics

Quiz
•
7th - 11th Grade
10 questions
python quiz

Quiz
•
6th Grade - Professio...
10 questions
LUA Test

Quiz
•
9th Grade - University
9 questions
Swift Playgrounds

Quiz
•
9th Grade
Popular Resources on Wayground
10 questions
Video Games

Quiz
•
6th - 12th Grade
10 questions
Lab Safety Procedures and Guidelines

Interactive video
•
6th - 10th Grade
25 questions
Multiplication Facts

Quiz
•
5th Grade
10 questions
UPDATED FOREST Kindness 9-22

Lesson
•
9th - 12th Grade
22 questions
Adding Integers

Quiz
•
6th Grade
15 questions
Subtracting Integers

Quiz
•
7th Grade
20 questions
US Constitution Quiz

Quiz
•
11th Grade
10 questions
Exploring Digital Citizenship Essentials

Interactive video
•
6th - 10th Grade
Discover more resources for Computers
10 questions
Exploring Digital Citizenship Essentials

Interactive video
•
6th - 10th Grade
20 questions
Analog vs Digital

Quiz
•
9th - 12th Grade
10 questions
Exploring Cybersecurity Techniques and Threats

Interactive video
•
6th - 10th Grade
10 questions
Understanding the Internet and Data Transmission

Interactive video
•
7th - 12th Grade