Swift Quiz #01: Constants and Variables
Topics : Declaration, Type Annotation, Naming, and Printing
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 the var 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.
What keyword is used to declare constants in Swift?
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.
Are the below constants valid?
let 你好 = “你好世界” let 🐶🐮 = “dogcow”
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
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.
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.
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.
How do you print the current value of a constant or variable in Swift?
Answer: b) print()
In Swift, you can print the current value of a constant or variable using the print() function.
What is the default terminator used by the print() function?
a) Line break
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.
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.
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.
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.