Solved 50+ iOS SWIFT MCQ's

Objective Questions  iOS With Answers


  
      
Question 1
Given the code below, what data type does i variable have?

let i = 10.2


Correct answer: Double.
Explanation: When given a floating-point number, Swift's type inference will use the Double data type.



Question 2
What output will be produced by the code below?

enum Weather {
    case sunny
    case cloudy
    case windy(speed: Int)
}

let today: Weather = .windy(speed: 10)

switch today {
case .sunny, .cloudy:
    print("It's not that windy")
case .windy(let speed) where speed >= 10:
    print("It's very windy")
default:
    print("It's a bit windy")
}



Correct answer: "It's very windy".
Explanation: The windy case value has an associated value to store the wind speed as an integer. In the code, this is set to 10, which means the "It's very windy" case will be triggered in the switch block.


Question 3
When this code is executed, what will the third constant contain?

let first = ["Sulaco", "Nostromo"]
let second = ["X-Wing", "TIE Fighter"]
let third = first + second



Correct answer: "Sulaco", "Nostromo", "X-Wing", "TIE Fighter".
Explanation: Swift arrays can be joined together using the + operator, which adds the right-hand array to the end of the left-hand.


Question 4
What output will be produced by the code below?

var i = 2

repeat {
    i *= i * 2
} while i < 100

print(i)



Correct answer: 128.

Explanation: Each time the loop goes around, the i is doubled then multiplied by itself. The first time through the loop it will be 8, and the second time it will be 128, at which point the loop will exit and print 128. 


Question 5 
When this code finishes executing, how many strings will the names array contain?


let names = [String]()
names.append("Amy")
names.append("Clara")
names.append("Rory")
 










Correct answer: This code will not compile.
Explanation: The names array was declared using let, which makes it a constant. This means it is a compile error to try to use append() to add strings to it. 


Question 6 
What output will be produced by the code below?
struct Starship {
    var name: String
}

let tardis = Starship(name: "TARDIS")
var enterprise = tardis
enterprise.name = "Enterprise"

print(tardis.name)










Correct answer: "TARDIS".

Explanation: Although enterprise was created as a copy of tardis, it is a struct and therefore a value type: changing enterprise will not also change tardis.


Question 7
What output will be produced by the code below?

for i in 3...1 {
    print(i)
}



Correct answer: This code will compile but crash.

Explanation: This code will be compiled successfully, but crash at runtime: Swift does not allow you to generate ranges where the initial value is greater than the end value.


Question 8
When this code is executed, what will be the value of the j constant?

let i = "5"
let j = i + i



Correct answer: "55" (a string).

Explanation: When used with strings, the + operator acts to append one string to another. In this case, it merges "5" onto "5" to make "55" 


Question 9 
What output will be produced by the code below?
final class Dog {
    func bark() {
        print("Woof!")
    }
}

class Corgi : Dog {
    override func bark() {
        print("Yip!")
    }
}

let muttface = Corgi()
muttface.bark()











Correct answer: This code will not compile.

Explanation: This code attempts to create a new class, Corgi, that inherits from an existing class, Dog. Ordinarily that would be fine, but here the Dog class has been marked as final, which means it cannot be inherited from.


Question 10
When this code is executed, what value will num have?

let num = UInt.min



Correct answer: 0.

Explanation: UInt means "unsigned integer", which is a whole number that cannot be less than zero.
 



Question 11
What output will be produced by the code below?
let names = ["Serenity", "Sulaco", "Enterprise", "Galactica"]

for name in names where name.hasPrefix("S") {
    print(name)
}









Correct answer: "Galactica".

Explanation: Using names.last will return an optional string, but the if let unwraps that safely to produce "Galactica".




Question 12
When this code is executed, what is the value of myStr?

let myStr: String
myStr = "shiny"



Correct answer: "shiny".

Explanation: Swift constants do not have to be given an initial value, as long as they are given a value only once and before they are used.



Question 13
How many bits are used to store an Int?



Correct answer: It depends on the device.

Explanation: Swift's integers are 32-bit on 32-bit devices such as iPhone 5 and earlier, and 64-bit on 64-bit devices such as iPhone 5s and later.



Question 14
What output will be produced by the code below?

let names = ["Amy", "Clara"]

for i in 0 ... names.length {
    print("Hello, \(names[i])!")
}



Correct answer: This code will not compile.

Explanation: Swift arrays do not have a length property; this code should use count. With that replacement the code will compile, however it will then crash because it uses the closed range operator (...), which will cause an out-of-bounds error when reading into the array.
 


Question 15 
What output will be produced by the code below?
import Foundation
let number = 16
print("\(number) squared is \(number * number), and its square root is \(sqrt(number))")









Correct answer: "16.0 squared is 256.0, and its square root is 4.0".

Explanation: Using its type inference, Swift will consider number to be an Double, which will be interpolated correctly into the string even when sqrt() is called. When Doubles are interpolated into strings, they have .0 appended to their values when they have no fractional digits.


Question 16
What output will be produced by the code below?

var i = 2

do {
    print(i)
    i *= 2
} while (i < 128)



Correct answer: This code will not compile.

Explanation: The do keyword is invalid here; the programmer should use repeat instead.





Question 17
 

What output will be produced by the code below?


func sayHello(to name: String) -> String {
    return "Howdy, \(name)!"
}

print("\(sayHello(to: "Jayne"))")
 









Correct answer: "Howdy, Jayne!".

Explanation: This is a basic example of string interpolation in action. As well as replacing simple values, Swift can also call a function as part of interpolation, as seen here.
 


Question 18 
What output will be produced by the code below?
for i in 1...3 {
    print(i)
}









Correct answer: 1, 2, 3.

Explanation: This uses the closed range operator (...) to loop from 1 to 3 inclusive.




Question 19
What output will be produced by the code below?

let names = ["Chris", "Joe", "Doug", "Jordan"]

if let name = names[1] {
    print("Brought to you by \(name)")
}





Correct answer: This code will not compile.

Explanation: Subscripting an array of strings will return a String rather than a String?, which means it is a compile error to attempt to unwrap it using if let.




Question 20
What output will be produced by the code below?

let names = ["Amy", "Rory"]

for name in names {
    name = name.uppercased()
    print("HELLO, \(name)!")
}



Correct answer: This code will not compile.

Explanation: This code will not compile because it modifies name inside the loop. If you want to do this, you must use the var keyword like this: for var name in names.




Question 21
You are using a method named willThrowAnError(), and it is marked using throws because it will always throw the error "Forbidden". Given this context, what output will be produced by the code below?
do {
    try willThrowAnError()
} catch {
    print("The error message was: \(error)")
}







Question 22
What output will be produced by the code below?
let i = 3

switch i {
case 1:
    print("Number was 1")
case 2:
    print("Number was 2")
case 3:
    print("Number was 3")
}









Question 22
Which of the following structures has both computed and stored properties?
  • struct Rect { var origin = CGPointZero var center: CGPoint { get { // } set { // } } }
  • struct Rect { var center: CGPoint { get { // } set { // } } }
  • struct Rect { let origin = CGPointZero }
  • struct Rect { var origin = CGPointZero var center: CGPointMake(0,0) }

Question 23

Which one of the below functions definitions is wrong considering Swift language?
  • func haveChar(#string: String, character: Character) -> (Bool)
  • func mean(numbers: Double...) -> Double
  • func minMax(array: [Int]) -> (min: Int, max: Int)?
  • func minMax(array: [Int]) -> (min: Int?, max: Int?)

Question 24
Which keyword do you use to declare enumeration?
a. var
b. let
c. enum

d. e(num)


Question 25
Which of the following declares a mutable array in Swift?
  • let x = [Int]()
  • var x = [Int]
  • var x = [Int]()
  • let x = [Int]

Question 26
Which of these is an appropriate syntax for declaring a function that takes an argument of a generic type?
  • generic func genericFunction(argument: T) { }
  • func genericFunction<T>(argument) { }
  • func genericFunction(argument: T<Generic>) { }
  • func genericFunction<T>(argument: T) { }

Question 27
Which one creates a dictionary with a key type of Integer and value of String?
  • var dict:[Int: String] = ["one":1]
  • var dict: = [1: "name"] : [2, "name2"]
  • var dict: [Int: String] = [1:"one"]
  • var dict: [String: Int] = [1:"one"]

Question 28
Which of these is not a valid property declaration in Swift?
  • final let x = 0
  • final lazy let x = 0
  • final lazy var x = 0
  • final var x = 0

Question 29
All Swift classes must inherit from which root class?
  • Not Required
  • NSObject
  • NSRootObject
  • @ObjC

Question 30
Which of the following statements could be used to determine if a given variable is of String type?
  • if unknownVariable is String { }
  • if unkownVariable: String { }
  • if unkownVariable = String() { }
  • if unkownVariable <> String[] { }

Question 31
What would be used for safe casting and to return nil if failed?
  • as?
  • as!
  • !as?
  • !as!




Comments

  1. Why do many people replace Macbook Air for iPad Pro
    http://zapplerepair.com/why-people-replace-Macbook-Air-for-iPad-Pro.html

    ReplyDelete
  2. Google play music untuk memasukan lagu musik dari iTunes Mac ke Android
    http://zapplerepair.com/dari-iTunes-pindah-music-ke-Android-pakai-Google-play-music.html

    ReplyDelete
  3. Thank you for sharing such helpful post regarding interview question.



    Healthcare mobile app development

    ReplyDelete
  4. Great contribution by you for iOS Developers and Job Seekers.Keep the good work doing
    swift (iOS) interview questions and answers

    ReplyDelete

Post a Comment

Popular posts from this blog

Top 16 Mobile App Development Companies in India | App Developers 2017

CCEE CDAC Exam Questions

CDAC CCEE EXAM Prepration Link/PDF [Syllabus Topic Wise ] C++ & DS