Collections & Strings Questions with Answers

28 practice questions on Collections & Strings (Swift) with answers and step-by-step solutions. Try 10 samples below, then practise all 28 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

let d = ["a": 1]
print(d["z"] ?? 0)
  1. nil
  2. Crash
  3. 1
  4. 0

Answer: D. 0

Explanation

A missing dictionary key returns nil, so ?? 0 gives 0.

Step-by-step solution

  1. d["z"] is nil.
  2. ?? default.
  3. 0.

Why the other options are wrong

  • A. The default is applied.
  • B. Missing keys return nil, no crash.
  • C. Key z does not exist.

Question 2Easy

What is the output?

print([1, 2, 3, 4].dropFirst(2))
  1. [1, 2]
  2. [3, 4]
  3. [2, 3, 4]
  4. [4]

Answer: B. [3, 4]

Explanation

dropFirst(2) removes the first two elements.

Step-by-step solution

  1. Drop 1, 2.
  2. Remaining 3, 4.
  3. [3, 4].

Why the other options are wrong

  • A. dropFirst removes from the start.
  • C. Two elements are dropped.
  • D. Only two are dropped.

Question 3Easy

What is the output?

var s: Set = [1, 2]
s.insert(2)
print(s.count)
  1. 3
  2. 1
  3. 2
  4. Compile error

Answer: C. 2

Explanation

A Set holds unique values; inserting an existing element changes nothing.

Step-by-step solution

  1. Set {1, 2}.
  2. 2 already exists.
  3. Count 2.

Why the other options are wrong

  • A. Sets ignore duplicates.
  • B. 1 and 2 are both there.
  • D. Insert is valid.

Question 4Easy

What is the output?

let a = [1, 2, 3]
print(a.count)
  1. 2
  2. 4
  3. 3
  4. 6

Answer: C. 3

Explanation

count is the number of elements.

Step-by-step solution

  1. The array has the elements 1, 2 and 3.
  2. Count is the number of elements.
  3. Print outputs 3 (option C).

Why the other options are wrong

  • A. There are three elements, not two.
  • B. 4 would count one element too many.
  • D. 6 is not the element count.

Tip: Count gives the number of elements, not their sum.

Question 5Medium

What is the output?

print([3, 1, 2].sorted(by: >))
  1. [3, 2, 1]
  2. [1, 2, 3]
  3. [3, 1, 2]
  4. [2, 1, 3]

Answer: A. [3, 2, 1]

Explanation

Sorting with > as the comparison puts the larger elements first.

Step-by-step solution

  1. Compare with >.
  2. Largest first.
  3. [3, 2, 1].

Why the other options are wrong

  • B. > sorts descending.
  • C. sorted returns a new ordered array.
  • D. Not an arbitrary arrangement.

Question 6Medium

What is the output?

print("a-b-c".split(separator: "-"))
  1. ["a", "b", "c"]
  2. [a, b, c]
  3. a b c
  4. ["a-b-c"]

Answer: A. ["a", "b", "c"]

Explanation

split returns an array of substrings, which print with quotes.

Step-by-step solution

  1. Split at -.
  2. Three parts.
  3. ["a", "b", "c"].

Why the other options are wrong

  • B. print shows strings with quotes inside an array.
  • C. It is an array.
  • D. The split happens at each dash.

Question 7Medium

What is the output?

var d = ["a": 1]
d["b"] = 2
d["a"] = nil
print(d.count)
  1. 2
  2. 0
  3. Runtime crash
  4. 1

Answer: D. 1

Explanation

Assigning nil to a key removes it, so only "b" remains.

Step-by-step solution

  1. D starts with one key: a with value 1.
  2. d["b"] = 2 adds a second key, so there are 2 keys.
  3. d["a"] = nil removes key a, leaving only b.
  4. So d.count is 1 (option D).

Why the other options are wrong

  • A. The count is 2 only before the nil assignment removes a.
  • B. Key b is still in the dictionary, so the count is not 0.
  • C. Assigning nil to a dictionary key is valid and does not crash.

Tip: Assigning nil to a dictionary key removes that key.

Question 8Medium

What is the output?

print([1, 2, 3].contains(2), [1, 2, 3].firstIndex(of: 3) ?? -1)
  1. true 3
  2. true 2
  3. false 2
  4. true -1

Answer: B. true 2

Explanation

2 is present, and 3 sits at zero-based index 2.

Step-by-step solution

  1. contains(2) true.
  2. 3 at index 2.
  3. true 2.

Why the other options are wrong

  • A. Index counts from 0.
  • C. 2 is in the array.
  • D. 3 is found at index 2.

Question 9Hard

What is the output?

print("héllo".count, "héllo".utf8.count)
  1. 5 5
  2. 6 6
  3. 6 5
  4. 5 6

Answer: D. 5 6

Explanation

count counts Characters (5). In UTF-8 the é uses two bytes, giving 6.

Step-by-step solution

  1. Characters: h é l l o = 5.
  2. é is 2 UTF-8 bytes.
  3. Bytes: 6.

Why the other options are wrong

  • A. é takes two bytes in UTF-8.
  • B. count counts characters.
  • C. Reversed order.

Question 10Hard

What is the output?

let a = [1, 2, 3]
var b = a
b.append(4)
print(a.count, b.count)
  1. 4 4
  2. 3 4
  3. 3 3
  4. 4 3

Answer: B. 3 4

Explanation

Arrays are value types with copy-on-write, so changing b leaves a untouched.

Step-by-step solution

  1. b = a copies the value.
  2. Only b gets 4.
  3. a=3, b=4.

Why the other options are wrong

  • A. Arrays are value types: b is a copy.
  • C. b was appended to.
  • D. a is unchanged.

Practise all 28 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Swift topics · All topics