Chapter 1.1: Computers
Multiple Choice · Computer Basics
Question 1: Basic Parts
Which statement about the basic parts of a computer is correct?
B: The processor computes (ALU, control unit, registers); RAM temporarily holds the running program and its data and is erased when power is lost. The disk is large, slow and permanent: the pantry in the kitchen analogy. A swaps the roles; C confuses the disk with the cache; D ignores the permanence of the disk.
Output Question · Numbers
Question 2: Base Conversions
What does the program print?
System.out.println(Integer.toBinaryString(45));
System.out.println(Integer.toHexString(171));
System.out.println(Integer.parseInt("777", 8));
System.out.println(0x1F + 010);Show answer
101101 ab 511 39
45 = 32 + 8 + 4 + 1 → 101101. 171 = 10·16 + 11 → ab (toHexString prints lowercase letters).
777₈ = 7·64 + 7·8 + 7 = 511. 0x1F = 31, 010 has a leading zero, so it is octal: 8 → 31 + 8 = 39.
Step-by-Step Trace · Numbers
Question 3: Decimal to Binary: 22
What does the code print for 22? How many times does the loop run?
number = 22; the result string starts empty.
Condition true. remainder = 22 % 2 = 0 is added to the front of the string → "0"; number = 22 / 2 = 11.
Condition true. remainder = 11 % 2 = 1 is added to the front of the string → "10"; number = 11 / 2 = 5.
Condition true. remainder = 5 % 2 = 1 is added to the front of the string → "110"; number = 5 / 2 = 2.
Condition true. remainder = 2 % 2 = 0 is added to the front of the string → "0110"; number = 2 / 2 = 1.
Condition true. remainder = 1 % 2 = 1 is added to the front of the string → "10110"; number = 1 / 2 = 0.
0 > 0 is false, the loop ends after 5 iterations. 22 = 16 + 4 + 2 → 10110. Because each remainder is added at the front, the bits end up in the right order.
int number = 22; String binary = ""; while (number > 0) { int remainder = number % 2; binary = remainder + binary; number = number / 2; } System.out.println(binary);
Multiple Choice · Numbers
Question 4: −20 in Two's Complement
Which is the 8-bit two's complement representation of −20?
D: 20 = 00010100 → flip the bits: 11101011 (B, the ones' complement) → add 1: 11101100. Check: 11101100 as unsigned is 236 = 256 − 20. A only sets the sign bit to 1 (sign-magnitude representation); C is +20.
Output Question · Numbers
Question 5: Overflow
What does the program print?
byte b = 126; b++; b++; System.out.println(b); int m = Integer.MAX_VALUE; System.out.println(m + 2); short s = (short) 40000; System.out.println(s); System.out.println((byte) 0b10110110);
Show answer
-128 -2147483647 -25536 -74
byte holds at most 127: 126 → 127 → −128 (wraps around). MAX_VALUE + 2 = −2147483648 + 1.
40000 does not fit in a 16-bit short: 40000 − 65536 = −25536. 10110110₂ = 182; the leftmost bit is 1 → 182 − 256 = −74. Java does not report an error on overflow.
Spot the Bug · Numbers
Question 6: Which Line Does Not Compile?
Each line is tried in a separate program. Which do not compile, and why? In the line that compiles, what is z?
byte b = 200; // (1) long x = 3000000000; // (2) char c = "A"; // (3) int y = 08; // (4) int z = 010; // (5)
Show answer
- (1)
possible lossy conversion from int to byte: byte holds at most 127. - (2)
integer number too large: the literal is treated as int; the correct form is3000000000L. - (3)
String cannot be converted to char: a character is written in single quotes,'A'. - (4)
';' expected: a leading 0 means octal, and octal has no digit 8. - (5) compiles:
010is octal 10 = 8.
Multiple Choice · Characters and Strings
Question 7: UTF-8 Byte Count
Which two numbers does the code print?
String k = "çiğdem";
System.out.println(k.length() + " "
+ k.getBytes(StandardCharsets.UTF_8).length);C: length() is the number of chars: ç-i-ğ-d-e-m = 6. In UTF-8, ASCII letters take 1 byte, while ç (U+00E7) and ğ (U+011F) take 2 bytes each: 4 + 2 + 2 = 8. D counts every character as 2 bytes (like UTF-16); A assumes 1 byte per letter.
Step-by-Step Trace · Characters and Strings
Question 8: Shifting Letters
What does the program print? ('K' = 75, 'e' = 101, 'd' = 100, 'i' = 105)
Every letter of the string is a number (its code is shown below).
charAt(0) = 'K' (75). ch + 3 gives an int: 78; (char) turns it into 'N', which is appended to the end.
charAt(1) = 'e' (101). ch + 3 gives an int: 104; (char) turns it into 'h', which is appended to the end.
charAt(2) = 'd' (100). ch + 3 gives an int: 103; (char) turns it into 'g', which is appended to the end.
charAt(3) = 'i' (105). ch + 3 gives an int: 108; (char) turns it into 'l', which is appended to the end.
i = 4 < 4 is false, the loop ends: Nhgl. Every letter moved 3 places forward in the alphabet (Caesar cipher).
String word = "Kedi"; String result = ""; for (int i = 0; i < word.length(); i++) { char ch = word.charAt(i); int code = ch + 3; result = result + (char) code; } System.out.println(result);
Output Question · Characters and Strings
Question 9: Emoji and length()
What does the program print? (€ = U+20AC, 😀 = U+1F600)
String s = "A€😀";
System.out.println(s.length());
System.out.println(
s.codePointCount(0, s.length()));
System.out.println(
s.getBytes(StandardCharsets.UTF_8).length);Show answer
4 3 8
A Java String is stored as UTF-16: A and € are one char each, while 😀, above U+FFFF, takes two chars (a surrogate pair) → length() = 4.
There are 3 code points. UTF-8 bytes: A 1, € 3 (U+0800–U+FFFF), 😀 4 → 8.
Output Question · Structured Information
Question 10: How Many Bytes in an Image?
What does the program print for an uncompressed 800 × 600 pixel image that uses 3 bytes per pixel (R, G, B)?
long pixels = 800L * 600; long bytes = pixels * 3; System.out.println(bytes); System.out.println(bytes / 1024 + " KiB"); int wrong = 100000 * 100000; System.out.println(wrong); long correct = 100000L * 100000; System.out.println(correct);
Show answer
1440000 1406 KiB 1410065408 10000000000
800 · 600 = 480,000 pixels, × 3 = 1,440,000 bytes; 1,440,000 / 1024 = 1406 (integer division, the fraction is dropped).
100000 · 100000 = 10¹⁰ does not fit in an int; the remainder modulo 2³², 1410065408, is printed and there is no error. 100000L makes the product a long.
Multiple Choice · Stored Program
Question 11: Memory Hierarchy
Which is the correct order from fastest to slowest access speed?
A: Registers are inside the processor and are the fastest; the L1, L2 (and L3) caches are close to the processor, small and fast; RAM is large but slower, and the disk is the largest and slowest. D gives the same order reversed. As speed increases, size shrinks and cost rises.
Multiple Choice · Stored Program
Question 12: Address Bus
A processor's address bus has 16 lines and each address selects one byte. What is the maximum amount of memory that can be addressed?
C: 16 lines select 2¹⁶ = 65,536 different addresses: 64 KiB. B is 2¹⁵; D is the limit of a 32-line bus (2³² bytes). The data bus carries the value, the control bus carries the “read/write” signal.
Step-by-Step Trace · Stored Program
Question 13: Toy Program with a Jump
The toy computer starts at address 200 ([x]: the value at address x). When the program finishes, what is in memory[209] and what is PC?
Program and data are in the same memory. PC = 200.
Fetch: the instruction at 200 goes into IR and PC becomes 201. Execute: memory[206] = 50 → R1.
Fetch: PC first becomes 202. Execute: JUMP sets PC to 203; the ADD at 202 never runs.
Fetch: PC 204. Execute: the ALU computes 50 − 8 = 42 and writes it to R1.
Fetch: PC 205. Execute: R1 = 42 is written to memory[209] (control bus: “write”).
Fetch: PC 206. HALT: the processor stops. memory[209] = 42, PC = 206.
200: LOAD R1, [206] ; R1 ← [206] 201: JUMP 203 ; PC ← 203 202: ADD R1, [207] ; R1 ← R1 + [207] 203: SUB R1, [208] ; R1 ← R1 − [208] 204: STORE R1, [209] ; [209] ← R1 205: HALT ; stop 206: 50 207: 25 208: 8 209: 0
Multiple Choice · von Neumann Architecture
Question 14: von Neumann and Harvard
Which feature distinguishes the von Neumann architecture from the Harvard architecture?
D: In the stored-program idea, both instructions and data are numbers kept in the same memory and carried over the same data path; this single path creates the von Neumann bottleneck. In the Harvard architecture (e.g. AVR microcontrollers) instruction and data memories are separate. C describes ENIAC's manual rewiring.
Output Question · von Neumann Architecture
Question 15: Instruction Encoding
A toy computer has 8-bit instructions: 2-bit opcode (00 add, 01 subtract, 10 multiply, 11 divide), 2-bit destination, and two 2-bit source registers. (a) What does the code print? (b) What does 01101110 do? (c) How is “R2 ← R3 × R1” encoded?
int instr = 0b01101110;
System.out.println(instr);
System.out.println(instr / 64 + " "
+ (instr / 16) % 4 + " "
+ (instr / 4) % 4 + " " + instr % 4);Show answer
110 1 2 3 2
(a) 01101110₂ = 64 + 32 + 8 + 4 + 2 = 110. 110 / 64 = 1; 110 / 16 = 6, 6 % 4 = 2; 110 / 4 = 27, 27 % 4 = 3; 110 % 4 = 2.
(b) 01 = subtract, destination R2, sources R3 and R2: R2 ← R3 − R2.
(c) 10 · 10 · 11 · 01 → 10101101 (= 173).