Swift MCQ Question

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 34

Swift MCQ Question

1. Choose all correct statements.

 Swift 1.0 was released in Oct 2014

 Swift 1.0 was released in June 2014 in WWDC event

 Swift 2.0 was released in Oct 2014

 Swift 2.0 was released in June 2015 in WWDC event

2. Choose all correct statements.

 Swift was developed by Apple and IBM together as open-source


project

 Swift is available on MacOS and Linux

 Swift was first available with Xcode 6.0

 Swift.org is dedicated website to download swift compiler for


MacOS.

3. a. To submit your App to the App Store you must build your app using
the version of Swift that comes included within Xcode.
b. Playgrounds are supported for downloadable Swift packages
separately on Ubuntu platform.

 Both statements are right.

 Only a is right.

 Only b is right.

 Both are wrong.


4. Choose all correct statements.

 Swift's inter-operability feature allows code written in C and


Objective-C to be used in Swift code and vice versa smoothly.

 Swift's mix and match feature allows C and Objective-C source


files to be used in Swift project and vice versa.

 Using inter-operability feature C++ code could be directly used in


Swift code.

 Using migration tool swift file could be converted to Objective-C


and vice versa.

5. Choose all correct statements.

 Swift code file does not need separate files for declaring interface
and implementation of class.

 The file extension for swift code is .swt

 Objective-C code can't be written in a swift class

 Existing C-library can't be used in swift directly but using migration


tool we can convert it to swift code and use.

6. Choose all correct statements.

 Swift is a general purpose programming language.

 Swift is a scripting language.

 We can create a complete Operating System using Swift.

 Swift can't be used to develop application's other than iOS.


7. Choose all correct statements.

 Swift doesn't support modern feature like lambda/block as


available in other programming language.

 Swift has advance error handling model which provides feature to


create our own custom error types.

 Swift has automatic memory handling mechanism using ARC.

 Swift code is compiled in C code in first pass and re-compiled


again to native code.

 Swift code is directly compiled into native (machine) code without


any intermediate code compilation into C.

8. Choose all correct statements.

 Swift supports playground which is an innovative feature that


allows programmers to experiment with Swift code and see the results
immediately, without the overhead of building and running an app.

 Playground is a feature of Xcode but not Swift.

 Swift supports REPL.

 Playground doesn't support Objective-C code separately.

 Playground can run code written in mixed languages (Swift and


Objective-C) both in one program.

9. Choose all correct statements.

 Swift REPL only runs on command line (terminal)

 Swift REPL can run on LLDB console in Xcode.


 Swift code can separately be compiled on command line
using swiftc command

 Swift code can't take command line argument like other


programming languages.

10. True or false statements

a. Playground code must be executed to see output on debugger


console.

 True   False

b. Playground code's output could be seen without executing the


code, on result sidebar (usually stays on right side pannel of
Xcode editor).

 True   False

c. Playground files are saved with an extension of playground (e.g


helloworld.playground).

 True   False

d. Playground files can be shared separately.

 True   False

e. Playground file (.playground) is actually a workspace file and it


has Contents.swift file hidden inside the package.

 True   False

f. Other swift source codes and resources such as images could be


added to a playground file.

 True   False
g. XCPlayground framework is required to add UIView and
UIViewController in Playground code.

 True   False

h. Playground pages are similar to individual playground file but it


is added to another playground file (parent).

 True   False

i. Adding the first page to a playground will show two new pages.
One is the current content of the playground editor and the other
is the new page.

 True   False

j. Playground pages have shared as well as separate resources.

 True   False

Skill Test Set 2: Variables, Constants and Data Types

1. Choose the correct statements:

 let and var keywords are used for immutable and mutable data


storage respectively.

 The value of a constant doesn’t need to be known at compile time,


but you must assign it a value exactly once.

 Values stored in one type can be changed automatically (implicitly)


to other type.

 String(width) and \(width) are same and it means width is


converted into String type.
2. let name = "CodingBull"
name = "www.codingbull.com"
 It's compile time error

 It's runtime error

 It's not an error

 Syntax is wrong as semicolons are missing in both the lines.

3. var name : String? = "CodingBull"
name = name + 10

 It's compile time error

 It's runtime error

 It's not an error

 Syntax is wrong as semicolons are missing in both the lines.

4. a. Double represents a 64-bit floating-point number while Float


represents 32-bit floating-point number.
b. Double has a precision of at least 15 decimal digits while Float has
upto 6 decimal digits.

 Both statements are right.

 Only a is right.

 Only b is right.

 Both are wrong.


5. Which syntax is correct among the below given options, to create a
dictionary with a key of type Integer and value of String?

 var dict: [Int: String] = ["one": 1 , "two": 2]

 var dict = [Int, String] = [1, "one", 2, "two"]

 var dict = [Int: String] = [1: "one", 2: "two"]

 var dict = {Int: String} = {1: "one", 2: "two"}

6. Which of the following statement correctly declares a mutable array?

 let array = [Int]()

 var array = [Int]()

 let array = [Int]

 var array = [Int]

7. What is the type of String, Array and Dictionary?  Class

Structure

Enumeration

Variables

8. Which of these is not a valid property declaration?   final let x = 0

 final lazy let x = 0


 final lazy var x = 0

 final var x = 0

9. Choose the correct statements:

 Short cut literal for creating empty Array and Dictionary both, is
same and it is [ ].

 Optional data-type either has some value or is nil to indicate value


is missing.

 ?? [Double question mark] operator is used to provide default


value to an optional value.

 Forced unwrapping of the optional's value is done by adding an


exclamation mark (!) with optional's name and it means that optional
data-type has some value stored.

10. What is the size of Int?

It is 32 bits.

It is 64 bits.

It is platform dependent and could be 32bits or 64bits.

Int is not a valid keyword but it should be either Int8, Int16, Int32
or Int64.

11. Which Swift Type is used to group multiple values into a single
compound value? For example:

let compoundValue = (3, 5)
 GroupObject

 Tuple

 Ordered

 Struct

12. Which of the following statements could be used to determine is


a given variable is of String type?

 if unknownVariable is String {}

 if unknownVariable: String {}

 if unknownVariable = String {}

 if unknownVariable <> String {}

 if String.hierarchy(unknownVariable) { }

 if (String)unknownVariable { }

13. What would be used for safe casting and to return nil if failed?

 as?

 as!

 !as?

 !as!
14. Which one is the correct keyword for defining a constant in

Swift? ( or To declare a constant in Swift you would use: )   const

 contant

 final

 let

 def

Skill Test Set 3: Basic operators, Branch, Repetitions

1. What would be the output of following code snippet in Swift?

var a = 12
if a >= 12
print("Good after noon")
else
print("Good Morning!")

 Good after noon

 Good Morning!

 Compiler error

 12

2. What would be the output of following code snippet in Swift?

var fullUrl = 1
if (fullUrl) {
print("http://www.codingbull.com")
} else {
print("codingbull.com")
}

 http://www.codingbull.com

 codingbull.com

 Compiler error

 Runtime crash

3. What set of keywords is most commonly used to iterate over a


collections of items?

 for each

 switch case

 repeat while

 for in

4. Which is not a control transfer statement is Swfit?

 goto

 fallthrough

 break

 return

 continue
5. Which keyword in the context of a Switch statement is required to
force the execution of a subsequent case?

 fallthrough

 continue

 break

 return

6. Which of the following could be used to loop through the range of


numbers 5-12 including 12?

 for i in 512

 for i in 5,i>12,i++

 for i in 5..<12

 for i in 4..<12

7. To loop through a range of the numbers 1-9 without using 9, you


would write…

 for i in 0…8

 for i in 0>..9

 for i in 1<..8

 for i in 1..<9

8. What would be the output of following swift code snippet?


var i = 10
repeat {
print(, separator: " ", terminator: ",")
i -= 1
} while i > 0

 10,9,8,7,6,5,4,3,2,1,

 10,9,8,7,6,5,4,3,2,1

 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,

 10, 9, 8, 7, 6, 5, 4, 3, 2, 1

9. Which of these operators is used to check whether or not two


instances are identical?

 ==

 =

 equalsTo

 ===

 identicalTo

10. Which keyword in the context of Swift statement is required to


force the execution of a subsequent case?

 Fallthrough

 Continue
 Release

 DropEnd

11. Which of these is a valid syntax for iterating through the keys
and values of a dictionary:

let dictionary = [keyOne : valueOne, keyTwo : valueTwo]

 for (key, value) in dictionary { println("Key: \(key) Value: \


(value)") }

 for (key, value) in enumerate(dictionary) { println("Key: \(key)


Value: \(value)") }

 for (key, value) in (dictionary.keys, dictionary.values)


{ println("Key: \(key) Value: \(value)") }

 for (key, value) in dictionary.enumerate() { println("Key: \(key)


Value: \(value)") }

12. What is the output of this segment of code?

var x = 0
for index in 1...5 {
++x
}
println("\(x)")

 0

 compile error
 5

 4

13. How can we use optional binding to determine if the variable


string is not nil?

 < if let str = string {…} >

 < if string {…} >

 < if string as String {…} >

 < if let string {…} >

Skill Test Set 4: Functions

1. What keyword is used before function name to declare a function in


Swift?

 function

 func

 @function

 No keyword is used.

2. a. func printName(name: String!) -> Void{ }
b. function printName(name: String) { }

Which one of the above two statements is correct?

 a is incorrect.

 b is incorrect.
 Both a & b are incorrect.

 Both a & b are correct.

3. a. If no return type is mentioned then function returns special value of


Type Void.
b. Void is an empty tuple means a tuple with zero element.

 Both statements are right.

 Only a is right.

 Only b is right.

 Both are wrong.

4. A function that has a defined return type must always return a value
because :

 the statement where function is called will expect a value to be


returned, so in case if value is not returned, it could lead to crash.

 it won't allow control to fall out of the bottom of the function
without returning a value, and attempting to do so will result in a
compile-time error.

 it's a convention to do so otherwise it's fine.

 it will cause a runtime error.

5. Swift function can return any number of values

 True, because it can return a tuple.

 False, because even a tuple is a single compound value which


could have multiple values inside.
6. The function parameters are constant by default.

 True

 False.

7. func add(value1: Int, value2: Int) -> Int {


value1 = value1 + value2
return value1
}
add(10, value2: 20)

What will the output?

 10

 30

 Compile time error

 Runtime crash

8. Which one of the below function 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?) { }


9. When declaring a function, what symbol is used to indicate that an
internal parameter name should also be used as an external
parameter?

  _   [underscore]

 @  [at]  

  :    [colon]

 #   [hash]

10. Which of following statements about functions is wrong?

 In-out parameters might have a default value

 Function might have multiple return values

 Function might not have return values

 Function names might be the same with another but at least one
parameter should be different

Skill Test Set 5: Class, Structures and OOPs Concept

1. Which keyword do you use to define a class?

 stuct

 class

 Class

 interface

 @class
2. Which root class, swift classes required to inherit from ?

 Not required

 NSObject

 NSRootObject

 Object

3. What is the name of the deinitilizer in a class declaration

 dealloc

 release

 finalize

 deinit

4. Which keyword is used in Swift when we want a property of a class to


initialize when it is accessed for the first time?

 let

 var

 const

 lazy

5. Which keyword do you use to define a structure?


 struct

 Struct

 structure

 @struct

6. Which keyword is used on a function inside an struct to indicate that


the function will modify self?

 modifier

 mutating

 mutable

 mod

 mut

7. Choose the right statements about Class and Structure.

 Class can adopt protocols but Structure can't.

 Structure can't inherit from other Structure but Class can.

 Class and Structure both can set-up initialiser methods and


initialise values.

 Class and Structure both can set-up de-initialiser methods and


clean up memory for it's resources.
8. struct SomeStruct {
var a: Int
}

Select all right code-snippets which could be used to use the above
defined structure (SomeStruct).

 var s1 = someStruct(10)

 var s1 = someStruct(a: 10)

 var s1: someStruct = someStruct(10)

 var s1 = someStruct()
s2.a = 10

9. What type of object are Swift Structures?

Reference Type

Memory Type

Abstract Type

Value Type

10. Few question are based on the class definition written below:

class Square: NamedShape {
var sideLength: Double

func area() -> Double {
return sideLength*sideLength
}
}
a. The name of the class's only method is __.

 NamedShape

 Square

 area

 Double

 sideLength

b. What type of return does the function ‘area’ give?

 Int

 The area of a square

 Double

 area

c. In the below text, what is the property name?

 NamedShape

 Square

 Double

 sideLength
d. In the bellow text, what is the super class name?

 Square

 NamedShape

 Double

 area

11. How could we create a subclass of the Structure, CGRect?

 struct MyRect: CGRect {}

 struct CGRect(MyRect) {}

 You can not subclass a Structure

 struct MyRect extends CGRect {}

12. What will be the final value of "a.data" and "b.data" after
following codes are executed?

struct A { var data: Int = 2 } var a = A() var b = a var c = b c.data = 10 a.data = 5

 a.data = 10 and b.data = 2

 a.data = 2 and b.data = 5

 a.data = 5 and b.data = 5

 a.data = 5 and b.data = 2


13. Can Structures be type cast in Swift?

 Yes

 No

 Only those deriving from NSObject

 Only when they conform to protocol TypeCast

Skill Test Set 6: Closures, Enum, Generics

1. Which keyword do you use to declare enumeration?

 enum

 enumeration

 Enum

 NSEnum

2. When declaring an enumeration, multiple member values can appear


on a single line, separated by which punctuation mark?

 ; [Semi-colon]

 : [Colon]

 , [Comma]

 ? [Question mark]

3. Choose the correct syntax for generic function :


 func genericFunc(argument: T) { }

 func genericFunc(argument) { }

 generic func genericFunc(argument: T) { }

 func genericFunc(argument: T) { }

4. Which of these is a valid definition of a generic function that


incorporates inout parameters in Swift?

 func swap(inout a: T, inout b: T) { let temp = a a = b b = temp }

 func swap(inout a: U, inout b: T) { let temp = a a = b b = temp }

 func swap( a: U, b: T) { let temp = a a = b b = temp }

 func swap( a: T, b: T) { let temp = a a = b b = temp }

5. Which of the followings could be used to indicate the Function Type of


the following function:

func joinStrings(stringOne: String, stringTwo: String) -> String {


return stringOne + stringTwo
}

 func(String, String -> String)

 (String, String) -> String

 {String, String} -> String

 {String, String}(String)
6. How do closures capture references to variables by default?

 By weak reference.

 By strong reference.

 By unowned reference.

 By copy.

7. Which keyword is used on a function inside an Enumeration to indicate


that the function will modify self?

 modifier

 mutating

 mutable

 mod

 mut

8. Swift extensions are similar to categories in Objective-C except :

 Swift extension might have a specific name.

 Swift extension does not add functionality to previously defined


types.

 Swift can override methods from original type.

 Swift extensions are not named.


9. If we have a class named MyClass with a nested enum called Status,
declared like so:

class MyClass {
enum Status {
case On, Off
}
}

How would one indicate that a variable is an enum of type Status


outside the context of MyClass?

 var status: MyClass.Status = .On

 var status: Status = .On

 var status: MyClass = .On

 var status: MyClass(Status) = .On

10. Which one of the following types can be used as raw value types
for an enumeration?

 Bool

 Array

 Int, String, Float

 Dictionary

11. Swift extensions are similar to categories in Objective-C except :

 Swift extension might have a specific name.

 Swift extension does not add functionality to previously defined


types.

 Swift can override methods from original type.

 Swift extensions are not named.

12. What is a trailing closure?

A closure expression that is called directly after another closure


expression.

A closure expression that is written outside of (and after) the


parentheses of the function call is supports.

A closure expression that is declared within the scope of another


closure expression.

A closure expression that is declared as the property of an object.

13. Choose the answer that declares an optional closure.

 < var closureName: (parameterTypes) -> (returnType) >

 < typealias closureType: (parameterTypes) -> (returnType) >

 < var closureName: ((parameterTypes) -> (returnType)) ?>

 < let closureName: (closureTypes) = { ... } >

14. How could the following closure be rewritten to use shorthand


arguments? s2})>

 < reversed = sorted(names, { $0 ,$1 in $0 > $1 } ) >

 < reversed = sorted(names, { $0 > $1 } ) >


 < reversed = sorted(names, { $0 ,$1 } ) >

 < reversed = sorted( { $0 > $1 } ) >

15. Which of the following statements is true regarding Swift


closures and functions?

 Functions and Closures are not related.

 A Function is a Closure declared within the scope of a Class.

 A Function is a named Closure.

 Closures can’t be used as arguments, Functions can.

16. What symbol is used like a tuple to access arguments in


Abbreviated Swift Closure syntax?

 $

 *

 &

 @

 ~

17. How could you call the following function that takes a closure as
an argument using trailing closure syntax:
()) { // function body goes here }>

 < funcWithClosure ({
//closure’s body goes here
})>
 <func funcWithClosure ({
//closure’s body goes here
})>

 < funcWithClosure() {
//closure’s body goes here
}>

 < funcWithClosure {
//closure’s body goes here
)>

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

 Lambda

 Callback

 Closure

 Blocks

19. Which is correct regarding Swift enumeration members when


they are defined?

 Members are assigned a default integer value.

 Members are assigned a random default integer value.

 Members are not assigned default integer values.

20. How can we use optional binding to determine if the variable


string is not nil?

 < if let str = string {…} >


 < if string {…} >

 < if string as String {…} >

 < if let string {…} >

21. Let’s assume “numbers” is an array of unsorted integers. Which


of these could be used to sort numbers?

 numbers.sort({$0, $1 in $0 > $1})

 numbers.sort({s1 > s2})

 numbers.sort({$0 > $1})

 numbers.sort(){s1 > s2}

22. Which of these could be an appropriate protocol declaration in


Swift?

 @objc protocol someProtocal { optional var first: Int { get } }

 @objc protocol someProtocal { optional var first: Int { set } }

 protocol someProtocal { optional var first: Int { get } }

 protocol someProtocal { var first: Int { set } }

Skill Test Set 7: Extensions, Protocols

1. Which keyword do you use to define a protocol?

 protocol

 @interface
 @protocol

 Protocol

2. Which is correct for Enumerations?

 Enumerations can define initializers.

 Enumerations cannot conform to protocols.

 Enumerations cannot conform to protocols.

3. What is the type of Swift Enumerations?

 Reference type

 Class type

 Collection type

 Value type

4. What keyword is used to indicate a custom operator that will appear in


between two targets, similar to the addition operator in this example?
var sum = 10 + 10

 @inter

 between

 infix

 @center
5. To which of these type does ARC apply?

 Class

 Structure

 Enumeration

 Basic types (String, Int, Bool)

6. Why are IBOutlets declared with a weak attribute by default?

 IBOutlets are not declared with a weak attribute by default

 To save memory

 To increase loading speed

 They are already retained by the view

7. What does the retainCount specify in ARC?

 The current number of strong references to an object.

 The current number of instances of an object.

 The total number of objects currently being retained in memory.

 The total number of times an object has been allocated.

8. How do closures capture references to variables by default?

 By weak reference.

 By strong reference.


 By unowned reference.

 By copy.

9. Which keyword is used on a function inside an Enumeration to indicate


that the function will modify self?

 modifier

 mutating

 mutable

 mod

 mut

10. Which of the following statements about Extensions is false?

 Extensions can add new functionality to a type.

 Keyword for Extensions is \"extension\".

 Extension can override existing functionality.

 Extensions can make an existing type confirm to a protocol.

You might also like