wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Pathway

Total questions: 45

Worksheet time: 1hrs 8mins

Name
Class
Date
1.

Which keyword is used to declare a constant in Swift?

a)

let

b)

var

c)

const

2.

In Swift, can the value of a constant be changed after it has been declared?

a)

True

b)

False

3.

Which Swift data type is used to represent a sequence of characters?

a)

String

b)

Character

c)

Array

d)

Set

4.

Which property can be used to get the number of characters in a string in Swift?

a)

count

b)

length

c)

size

d)

sizeOf

5.

How can we write a single-line comment in Swift?

a)

<!-- This is a comment -->

b)

/* This is a comment */

c)

// This is a comment

d)

# This is a comment

6.

What is readLine() used for in Swift?

a)

To read a text file

b)

To read user input from the command line

c)

To read data from a URL

d)

To read an array

7.

In Swift, what does the ! symbol mean when placed after a variable?

a)

It declares a new Optional variable

b)

It increments the value of a variable

c)

It implicitly unwraps an Optional variable

d)

It indicates a factorial operation

8.

How do you create an array of integers in Swift?

a)

var array = [Integer]()

b)

var array: [int] = []

c)

var array: [Int] = []

d)

var array = []

9.

What is the difference between information and data?

a)

Data is raw facts, while information is processed and meaningful data

b)

Information is processed data, while data is raw facts

c)

Information is more valuable than data

d)

There is no difference

10.

How do you declare a constant in Swift?

a)

let name = "John"

b)

const name = "John"

c)

var name = "John"

d)

constant name = "John"

11.

What is the difference between a variable and a constant?

a)

Constants can change their value, variables cannot change their value

b)

Variables can change their value, constants cannot change their value

c)

Variables and constants can both change their values

d)

Variables and constants cannot change their values

12.

How is the ternary operator used in Swift?

a)

if condition: value1 else: value2

b)

condition then value1 else value2

c)

if condition then value1 else value2

d)

condition ? value1 : value2

13.

How do you declare a function in Swift?

a)

function name() {}

b)

func name() {}

c)

define name() {}

d)

declare name() {}

14.

What is an Action

a)

A variable that stores the state of a UI element.

b)

A connection between two UI elements.

c)

A function in Swift code that is triggered by a user interaction with a UI element.

d)

A property of a UI element.

15.

What is the difference between a function parameter and an argument label?

a)

Function parameters are used to pass data to a function, argument labels are used to name the parameters when calling the function

b)

Function parameters are used to name the parameters when calling the function, argument labels are used to pass data to a function

c)

Function parameters and argument labels are the same thing

d)

There is no difference

16.

Write Swift code to append the value 6 to the end of the numbers array.

a)

numbers.append(6)

b)

numbers += 6

c)

numbers.add(6)

d)

numbers.pop(6)

17.

What is a View Controller Scene Hierarchy?

a)

What is a View Controller Scene Hierarchy?

b)

The arrangement of Views within a View Controller.

c)

. A hierarchical representation of the View Controllers and their relationships within an application.

d)

The way in which data is passed between View Controllers.

18.

What is an Outlet?

a)

A function that responds to user interactions.

b)

A connection between a UI element in Interface Builder and a variable in Swift code.

c)

A property of a UI element.

d)

A method that modifies the state of a UI element.

19.

Which file is responsible for handling the app's initial launch and setup?

a)

ViewController

b)

SceneDelegate

c)

AppDelegate

d)

Main

20.

Which file contains the app's icons, images, and other?

a)

AppDelegate

b)

Info.plist

c)

ViewController

d)

Assets.xcassets

21.

Which keyboard shortcut should I use to open the correct view controller?

a)

(control+option+command) + return

b)

(control+option+command) + return right

c)

(control+option+command) + up

d)

(control+option+command) + down

22.

Which method is called when a UIViewController is fully removed from the screen?

a)

viewDidLoad

b)

viewWillAppear

c)

viewDidAppear

d)

viewDidDisappear

23.

What is the relationship between the ViewController and the Main.storyboard file?

a)

They are completely separate entities.

b)

The ViewController file contains the UI design for the Main.storyboard file.

c)

The Main.storyboard file defines the UI layout, and the ViewController class handles the logic behind that UI.

d)

The Main.storyboard file is a blueprint for the ViewController class.

24.

How can you set a custom font for a UILabel programmatically?

a)

myLabel.font = "Arial"

b)

myLabel.setFont(name: "Arial", size: 16)

c)

myLabel.font = UIFont(name: "Arial", size: 16)

d)

myLabel.font = UIFont.systemFont(ofSize: 16)

25.

How can you access the UINavigationController from within a UIViewController?

a)

self.navigationController

b)

view.navigationController

c)

superview.navigationController

d)

navigationController

26.

How can you programmatically change the background color of a UINavigationBar?

a)

navigationBar.backgroundColor = .red

b)

navigationBar.setBackgroundColor(.red)

c)

navigationBar.barTintColor = .red

d)

navigationBar.setBarTintColor(.red)

27.

Which method is used to create a UIAlertController in Swift?

a)

UIAlertController.create(title: "Alert", message: "Message")

b)

showAlert(title: "Alert", message: "Message")

c)

UIAlertController(title: "Alert", message: "Message", preferredStyle: .alert)

d)

UIViewController.showAlert(title: "Alert", message: "Message")

28.

How can you display an image from the Assets.xcassets file in a UIImageView?

a)

myImageView.image = UIImage(named: "myImage")

b)

myImageView.setImage(named: "myImage")

c)

myImageView.image = "myImage"

29.

Which area in the Xcode interface allows you to view and edit your project files?

a)

Toolbar area

b)

Debug area

c)

Editor area

d)

Inspector area

30.

Which area in Xcode allows you to view and modify the properties of a selected object, such as a UI element or a file?

a)

Toolbar area

b)

Debug area

c)

Editor area

d)

Inspector area

31.

What is the ouput of the following code?
let myArray = [1, 2, 3].enumerated().reversed()

print(myArray)

a)

[(2, 3), (1, 2), (0, 1)]

b)

[(0, 1), (1, 2), (2, 3)]

c)

[(2, 3), (0, 1), (1, 2)]

32.

What is the ouput of the following code?

func sumOfSquares(array: [Int]) -> Int {

var sum = 0

for i in 0..<array.count {

sum += array[i] * array[i]}

return sum}

print(sumOfSquares(array: [1, 2, 3]))

a)

6

b)

14

c)

Error

d)

[1,2,3]

33.

What is the ouput of the following code?

let myOptional: String? = nil

let myString = myOptional?.uppercased()

print(myString)

a)

Hello

b)

Error

c)

Nil

34.

What is the ouput of the following code?

func findMissingNumber(array: [Int]) -> Int {

let n = array.count + 1

let expectedSum = n * (n + 1) / 2

let actualSum = array.reduce(0, +)

return expectedSum - actualSum }

print(findMissingNumber(array: [1, 2, 4, 5]))

a)

15

b)

Error

c)

3

d)

[1,2,4,5]

35.

What is the output of the following code?

func fibonacci(n: Int) -> Int {

if n == 0 {

return 0

} else if n == 1 {

return 1

} else {

return fibonacci(n: n - 1) + fibonacci(n: n - 2) } }

print(fibonacci(n: 10))

a)

10

b)

Error

c)

55

d)

'10'

36.

What is the output of the following code?

func isPalindrome(str: String) -> Bool {

let reversedString = String(str.reversed())

return str == reversedString }

print(isPalindrome(str: "racecar"))

a)

true

b)

false

c)

error

d)

"racecar"

37.

What is the output of the following code?

let myArray = [1, 2, 3]

let myTuple = (myArray[0], myArray[1])

print(myTuple.1)

a)

1

b)

2

c)

error

d)

1 2

38.

What is the output of the following code?

func findMissingNumber(array: [Int]) -> Int {

let n = array.count + 1

let expectedSum = n * (n + 1) / 2

let actualSum = array.reduce(0, +)

return expectedSum - actualSum }

print(findMissingNumber(array: [1, 2, 4, 5]))

a)

1 2 3

b)

3

c)

15

d)

Error

39.

What will the following code print?

func makeIncrementer(incrementAmount: Int) -> () -> Int {

var total = 0

let incrementer: () -> Int = {

total += incrementAmount

return total }

return incrementer }

let incrementByTwo = makeIncrementer(incrementAmount: 2)

print(incrementByTwo())

print(incrementByTwo())

a)

2 4

b)

2 0

c)

4 6

d)

2 2

40.

What will the following code print?

func outer() {

func inner() {

print("Inner")}.

inner() }.

outer()

a)

inner

b)

outner

41.

What is the output of the following code?

let numbers = [1, 2, 3, 4, 5]

let firstThree = numbers.prefix(3)

print(firstThree)

a)

[1, 2]

b)

4, 5]

c)

[1, 2, 3]

42.

What is the output of the following code?

let numbers = [1, 2, 3, 4, 5]

var sum = 0

for number in numbers {

sum += number }

print(sum)

a)

10

b)

15

c)

20

d)

5

43.

What is the output of the following code?

let text = "Swift is fun"

let index = text.firstIndex(of: "f")

print(index)

a)

4

b)

5

c)

6

d)

7

44.

What is the output of the following code?

let myArray = [5, 1, 4, 2, 3]

let sortedArray = myArray.sorted()

print(sortedArray)

a)

[5, 1, 4, 2, 3]

b)

[3, 2, 4, 1, 5]

c)

[1, 2, 3, 4, 5]

d)

[5, 4, 3, 2, 1]

45.

Choose the command to print the string "Hello, John!" using string interpolation given let name = "John"

a)

print("Hello, \(name)!)

b)

print("Hello, " + name + "!")

c)

print("Hello, " + name)!

d)

print("Hello, " + name)