B.Tech Even Semester Examination 2022–2023 · Paper Code: ESCS201 · Full Marks: 70
continue in loop?
[ 1 Mark ]
Answer: The continue statement is used inside loop structures (for, while, do-while) to prematurely skip the remaining statements inside the loop body for the current iteration and jump directly to the loop's continuation or condition evaluation for the next cycle.
Answer: In legacy non-standard C libraries (like Borland Turbo C), the function strrev() is used to reverse a string in-place.
Note: Because strrev() is not part of the standard ANSI/ISO C library, standard C programs implement string reversal manually using two pointers swapping characters from the start and end of the string.
Answer:
• Worst and Average Case: \(O(n^2)\) (quadratic time).
• Best Case: \(O(n)\) (linear time, when an optimized boolean swap flag is used on an already sorted input array).
Answer: The C Preprocessor is a system program that processes the C source code text before the compiler begins syntax analysis. It executes preprocessor directives starting with `#` (such as #include for header file inclusion, #define for macro constants, and #ifdef for conditional compilation) and produces an expanded source code translation unit.
Answer: Stack (specifically, the Runtime Call Stack).
Explanation: Every time a recursive function calls itself, a new stack frame (activation record) containing local variables, parameters, and return memory addresses is pushed onto the call stack. When the base case is reached and functions return, their frames are popped sequentially in LIFO (Last-In, First-Out) order.
Answer: <string.h>.
Explanation: This standard library header declares essential string manipulation and memory handling functions such as strlen(), strcpy(), strcat(), strcmp(), memcpy(), and memset().
Answer: A nested if-else statement refers to placing an entire if or if-else conditional block inside the body of another outer if or else block. It is used to test multi-level, hierarchical, or dependent logical decisions.
goto statement in C?
[ 1 Mark ]
Answer: The goto statement performs an unconditional jump from its current execution line to a user-defined label inside the same function scope (e.g., goto error_handler; ... error_handler:).
While generally discouraged in structured programming due to creating tangled "spaghetti code", its legitimate use case in C is clean error handling and jumping out of deeply nested multi-level loops where a simple break cannot reach.
Answer: Linear Search (sequential search) is the simplest searching algorithm where we examine each array element sequentially from the beginning (index 0) to the end until a match for the target search key is found or the list is exhausted. Its worst-case time complexity is \(O(n)\).
Answer: A Macro is a fragment of code or a symbolic constant that has been assigned a name using the #define preprocessor directive. When the preprocessor runs, it performs inline text replacement—substituting every occurrence of the macro name in the code with its defined replacement text before compilation.
printf("%d",--a);
[ 1 Mark ]
Explanation: The operator --a is the pre-decrement operator. It decrements the value of variable a by 1 before the expression is evaluated or passed to printf. Thus, if \(a = 2\), `--a` changes \(a\) to 1 and immediately prints 1.
Structure Definition: A structure (struct) is a user-defined composite data type in C that groups together related variables of different data types under a single unified name. Each variable inside a structure is called a member.
Array of Structures: An array of structures is an array where each individual element is a structure variable of the same type. It is widely used to store tabular records, such as databases of students, employees, or books.
Mathematical Formula: For a quadratic equation \(ax^2 + bx + c = 0\), the discriminant is \(D = b^2 - 4ac\).
• If \(D > 0\): Two real and distinct roots: \(r_1, r_2 = \frac{-b \pm \sqrt{D}}{2a}\).
• If \(D = 0\): Two real and equal roots: \(r_1 = r_2 = \frac{-b}{2a}\).
• If \(D < 0\): Complex conjugate roots: \(\text{Real Part} = \frac{-b}{2a}, \text{Imaginary Part} = \frac{\sqrt{-D}}{2a}\).
Below is the standard diagrammatic flowchart representation for calculating factorial \(N!\):
Answer: The conditional operator (also known as the ternary operator) is the only operator in C that takes three operands. Its symbols are a question mark and a colon: ? :.
Syntax: condition ? expression_if_true : expression_if_false;
How it works: The compiler evaluates condition. If it evaluates to true (non-zero), the entire ternary expression yields the value of expression_if_true. If false (zero), it yields the value of expression_if_false. It serves as a compact, inline shorthand for simple if-else statements.
#include <stdio.h>
int main() {
int a[5] = {1, 2, 3, 4, 5};
int i;
for (i = 0; i < 5; i++)
if ((char)a[i] == '5')
printf("%d\n", a[i]);
else
printf("FAIL\n");
}
[ 5 Marks ]
Detailed Logic Breakdown:
1. Notice the condition in the if statement: if ((char)a[i] == '5').
2. The integer array contains numbers: 1, 2, 3, 4, 5. When element a[4] (which is the integer 5) is cast to a character via (char)a[i], it results in the character with ASCII decimal code 5 (an unprintable control code).
3. However, on the right side of the comparison, the character literal '5' (in single quotes) represents the ASCII digit '5', whose numeric decimal code is exactly 53!
4. Since integer 5 never equals ASCII code 53, the condition is false for all 5 array iterations! Thus, the else branch executes 5 times, printing FAIL on 5 lines.
(a) Difference Between Array and Structure [4 Marks]:
| Feature | Array | Structure (struct) |
|---|---|---|
| Data Types | Homogeneous collection: all elements must have the exact same data type (e.g., all ints). | Heterogeneous collection: members can be of completely different data types (int, float, char, array). |
| Element Access | Elements are accessed via integer indices (subscripts) like arr[0]. |
Members are accessed using dot operator (.) or arrow operator (->) like s.name. |
(b) Difference Between Structure and Union [3 Marks]:
In a structure, memory is allocated independently for all members simultaneously, making total size equal to the sum of members. In a union, all members share the exact same starting memory address, making total size equal to the largest single member. Thus, only one union member can be active at any given time.
(c) Program Using Structure for 10 Students with Average Marks [8 Marks]:
(a), (b), (c) Integrated C Program Solving All Three Parts:
(a) Operating System Definition [4 Marks]: An Operating System (OS) is system software that manages computer hardware and software resources and acts as an intermediary interface between computer users and physical CPU/memory hardware. Examples: Windows, Linux, macOS, Android.
(b) Functions of an OS [4 Marks]:
1. Process & Processor Management: Scheduling CPU time among concurrent running processes (multitasking).
2. Memory Management: Allocating and deallocating RAM memory space to programs dynamically.
3. File & Storage Management: Organizing data into hierarchical directories and files on disk drives.
4. I/O Device Management: Communicating with peripherals via hardware device drivers.
(c) What is Booting? [3 Marks]: Booting is the initial startup sequence executed by a computer when powered on. The BIOS/UEFI firmware runs Power-On Self-Tests (POST), locates the Master Boot Record (MBR), and loads the Operating System kernel from disk storage into active RAM memory.
(d) Compiler vs Interpreter [4 Marks]: A compiler translates the entire source code into a standalone machine code executable file in one pre-run batch, executing faster. An interpreter translates and executes source code line-by-line during runtime without creating a standalone binary file.
strcat() function in C.(a) Print Diagonal Elements of 5 x 5 Matrix [5 Marks]:
(b) Concatenate Two Strings Without strcat() [5 Marks]:
(c) Sort n Elements from an Array using Bubble Sort [5 Marks]:
(a) Convert Binary to Decimal \((1101.01)_2\) [4 Marks]:
• Integer part: \(1 \times 2^3 + 1 \times 2^2 + 0 \times 2^1 + 1 \times 2^0 = 8 + 4 + 0 + 1 = 13\).
• Fractional part: \(0 \times 2^{-1} + 1 \times 2^{-2} = 0 + 0.25 = 0.25\).
• Total Decimal Value: \((13.25)_{10}\).
(b) What is 2's Complement? [4 Marks]:
The 2's complement of a binary number is obtained by first inverting all its bits (1's complement) and then adding 1 to the least significant bit (LSB). It is the universal standard representation for signed negative integers in computers because it allows arithmetic subtraction to be performed using addition circuitry without having two representations for zero.
Example: To find 2's complement of \(00111_2\) (+7):
1's complement (invert bits): `11000`
Add 1 to LSB: `11000 + 1` = 11001 (-7 in 5-bit 2's complement).
(c) Subtraction Using 2's Complement: \(10101_2 - 00111_2\) [4 Marks]:
We perform: \(10101_2 + (\text{2's complement of } 00111_2)\).
From (b), 2's complement of `00111` is `11001`.
Now add:
`10101` (21 in decimal)
+ `11001` (-7 in decimal)
---
= `101110`
Since this is 5-bit arithmetic, we discard the final carry out (the 6th bit `1`).
Result: \(01110_2\) (which is exactly \(21 - 7 = 14_{10}\)!).
(d) Convert Decimal to Octal \((5673)_{10}\) [3 Marks]:
Divide repeatedly by 8 and record remainders from bottom to top:
• \(5673 \div 8 = 709\), remainder 1
• \(709 \div 8 = 88\), remainder 5
• \(88 \div 8 = 11\), remainder 0
• \(11 \div 8 = 1\), remainder 3
• \(1 \div 8 = 0\), remainder 1
Reading remainders from bottom to top: \((13051)_8\).