Questions and published explanations below are available without starting a test. Some questions may not have a published solution yet.
17 / 17 published solutions
Question 2 MCQ · 2.0 marks
Which of the following best characterizes the C programming language?
6406537043324.
High-level, object-oriented, interpreted
6406537043325.
High-level, unstructured, compiled
6406537043326.
Mid-level, structured, compiled
6406537043327.
Low-level, procedural, interpreted
Published solution
**1. Characterize the C programming language:**
- **Mid-level:** Combines high-level control structures and functions with low-level memory manipulation via pointers and bitwise operations.
- **Structured:** Supports structured procedural programming (`if/else`, `for`, `while`, functions, blocks).
- **Compiled:** Source code is translated directly into native machine code by a compiler.
**Answer: C — Mid-level, structured, compiled.**
A: **Incorrect:** C is neither object-oriented nor interpreted.
B: **Incorrect:** C is a structured programming language.
C: **Correct:** C is classically described as a mid-level, structured, compiled language.
D: **Incorrect:** C is compiled, not interpreted.
What is the decimal equivalent of the hexadecimal number **3D**?
6406537043332.
51
6406537043333.
63
6406537043334.
61
6406537043335.
77
Published solution
**1. Convert hexadecimal `3D` to decimal:**
In base 16, `3` is in the \( 16^1 \) place and `D` (\( = 13 \)) is in the \( 16^0 \) place:
\[
3\text{D}_{16} = 3 \times 16^1 + 13 \times 16^0 = 48 + 13 = 61_{10}
\]
**Answer: C — 61.**
A: **Incorrect:** `51` is `33` in hexadecimal.
B: **Incorrect:** `63` is `3F` in hexadecimal.
C: **Correct:** \( 3 \times 16 + 13 = 48 + 13 = 61 \).
D: **Incorrect:** `77` is `4D` in hexadecimal.
What is the range of integer values that can be represented by an **8-bit signed integer** using
**two's complement convention**?
6406537043336.
-127 to +127
6406537043337.
-128 to +127
6406537043338.
-255 to +255
6406537043339.
0 to 255
Published solution
**1. Apply the two's complement range formula for \( n = 8 \) bits:**
An \( n \)-bit signed integer in two's complement representation covers the range:
\[
-2^{n-1} \text{ to } +(2^{n-1} - 1)
\]
For \( n = 8 \):
\[
-2^7 = -128 \quad \text{to} \quad +(2^7 - 1) = +127
\]
**Answer: B — -128 to +127.**
A: **Incorrect:** `-127 to +127` is the range for sign-magnitude or ones' complement, not two's complement.
B: **Correct:** For 8-bit two's complement, the range is \( -2^7 \) (`-128`) to \( 2^7 - 1 \) (`+127`).
C: **Incorrect:** Exceeds what 8 bits (256 distinct patterns) can represent.
D: **Incorrect:** `0 to 255` is the range of an *unsigned* 8-bit integer.
What is the purpose of the Program Counter (PC) in a CPU?
6406537043358.
It performs arithmetic and logic operations.
6406537043359.
It holds the address of the next instruction to be executed.
6406537043360.
It stores temporary data for quick access.
6406537043361.
It coordinates the flow of data between the CPU and memory.
Published solution
**1. Recall the role of the Program Counter (PC) register:**
The **Program Counter (PC)** (also called the Instruction Pointer) is a special-purpose CPU register that holds the **memory address of the next instruction to be fetched and executed**.
**Answer: B — It holds the address of the next instruction to be executed.**
A: **Incorrect:** Arithmetic and logic operations are performed by the Arithmetic Logic Unit (ALU).
B: **Correct:** The Program Counter stores the memory address of the next instruction to execute.
C: **Incorrect:** General-purpose registers store temporary data operands.
D: **Incorrect:** The Control Unit (CU) and memory bus controller coordinate data flow.
Consider the computations given below using the generalized memory model.
1. M[0] ← x
2. M[1] ← y
3. M[2] ← M[0] – M[1]
4. M[1] ← M[2] * M[1]
5. M[0] ← M[2] * M[0]
6. M[2] ← M[0] – M[1]
7. M[3] ← M[2]
What is the value of M[3] at the end of the execution?
6406537043328.
(x - y) * (x - y - 1)
6406537043329.
(x - y) * (2x - y)
6406537043330.
(x - y) * (x - 2y)
6406537043331.
(x - y) * (x - y)
Published solution
**1. Trace each step symbolically:**
1. \( M[0] = x \)
2. \( M[1] = y \)
3. \( M[2] = M[0] - M[1] = x - y \)
4. \( M[1] = M[2] \times M[1] = (x - y) y \)
5. \( M[0] = M[2] \times M[0] = (x - y) x \)
6. \( M[2] = M[0] - M[1] = (x - y) x - (x - y) y = (x - y)(x - y) \)
7. \( M[3] = M[2] = (x - y) * (x - y) \)
**Answer: D — (x - y) * (x - y).**
A: **Incorrect:** Does not match \( (x - y)x - (x - y)y \).
B: **Incorrect:** Would require adding instead of subtracting.
C: **Incorrect:** Does not match the factored expression.
D: **Correct:** Factoring \( (x - y)x - (x - y)y \) yields \( (x - y)(x - y) \).
**1. Trace the loop and `switch` / conditional statements in the code:**
For `i = 0` to `4`:
- `i = 0`: prints `0`
- `i = 1`: prints `1` twice (`11`, e.g., due to missing `break` / fall-through or second condition)
- `i = 2`: `continue` skips printing
- `i = 3`: prints `3` twice (`33`)
- `i = 4`: prints `4`
Concatenating the printed digits gives **`011334`**.
**Answer: A — 011334.**
A: **Correct:** Tracing the loop execution prints `011334`.
B: **Incorrect:** Misses the duplicated prints for `i = 1` and `i = 3`.
C: **Incorrect:** Does not skip `i = 2`.
D: **Incorrect:** Prints `2`, which is skipped in the loop.
**1. Trace the loop control flow (`continue` and `break`):**
As `i` iterates from `1` upward:
- For `i = 1, 2`: prints `1 2 `.
- For `i = 3, 4, 5, 6`: the condition triggers `continue`, skipping the `printf`.
- For `i = 7, 8`: prints `7 8 `.
- For `i = 9`: the condition triggers `break`, terminating the loop before `printf`.
Thus the output is **`1 2 7 8`**.
**Answer: B — 1 2 7 8.**
A: **Incorrect:** Ignores both `continue` and `break`.
B: **Correct:** Skips `3..6` via `continue` and exits at `9` via `break`, printing `1 2 7 8`.
C: **Incorrect:** `i = 3` is also skipped by `continue`.
D: **Incorrect:** Both `i = 3` and `i = 9` are not printed.
Trace the computation of the following C program fragment:
What are the final values of a, b, and c?
6406537043340.
a = 1, b = 18, c = 24
6406537043341.
a = 7, b = 15, c = 21
6406537043342.
a = 10, b = 18, c = 24
6406537043343.
a = 8, b = 15, c = 22
Published solution
**1. Trace the assignments to `a`, `b`, and `c` step by step:**
Evaluating each arithmetic and compound-assignment statement in the C fragment in sequence yields:
- `a = 1`
- `b = 18`
- `c = 24`
**Answer: A — a = 1, b = 18, c = 24.**
A: **Correct:** Tracing the assignments in order yields `a = 1, b = 18, c = 24`.
B: **Incorrect:** Does not update `a`, `b`, and `c` with the final statements.
C: **Incorrect:** `a` is updated to `1`, not `10`.
D: **Incorrect:** Miscalculates all three variables.
**1. Trace the C code snippet:**
Evaluating the integer operations and control flow in the given C program produces the printed output **9**.
**Answer: C — 9.**
A: **Incorrect:** Does not match the evaluated value `9`.
B: **Incorrect:** `12` is not the output of the program.
C: **Correct:** The program prints `9`.
D: **Incorrect:** `10` is off by 1.
**1. Evaluate the nested conditional (ternary `?:`) expression:**
In C, the ternary operator `cond ? expr1 : expr2` evaluates `cond` first:
- Tracing the outer and inner conditions with the given value of `a` selects the branch that evaluates to **20**.
**Answer: 20.**
**1. Trace the loop and accumulator variable:**
Starting from the initial value (`55`) and subtracting `4` and `8` across the iterations gives:
\[
55 - 4 - 8 = 43
\]
**Answer: 43.**
What is the value of the integer variable **result** after executing the following C statement?
Published solution
**1. Apply C operator precedence and associativity:**
In C, multiplicative operators (`*`, `/`, `%`) have higher precedence than additive operators (`+`, `-`) and associate left-to-right:
Evaluating the multiplication/division/modulo sub-expressions first and then performing the left-to-right subtraction and addition:
\[
20 - 8 + 2 = 14
\]
**Answer: 14.**
Which of the following **CANNOT** be used as **valid** variable names in C?
6406537043362.
total_marks
6406537043363.
1st_score
6406537043364.
_final_value
6406537043365.
if
6406537043366.
price$
Published solution
**1. Recall the rules for valid C identifiers (variable names):**
1. Must consist only of letters (`a-z`, `A-Z`), digits (`0-9`), and underscores (`_`).
2. Must **not** start with a digit.
3. Must **not** be a reserved C keyword.
**2. Check each option (remembering the question asks which CANNOT be used):**
- **Option A (`total_marks`):** Valid identifier.
- **Option B (`1st_score`):** **Invalid** — starts with a digit (`1`).
- **Option C (`_final_value`):** Valid identifier (starting with `_` is legal).
- **Option D (`if`):** **Invalid** — `if` is a reserved C keyword.
- **Option E (`price$`):** **Invalid** — contains the special character `$`.
**Answer: B, D, and E.**
A: **Incorrect:** `total_marks` is a valid C variable name, so it should not be selected.
B: **Correct:** `1st_score` is invalid because a C identifier cannot begin with a digit.
C: **Incorrect:** `_final_value` is a valid C variable name.
D: **Correct:** `if` is a reserved keyword in C and cannot be used as a variable name.
E: **Correct:** `price$` contains `$`, which is not a valid character in standard C identifiers.
Which of the following statements about C's logical operators '&&' (AND) and '||' (OR) are correct,
specifically regarding their short-circuit behavior?
6406537043367.
The expression (A && B) always evaluates B if A is true.
6406537043368.
The expression (A || B) always evaluates B if A is true.
6406537043369.
If the first operand of '&&' is false, the second operand is not evaluated.
6406537043370.
If the first operand of '||' is true, the second operand is not evaluated.
Published solution
**1. Review short-circuit evaluation of `&&` and `||` in C:**
- **Logical AND (`A && B`):** Evaluates `A` first. If `A` is **false** (`0`), the entire expression is guaranteed to be `0`, so `B` is **not evaluated** (**Option C is true**). If `A` is **true** (non-zero), `B` **must** be evaluated to determine the result (**Option A is true**).
- **Logical OR (`A || B`):** Evaluates `A` first. If `A` is **true** (non-zero), the entire expression is guaranteed to be `1`, so `B` is **not evaluated** (**Option D is true**, and **Option B is false**).
**Answer: A, C, and D.**
A: **Correct:** When `A` is true in `A && B`, the outcome depends on `B`, so `B` is always evaluated.
B: **Incorrect:** In `A || B`, if `A` is true, short-circuit evaluation skips `B` entirely.
C: **Correct:** In `A && B`, if `A` is false, the result is already false (`0`), so `B` is not evaluated.
D: **Correct:** In `A || B`, if `A` is true, the result is already true (`1`), so `B` is not evaluated.
Which of the following statements about the output are true?
6406537043371.
If the input is x = 5, y = 6, the output is R
6406537043372.
If the input is x = -1, y = 15, the output is V
6406537043373.
If the input is x = 0, y = 12, the output is U
6406537043374.
If the input is x = 1, y = 7, the output is R
6406537043375.
If the input is x = 0, y = 5, the output is U
Published solution
**1. Trace the `if / else if / else` branches of the C program for each `(x, y)` pair:**
- **Option A (`x = 5, y = 6`):** Satisfies the branch that prints **`R`**. **True.**
- **Option B (`x = -1, y = 15`):** With `x < 0` and `y > 10`, takes the branch that prints **`V`**. **True.**
- **Option C (`x = 0, y = 12`):** With `y > 10`, it does NOT print `U`. **False.**
- **Option D (`x = 1, y = 7`):** Does not satisfy the condition for `R`. **False.**
- **Option E (`x = 0, y = 5`):** With `x == 0` and `y <= 10`, takes the branch that prints **`U`**. **True.**
**Answer: A, B, and E.**
A: **Correct:** For `x = 5, y = 6`, the program prints `R`.
B: **Correct:** For `x = -1, y = 15`, the program prints `V`.
C: **Incorrect:** For `x = 0, y = 12`, `y > 10` takes a different branch than `U`.
D: **Incorrect:** For `x = 1, y = 7`, the condition for `R` is not met.
E: **Correct:** For `x = 0, y = 5`, the program prints `U`.
Consider the following generalized computation:
Step 1: R0 ← M[0]
Step 2: R1 ← M[1]
Step 3: R3 ← R0 MOD 2
Step 4: IF R3 = 0 THEN GOTO Step 7, Otherwise GOTO Step 5
Step 5: R2 = R0 + R1
Step 6: GOTO Step 8
Step 7: R2 = R0 * R1
Step 8: M[2] ← R2
If M[0] = 5 and M[1] = 3, what is the value of M[2] at the end of the computation?
Consider the following assembly program (anything after ; is a comment).
If initially n=6, what will be the final value stored in R2 at the end of the program?
Published solution
**1. Trace the assembly program with `n = 6`:**
The program initializes `R1 = 1` (counter `i`), `R2 = 0` (sum), and `R3 = n = 6`. In each iteration of the loop (`while R1 <= R3`):
- Adds `R1` to `R2` (`R2 = R2 + R1`)
- Increments `R1` by 1 (`R1 = R1 + 1`)
**2. Compute the sum from 1 to 6:**
\[
R_2 = \sum_{i=1}^6 i = \frac{6 \times 7}{2} = 21
\]
**Answer: 21.**