Questions and published explanations below are available without starting a test. Some questions may not have a published solution yet.
15 / 16 published solutions
Question 82 MCQ · 2.0 marks
What is the role of the Program Counter (PC) in the CPU?
6406533822452.
To store the results of computations
6406533822453.
To store the address of the next instruction to execute
6406533822454.
To manage input and output operations
6406533822455.
To provide temporary data storage
Published solution
**1. Understand instruction execution:** The CPU repeatedly fetches an instruction, decodes it and executes it. It needs an address to know where to fetch the next instruction.
**2. Identify the register:** The program counter stores that instruction address. During ordinary sequential execution it advances to the following instruction; a branch or jump can replace it with a different address.
**Answer: B — To store the address of the next instruction to execute.**
A: **Incorrect:** Computation results belong in data registers or memory; this is not the defining role of the PC.
B: **Correct:** The PC identifies the next instruction to fetch and execute.
C: **Incorrect:** Input and output are handled by I/O instructions and device controllers.
D: **Incorrect:** General-purpose registers provide temporary data storage; the PC has the specific instruction-address role.
What is the correct order of memory levels based on **size** (smallest to largest)?
6406533822456.
Cache Memory → Registers → Main Memory → Secondary Memory
6406533822457.
Registers → Cache Memory → Main Memory → Secondary Memory
6406533822458.
Secondary Memory → Main Memory → Cache Memory → Registers
6406533822459.
Main Memory → Cache Memory → Secondary Memory → Registers
Published solution
**1. Compare capacities:** Registers are a very small set of storage locations inside the CPU. Cache stores more data close to the CPU. Main memory, usually RAM, stores active programs and data. Secondary storage, such as an SSD, normally has much greater capacity.
**2. Order by size:** The usual hierarchy from smallest capacity to largest is registers, cache, main memory, secondary memory. This question asks about capacity; access speed usually follows the reverse ordering.
**Answer: B — Registers → Cache Memory → Main Memory → Secondary Memory.**
A: **Incorrect:** Cache normally has more capacity than the registers, so the first two levels are reversed.
B: **Correct:** All four levels follow the usual increasing-capacity order.
C: **Incorrect:** This is approximately the decreasing-capacity order.
D: **Incorrect:** RAM is much larger than registers and cache; it cannot come first in an increasing-capacity order.
**1. Define associativity:** When operators at the same precedence level occur together, associativity determines how the expression groups.
**2. Apply assignment grouping:** Assignment groups from right to left: the expression a = b = c means a = (b = c). First the value assigned to b is the value used by the assignment to a.
**3. Distinguish grouping from evaluation order:** Right-to-left associativity describes syntactic grouping. It does not mean that every operand in a C expression is evaluated from right to left.
**Answer: A — Assignment operator (=).**
A: **Correct:** The assignment operator = has right-to-left associativity.
B: **Incorrect:** Logical AND groups from left to right; it also evaluates its left operand first and short-circuits when false.
C: **Incorrect:** Logical OR groups from left to right; it short-circuits when its left operand is true.
D: **Incorrect:** Addition groups from left to right at its precedence level.
Consider the generalized computation given below:
1. R[0] ← M[0]
2. R[1] ← M[1]
3. R[2] = R[1] - R[0]
4. If R[2] != 0, set R[1] = R[1] + 2 and go back to step 3. Otherwise, go to step 5.
5. R[2] = R[0] + R[1]
6. M[2] ← R[2]
For which value of **M[0]** and **M[1]**, the computation will enter into an **infinite loop**?
6406533822460.
M[0] = 5, M[1] = 3
6406533822461.
M[0] = 7, M[1] = 3
6406533822462.
M[0] = 8, M[1] = 2
6406533822463.
M[0] = 10, M[1] = 3
Published solution
**1. Identify the stopping condition:** The loop stops only when R[1] equals R[0], because their difference must become zero.
**2. Express the changing value:** After k increments, where k is a nonnegative integer, the second register is \[R[1]=M[1]+2k.\] Therefore termination requires \(M[0]-M[1]\) to be a nonnegative even integer.
**3. Apply the condition:** With M[0] = 10 and M[1] = 3, termination would require \(3+2k=10\), giving \(k=3.5\). No integer iteration has that value. The sequence 3, 5, 7, 9, 11, ... remains odd and never equals 10.
**Answer: D — M[0] = 10, M[1] = 3.**
A: **Incorrect:** Starting at 3, one increment reaches 5, so the computation terminates.
B: **Incorrect:** Starting at 3, two increments reach 7, so it terminates.
C: **Incorrect:** Starting at 2, three increments reach 8, so it terminates.
D: **Correct:** The changing register remains odd while the target is even; equality never occurs.
**1. Read the initial state:** R1 starts at 10 and R2 starts at 3. Each loop adds the current R2 to R1, then decrements R2. The comparison repeats the loop while R2 is nonzero.
**2. Dry-run each iteration:** The states after addition and decrement are (R1, R2) = (13, 2), then (15, 1), then (16, 0). The final comparison ends the loop.
**3. Describe the computation:** The accumulated increment is \(3+2+1=6\), so R1 finishes at \(10+6=16\). More generally, for an initially positive integer R2 = n, the increment is \(n(n+1)/2\).
**Answer: B — Increment R1 by the sum of values from R2 to 1.**
A: **Incorrect:** The ADD instruction increases R1; it does not subtract from it.
B: **Correct:** The program adds the descending positive values 3, 2 and 1 to R1.
C: **Incorrect:** Repeated addition here uses changing addends, giving 16 rather than the product 30.
D: **Incorrect:** There is no division instruction or repeated-subtraction division algorithm.
**1. Find the entry point:** Initially a = 2 and b = 0. The switch therefore jumps directly to case 2; case 1 is skipped.
**2. Follow fall-through:** There is no break after case 2 or case 3. Case 2 makes b = 5, case 3 makes b = 10, and case 4 makes b = 15.
**3. Stop at break:** The break in case 4 exits the switch, so default does not reset b. The printf statement prints 15.
**Answer: B — 15.**
A: **Incorrect:** 10 counts only two additions, but case 4 also executes.
B: **Correct:** Three additions of 5 occur before the break, giving 15.
C: **Incorrect:** The default branch is not executed after the break.
D: **Incorrect:** 5 would be the result if a break immediately followed case 2; it does not.
**1. Interpret the condition:** The loop considers i from 1 through n = 8. The test i % 2 == 0 is true exactly when i is even.
**2. Trace the accumulator:** result starts at 1 and is multiplied by each selected i. The successive values are \(1\times2=2\), \(2\times4=8\), \(8\times6=48\), and \(48\times8=384\). Odd i values do not change result.
**3. State its purpose:** The code computes the product of the even integers from 1 through n, inclusive. For n = 8 the printed value is 384.
**Answer: B — Product of all even numbers from 1 to n.**
A: **Incorrect:** A factorial multiplies every integer from 1 through n; this code skips odd integers.
B: **Correct:** The parity test selects even integers and the multiplication assignment accumulates their product.
C: **Incorrect:** The accumulator uses multiplication, not addition.
D: **Incorrect:** Odd integers fail the condition and are skipped.
**1. Determine when break runs:** The first if statement stops the loop when i is divisible by both 6 and 8. Their least common multiple is \[\operatorname{lcm}(6,8)=24.\] Thus the loop stops at i = 24 before executing any print in that iteration.
**2. Determine which earlier values print:** For i from 1 through 23, the second if skips even numbers using continue. The print therefore runs only for 1, 3, 5, ..., 23.
**3. Count the odd values:** This arithmetic sequence has \((23-1)/2+1=12\) terms, so the message is printed 12 times.
**Answer: A — 12.**
A: **Correct:** There are exactly 12 odd values before the terminating value 24.
B: **Incorrect:** This misses two of the odd values from 1 through 23.
C: **Incorrect:** 15 would count all odd numbers through 30, ignoring the break at 24.
D: **Incorrect:** The terminating iteration does not print, and only 12 preceding iterations qualify.
Convert the binary number **10101010** to its decimal equivalent.
6406533822464.
170
6406533822465.
180
6406533822466.
160
6406533822467.
175
Published solution
**1. Use binary place values:** Binary digits have weights that are powers of 2, starting with \(2^0\) at the rightmost digit.
**2. Expand the given number:** \[10101010_2=1(2^7)+1(2^5)+1(2^3)+1(2^1)=128+32+8+2=170.\] The zero digits contribute nothing.
**Answer: A — 170.**
A: **Correct:** The nonzero binary place values sum to 170.
B: **Incorrect:** 180 has binary representation 10110100, not the supplied digits.
C: **Incorrect:** 160 corresponds to 10100000 and omits the 8 and 2 contributions.
D: **Incorrect:** 175 corresponds to 10101111 and includes additional low-order bits.
Convert the hexadecimal number **0x7C5D** to its equivalent decimal representation:
6406533822468.
31835
6406533822469.
31836
6406533822470.
31837
6406533822471.
31838
Published solution
**1. Interpret hexadecimal notation:** The prefix 0x means base 16. The letter C represents 12 and D represents 13.
**2. Expand by place value:** \[\begin{aligned}0x7C5D&=7(16^3)+12(16^2)+5(16)+13\\ &=28672+3072+80+13\\ &=31837.\end{aligned}\]
**Answer: C — 31837.**
A: **Incorrect:** 31835 is two less than the correct place-value sum.
B: **Incorrect:** 31836 is one less than the correct sum.
C: **Correct:** This equals the sum of all four hexadecimal place contributions.
D: **Incorrect:** 31838 is one more than the correct sum.
**1. Apply operator precedence:** Initially a = 10, b = 20 and c = 30. In a + b * c / b - a, multiplication and division bind more tightly than addition and subtraction.
**2. Evaluate multiplication and division:** These operators group left to right, so \(b*c/b=(20*30)/20=600/20=30\). All values fit in int, and this division is exact.
**3. Complete the expression:** The remaining expression is \(10+30-10=30\). The assignment stores 30 in result and printf prints 30.
**Answer: C — 30.**
A: **Incorrect:** 25 does not follow from the specified precedence and values.
B: **Incorrect:** 15 is not obtained by evaluating the multiplication/division group correctly.
C: **Correct:** The multiplication/division group is 30 and the a terms cancel.
D: **Incorrect:** 20 is the original value of b, not the evaluated result.
Which of the following is true for an 8-bit 2's complement representation?
6406533822496.
The rightmost bit represents the sign (0 for positive, 1 for negative).
6406533822497.
The maximum positive number that can be represented is 127.
6406533822498.
The minimum negative number that can be represented is -127.
6406533822499.
There are 256 possible values that can be represented.
Published solution
**1. State the representation:** In an n-bit two’s complement integer, the highest-order bit has weight \(-2^{n-1}\); the other bits have the usual positive binary weights.
**2. Derive the range:** For n = 8, \[\text{minimum}=-2^7=-128,\qquad\text{maximum}=2^7-1=127.\] The patterns 10000000 and 01111111 represent these two endpoints.
**3. Count the values:** Eight bits have \(2^8=256\) distinct patterns. Two’s complement assigns each a distinct integer, giving the inclusive range from -128 through 127.
**Answer: B, D — The maximum positive number that can be represented is 127.; There are 256 possible values that can be represented..**
A: **Incorrect:** The sign is indicated by the leftmost, highest-order bit; the rightmost bit has weight 1.
B: **Correct:** 01111111 represents 64 + 32 + 16 + 8 + 4 + 2 + 1 = 127.
C: **Incorrect:** The minimum is -128, not -127; the range is asymmetric by one value.
D: **Correct:** There are 256 patterns and therefore 256 represented integer values.
**1. Apply identifier rules:** An ordinary C identifier can begin with a letter or underscore. Later characters may include letters, underscores and digits. A keyword cannot be used as a variable name.
**2. Check the candidates:** _123Var begins with an underscore and uses allowed later characters. max_length also uses only allowed characters and is not a keyword. A hyphen is an operator, an initial digit is disallowed, and break is a keyword.
**Answer: A, E — _123Var; max_length.**
**Convention note:** Names beginning with an underscore have reservation rules. In particular, _123Var is reserved for file-scope identifiers; the selection concerns identifier syntax and allows ordinary block-scope local use.
A: **Correct:** _123Var is lexically a valid identifier. It can name an ordinary block-scope local variable; see the namespace qualification below.
B: **Incorrect:** The hyphen is a minus token, so Test-Name is not one identifier.
C: **Incorrect:** A C identifier cannot begin with the digit 1.
D: **Incorrect:** break is a reserved language keyword.
E: **Correct:** The name begins with a letter and contains only letters and an underscore.
Consider the generalized computation given below:
1. R[0] ← M[0]
2. R[1] ← M[1]
3. R[1] = R[1] - R[0]
4. R[0] = R[0] + R[1]
5. R[1] = R[0] - R[1]
6. M[2] ← R[1]
What will be the value of M[2] at the end of computation if M[0] = 4 and M[1] = 7?
Published solution
**1. Load the inputs:** Initially R[0] = 4 and R[1] = 7. Each assignment uses the register values that exist at that moment.
**2. Execute the updates in order:** Step 3 gives R[1] = 7 - 4 = 3. Step 4 then gives R[0] = 4 + 3 = 7. Step 5 gives R[1] = 7 - 3 = 4.
**3. Store the result:** The final instruction copies R[1] into M[2], so M[2] = 4. Algebraically the three arithmetic updates exchange the two original register values.
**Answer: 4.**
Find the minimum number of bits required to represent an instruction set where there are **16**
**unique instructions**, and each instruction can take at most **3 operands.**
● The first operand should be able to represent **32 registers.**
● The second operand should be able to represent **-63 to 63** in signed magnitude form.
● The third operand is a single flag (**1-bit**).
Published solution
**1. Encode the instruction operation:** To distinguish N alternatives, a fixed-width field needs \(\lceil\log_2N\rceil\) bits. Sixteen operations therefore require \(\log_2 16=4\) opcode bits.
**2. Encode the operands:** The register field needs \(\log_2 32=5\) bits. Signed magnitude stores the sign separately; magnitudes 0 through 63 need 6 bits, so the signed field needs \(1+6=7\) bits. The flag needs 1 bit.
**3. Add the field widths:** \[\text{instruction length}=4+5+7+1=17\text{ bits}.\] This is the required length for an instruction containing all three operands, without extra padding or metadata.
**Answer: 17.**