Sample Solution

  1. Write the output of the following C codes, with proper justification for each.
    i)
    main()
    {
        int x = 2, y = 2, z = 2;
        x = y == z;
        y = z == x;
        z = x == y;
        printf("%d", z);
    }
ii)
    main()
    {
        int x = -1, y;
        x++;
        y = x ? 2+x : 3-x;
        printf("%d", y);
    }
iii)
main()
    \{
        int i, j, \(a=0\);
        for ( \(i=1, j=1 ; i<=5 ; j++\) )
        \{
            \(a=a+i * j++;\)
            \(i=j ;\)
        \}
        printf("\%d ", a);
    \(\}\)

iv)

main()
    {
        int x = 5, y = 12;
        int *p = &x, *q = &y, *r;
        r = p;
        p = q;
        q}=r
        printf("%d, %d", x, y);
    }



v)
func(int x)
    {
        static int a = 0;
        a += x;
        return(a);
    }
    main()
    {
        int p;

for(p = 1; p <= 5; ++p)
    printf("%d", fun(p));
}
        
  1. (a) Write a C program that takes a list of floating point numbers as input from the keyboard, and prints their mean and standard deviation.
(b) Parenthesise the following C statement for its proper evaluation.
x + = a % b + c d / e ! = f + + g x + = a % b + c d / e ! = f + + g x+=a%b+c**d//e!=f++-gx+=a \% b+c * d / e!=f++-gx+=a%b+cd/e!=f++g
Further, if all the variables from a a aaa to g g ggg are initialised by 2, then what will be the value of x x xxx ?
(c) Write a loop that examines each character in a character type array called text, and determines how many of the characters are letters, how many are digits, how many are whitespace characters, and how many are other characters.
  1. (a) Explain the arguments of the functions scanf () and printf (), and how they work with an example for each.
(b) Suppose you wish to solve the equation x 5 + 3 x 2 10 = 0 x 5 + 3 x 2 10 = 0 x^(5)+3x^(2)-10=0x^5+3 x^2-10=0x5+3x210=0 for x x xxx. Rearrange this equation to get an appropriate fixed point iteration method. Then write a program to get an approximate value of the root of the equation. The initial guess must be entered by the user. Your programme must be able to flag a warning message if the initial guess is too far from the exact root. In that case, your program must be able to suggest an initial guess to the user. Terminate the program and print the current value of the root as soon as the difference between two successive approximations becomes smaller than 0.005 .
(c) Explain the use of the string library functions strncpy () and strncat (), with the help of an example for each.
  1. (a) Write a C program to check whether a given string is a palindrome or not.
(b) Explain, with an example, the difference between passing by value and passing by reference.
(c) How would you differentiate between the terms pointer to an array and array of pointers? Give proper examples.
  1. (a) Explain how will you allocate memory dynamically for a two dimensional array of floating point numbers.
(b) You might recall that the Euler number e e eee can be represented by the following series:
e = 1 + 1 1 ! + 1 2 ! + 1 3 ! + e = 1 + 1 1 ! + 1 2 ! + 1 3 ! + e=1+(1)/(1!)+(1)/(2!)+(1)/(3!)+dotse=1+\frac{1}{1!}+\frac{1}{2!}+\frac{1}{3!}+\ldotse=1+11!+12!+13!+
Write a program that approximates e e eee with desired number of terms as entered by the user.
(c) What do you understand by a nested structure? Explain with an example. Also, explain how would you access the members of a structure or a nested structure using pointers?
(d) Given an integer n n nnn, what does the following function compute?
int function(int n)
{
    int i;


    for(i = 0; n != 0; n %=10, i++)
    return i;
}
(e) Explain, with an example, the use of the keywords break and continue.
  1. (a) Write a C program which reads a line of text from keyboard, and writes the line to a file with lower case letters converted to upper case and vice-versa.
(b) Write the inorder, preorder and postorder traversals of the following binary search tree.
original image
(c) Define a structure of type hms containing three integer members, called hour, minute and second, respectively. Then define a union containing two members, each a structure of type hms. Call the union members local and home, respectively. Declare a pointer variable called time that points to this union.
  1. Write a function that evaluates an expression in RPN that is given as a string.
  2. (a) Explain the meaning of the terms garbage collection, fragmentation, relocation and compaction.
(b) What do you understand by file organisation? Explain the methods of file organisation.

Answer:

  1. Write the output of the following C codes, with proper justification for each.
    i)
    main()
    {
        int x = 2, y = 2, z = 2;
        x = y == z;
        y = z == x;
        z = x == y;
        printf("%d", z);
    }
ii)
    main()
    {
        int x = -1, y;
        x++;
        y = x ? 2+x : 3-x;
        printf("%d", y);
    }
iii)
main()
    \{
        int i, j, \(a=0\);
        for ( \(i=1, j=1 ; i<=5 ; j++\) )
        \{
            \(a=a+i * j++;\)
            \(i=j ;\)
        \}
        printf("\%d ", a);
    \(\}\)

iv)

main()
    {
        int x = 5, y = 12;
        int *p = &x, *q = &y, *r;
        r = p;
        p = q;
        q}=r
        printf("%d, %d", x, y);
    }



v)
func(int x)
    {
        static int a = 0;
        a += x;
        return(a);
    }
    main()
    {
        int p;

for(p = 1; p <= 5; ++p)
    printf("%d", fun(p));
}
        

Answer:


i)

main()
{
    int x = 2, y = 2, z = 2;
    x = y == z;
    y = z == x;
    z = x == y;
    printf("%d", z);
}

Analysis:

  1. Initial Values: x = 2, y = 2, z = 2.
  2. First Assignment: x = y == z
    • y == z compares y (2) and z (2). Since they are equal, the result is 1 (true in C).
    • So, x = 1.
  3. Second Assignment: y = z == x
    • z == x compares z (2) and x (1). They are not equal, so the result is 0 (false in C).
    • So, y = 0.
  4. Third Assignment: z = x == y
    • x == y compares x (1) and y (0). They are not equal, so the result is 0 (false in C).
    • So, z = 0.
  5. Output: printf("%d", z) prints the value of z, which is 0.

Output:

0

Justification:

The == operator returns 1 for true and 0 for false. The assignments update the variables based on equality comparisons, and the final value of z is 0 because x (1) is not equal to y (0).

ii)

main()
{
    int x = -1, y;
    x++;
    y = x ? 2+x : 3-x;
    printf("%d", y);
}

Analysis:

  1. Initial Values: x = -1, y is uninitialized.
  2. Increment: x++
    • Post-increment increases x from -1 to 0.
  3. Ternary Expression: y = x ? 2+x : 3-x
    • The condition x is evaluated. Since x = 0, and 0 is false in C, the expression takes the false part: 3 - x.
    • Compute: 3 - x = 3 - 0 = 3.
    • So, y = 3.
  4. Output: printf("%d", y) prints the value of y, which is 3.

Output:

3

Justification:

The ternary operator ?: evaluates x as a condition. Since x is 0 (false), the expression 3 - x is executed, resulting in y = 3.

iii)

main()
{
    int i, j, a=0;
    for (i=1, j=1 ; i<=5 ; j++)
    {
        a = a + i * j++;
        i = j ;
    }
    printf("%d", a);
}

Analysis:

  • The code has unusual parentheses (e.g., \(a=0\)), which I’ll assume are transcription artifacts. I’ll treat it as standard C syntax: int i, j, a = 0; and so on.
  • Let’s simulate the for loop step-by-step:
    1. Initialization: i = 1, j = 1, a = 0.
    2. Condition: i <= 5 is checked before each iteration.
    3. Body and Update:
      • Iteration 1:
        • i = 1, j = 1.
        • a = a + i * j++a = 0 + 1 * 1 = 1, then j++ makes j = 2.
        • i = ji = 2.
        • Increment: j++ (in for) → j = 3.
      • Iteration 2:
        • Check: i = 2 <= 5 (true).
        • a = a + i * j++a = 1 + 2 * 3 = 1 + 6 = 7, then j++ makes j = 4.
        • i = ji = 4.
        • Increment: j++j = 5.
      • Iteration 3:
        • Check: i = 4 <= 5 (true).
        • a = a + i * j++a = 7 + 4 * 5 = 7 + 20 = 27, then j++ makes j = 6.
        • i = ji = 6.
        • Increment: j++j = 7.
      • Iteration 4:
        • Check: i = 6 <= 5 (false).
        • Loop exits.
  1. Final Value: a = 27.
  2. Output: printf("%d", a) prints 27.

Output:

27

Justification:

The loop computes a by accumulating i * j, where i is updated to the post-incremented j each iteration. The sequence of (i, j) pairs contributing to a is (1, 1), (2, 3), and (4, 5), summing to 1 + 6 + 20 = 27.

iv)

main()
{
    int x = 5, y = 12;
    int *p = &x, *q = &y, *r;
    r = p;
    p = q;
    q = r;
    printf("%d, %d", x, y);
}

Analysis:

  • There’s a syntax error: q}=r should be q = r;. I’ll assume this is a typo.
  1. Initial Setup:
    • x = 5, y = 12.
    • p = &x (pointer p points to x’s address).
    • q = &y (pointer q points to y’s address).
    • r is an uninitialized pointer.
  2. Pointer Assignments:
    • r = pr now points to x’s address (&x).
    • p = qp now points to y’s address (&y).
    • q = rq now points to x’s address (&x).
  3. Key Point: No dereferencing (e.g., *p = ...) occurs, so the values of x and y remain unchanged.
  4. Output: printf("%d, %d", x, y) prints x = 5, y = 12.

Output:

5, 12

Justification:

The code manipulates pointers (p, q, r), but only their addresses are swapped, not the values they point to. Thus, x and y retain their original values.

v)

func(int x)
{
    static int a = 0;
    a += x;
    return(a);
}
main()
{
    int p;
    for(p = 1; p <= 5; ++p)
        printf("%d", func(p));
}

Analysis:

  • Assume func is implicitly int func(int x) (return type omitted, which is allowed in old C but not modern standards).
  • The static int a retains its value across calls to func.
  1. Initial State: a = 0 (initialized only once).
  2. Loop Execution:
    • p = 1: func(1)a = 0 + 1 = 1, returns 1, prints 1.
    • p = 2: func(2)a = 1 + 2 = 3, returns 3, prints 3.
    • p = 3: func(3)a = 3 + 3 = 6, returns 6, prints 6.
    • p = 4: func(4)a = 6 + 4 = 10, returns 10, prints 10.
    • p = 5: func(5)a = 10 + 5 = 15, returns 15, prints 15.
  3. Output: printf("%d", func(p)) prints each return value without separators (e.g., no space or newline).

Output:

1361015

Justification:

The static variable a accumulates the input x across calls, acting as a running sum: 1, 1+2, 1+2+3, 1+2+3+4, 1+2+3+4+5. The printf outputs these values consecutively.

Final Outputs:

  1. 0
  2. 3
  3. 27
  4. 5, 12
  5. 1361015

Scroll to Top
Scroll to Top