COMPUTER SCIENCE: Programming with C (CSCDSM-151T)
FYUG Even Semester Exam, 2024

Course No: CSCDSM-151T | Full Marks: 70 | Time: 3 Hours

Subject: Computer Science

Paper Code: CSCDSM-151T

Semester: 2nd Semester (FYUG)

Exam Name: Even Semester Exam, 2024


SECTION-A (Answer any ten) 2 × 10 = 20 Marks

1. What is variable? How do you declare a variable in C?

A variable is a named storage location in memory used to hold a data value that can be modified during program execution. To declare a variable, you specify the data type followed by the variable name: int count;.

2. What are the different data types used in C?

C provides several categories of data types:

  • Primary: int, char, float, double, void.
  • Derived: Arrays, Pointers, Functions.
  • User-defined: struct, union, enum.

3. Define token. Give example.

A token is the smallest individual unit in a C program that is meaningful to the compiler. Examples include keywords (if), identifiers (total), constants (10), and operators (+).

4. What do you mean by function prototype? Give example.

A function prototype is a declaration that tells the compiler about a function's name, return type, and parameters before its actual implementation.

Example: int add(int a, int b);

5. Write down the benefits of using function.

  • Modularity: Divides a large program into smaller, manageable parts.
  • Reusability: Code can be called multiple times without rewriting.
  • Ease of Debugging: Errors are easier to isolate in specific functions.

6. What are actual arguments and formal arguments? Give examples.

Actual arguments are values passed to the function when it is called. Formal arguments are variables declared in the function definition that receive those values.

void sum(int x, int y); // x, y are formal arguments
int main() {
    sum(10, 20); // 10, 20 are actual arguments
}
    

7. What is pointer? How do you declare a pointer variable?

A pointer is a variable that stores the memory address of another variable. It is declared using the asterisk (*) operator: int *p;.

9. Define array. How do you initialize a one-dimensional array?

An array is a collection of homogeneous data elements stored in contiguous memory locations. One-dimensional arrays are initialized as: int marks[3] = {80, 85, 90};.

12. What are the differences between array and structure?

Feature Array Structure
Data Type Homogeneous (same type) Heterogeneous (different types)
Access Using index numbers Using dot (.) operator

15. What is string? Write the purpose of null string in C.

A string is an array of characters terminated by a null character (\0). The null string (\0) indicates the end of the character sequence, allowing functions like printf or strlen to know where the string stops in memory.


SECTION-B (Answer any five) 10 × 5 = 50 Marks

16. Explain 'while', 'do-while' and 'for' loops with suitable examples. Difference between 'while' and 'do-while'. [10]

  • while: Entry-controlled loop. Checks condition before execution. while(i < 5) { ... i++; }
  • do-while: Exit-controlled loop. Executes body at least once. do { ... i++; } while(i < 5);
  • for: Concise loop containing initialization, condition, and increment in one line. for(i=0; i < 5; i++) { ... }

Key Difference: A while loop may never execute if the condition is initially false, whereas a do-while loop is guaranteed to execute at least once.

17. Discuss the structure of a C program. Write programs for Area of Circle and Prime Numbers. [2+4+4=10]

Structure of C Program: Documentation section, Link section (headers), Definition section, Global declaration, Main function, and Subprograms.

// Area of Circle
#include <stdio.h>
int main() {
    float r, area;
    printf("Enter radius: ");
    scanf("%f", &r);
    area = 3.14 * r * r;
    printf("Area = %f", area);
    return 0;
}
    

18. Define storage classes in C. Discuss Static and Auto. [10]

Storage classes define the scope, lifetime, and initial value of a variable.

  • Auto: Default class for local variables. Stored in stack; destroyed when function exits.
  • Static: Variables retain their value even after the function finishes its execution and are initialized only once.

19. Recursion vs Iteration. Factorial program using both. [2+4+4=10]

Recursion is a function calling itself, while iteration uses loops. Recursion is often more elegant but uses more stack memory.

// Recursive Factorial
int fact(int n) {
    if (n == 0) return 1;
    return n * fact(n-1);
}
    

22. (b) Define tag, member, dot operator, and struct keyword. [1×4=4]

  • struct: Keyword used to declare a structure.
  • tag: The name given to a specific structure type.
  • member: Individual variables inside the structure.
  • dot (.) operator: Used to access members of a structure variable.

24. (a) Program to copy one file to another. (b) Macro vs Function. [6+4=10]

Macro vs Function: Macros are pre-processed (text replacement) and faster as there is no function call overhead, but they do not check for data types like functions do.

// File Copy Snippet
while ((ch = fgetc(source)) != EOF)
    fputc(ch, target);