Understanding Variable Scope in Swift

Understanding Variable Scope in Swift

9th Grade

10 Qs

quiz-placeholder

Similar activities

Hangman Game - Programming Questions

Hangman Game - Programming Questions

7th - 11th Grade

15 Qs

Term (3) PC8.1 : Describe a function

Term (3) PC8.1 : Describe a function

9th Grade

7 Qs

Python Functions

Python Functions

9th - 12th Grade

8 Qs

Exploring Swift Programming

Exploring Swift Programming

9th Grade

10 Qs

Module 2 - Lesson 7

Module 2 - Lesson 7

6th - 12th Grade

13 Qs

PC4.8-PC4.9 CSC403 T2

PC4.8-PC4.9 CSC403 T2

9th Grade

10 Qs

Functions in C++

Functions in C++

9th - 12th Grade

10 Qs

CMU CS Academy Unit 2 Part 3

CMU CS Academy Unit 2 Part 3

9th Grade

11 Qs

Understanding Variable Scope in Swift

Understanding Variable Scope in Swift

Assessment

Quiz

Computers

9th Grade

Easy

Created by

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

Create resources
Host any resource
Get auto-graded reports
or continue with
Microsoft
Apple
Others
By signing up, you agree to our Terms of Service & Privacy Policy
Already have an account?