1.
Which keyword do you use to declare constants in Swift?
Correct Answer
B. Let
Explanation
In Swift, the keyword "let" is used to declare constants. Constants are values that are set once and cannot be changed throughout the execution of the program. Using "let" helps in creating safer and more predictable code, especially when you know that the value should not change after its initial setup. This contrasts with "var," which is used for variables that can be modified after their initial declaration. Using the correct keyword for constants and variables is fundamental in Swift programming to manage data effectively and reduce errors.
2.
How many types of integers are there in Swift?
Correct Answer
D. 4
Explanation
In Swift, there are four main types of integers. These include Int and UInt, which are signed and unsigned integers respectively, and their size variants Int8, Int16, Int32, Int64, and UInt8, UInt16, UInt32, and UInt64. These variants are used to specify integers of different sizes, where the number following Int or UInt indicates the number of bits. This allows developers to choose the appropriate type based on the required size and whether a signed or unsigned integer is needed, optimizing memory usage and performance in applications.
3.
Which bit form is not used for presenting Swift integers?
Correct Answer
A. 24
Explanation
In Swift, integers are typically represented in several standardized bit forms, specifically 8, 16, 32, and 64 bits, corresponding to types such as Int8, Int16, Int32, and Int64. The number 24 does not correspond to any standard integer type in Swift, making it the correct answer as it is not used for representing Swift integers. These bit forms are essential for determining the amount of memory that each integer type uses and its range of possible values, with each type catering to different requirements based on the size and performance needs of the application.
4.
What type of integer is denoted by "Int8"?
Correct Answer
B. Signed
Explanation
In Swift, the "Int8" denotes a signed integer type that occupies 8 bits of memory. The term "signed" indicates that this type of integer can represent both positive and negative values. Unlike unsigned integers, which can only represent positive numbers, signed integers like Int8 use one of the bits as a sign bit to indicate whether the number is positive or negative. This allows Int8 to range from -128 to 127, effectively using the available 8 bits to cover a range of values including zero and negative numbers.
5.
What are numbers with fractional components called in Swift?
Correct Answer
C. Floating numbers
Explanation
In Swift, numbers that include fractional components are referred to as "floating numbers" or more formally, floating-point numbers. These types of numbers are used to represent values that require more precision than integers can provide, such as measurements, calculations involving fractions, or when mathematical precision is crucial. Floating-point numbers in Swift are typically declared using the Float or Double types, with Float representing a 32-bit floating-point number and Double representing a 64-bit floating-point number, offering more precision and a wider range of values.
6.
How do you write comments in Swift?
Correct Answer
C. /* and */
Explanation
In Swift, comments that span multiple lines are enclosed between /* and */. This syntax allows you to write explanations or annotations within your code that are ignored by the compiler. The /* marks the beginning of the comment, and */ marks the end. Anything written between these two symbols will not affect the execution of the program but can be used to provide valuable information about what the code does, making it easier for others (or yourself at a later time) to understand the code's purpose and functionality.
7.
What are the collection types in Swift?
Correct Answer
A. Dictionary and array
Explanation
In Swift, the primary collection types are dictionaries, arrays, and sets. This makes the correct options dictionary and array, as they are two of the three fundamental collection types used to store collections of data in Swift. Arrays store data in an ordered list where each value is associated with a unique index. Dictionaries store data in a collection of key-value pairs where each value is associated with a unique key, which is used to access or modify the value. Sets, although not mentioned in the answer choices, store distinct values of the same type in a collection with no defined ordering.
8.
Which of these is not a control transfer statement in Swift?
Correct Answer
C. Change
Explanation
In Swift, control transfer statements include 'break', 'continue', and 'return', among others. These statements are used to change the flow of execution in a program. 'Break' is used to stop the current loop or switch statement. 'Continue' skips the remaining code in the current loop iteration and begins the next iteration. 'Return' exits from the current function, possibly returning a value. 'Change' is not recognized as a control transfer statement in Swift programming; it does not exist within the language's syntax for controlling execution flow.
9.
What does the question mark (?) indicate when used in Swift?
Correct Answer
C. Optional
Explanation
In Swift, the question mark (?) is used to denote an optional type. This means that a variable can hold either a value of a specified data type or no value at all (nil). Opting for optionals is a fundamental aspect of Swift, designed to handle the absence of a value safely. By using optionals, developers can write safer code that explicitly handles the cases of missing or undefined values without causing runtime errors. This allows for more robust and error-resistant programming, particularly when dealing with data that might not always be present.
10.
What should you use to provide a default value for a variable in Swift?
Correct Answer
B. ??
Explanation
In Swift, the ?? operator is used to provide a default value for a variable. This operator is known as the nil coalescing operator. It is used with optionals to provide a default value in case the optional contains nil. For instance, if you have an optional string variable that might be nil, you can use ?? to specify a default string to be used if the optional is indeed nil. This operator helps in avoiding runtime errors by ensuring that variables have usable values, thus contributing to the robustness and reliability of the code.