Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

What do you understand about protocols in ios Swift?

Answer»

The PROTOCOL is a concept that is similar to a Java interface and is a highly common component of the Swift programming language. A protocol is a set of attributes, methods, and other requirements that are appropriate for a specific activity. The protocol, in its most basic form, is an interface that describes some methods and characteristics. Instead of implementation, the protocol is described as a skeleton of properties or methods. Enumerations, functions, and classes can be used to implement properties and methods. After the structure, ENUMERATION, or class type names, protocols are declared. It is possible to declare both a single and several protocols. COMMAS are used to separate multiple protocols.

A protocol can be defined in a similar way to STRUCTURES, enumerations, and classes:

Protocol demoProtocol{// the protocol definition will be GOING at this place}

Multiple protocols can be defined by separating them using commas:

Class demoClass: demoSuperclass, protocolOne, protocolTwo{// the Structure definition will be going at this place}
2.

What is the use of the "mutating" keyword in ios Swift?

Answer»

Ios SWIFT structs are immutable since they are of the value type. Other variables, for example, cannot modify the values of structure at any point in time. Only the "mutating" keyword is necessary to change the values of SELF variables within the FUNCTION of the structure. Let us take the following CODE snippet for example:

struct demoStruct { var foo: String = "Initial String" func transformString() { foo = "Transformed String". //The above results in a compile time error: Cannot assign to property: 'self' is immutable. //We need to MARK the method 'mutating' to make 'self' mutable. }}

We get a compile-time error when we try to alter the value of variable "foo" inside a function declared in the struct itself.

As a result, we will need to create a mutating function to update the value inside the structure. As a result, the correct code is:

struct demoStruct { var foo: String = "Initial String" mutating func transformString() { foo = "Transformed String".  }}
3.

Name the JSON framework which is supported by iOS.

Answer»

iOS supports the SBJson framework. The SBJson framework adds more CONTROL and flexibility to the JSON handling process. It is a well DESIGNED and extremely adaptable framework that ALLOWS APIs (Application Programming Interfaces) to function in a variety of WAYS. SBJSON is one of the many open-source JSON parsers or generators created with Objective-C. These allow you to use JSON easily when coding Objective-C apps.

4.

What are some features that Swift classes can support but Swift structs cannot?

Answer»

Given below are some FEATURES which Swift classes can support but Swift structs cannot:

  • To develop our own custom VIEW controller subclasses, we can INHERIT from another class, such as UIViewController. This cannot be achieved using Swift structs.
  • Before a class is destroyed, it can be deinitialized by calling the deinit() function. This cannot be DONE for Swift structs.
  • Structures are value types, while classes are reference types.
    • Value Types: When you copy a value TYPE (for example, when it is assigned, initialised, or passed into a function), each instance preserves its own copy of the data. If you update one instance, it does not affect the other.
    • Reference Types: When you copy a reference type, the data is shared among all instances. The reference is copied, but the data it refers to is not. When one is altered, the other is altered as well.
5.

What are some common features in Swift structures and Swift classes?

Answer»

Some common features in Swift structures and Swift classes are as follows:

  • Swift Structs and Swift classes can both define attributes for STORING values and functions.
  • With init, both structures and classes in Swift can create initializers to set up their initial STATE ()
  • They can be extended using extensions.
  • They can FOLLOW PROTOCOLS, such as those used in Protocol Oriented Programming.
  • They can collaborate with generics to create types that are adaptable and reusable.
6.

State the control transfer statements present in ios Swift.

Answer»

The control transfer statements present in ios Swift are as FOLLOWS:

  • Continue: The continue statement skips the current iteration of a loop and directs the program's control flow to the next iteration.
  • BREAK: When the break statement is FOUND, a loop is immediately terminated.
  • Return: In Swift, the return statement is used in functions and methods to return VALUES that MEET our needs. In the Swift programming language, we can return values from functions/methods based on our requirements by utilising the return keyword.
  • Fallthrough: The fallthrough keyword merely directs code execution to the statements contained in the following case (or default case) block.
7.

In Swift, what does the init() method do?

Answer»

The process of preparing an instance of an enumeration, structure, or class for use is known as initialization. Initializers are also used when a new instance of a type is created. An initializer is a no PARAMETER instance method. The init keyword can be WRITTEN using the initializer. Their PRIMARY ROLE is to ensure that new instances of a type are correctly initialized before they’re used for the first time.

Its syntax is given below:

init(){// New Instances are initialized here}
8.

In iOS Swift, what types of objects are considered basic data types?

Answer»

For various purposes, Swift uses a common set of basic data types such as Boolean values, integers, and strings:

  • Int: The integer value is stored in the int variable. Example - 5
  • Double and Float: When working with DECIMAL numbers in Swift, double and float are TAKEN into account. Example - 5.78
  • Bool: The bool type is used to store the value of a Boolean. True and false conditions are used in Swift. Example - true
  • String: In Swift, the user defines the content that is enclosed by double quotes in String LITERALS. Example - "Muskan"
  • Arrays: Arrays are collections of OBJECTS of the same type from a list. Example - [2,4,11,90,78]
  • Dictionaries: A dictionary is an unsorted collection of elements of a specific type linked by a SINGLE key. Example - [{name:"Nidhi"}]
9.

Explain the various steps (or execution states) involved in the development of an ios Swift application.

Answer»

The various steps (or execution states) involved in the development of an ios SWIFT application are as follows:

  • Not Running: This is a SIMPLE condition in which our program has not been launched or no code has been executed. The program has been terminated by the SYSTEM.
  • Inactive: This is merely a stage in the process of becoming active. Our program is in an inactive state, which MEANS it is running in the background but unable to receive events.
  • Active: This is the main execution state, in which our software is running and receiving events.
  • Background: This is the condition in which our application is running in the background while still being able to run code.
  • Suspended: This state indicates that our application has not been terminated but is present in the background and that the system has suspended it for the TIME being. Now, the application cannot execute any code.
10.

List down three ways in which we can append two arrays in ios Swift.

Answer»

Let US consider that the two arrays are DECLARED as FOLLOWS:

var firstArray = ["Sonal", "RAHUL"]let secondArray = ["Nawaz", "Riya"]

A thing to be noted is that the first ARRAY has been kept mutable so we can append the second array to it. The three ways in which we can append the second array to the first one are as follows:

  • Using the method "append(contentsOf: )"- In this method, the contents of the second array are copied into the first array.
firstArray.append(contentsOf: secondArray)
  • Using the "+=" operator - In this method also, the contents of the second array are copied into the first array.
firstArray += secondArray
  • Appending two arrays by using the "+" operator and adding the result to a new array -
let thirdArray = firstArray + secondArray
11.

Throw light on some of the differences between Swift and Objective C.

Answer»

Some of the DIFFERENCES between ios Swift and Objective C are as follows:

Comparison Parameterios SwiftObjective C
Type of Programming Language.ios Swift is an object-oriented and functional programming language.Objective C is a class-based object-oriented programming language.
Dynamic LibrariesDynamic Libraries are supported by ios Swift. For instance, system iOS and macOS libraries are dynamic. In other words, these APPLICATIONS will receive improvements from Apple’s updates without new build submission.Dynamic Libraries are not supported by Objective C.
TuplesTuples are supported by ios Swift.Tuples are not supported by Objective C.
Usage of SemicolonsUsage of Semicolons is not mandatory in ios Swift.Usage of Semicolons is mandatory in Objective C.
Method definition in Classes, Structures and ENUMERATIONSThe definition of methods in Classes, Structures and Enumerations is allowed in ios Swift.The definition of methods in Classes, Structures and Enumerations is not allowed in Objective C.
12.

What is the difference between the "==" operator and the "===" operator in ios Swift?

Answer»

The fundamental difference between the "==" operator and the "===" operator in ios Swift is that the equal to "==" operator compares value types to see if the values are the same while the equivalent to "===" operator compares reference types to see if the references point to the same instance (both point to the same memory address) or not.  Let us consider the following example for understanding the difference between the two in a better way:

class Human: Equatable { let id: Int let nameOfPerson: String init(id: Int, nameOfPerson: String) { self.id = id self.nameOfPerson = nameOfPerson } static func == (left: Human, RIGHT: Human) -> BOOL { return left.id == right.id }}let human1 = Human(id: 2, nameOfPerson: "JANVI")let human2 = Human(id: 2, nameOfPerson: "Janvi")

Now, for the piece of code given below, we can say that the two human instances are equal since their id is the same. Therefore, "Equal Instances!" gets printed.

if human1 == human2 { PRINT("Equal Instances!")}else{ print("Instances Not Equal!")}

Now, for the piece of code given below, we can say that the two human instances are not equivalent even though their id is the same since they point to different areas in the Heap Area, that is, they point to different ADDRESSES. Therefore, "Instances are not Equivalent!" gets printed.

if human1 === human2 { print("Equivalent Instances!")}else{ print("Instances are not Equivalent!")}
13.

Highlight the key difference between Upcast and Downcast in ios Swift.

Answer»

The key difference between UPCAST and Downcast in ios SWIFT is that upcasting from a derived to a base CLASS can be verified at compile-time and will never fail to compile and Downcasts, on the other HAND, can fail to compile since the precise class is not always KNOWN. It is possible that the UIView you have is a UITableView or a UIButton.

14.

How is memory management done in iOS Swift?

Answer»

Automatic Reference Counting is used by Swift (ARC) in order to do memory management. This is the same thing in Swift as it is in OBJECTIVE C in terms of notion. When you assign or unassign instances of classes (reference types) to constants, properties, and variables, ARC keeps track of STRONG REFERENCES to those instances and increases or decreases their reference COUNT correspondingly. It frees up memory consumed by objects with a reference count of zero. Because value types are copied when assigned, ARC does not raise or DECREASE its reference count. If you don't declare differently, all references will be strong references by default.

15.

List some advantages and disadvantages of using ios Swift.

Answer»

Some advantages of using ios Swift are as follows:

  • It is very fast in terms of execution and both type-safe and memory-safe: Swift is an extremely fast language as far as execution speed is concerned. Also, it is very easy to learn and code in Swift is easy. Type safety refers to the language's ability to prevent type mistakes. Memory safety refers to the absence of FLAWS caused by uninitialized pointers, which could cause a program to crash. Developers can discover any code flaws with a shorter feedback loop (where outputs are sent back as inputs, which determines the cause and effect of that loop), which reduces debugging time and eliminates the danger of low-quality code.
  • Swift is interoperable with Objective C: In Swift, Projects can be WRITTEN in either Objective C or C++ because they are interoperable. This is particularly beneficial for large projects that are being upgraded when new Swift features are added and subsequently incorporated into the Objective C core.
  • Applications made using ios Swift are easy to maintain: Swift makes it simple to maintain an application once it has been built. Swift merges the Objective C header (.m) and implementation files (.h) into a single programme (.swift) file, as opposed to Objective C, which is managed in two separate files. It is worth noting that Swift has dependencies. On macOS, Swift is ALREADY installed and ready to use; however, on Linux, you must first install essential dependencies, such as Python and then use it.
  • Applications developed using Swift provide a better experience to its users: Swift developed applications take less time to install and consume less on device memory, giving users a better application experience.
  • Swift has efficient memory management: Swift expands on its ObjectiveC predecessor with a mechanism called Automatic Reference Counting (ARC). The ARC identifies which class instances aren't in use and removes them from developers' workspaces. This frees up time for developers to focus on the application's performance rather than lowering CPU or memory usage.
    Swift provides Application Binary Interface (ABI) stability: Swift's Application Binary Interface (ABI) is the binary version of the Swift Application Programming Interface (API). While ABI stability is an important accomplishment for any programming language, "the ultimate advantage to the Swift ecosystem was to enable binary compatibility for applications and libraries," according to Swift. In practice, the ABI enables code compiled with multiple versions of Swift, as well as Objective-C, to communicate with one another.
  • The usage of optionals in Swift: Optionals are a programming technique that allows developers to avoid application crashes because of Null Pointers while maintaining clean code across the application. Consider it a wrapper type that protects the VALUE inside. An optional might be filled with data or left blank. Optionals must be unwrapped to be sure, and if done correctly, will not cause crashes.

Some disadvantages of using ios Swift are as follows:

  • Swift is a new language: Swift is a newcomer compared to Objective C, which has been around since the 1980s. Swift came into being in 2014. It MAY experience growth pains as a result of this. Swift has a restricted set of tools and libraries, despite recent improvements with ABI Stability and backward compatibility. Also, there is not a very big community of ios Swift developers to help the budding ios Swift developers with their problems.
  • Binary compatibility does not work every time: Despite the fact that Swift 5.1 has ABI Stability, code generated using multiple versions of Swift can cause issues. Code might have been built as a static library and put into a project as a dependency when developers mostly used Objective C. It was impossible to develop static libraries in Swift prior to the release of Swift's ABI. While this is now possible, including those dependencies in a project poses challenges.
  • It is not a Reflective Language: Reflection is a technique that can be used to watch and change the execution of a program while it is running. A reflection oriented software component can track the execution of a code enclosure and adjust its behaviour to meet the enclosure's goals. This is usually performed by assigning programme code dynamically at runtime. Swift is not a reflective programming language in the same way that Java or Kotlin are. Instead, it provides an alternative: the Mirror feature. Swift can "self describe" an object with this, but it can't alter it from within. If Swift had a reflection, it would automatically inject dependencies, however, this is thought to be impossible to achieve.