Answer these 300+ Swift MCQs and see how sharp is your knowledge of Swift.
Scroll down and let's start!
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 defined, 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
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.
A. Only willset observer is called.
B. Only didset observer is called.
C. Neither willset nor didset observer is called.
In Apple Swift, which of the given statements is correct about initializers?
1. Failable and non-failable initializers can be defined 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
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
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.
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.
A. =
B. I=
C. ===
D. =
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
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.
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
Choose True or False.
In Apple Swift, structures are reference types, whereas classes are value types.
A.
True
B.
False
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
A. Variable parameters
B. In-out parameters
C. Variadic parameters
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
A. (i)
B. (ii)
C. (iii)
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
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
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
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
A. #: (Hash)
B. _ (Underscore)
C. & (Ampersand)
D. $ (Dollar)
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.
A. Weak references
B. Unowned references
C. None of the above references can be used
A. $
B. ?
C. !
D. @
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.
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
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
A. Dictionary
B. Lists
C. Both a and b
D. Both b and c
E. Both a and c
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
A. only one parameter.
B. only two parameters.
C. only three parameters.
D. any number of parameters.
A. Public access
B. Private access
C. Protected access
D. Internal access
A. zero(0)
B. nil
C. NULL
A. They can define 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.
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.
A. Initializers can be defined by structures to set up their starting state.
B. Methods can be defined 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.
A. Global constants
B. Local constants
C. Global variables
D. Local variables
A. ?:
B. &&
C. &’
D. ??
A. In the Apple Swift language. information about a property is defined in multiple locations.
B. An Apple Swift property does not have a corresponding instance variable.
C. Computed properties can be defined by enumerations in the Apple Swift language.
D. None of the above.
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.
A. 8% 2 returns 0
B. 16 % 5 returns 1.0
C. 23 % 7.5 returns 0.5
D. -9 96 4 returns 1
A. Assignment operator (=)
B. Ternary conditional operator (?:)
C. Right bit shift and assign (>>=)
D. Add and assign (+=)
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
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.
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?)
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)") }
What is the name of the Swift language feature that Objective-C Blocks are translated into?
A. Lambda
B. Callback
C. Closure
D. Selector
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]
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() })
What is the name of the deinitializer in a Class declaration?
A. deinit
B. dealloc
C. release
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) { }
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