2079

CSC115 · TU past paper

C Programming 2079 question paper

The complete TU 2079 exam paper for C Programming (CSC115), all 12 questions with solved model answers written to the mark scheme.

Tap a question to open its answer.

  1. Q1.10 marksFunction Arguments and Return TypesAnswer

    Why do we need a break and continue statement? Define formal argument and actual argument in function with examples. Identify and list the errors in the following code.

    int main() {
        int a, b, c
        scanf("%d%d%d, &a, &b, &c);
        sum(a, b, c);
        return -1;
    }
    
    void sum(int x, int y, int z) {
        int sum;
        sum = a + b + c;
        return sum;
    }
    

    [10]

    --- The break statement is used to immediately exit from a loop (for, while, do-while) or a switch statement before its normal termination condition is reached. Without break, a loop would continue executing even when a desired condition has been met, wasting processing time or producing incorrec...

  2. Q2.10 marksExit functionAnswer

    What is the difference between exit() and exit(1)? Discuss the need of nested structure with an example. Write a program to find the value of x^y without using POW code.[10]

    --- The exit() function is defined in <stdlib.h and is used to terminate a program immediately from any point in the code, including from inside functions or loops. Feature exit(0) exit(1) --------- Meaning Normal / successful termination Abnormal / error termination Signal to OS Program complete...

  3. Q3.10 marksDecision Making and LoopingAnswer

    Write a program to demonstrate the following menu-driven program. The user will provide an integer and alphabet for making choice and the corresponding task has to be performed according as follow:A. Find Odd or Even B. Find Positive or Negative C. Find the Factorial value D. Exit The choice will be displayed until the user will give 'D' as a choice.[10]

    --- --- --- The menu is placed inside a do-while loop so that: - The menu is displayed at least once. - The loop continues repeating until the user enters 'D' (Exit). Each menu option is handled by a separate case block: Choice Task Performed ----------------------- A or a Checks if number is Odd...

  4. Q4.5 marksArithmetic operatorAnswer

    How do you swap the values of two integers without using the third temporary variable? Justify with the example. [5]

    Normally, swapping two variables requires a third temporary variable. However, we can swap two integers without a temporary variable using either: 1. Arithmetic operators (Addition and Subtraction) 2. Bitwise XOR operator --- Step Operation a b ----------------------- Initial -- 10 20 Step 1 a = ...

  5. Q5.5 marksNested and Recursive FunctionAnswer

    Write a program to find the sum of digits of a given integer using recursion. [5]

    The sum of digits of a number can be found recursively by: - Base case: If the number is 0, return 0 - Recursive case: Return (number % 10) + sumOfDigits(number / 10) For example, for n = 123: - 123 % 10 = 3, then call sumOfDigits(12) - 12 % 10 = 2, then call sumOfDigits(1) - 1 % 10 = 1, then cal...

  6. Q6.5 marksConstants/ LiteralsAnswer

    Differentiate between constant and literals. Why do we need to define the type of data? [5]

    --- Basis Constant Literal --------- Definition A constant is a named memory location whose value cannot be changed during program execution. A literal is a fixed, raw value that appears directly in the source code. Has a Name? Yes, it has an identifier/name. No, it has no name; it is the value i...

  7. Q7.5 marksIntroduction to ArrayAnswer

    Write a program to find the second largest number in the given array of numbers. [5]

    1. Read N numbers into an array. 2. Find the largest element first. 3. Then find the largest element among all elements that are NOT equal to the largest -- that gives the second largest. --- --- --- Step What happens ------------------- First loop Reads N numbers into arr[] Second loop Finds lar...
  8. Q8.5 marksArray of structureAnswer

    Create a structure 'Employee' having Name, Address, Salary, and Age as member functions. Display the name of the employee having age between 40 and 50 and are living in Kathmandu. [5]

    Concept Description ------ struct Employee Defines the structure with 4 members emp[100] Array of structure to store multiple employees strcmp() Compares Address string with "Kathmandu" (returns 0 if equal) Age = 40 && Age <= 50 Checks if age falls in the required range Both conditions joined by ...

  9. Q9.5 marksPointers as Function ArgumentsAnswer

    List any one advantage and disadvantage of the pointer. How do you pass pointers as function arguments? [5]

    Returning more than one value from a function: A pointer allows a called function to directly modify the original variable in the calling function. This means a single function call can effectively return multiple modified values, which is not possible with normal call by value. --- Risk of unint...

  10. Q10.5 marksInput Output Operations in FileAnswer

    Suppose a file named 'Num.txt' contains a list of integers. Write a program to extract the prime numbers only from that file and write them on 'Prime.txt' file. [5]

    • Read integers from Num.txt using fscanf() - Check each integer for primality using a loop - Write prime numbers to Prime.txt using fprintf() - Use FILE pointer and formatted I/O --- --- Step Description ------------------- fopen("Num.txt", "r") Opens Num.txt in read mode; returns FILE pointer f...
  11. Q11.5 marksUnionAnswer

    What is the advantage of the union over structure? List any four string library functions with the prototype. [5]

    The primary advantage of union over structure is memory efficiency. In a structure, each member is allocated its own separate memory space, so the total memory used is the sum of all members' sizes. In a union, all members share the same memory location. The compiler allocates memory equal to the...

  12. Q12.5 marksLocal and Global VariableAnswer

    Write short notes on: a. Local, Global, and Static variables b. Conditional Operator [5]

    --- Variables that are defined within the body of a function or a block are called local variables. Key characteristics: - Can be used only within the function or block in which they are declared - They are not accessible outside that function/block - The same variable name may be used in differe...