Swift MCQs

Swift MCQs

Answer these 300+ Swift MCQs and see how sharp is your knowledge of Swift.
Scroll down and let's start!

1:

Which of the following statements are incorrect regarding closures in Apple Swift?

1. In Apple Swift, a closure can capture all the references to variables from the surrounding context in which it is def‌ined, but cannot capture references to constants.

2. A nested function is a closure that has a name but it cannot capture any value from its enclosing function.

3. A global function is a closure that has a name and does not capture any value.


A.  

Only 2 and 3

B.  

Only1 and 3

C.  

Only1 and 2 

2:

In Apple Swift, which of the following parameters can be used in a closure expression?

1. Constant parameters

2. Variable parameters

3. Inout parameters

4. Variadic parameters

5. Tuple parameters


A.  

Only 1. 2. and 3

B.  

Only 1, 2, 3, and 5

C.  

Only 2, 3, and 5

D.  

Only 1. 2. 3. and 4

E.  

All of the given parameters can be used in a closure expression.

3: Which of the following options holds true for observers in Apple Swift, when a property is set in an initializer before delegation takes place?

A.   Only willset observer is called.

B.   Only didset observer is called.

C.   Neither willset nor didset observer is called.

4:

In Apple Swift, which of the given statements is correct about initializers?

1. Failable and non-failable initializers can be def‌ined with the same name and parameter types

2. No value can be returned by initializers.

3. A failable initializer cannot delegate to a non-failable initializer.


A.  

Only1

B.  

Only 2

C.  

Only 3

5:

What will be the output of the given Apple Swift code?

let a;zUlnt8 = 0b11101111

let sz;lnt8 = 0b00110011

let c=a&b

print(c)


A.  

21

B.  

25

C.  

35

D.  

45

6: In Apple Swift, which of the following statements is incorrect regarding a Switch statement?

A.   A Switch statement does not allow empty cases.

B.   A Break statement can never be used inside a Switch statement.

C.   If only a comment is contained in a Switch case, then it is reported as a compile-time error.

7: In Apple Swift, which of the following statements is incorrect regarding inheritance?

A.   Property observers can be added to inherited properties of a class.

B.   All the classes in Apple Swift are inherited from a universal base class.

C.   Multiple inheritance for classes cannot be supported by Apple Swift.

8: In Apple Swift, which of the following is the correct "identical to" operator?

A.   =

B.   I=

C.   ===

D.   =

9:

What will be the output of the following Apple Swift code?

func abc(param1: String) -> lnt[

print(param1)

return param1.characters.count

}

func xyz(param1: String) [

abc(paramI)

}

xyz.("John Smith")


A.  

10

B.  

John Smith

C.  

John Smith10

D.  

10John Smith

10:

What will be the output of the following code?

func abc(p: Double...) -> Double {

var sm: Double = O

for number in p[

sm += number

I

return sm I Double{p.count)

I

print(abc(6, 7.25, 16.75))


A.  

9.0

B.  

9.7 

C.  

10.0

D.  

Code will generate compilation/runtime error.

11:

How many times will the following Apple Swift code print the word "green"?

for p in 21...34[

if p%2==1[

var a = "red"

var b: String?

var c = b ?? a

print( c)

}

else if p%3==2{

let a = "green"

let e = "red"

var f = e ?? a

print(f)

}

else(

var g = "green"

var h: String?

var k = h ?? g

print(k)

}

}


A.  

2

B.  

4

C.  

5

D.  

6

12:

Choose True or False.

In Apple Swift, structures are reference types, whereas classes are value types.


A.  

True 

B.  

False 

13:

What will be the output of the following code?

class 81 (

func cn10

l

for var a = 9; a >= 1; --a[

for var b = 9; b >= 0; --b[

if b%2==a/2 [

print((b%4).terminator: "")

I

I

print("")

III

let s = a10

s.cn10


A.  

311201

11201

20120 


B.  

1111

1111

00000


C.  

13131

13131

02020  


14: In Apple Swift, which type of parameters cannot be used by subscripts?

A.   Variable parameters

B.   In-out parameters

C.   Variadic parameters

15:

Which of the following statements are valid regarding an assignment operator in Apple Swift?

1. let b=10

2. let (a,b) = (2, 3)

3. if a = b { )


A.  

Only1 and 2

B.  

Only 2 and 3

C.  

Only1 and 3

D.  

All the three statements are valid

16:

Analyze the Apple Swift code given in Image 1, and choose its correct output from Image

A.   (i)

B.   (ii)

C.   (iii)

17:

What will be the output of the last line of the given Apple Swift code?

let a=4

print(a&+0)

print(a&-O)

print(a&0)

print(a<<1)

print(a>>1)

print(a<<2)


A.  

4

B.  

8

C.  

16

D.  

0

18:

Analyze the following Apple Swift code and select its correct output from the given options.

struct a1 [

var x = 0.0, y = 0.0

}

struct b1 {

var w = 0.0. h = 0.0

}

struct Rt{

var n1 = a1()

var 51 = b1()

var c1: a1 {

getl

let cX = n1.x + (sI.w / 2)

let CY = n1.y -|- (s1.h / 2)

return a1(x: cX. y: cY)

}

set(cc) [

n1.x = cc.x - (s1.w ‘36 4)+2.5

n1.y = my - (sl.h % 4)+2.5

I I I

var rr = Rt(n1: a1(x: 0.0, y: 0.0),

$1: b1(w: 10.0, h: 10.0))

let isc = rr.c1

rr.c1 = a1(x: 16.0, y: 16.0)

print("\(rr.n1.x), \(rr.n1.y)")


A.  

10,0,10.0 

B.  

12.5. 12.5

C.  

16.5.16.5

D.  

18.5.18.5

19:

Based on your analysis of the following Apple Swift code, identify from the given options the code that will generate compilation error when executed.

func abc(inout a: T, inout _ b: T) {

let a1 = a

a = b

b = a1

print("\(a) \(b)")

}


A.  

var z =

B.  

var p = 5.0

var q = 22/50

abc(&p, &q)


C.  

var r = 2.15

var s =19.0/31

abc(&r. 8:5)


D.  

Both options a and b

E.  

Both options a and c

20: In Apple Swift, if we dont want to provide an external name for the second parameter of a method, then which of the following symbols can be used to override the default behavior?

A.   #: (Hash)

B.   _ (Underscore)

C.   & (Ampersand)

D.   $ (Dollar)

21:

What will be the output of the following Apple Swift code?

enum Abc [

case Mark

case John

case Andrew

case Lucy

case June

case Kate

]

enum Class [

case First. Second. Third. Fourth. Fifth. Sixth. Seventh

1

var n1 = Abc.Lucy

n1 = .John

print(n1)

let sp = Class.Sixth

sp = .Third

switch sp {

case .Third:

print("Perrnitted to the cricket ground")

default:

print("Not Permitted to the cricket ground")

]


A.  

John

Not Permitted to the cricket ground


B.  

(Enum Value)

Not Permitted to the cricket ground 


C.  

John

Permitted to the cricket ground


D.  

Code will give compilation error.  

22: ln Apple Swift, which of the following references should be used when it is known that the reference will never be nil during its lifetime?

A.   Weak references

B.   Unowned references

C.   None of the above references can be used

23: In Apple Swift, which of the following symbols is used to write an implicitly unwrapped optional?

A.   $

B.   ?

C.   !

D.   @

24: In Apple Swift, a class:

A.   must have at least one designated initializer.

B.   must have at least two designated initializers.

C.   must have at least three designated initializers.

D.   cannot have a designated initializer.

25:

What will be the output of the given Apple Swift code?

var nm = 5.0

class abc[

var x = 0.0

var y: String [

return "\(nm)"

]

func kp0[

println("\(n m)")

1}

let s=abc()

println(s.kp0)

class sd1: abc[

var hB = false

func k1(){

let i = abc();

println("\(i.y)")

let be = sd1()

bc.hB = true

println("\(bc.y)")

println(i.kp())

]}

class Tn: abc[

var nm=15.0

override func kp0[

println(15.0)

]}

let tr = Tn0

tr.kp0


A.  

5.0

5.0 


B.  

15.0

15.0 


C.  

15.0

5.0


D.  

5.0

15.0


26:

What will be the output of the following code?

class a1 {

func cn10

{

for var a = 9; a >=1; --a {

for var b = 9; b >= 0; --b{

if b%2==a/2 [

print((b%4),terminator: "“)

}

}

print("")

}}}

lets = a1()

s.cn1()

A.  

1 1 2 01

1 1 2 O 1

2 01 2 0


B.  

1 11 11

1 1 1 1 1          

O 0 0 0 0  


C.  

13131

13131

02020


27: In Swift, which of the following collection types is/are implemented as (a) generic collection(s)? 0 Array

A.   Dictionary

B.   Lists

C.   Both a and b

D.   Both b and c

E.   Both a and c

28:

Based on your analysis of the above code. find out the output generated by the following lines.

let b1 = abc([9,-7,3,110,4,721)

print("number: \(b1.x), \(b1.y)")


A.  

number: 27, 216

B.  

number: 216, 27 

C.  

number: -21, 330  

D.  

number: 330, -21

29: In Apple Swift, subscripts can take:

A.   only one parameter.

B.   only two parameters.

C.   only three parameters.

D.   any number of parameters.

30: Which of the following access levels is NOT provided by Apple Swift?

A.   Public access

B.   Private access

C.   Protected access

D.   Internal access

31: In Apple Swift, if an optional variable is defined without providing an initial value, then that particular variable is automatically set to which of the following?

A.   zero(0)

B.   nil

C.   NULL

32: In Apple Swift, which of the following statements are correct about extensions?

A.   They can def‌ine type methods.

B.   They can override an existing functionality.

C.   They can define new nested types, but not subscripts.

D.   They can add computed properties.

33: Which of the following statements are incorrect regarding a deinitializer in Apple Swift?

A.   A deinitializer of a class is written with a 'deinit' keyword.

B.   There can be at most two deinitializers per class.

C.   A deinitializer always takes one parameter.

D.   Superclass deinitializers are always called.

34: In Apple Swift, which of the following statements are incorrect regarding structures?

A.   Initializers can be def‌ined by structures to set up their starting state.

B.   Methods can be def‌ined by structures to provide functionality.

C.   Type casting enables us to verify and interpret the type of a structure instance at runtime.

D.   Reference counting permits more than one reference to a structure instance.

35: In Apple Swift, which of the following variables and constants are computed lazily?

A.   Global constants

B.   Local constants

C.   Global variables

D.   Local variables

36: ln Apple Swift. which of the following operators are right-associative?

A.   ?:

B.   &&

C.   &’

D.   ??

37: Which of the following statements are correct about properties in the Apple Swift language?

A.   In the Apple Swift language. information about a property is def‌ined in multiple locations.

B.   An Apple Swift property does not have a corresponding instance variable.

C.   Computed properties can be def‌ined by enumerations in the Apple Swift language.

D.   None of the above.

38: In Apple Swift, which of the following statements are correct regarding protocols?

A.   They can be used as a return type in a function.

B.   They cannot support multiple inheritance.

C.   They cannot be used as the type of a property.

D.   They can be used as the type of items in a dictionary.

39: In Apple Swift, which of the following options hold true?

A.   8% 2 returns 0

B.   16 % 5 returns 1.0

C.   23 % 7.5 returns 0.5

D.   -9 96 4 returns 1

40: In Apple Swift, which of the following operators cannot be overloaded?

A.   Assignment operator (=)

B.   Ternary conditional operator (?:)

C.   Right bit shift and assign (>>=)

D.   Add and assign (+=)

41:

What is the name of the Xcode generated header file used to import Swift classes into an Objective-C Class given a product module named Example?

A.   ExampleSwift.h

B.   Example.Swift.h

C.   Example+Swift.h

D.   Example-Swift.h

42:

What does a retainCount represent in ARC?

A.   The current number of strong references to an object.

B.   The current number of instances of an object.

C.   The total number of objects currently being retained in memory.

D.   The total number of times an object has been allocated.

43:

Which one of the below functions definitions is wrong considering Swift language?

A.   func haveChar(#string: String, character: Character) -> (Bool)

B.   func mean(numbers: Double...) -> Double

C.   func minMax(array: [Int]) -> (min: Int, max: Int)?

D.   func minMax(array: [Int]) -> (min: Int?, max: Int?)

44:

Which of these is a valid syntax for iterating through the keys and values of a dictionary?
let dictionary = [keyOne : valueOne, keyTwo : valueTwo]

A.   for (key, value) in dictionary { println("Key: (key) Value: (value)") }

B.   for (key, value) in enumerate(dictionary) { println("Key: (key) Value: (value)") }

C.   for (key, value) in (dictionary.keys, dictionary.values) { println("Key: (key) Value: (value)") }

D.   for (key, value) in dictionary.enumerate() { println("Key: (key) Value: (value)") }

45:

What is the name of the Swift language feature that Objective-C Blocks are translated into?

A.   Lambda

B.   Callback

C.   Closure

D.   Selector

46:

Which one creates a dictionary with a key type of Integer and value of String?

A.   var dict:[Int: String] = ["one":1]

B.   var dict: [Int: String] = [1:"one"]

C.   var dict: [String: Int] = [1:"one"]

D.   var dict = ["one":1]

47:

Which of these is an appropriate syntax for dispatching a heavy operation to a background thread?

A.   dispatch_async(DISPATCH_QUEUE_PRIORITY_BACKGROUND), { self.heavyOperation() })

B.   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIO RITY_BACKGROUND, 0), { self.heavyOperation() })

C.   DISPATCH_QUEUE_PRIORITY_BACKGROUND({ self.heavyOperation() })

D.   dispatch_async({ self.heavyOperation() })

48:

What is the name of the deinitializer in a Class declaration?

A.   deinit

B.   dealloc

C.   release

49:

Which of these is an appropriate syntax for declaring a function that takes an argument of a generic type?

A.   func genericFunction(argument: T<Generic>) { }

B.   func genericFunction<T>(argument) { }

C.   generic func genericFunction(argument: T) { }

D.   func genericFunction<T>(argument: T) { }

50:

Which of these is not a valid property declaration in Swift?

A.   final let x = 0

B.   final lazy let x = 0

C.   final lazy var x = 0

D.   final var x = 0