wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Exam03

Total questions: 45

Worksheet time: 2hrs 30mins

Name
Class
Date
1.
Given the following code: var name: String? = "Alice" var greeting: String if let unwrappedName = name { greeting = "Hello, \(unwrappedName)" } else { greeting = "Hello, Guest" } print(greeting) If name is changed to nil, what will be printed?
a)
"Hello, Alice"
b)
"Hello, Guest"
c)
A compilation error
d)
A runtime error
2.
let scores = ["Alice": 90, "Bob": 85, "Charlie": nil] let charlieScore = scores["Charlie"] ?? 0 print(charlieScore) What will be printed?
a)
nil
b)
Optional(0)
c)
A compilation error
3.
Which is the correct way to declare a closure that accepts an Int and returns a String?
a)
let myClosure = (Int) -> String { ... }
b)
let myClosure: (Int) -> String = { num in String(num) }
c)
let myClosure = { (num: Int) -> String in return String(num) }
d)
Both B and C are correct
4.
Given the code: struct Point { var x: Int var y: Int } var p1 = Point(x: 10, y: 20) var p2 = p1 p2.x = 30 print(p1.x) What will be printed?
a)
10
b)
30
c)
20
d)
A compilation error
5.
Which of the following is not true about structs in Swift?
a)
structs are value types.
b)
structs can have properties and methods.
c)
structs support inheritance from another class.
d)
structs can conform to protocols.
6.
class Vehicle { var speed: Double = 0.0 func accelerate() { speed += 10 } } class Car: Vehicle { var brand: String init(brand: String) { self.brand = brand super.init() } override func accelerate() { speed += 20 } } let myCar = Car(brand: "Toyota") myCar.accelerate() print(myCar.speed) What will be printed?
a)
10
b)
20
c)
A compilation error
7.
What happens if a subclass does not call super.init() in its designated initializer?
a)
It will automatically call super.init().
b)
A compilation error.
c)
The superclass's properties will not be initialized.
d)
The subclass's properties will not be initialized.
8.
When should you use the final keyword for a class in Swift?
a)
To create an abstract class.
b)
To ensure the class is a singleton.
c)
To allow the class to be used as a protocol.
d)
To prevent other classes from inheriting from it and to enable (minor) performance optimization.
9.
Which keyword is used to declare a constant in Swift?
a)
var
b)
let
c)
const
d)
static
10.
Which keyword is used to declare a variable in Swift?
a)
var
b)
let
c)
constant
d)
variable
11.
Which data type is used to represent a value that may or may not have a value in Swift?
a)
Int
b)
String
c)
Optional
d)
Bool
12.
To assign a value to an Optional variable and be sure it contains a value, which method would you use?
a)
Force Unwrapping (!)
b)
Optional Binding (if let)
c)
Guard Let (guard let)
d)
All of A, B and C
13.
Which operator is used to check the equality of two variables' values in Swift?
a)
=
b)
==
c)
===
d)
?
14.
Which operator is used to assign a value to a variable in Swift?
a)
=
b)
==
c)
===
d)
??
15.
To add 5 to variable x and assign the result back to x, which shorthand is correct?
a)
x = x + 5
b)
x += 5
c)
x.add(5)
d)
Both A and B
16.
What is the default value of an Optional Int variable if not initialized?
a)
==
b)
""
c)
===
d)
nil
17.
Which is the correct way to declare a String constant with the value "Hello Swift"?
a)
var message: String = "Hello Swift"
b)
let message: String = "Hello Swift"
c)
let message = "Hello Swift"
d)
Both B and C are correct
18.
To compare two strings for equality of value, which operator do you use?
a)
==
b)
!=
c)
1
d)
>
19.
What is the nil-coalescing operator (??) used for?
a)
Force unwrapping an Optional.
b)
Checking if an Optional is nil.
c)
Providing a default value if an Optional is nil.
d)
Assigning a nil value to a variable.
20.
What is the result of the expression 5 + 3 * 2 in Swift?
a)
16
b)
11
c)
10
d)
8
21.
What does the ! (logical NOT) operator do?
a)
Converts a number to its negative.
b)
Inverts a boolean value.
c)
Force unwraps an Optional.
d)
Assigns a nil value.
22.
To execute a block of code if a condition is true, which construct do you use?
a)
if statement
b)
for-in loop
c)
while loop
d)
switch statement
23.
When is an else if block used?
a)
When you want to execute a block of code if the if condition is false.
b)
It does not exist in Swift.
c)
To check another condition if the initial if condition was true.
d)
To check another condition if the initial if condition was false.
24.
Which is the correct way to declare a function with no parameters and no return value?
a)
func sayHello() -> Void
b)
func sayHello() {}
c)
func sayHello()
d)
All of A, B are correct
25.
What is the main difference between class and struct in Swift?
a)
class is a reference type, struct is a value type.
b)
class is a value type, struct is a reference type.
c)
class can have properties, struct cannot.
d)
class cannot inherit, struct can.
26.
To define an initializer for a class or struct, which keyword do you use?
a)
new
b)
constructor
c)
init
d)
setup
27.
Can a struct inherit from another class?
a)
Yes
b)
No
c)
Only if the class is final
d)
Depends on the Swift version
28.
Inside a for-in loop, which statement is used to skip the current iteration and move to the next one?
a)
continue
b)
break
c)
return
d)
exit
29.
Consider the following code: struct Dimensions { var width: Double var height: Double } var size1 = Dimensions(width: 10, height: 20) var size2 = size1 size2.width = 30 print(size1.width) What will be printed?
a)
10
b)
30
c)
20
d)
Compile-time error
30.
Which of the following cannot be done by a struct?
a)
Have properties.
b)
Have methods.
c)
Conform to a protocol.
d)
Inherit from a superclass.
31.
To display a piece of text on the screen, which UIKit component would you use?
a)
UITextField
b)
UILabel
c)
UITextView
d)
UIButton
32.
Deinitializers (using the deinit keyword) can be defined in which type?
a)
class
b)
struct
c)
enum
d)
protocol
33.
Consider the code: struct Counter { var count: Int = 0 mutating func increment() { count += 1 } } var c = Counter() c.increment() print(c.count)
a)
The code will still run normally.
b)
A compile-time error will occur because structs are value types and the count property needs to be modified.
c)
The value of count will not change.
d)
A runtime error will occur.
34.
Inside a for-in loop, which statement is used to exit the loop completely?
a)
break
b)
continue
c)
return
d)
exit
35.
Consider the following code class Box { var value: Int init(value: Int) { self.value = value } } var box1 = Box(value: 100) var box2 = box1 box2.value = 200 print(box1.value) If the mutating keyword is removed from the increment() function, what will happen?
a)
100
b)
200
c)
nil
d)
Compile-time error
36.
Which keyword is used to override a method or property of a superclass in a subclass?
a)
new
b)
replace
c)
override
d)
super
37.
Consider the following code: class Engine { var horsepower: Int init(hp: Int) { self.horsepower = hp } } struct Car { var make: String var engine: Engine } var myEngine = Engine(hp: 200) var car1 = Car(make: "Honda", engine: myEngine) var car2 = car1 car2.make = "Toyota" car2.engine.horsepower = 300 print("Car1 Make: \(car1.make), Car1 Engine HP: \(car1.engine.horsepower)")
a)
Car1 Make: Honda, Car1 Engine HP: 200
b)
Car1 Make: Toyota, Car1 Engine HP: 300
c)
Car1 Make: Honda, Car1 Engine HP: 300
d)
Car1 Make: Toyota, Car1 Engine HP: 200
38.
Consider the following code: var scores: [String: Int] = ["Alice": 100, "Bob": 90, "Charlie": 85] scores["Bob"] = 95 scores["David"] = 110 scores["Alice"] = nil // Remove Alice print(scores.count)
a)
3
b)
2
c)
5
d)
4
39.
When accessing a value in a Dictionary using a key that does not exist, what will happen?
a)
A runtime error will occur.
b)
It will return nil (because Dictionary subscript access returns an Optional).
c)
It will add that key to the Dictionary with a default value of 0.
d)
A compile-time error occurs.
40.
Which Dictionary method allows you to add or update a value for a specific key and returns the old value if one existed?
a)
myDict[key] = value
b)
myDict.add(key: value)
c)
myDict.updateValue(value, forKey: key)
d)
myDict.insert(value, forKey: key)
41.
To allow a user to enter text, which UIKit component would you use?
a)
UILabel
b)
UITextField
c)
UITextView
d)
UIButton
42.
Which of the following is the correct way to declare a Dictionary in Swift, mapping String to Int?
a)
var myDict: [String, Int]
b)
var myDict: Dictionary<String: Int>
c)
var myDict: [String: Int]
d)
var myDict = Dictionary<String, Int>()
43.
Which keyword is used to declare an IBAction in Swift, allowing you to connect a UI action to code?
a)
IBAction
b)
IBOutlet
c)
selector
d)
func
44.
Which UIViewController lifecycle method is called after the controller's view has been loaded into memory?
a)
init()
b)
viewWillDisappear()
c)
viewDidLoad()
d)
viewDidAppear()
45.
To navigate between different screens in an iOS application, which controller is commonly used?
a)
UINavigationController
b)
UITabBarController
c)
UISplitViewController
d)
UIViewController