Swift Interview Questions | Eklavya Online

Swift Interview Questions

A Swift literal is a direct value of a variable or a constant. It may be a number, character or string. Literals are used to initialize or assign value to variables or constants.

  • Switch statement are used as a substitute for the long if-else-if statements.
  • Switch statement supports any type of data, synchronizes them and also checks for equality.
  • Break is not required in switch statement because there is no fall through in switch statement.
  • Switch statement must have covered all possible values for your variable.

The break statement is used within a loop where you have to immediately terminate a statement. It is also used to terminate a case in switch statement.

Enum is also known as Swift Enumeration. Enum is a data type which contains a set of the related values. It is declared in a class and its values are accessed through the instance members of that class.

Numbers with decimal values or fractional components are called floating numbers. For example: 1.34 is a floating point number. Floating point types can represent a wider range of values than integer types. There are two signed floating point number:

Double: It represents a 64 bit floating point number. It is used when floating point values are very large.

Float: It represents a 32 bit floating point number. It is used when floating point values does not need 64 bit precision.

Swift and Objective-C both are used in iOS development but both are significantly different in terms of efficiency and usage.

  • Swift is an open-source programming language developed by Apple platform and expanded to build on Linus while Objective-
  • C is not an open-source programming language and is limited to Apple.
  • Swift syntax is easy, clear and brief. It makes API’s easy to read and maintain while Objective-C is based on C language which is comparatively hard to use.
  • Swift is more rational and precise that’s why it has less number of code and easy to learn while Objective-C code is lengthy as double to Swift code.
  • Swift can be compiled as a dynamic framework while Objective-C cannot be compiled into static libraries and dynamic framework.

In Swift, question mark “?” is used in property declaration. It tells the compiler that this property is optional. The property may hold a value or not. It avoids runtime errors when accessing that property by using ?. This is useful in optional chaining and a variant of this example is in conditional clauses.

For example:

var optionalName : String? = “John”
if optionalName != nil {
print(“Your name is \(optionalName!)”)

The continue statement is used in Swift loop to change execution from its normal sequence. It stops currently executing statement and starts again at the beginning of the next iteration through the loop.

Regular Expression: Regular expressions are the special string patterns that specify how to perform a search through a string.

Responder Chain: Responder Chain is a hierarchy of objects that obtain the opportunity to respond to the events.

In Swift programming language, single-line comments are started with double slashes (//).

For example:

// This is a single line comment.
Multi-line comment: Multiline comments are started with a forward-slash followed by an asterisk (/*) and end with an asterisk followed by a forward-slash (*/).

For example:

/* this is multi
Line comment*/

We have to declare a question mark ??’ in the code to make a property optional. If a property doesn’t have a value, then the symbol ? helps to avoid the runtime error.

There are two varieties of collection types in Swift:

Array: In Swift, you can create an array of single type or an array of multiple type.

Dictionary: In Swift, dictionary is similar to hash table in other programing language. You can store a key-value pair in a dictionary and access the value by using the key.

Swift Dictionary is used to store the key-value pairs and access the value by using the key. It is just like hash tables in other programming languages.

Swift language consists of following Control transfer statements:

  • Continue
  • Break
  • Fallthrough
  • Return

Swift supports several types of operators. One of them is Half Open Range operator. The half open range operator specifies a range between two values a and b (a<b) where b is not included. It is known as half open range operator because it contains its first value only not the final value.

Inheritance is a process in which a class can inherit properties, methods and other characteristics from another class. Inheritance is supported in Swift programming language. There are two types of classes in Inheritance in Swift:

Sub class: The class which inherits the properties from other class is called child class or sub class.

Super class: The main class from where the subclass inherits the properties is known as parent class or super class.

In Swift programming language, Optional Chaining is a process of querying and calling properties. You can chain multiple queries together, but if any link in the chain is nil then, the entire chain fails.

Functions are the set of codes used to perform specific task. In Swift programming language, functions are used to pass local as well global parameter values inside the function’s call.

In Swift4, functions can be categorized in two types:

  • User Defined Functions
  • Built-in Functions (Library Functions)
  • Swift is very easy to learn and precise to use. It provides a safe programming approach.
  • In Swift programming language, variables and constants must be initialized before use.
  • Automatic memory management.
  • Swift uses “switch” function instead of “if” statement for conditional programming.
  • Swift follows Objective-C like syntax.
  • It checks Arrays and integers for overflow.

Lazy stored properties are used for a property whose initial values are not calculated until the first time it is used. A lazy stored property can be declared by writing the lazy modifier before its declaration. Lazy properties are useful when the initial value for a property is reliant on outside factors whose values are unknown.