Computer Science Fundamentals: cram pack

Formula sheets, the key method steps for every topic and 20 practice problems, with full solutions at the back.

New problemsBack to the course

In the print window, choose “Save as PDF” to keep a copy on your device.

1Formulas and methods

Number systems

Place values
binary 2k, octal 8k, hex 16k
To decimal
Σ dk·bk
From decimal
divide by the base, read the remainders bottom-up
Hex ↔ binary
one hex digit = 4 bits
Two’s complement
invert the bits and add 1

Propositional logic & proofs

Rows of a truth table
2ⁿ
Implication
p → q ≡ ¬p ∨ q ≡ ¬q → ¬p
Negated implication
¬(p → q) ≡ p ∧ ¬q
Biconditional
p ↔ q ≡ (p → q) ∧ (q → p)
De Morgan
¬(p ∧ q) ≡ ¬p ∨ ¬q, ¬(p ∨ q) ≡ ¬p ∧ ¬q
Distributive laws
p ∨ (q ∧ r) ≡ (p ∨ q) ∧ (p ∨ r), p ∧ (q ∨ r) ≡ (p ∧ q) ∨ (p ∧ r)
Negated quantifiers
¬∀x P(x) ≡ ∃x ¬P(x), ¬∃x P(x) ≡ ∀x ¬P(x)
Modus ponens, modus tollens
p → q, p ⊢ q; p → q, ¬q ⊢ ¬p

Method

  1. Count the different variables: the truth table has 2ⁿ rows, starting with all of them 1.
  2. Give every subformula its own column and fill the columns from the inside out.
  3. Apply the rule of each connective: → is 0 only for 1 → 0, ↔ is 1 when both sides agree.
  4. For equivalences, compare the final columns row by row; for an argument, look only at the rows in which every premise is 1.
  5. To negate a quantified statement, move ¬ inwards, swap ∀ and ∃, and negate the inner formula.

Digital logic: gates, K-maps & flip-flops

Basic gates
A·B, A + B, A′, (A·B)′, (A + B)′, A ⊕ B
Boolean rules
X + X′ = 1, X·X′ = 0, X + XY = X, XY + XY′ = X
De Morgan
(X·Y)′ = X′ + Y′, (X + Y)′ = X′·Y′
Karnaugh map order
00, 01, 11, 10 (Gray code)
Group of 2ᵏ cells
loses k variables
4-to-1 multiplexer
F = A′B′I0 + A′BI1 + AB′I2 + ABI3
Flip-flops
D: Q⁺ = D, T: Q⁺ = T ⊕ Q, JK: Q⁺ = JQ′ + K′Q
Counters
n-bit: 2ⁿ states; mod-N: smallest n with 2ⁿ ≥ N; fout = fclk/N
Ripple counter
fmax = 1/(n·tpd)
Ring and Johnson counters
n and 2n states

Method

  1. For a circuit, work from the inputs to the output one gate at a time and write the value on every wire, or write its expression and fill in the truth table.
  2. For simplification, put the 1s (and the don’t-cares as X) into a Karnaugh map with Gray-code rows and columns: 00, 01, 11, 10.
  3. Cover the 1s with as few and as large groups of 1, 2, 4, 8 or 16 cells as possible, wrapping round the edges; each group is one product term made of the variables that stay constant. Group the 0s for the POS form.
  4. For flip-flops and counters, read the inputs at each active clock edge, write the state after every edge, and remember that an n-bit counter wraps round after 2ⁿ states.

Number theory & modular arithmetic

Division with remainder
a = q·n + r, 0 ≤ r < n
Euclid
gcd(a, b) = gcd(b, a mod b)
Least common multiple
lcm(a, b) = a·b/gcd(a, b)
Inverse
a·a⁻¹ ≡ 1 (mod n), gcd(a, n) = 1
Linear congruence
a·x ≡ b → x ≡ a⁻¹·b (mod n)
Euler’s totient
φ(n) = n·∏(1 − 1/p), φ(pᵏ) = pᵏ − pᵏ⁻¹
Fermat
aᵖ⁻¹ ≡ 1 (mod p)
RSA
n = pq, φ = (p − 1)(q − 1), d = e⁻¹ mod φ, c = mᵉ mod n, m = cᵈ mod n

Method

  1. Reduce every number modulo n first; remainders lie between 0 and n − 1.
  2. For a gcd, divide repeatedly with Euclid’s algorithm; the last nonzero remainder is the gcd.
  3. For an inverse, run the extended algorithm and keep the coefficient of a; add n if it is negative.
  4. Solve a·x ≡ b by multiplying with a⁻¹; combine two congruences by writing x = r₁ + m₁k.
  5. For powers, write the exponent in binary and square repeatedly; in RSA, d = e⁻¹ mod φ(n) with φ(n) = (p − 1)(q − 1).

Graph theory

Handshake lemma
Σ deg(v) = 2|E|
Complete graphs
|E(Kn)| = n(n − 1)/2, |E(Km,n)| = m·n
Regular graph
|E| = n·k/2
Tree and forest
|E| = n − 1, |E| = n − c
Leaves of a tree
L = 2 + Σ (deg(v) − 2) over deg(v) ≥ 3
Euler
circuit: all degrees even; path: exactly 2 odd degrees
Hamilton circuits in Kn
(n − 1)!/2
Dijkstra update
d(v) = min(d(v), d(u) + w(u, v))
Walks of length L
(Mᴸ)ij

Method

  1. Write down the degree of every vertex; the degrees add up to twice the number of edges.
  2. For Euler questions, check that the graph is connected and count the vertices with odd degree.
  3. For trees, use n − 1 edges and the handshake lemma.
  4. For Dijkstra, keep a table of tentative distances and settle the smallest unsettled vertex each round.
  5. For a minimum spanning tree, sort the edges by weight and skip every edge that closes a cycle (Kruskal), or grow one tree from a start vertex (Prim).

IPv4 subnetting

Hosts per subnet
2(32 − prefix) − 2 (/31: 2, /32: 1)
Block size
256 − (mask octet)
Network address
IP AND mask
Broadcast address
IP OR (NOT mask): all host bits set to 1
Subnets from n borrowed bits
2n

2Practice problems

1.Number systemsMedium
8-bit two's complement?−128?64?32?16?8?4?2?1

Write −16 as an 8-bit two's complement pattern. Read the pattern as an unsigned binary number: what is its value U in decimal?

N = −168 bit
U =
2.Number systemsHard
1598 codes, b = ? bits?2ᵇ⁻¹…?4?2?1

A system must give every one of 1598 items its own binary code of the same length. What is the smallest number of bits b needed?

1598
b = bit
3.Propositional logic & proofsMedium
¬(b ∨ a) ∧ ¬c¬(b ∨ a) ∧ ¬¬c¬(b ∨ a) → ¬¬c(b ∨ a) ∧ ¬¬c

You want to prove (b ∨ a) → ¬c by contradiction. Which statement do you assume at the start? Click it.

(b ∨ a) → ¬c
  1. 1¬(b ∨ a) ∧ ¬c
  2. 2¬(b ∨ a) ∧ ¬¬c
  3. 3¬(b ∨ a) → ¬¬c
  4. 4(b ∨ a) ∧ ¬¬c
4.Propositional logic & proofsHard
∀x (P(x) ∧ ¬Q(x))∀x (¬P(x) ∧ ¬Q(x))∀x (P(x) → ¬Q(x))∃x (P(x) → ¬Q(x))

Which statement is equivalent to ¬∃x (P(x) ∧ Q(x)), with the negation moved all the way inside? Click it.

¬∃x (P(x) ∧ Q(x))
  1. 1∀x (P(x) ∧ ¬Q(x))
  2. 2∀x (¬P(x) ∧ ¬Q(x))
  3. 3∀x (P(x) → ¬Q(x))
  4. 4∃x (P(x) → ¬Q(x))
5.Digital logic: gates, K-maps & flip-flopsMedium
ABCD000111100001111000010302040517061121130151141809111010

Use the Karnaugh map to find the minimal sum-of-products (SOP) form of F(A, B, C, D) = Σm(7, 8, 11, 12, 13, 14). How many literals L does it contain in total (a primed variable counts as one literal)?

F(A, B, C, D) = Σm(7, 8, 11, 12, 13, 14)
L =
6.Digital logic: gates, K-maps & flip-flopsHard
ABCD0001111000011110X001X302140517161121131150140809011110

For F(A, B, C, D) = Σm(4, 6, 7, 10, 12, 13, 15) + d(0, 3), where d marks the don't-care conditions, find the number N of essential prime implicants.

F(A, B, C, D) = Σm(4, 6, 7, 10, 12, 13, 15) + d(0, 3)
N =
7.Number theory & modular arithmeticMedium

Find the inverse of 38 modulo 67: the number x with 0 < x < 67 and 38·x ≡ 1 (mod 67).

a = 38n = 67
38−1 mod 67 =
8.Number theory & modular arithmeticHard
mc = me​ mod n77m = cd​ mod n?(n, e) = (85, 23)d

An RSA key uses p = 5, q = 17 and e = 23. Decrypt the ciphertext c = 77: find the message m.

p = 5q = 17e = 23c = 77
m =
9.Algorithms & complexityMedium

Why is the base of a logarithm usually left out in Big O, as in O(log n)?

  1. AAll logarithms in computer science have base 10, so it is implied
  2. BThe base only matters for small n
  3. CLogarithms with different bases differ only by a constant factor
  4. DLogarithms are always rounded to integers
10.Algorithms & complexityHard

Taking both endpoints of every edge in a maximal matching gives a vertex cover. How good is this cover?

  1. AOf unknown quality compared to the optimum
  2. BAt most twice the size of a minimum vertex cover
  3. CAt most log n times the minimum
  4. DAlways a minimum vertex cover
11.Data structuresMedium

Which data structure is used to check whether the brackets in "{[()]}" are balanced?

  1. AA binary search tree
  2. BA queue
  3. CA stack
  4. DA hash table
12.Data structuresHard

An object is stored as a key in a hash map, and then a field used by its hash function is changed. What is the likely result?

  1. ALooking up the object may fail because it sits in the bucket for its old hash
  2. BThe map throws an error at the moment the field changes
  3. CThe map automatically moves the key to its new bucket
  4. DNothing changes, because a key's hash is computed only once when the program starts
13.Graph theoryMedium

How many different Hamilton circuits H does the complete graph K7 have? Circuits that differ only in the starting vertex or the direction count as the same.

K7
H =
14.Graph theoryHard
125112013817ABCDE

Kruskal’s algorithm builds a minimum spanning tree. Click the edge it adds as edge number 2; skipped edges do not count.

k = 2
  1. 1edge AB
  2. 2edge AC
  3. 3edge BC
  4. 4edge BD
  5. 5edge CE
  6. 6edge DE
  7. 7edge BE
15.Databases & SQLMedium

What does SELECT name, CASE WHEN grade >= 5.5 THEN 'pass' ELSE 'fail' END FROM results return?

  1. AThe number of students who passed and failed
  2. BEach name with the label 'pass' or 'fail' depending on the grade
  3. CAn error because CASE is not allowed in SELECT
  4. DOnly the students who passed
16.Databases & SQLHard

The query FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.status = 'paid' unexpectedly drops customers with no orders. Why?

  1. AThe WHERE condition on o rejects the NULL-filled rows, turning it into an inner join
  2. BLEFT JOIN never keeps unmatched rows
  3. CThe join should use USING instead of ON
  4. Dstatus is a reserved word
17.Computer networksMedium

How many usable host addresses does a /26 IPv4 subnet have?

  1. A30
  2. B64
  3. C62
  4. D126
18.Computer networksHard

By the Nyquist formula, what is the maximum bit rate of a noiseless 3 kHz channel using 4 signal levels?

  1. A12 kbit/s
  2. B3 kbit/s
  3. C6 kbit/s
  4. D24 kbit/s
19.IPv4 subnettingMedium
/24 → /2711111111111111111111111111100000network bitshost bits

The network 192.168.10.0/24 must be divided into at least 8 equal subnets. If you borrow as few bits as possible, how many usable hosts H does each subnet have?

192.168.10.0/248 subnets
H =
20.IPv4 subnettingHard
last octet = 213112816403211608140211networkhost

A host has the address 10.20.203.213/25. What is the fourth octet x of its network address?

10.20.203.213/25
x =

3Solutions

1.Number systems

Answer: U = 240

Positional number systems: each digit is worth its value times a power of the base. In binary the places are worth 1, 2, 4, 8, 16, …; in hexadecimal 1, 16, 256, …, with A–F standing for 10–15. In 8-bit two's complement the leftmost bit is worth −128 instead of +128, which is how negative numbers are stored.

Invert and add one

16 = 0001 0000
inverted: 1110 1111
+1 → 1111 0000

Read it as unsigned

1111 0000₂ = 256 − 16 = 240

Answer

U = 240
2.Number systems

Answer: b = 11 bit

Positional number systems: each digit is worth its value times a power of the base. In binary the places are worth 1, 2, 4, 8, 16, …; in hexadecimal 1, 16, 256, …, with A–F standing for 10–15. In 8-bit two's complement the leftmost bit is worth −128 instead of +128, which is how negative numbers are stored.

Powers of two

210 = 1024 < 1598
211 = 2048 ≥ 1598

Answer

b = 11
3.Propositional logic & proofs

Answer: (b ∨ a) ∧ ¬¬c

Logical equivalence: two formulas are equivalent (≡) when they have the same value in every row of the truth table. A tautology is 1 in every row, a contradiction is 0 in every row, and a contingency is neither. Useful laws: De Morgan, ¬(a ∧ b) ≡ ¬a ∨ ¬b and ¬(a ∨ b) ≡ ¬a ∧ ¬b; a → b ≡ ¬a ∨ b ≡ ¬b → ¬a (contrapositive); ¬(a → b) ≡ a ∧ ¬b; and the distributive laws, a ∨ (b ∧ c) ≡ (a ∨ b) ∧ (a ∨ c).

Parts of the implication

a = b ∨ a, b = ¬c

Negation of the implication

¬(a → b) ≡ a ∧ ¬b = (b ∨ a) ∧ ¬¬c

Answer

(b ∨ a) ∧ ¬¬c

If the assumption leads to a contradiction, the implication must be true.

4.Propositional logic & proofs

Answer: ∀x (P(x) → ¬Q(x))

Quantifiers and arguments: ¬∀x P(x) ≡ ∃x ¬P(x) and ¬∃x P(x) ≡ ∀x ¬P(x). Move a negation inwards, swap every quantifier it passes, and negate the inner formula with the propositional laws, such as ¬(a → b) ≡ a ∧ ¬b. An argument is valid when the conclusion is 1 in every row in which all premises are 1. A single row with true premises and a false conclusion is a counterexample and shows the argument is invalid.

Move the negation inwards

¬∃x (P(x) ∧ Q(x)) ≡ ∀x (P(x) → ¬Q(x))

Answer

∀x (P(x) → ¬Q(x))

The other options either keep a quantifier unchanged, change the order of the quantifiers, or negate the inner formula wrongly.

5.Digital logic: gates, K-maps & flip-flops

Answer: L = 17

Karnaugh maps: the cells are ordered in Gray code (00, 01, 11, 10), so neighbouring cells, including the ones across the edges, differ in exactly one variable. Group the 1s in rectangles of 1, 2, 4, 8 or 16 cells, as large as possible and as few as possible, until every 1 is covered. Each group is one product term: the variables that stay constant inside it. A group of 2ᵏ cells loses k variables. Don't-cares (X) may be used to make groups bigger but never have to be covered. Grouping the 0s instead gives the minimal POS form.

ABCD000111100001111000010302040517061121130151141809111010

Group the 1s

Every group is a rectangle of 2ᵏ cells; edge cells are neighbours of the cells on the opposite edge.

AC′D′
ABD′
ABC′
A′BCD
AB′CD

Minimal SOP form

F = AC′D′ + ABD′ + ABC′ + A′BCD + AB′CD
L = 17

Answer

L = 17
6.Digital logic: gates, K-maps & flip-flops

Answer: N = 1

Karnaugh maps: the cells are ordered in Gray code (00, 01, 11, 10), so neighbouring cells, including the ones across the edges, differ in exactly one variable. Group the 1s in rectangles of 1, 2, 4, 8 or 16 cells, as large as possible and as few as possible, until every 1 is covered. Each group is one product term: the variables that stay constant inside it. A group of 2ᵏ cells loses k variables. Don't-cares (X) may be used to make groups bigger but never have to be covered. Grouping the 0s instead gives the minimal POS form.

Prime implicants

The largest groups that can be formed from 1s and Xs.

BC′D′, BCD, A′C′D′, A′CD, A′BD′, ABD, A′BC, ABC′, AB′CD′
ABCD0001111000011110X001X302140517161121131150140809011110

Essential prime implicants

AB′CD′
N = 1

Answer

N = 1
7.Number theory & modular arithmetic

Answer: 38−1 mod 67 = 30

Inverses and congruences: a has an inverse modulo n exactly when gcd(a, n) = 1. The extended Euclidean algorithm writes 1 = s·n + t·a, and then a⁻¹ ≡ t (mod n). A congruence a·x ≡ b (mod n) with gcd(a, n) = 1 has the solution x ≡ a⁻¹·b. Two congruences x ≡ r₁ (mod m₁) and x ≡ r₂ (mod m₂) with coprime moduli have one solution modulo m₁·m₂ (Chinese remainder theorem). Euler’s φ(n) counts the numbers from 1 to n that are coprime to n: φ(n) = n·∏(1 − 1/p).

Extended Euclidean algorithm

Each remainder r is written as r ≡ t·38 (mod 67).

r = 67, t = 0
r = 38, q = 1, t = 1
r = 29, q = 1, t = −1
r = 9, q = 3, t = 2
r = 2, q = 4, t = −7
r = 1, q = 2, t = 30

Inverse

1 ≡ 30·38 (mod 67)
x = 30
check: 38·30 = 1140 = 17·67 + 1

Answer

38−1 mod 67 = 30
8.Number theory & modular arithmetic

Answer: m = 53

Modular powers and RSA: compute aᵏ mod n by repeated squaring: write k in binary, square, reduce after every step and multiply the powers whose bit is 1. Fermat: aᵖ⁻¹ ≡ 1 (mod p) for a prime p not dividing a, so the exponent can be reduced modulo p − 1. RSA: choose primes p and q, n = p·q and φ(n) = (p − 1)(q − 1); the public exponent e is coprime to φ(n), and the private exponent is d = e⁻¹ mod φ(n). Encrypt with c = mᵉ mod n and decrypt with m = cᵈ mod n.

Private exponent

n = 85, φ(n) = 64
d = 23−1 mod 64 = 39

Decrypt by repeated squaring

39 = 32 + 4 + 2 + 1
771 ≡ 77 (mod 85)
772 ≡ 64 (mod 85)
774 ≡ 16 (mod 85)
778 ≡ 1 (mod 85)
7716 ≡ 1 (mod 85)
7732 ≡ 1 (mod 85)
7739 ≡ 77·64·16·1 ≡ 53 (mod 85)

Message

m = 53
check: 5323 mod 85 = 77

Answer

m = 53
9.Algorithms & complexity

Answer: C · Logarithms with different bases differ only by a constant factor

Why this is the answer

Changing the base multiplies the logarithm by a constant, and Big O ignores constant factors.

Answer

C · Logarithms with different bases differ only by a constant factor
10.Algorithms & complexity

Answer: B · At most twice the size of a minimum vertex cover

Why this is the answer

The matching edges share no vertices, so the optimum needs at least one vertex per edge, while this method takes two: a 2-approximation.

Answer

B · At most twice the size of a minimum vertex cover
11.Data structures

Answer: C · A stack

Why this is the answer

Push each opening bracket and pop when a closing bracket appears; it must match the popped bracket and the stack must be empty at the end.

Answer

C · A stack
12.Data structures

Answer: A · Looking up the object may fail because it sits in the bucket for its old hash

Why this is the answer

The key's location was chosen from its old hash; with a new hash, lookups search the wrong bucket, so keys should be immutable.

Answer

A · Looking up the object may fail because it sits in the bucket for its old hash
13.Graph theory

Answer: H = 360

Euler, Hamilton and trees: a connected graph has an Euler circuit (every edge once, back to the start) when every vertex has even degree, and an Euler path that is not a circuit when exactly two vertices have odd degree; the path then runs from one odd vertex to the other. A Hamilton circuit visits every vertex once; Kn has (n − 1)!/2 of them. A tree on n vertices has n − 1 edges, and a connected graph with m edges keeps a spanning tree after removing m − n + 1 edges.

Count

H = (7 − 1)!/2 = 720/2 = 360

Answer

H = 360
14.Graph theory

Answer: edge DE

Shortest paths and spanning trees: Dijkstra’s algorithm settles one vertex per round: the unsettled vertex with the smallest tentative distance. Its distance is then final, and every neighbour v gets d(v) = min(d(v), d(u) + w(u, v)). Kruskal’s algorithm builds a minimum spanning tree by taking the edges from light to heavy and skipping any edge that would close a cycle; Prim’s algorithm grows one tree from a start vertex by always adding the lightest edge that leaves it. A spanning tree of n vertices has n − 1 edges.

Kruskal’s algorithm

1. AC (5)
2. DE (8)
3. BC (11)
4. CE (13)

edge number 2

DE

Answer

edge DE
15.Databases & SQL

Answer: B · Each name with the label 'pass' or 'fail' depending on the grade

Why this is the answer

A CASE expression evaluates its conditions per row and returns the matching value, so every row gets a label.

Answer

B · Each name with the label 'pass' or 'fail' depending on the grade
16.Databases & SQL

Answer: A · The WHERE condition on o rejects the NULL-filled rows, turning it into an inner join

Why this is the answer

Unmatched rows have o.status NULL, so the WHERE test is not TRUE; moving the condition into the ON clause keeps every customer.

Answer

A · The WHERE condition on o rejects the NULL-filled rows, turning it into an inner join
17.Computer networks

Answer: C · 62

Why this is the answer

A /26 has 6 host bits: 2⁶ = 64 addresses, minus the network and broadcast addresses, gives 62 usable hosts.

Answer

C · 62
18.Computer networks

Answer: A · 12 kbit/s

Why this is the answer

2 × 3000 × log₂ 4 = 2 × 3000 × 2 = 12,000 bit/s. More levels raise the rate, but noise limits how many can be told apart.

Answer

A · 12 kbit/s
19.IPv4 subnetting

Answer: H = 30

IPv4 subnetting: an address has 32 bits. The prefix /p says how many leading bits identify the network; the remaining 32 − p bits number the hosts. A subnet has 2³²⁻ᵖ addresses, but the first (network address) and last (broadcast address) cannot be given to hosts, so 2³²⁻ᵖ − 2 hosts fit. Exceptions: a /31 (point-to-point link, RFC 3021) uses both of its 2 addresses for hosts, and a /32 is one single address (a single host or a loopback).

Bits to borrow

23 = 8 ≥ 8 → b = 3, /27

Hosts per subnet

25 − 2 = 30

Answer

H = 30
20.IPv4 subnetting

Answer: x = 128

IPv4 subnetting: an address has 32 bits. The prefix /p says how many leading bits identify the network; the remaining 32 − p bits number the hosts. A subnet has 2³²⁻ᵖ addresses, but the first (network address) and last (broadcast address) cannot be given to hosts, so 2³²⁻ᵖ − 2 hosts fit. Exceptions: a /31 (point-to-point link, RFC 3021) uses both of its 2 addresses for hosts, and a /32 is one single address (a single host or a loopback).

Block size

27 = 128

Subnet of this host

network: ⌊213/128⌋·128 = 128
broadcast: 128 + 128 − 1 = 255
usable hosts: 129 … 254

Answer

x = 128

ExamProblems · examproblems.com/courses/computer-science-fundamentals