**Topics: Declaration, Type Annotation, Naming, and Printing
Checkout the interactive quiz of this story here https://quizzesforyou.com/quiz/swiftconstsvariables
Quick Refresher:
Constants and variables associate a name with a value of a particular type. Constants have values that cannot be changed, while variables can be reassigned to different values in the future.
Constants are declared using the
let
keyword, and variables are declared using thevar
keyword.Type annotations can be provided when declaring constants or variables to explicitly specify their types. However, Swift can usually infer the type based on the initial value, so type annotations are not always necessary.
It is recommended to use constants (
let
) for values that won’t change, and variables (var
) for values that may change.Multiple constants or variables of the same type can be declared on a single line, separated by commas.
Constant and variable names can contain almost any character, including Unicode characters. However, there are some restrictions on characters that can be used in names.
Once a constant or variable is declared, its type cannot be changed, and you cannot redeclare it with the same name.
The
print(_:separator: terminator:)
function can be used to print the value of a constant or variable.String interpolation is a feature in Swift that allows you to include the value of a constant or variable within a string using placeholders.
-
Constants and variables are fundamental building blocks in Swift for storing and working with data.
Checkout the interactive quiz of this story here https://quizzesforyou.com/quiz/swiftconstsvariables
Quizzes:
Quiz 1:
What keyword is used to declare constants in Swift?
a) var
b) let
c) const
Answer: b) let
In Swift, the let
keyword is used to declare constants. Constants have values that cannot be changed once they are set.
The var
keyword is used to declare variables. Variables have values that can be changed after they are declared.
Quiz 2:
Are the below constants valid?
let 你好 = “你好世界”
let 🐶🐮 = “dogcow”
a) Yes
b) No
Answer: b) Yes
Constant and variable names can’t contain whitespace characters, mathematical symbols, arrows, private-use Unicode scalar values, or line- and box-drawing characters
Quiz 3:
What is the syntax for declaring multiple constants on a single line?
a) var x = 1, y = 1, z = 1
b) let x = 1, y = 1, z = 1
c) let x, y, z = 1
Answer: b) let x = 1, y = 1, z = 1
To declare multiple constants on a single line in Swift, separate them with commas and use the let
keyword for each constant.
Quiz 4:
What is the purpose of providing a type annotation in Swift?
a) To specify the value of a constant or variable.
b) To indicate the type of values a constant or variable can store.
c) To make the code more readable.
Answer: b) To indicate the type of values a constant or variable can store.
Type annotations in Swift are used to specify the type of values that a constant or variable can hold. They provide clarity and help prevent type-related errors.
Quiz 5:
What characters are not allowed in constant and variable names?
a) Whitespaces and mathematical symbols
b) Letters and numbers
c) Special characters and emojis
Answer: a) Whitespaces and mathematical symbols
Constant and variable names in Swift cannot contain whitespaces and mathematical symbols. They should be composed of letters, numbers, and certain special characters.
Quiz 6:
How do you print the current value of a constant or variable in Swift?
a) printValue()
b) print()
c) log()
Answer: b) print()
In Swift, you can print the current value of a constant or variable using the print()
function.
Quiz 7:
What is the default terminator used by the print()
function?
a) Line break
b) Space
c) Tab
Answer: a) Line break
Explanation: The print()
function in Swift uses a line break as the default terminator. This means that it adds a new line after printing the value by default.
Quiz 8:
What is the purpose of string interpolation in Swift?
a) To print the name of a constant or variable
b) To include the value of a constant or variable in a string
c) To format strings in a specific way
Answer: b) To include the value of a constant or variable in a string
String interpolation in Swift allows you to include the value of a constant or variable within a string. It helps create dynamic and readable strings.
Quiz 9:
var while
= 10.
Is the variable valid?
a) compilation error
b) No, it is not allowed
c) Only if you escape them with backticks (`)
Answer: a) Yes, but it is not recommended
It is possible to use reserved Swift keywords as constant or variable names by escaping them with backticks (`), it is generally not recommended. It can make code harder to read and understand.
Quiz 10:
Q1: What happens if you try to change the value of a constant in Swift?
a) It compiles successfully and updates the value.
b) It results in a compile-time error.
c) It results in a run-time error.
Answer: b) It results in a compile-time error.
In Swift, constants are designed to have values that cannot be changed once they are set. Therefore, if you try to change the value of a constant, it will result in a compile-time error.
Visit https://quizzesforyou.com for more quizzes
Top comments (0)