Skip to main content

Chapter 9

Structured Programming (old course)

TU BIM past exam questions

Past questions and answers

265 questions set from this chapter, 2 of them more than once. Most repeated first.

  • Asked 2 times
  • 2009 AD · 1 mark
  • 2017 AD · 1 mark

What is function?

Answer

A function is a named, self-contained block of code that performs a specific task. It may take arguments and return a value, and it can be called many times, which avoids repeating code and makes a program modular. Example: int add(int a, int b) { return a + b; }.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • Asked 2 times
  • 2002 AD
  • 2003 AD

C programs are converted into machine language with the help of
(a) An interpreter (b) A compiler (c) An operating system (d) None of the above

Answer

(b) A compiler

A compiler translates the whole C source program into machine language.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2001 AD · 5 marks

Explain the program given below written in C programming language and find the final output.
1 # include <conio.h>
2. # include <stdio.h>
3. void main ()
4. {
5.  int x = 50, y=10, a,b;
6.  clrscr();
7.  y = x++;
8.  x = y++ ;
9.  print f ("\n The value of x = %d, y =%d, z = %d",x,y,);
10. a = ++x;
11. b = y++ ;
12. print f ("\n The value of a =% d",b)
13. print f ("\n The value of b =% d",a+2);
14. getch() ;
15. }

Similar questions: Explain the program given below written in (2002 AD)

Answer

Explanation

The program declares x = 50, y = 10 and two more integers a, b, clears the screen, and then uses post-increment and pre-increment operators. (As printed, line 9 has z without a value and print f has a space, so the code would not compile; the intended printf is assumed, and z is not set, so only x and y are meaningful.)

Step by step:

  • Line 7: y = x++; assigns the old value of x to y, then increments x: y = 50, x = 51.
  • Line 8: x = y++; assigns the old y to x, then increments y: x = 50, y = 51.
  • Line 9: prints x = 50, y = 51 (z has no value).
  • Line 10: a = ++x; increments x first: x = 51, a = 51.
  • Line 11: b = y++; b gets the old y = 51, then y = 52.
  • Line 12: prints "The value of a" using b, so it prints 51.
  • Line 13: prints "The value of b" using a+2, so it prints 53.

Final output

The value of x = 50, y = 51, z = (garbage/undefined)
The value of a = 51
The value of b = 53

The core results are x = 50 and y = 51 on the first line, then 51 and 53.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD · 5 marks

Explain the program given below written in C programming language and find the final output.
# include <stdio.h>
# include <conio.h>
void main ()
{
  int x = 10, y, z;
  clrscr();
  y = --x ;
  z = x++ ;
  print f ("\n The value of x = %d, y =%d, z = %d",x,y,z);
  x = x+y+z;
  y = z++ ;
  print f ("\n The value of y =% d",x)
  print f ("\n The value of y =% d",y);
  print f ("\n The value of y =% d",z);
  if (y>z)
     print f ("\n The value of y is greater than z");
  else
     print f("\n The value of z is greater than y");
  getch() ;
}

Similar questions: Explain the program given below written in (2001 AD)

Answer

Explanation (ignoring the typing errors print f, the missing ; after the second printf, and the non-standard clrscr()/getch()):

  1. x = 10; y = --x decrements first, so x=9x = 9, y=9y = 9.
  2. z = x++ assigns first and then increments, so z=9z = 9, x=10x = 10.
  3. The first printf prints x=10x = 10, y=9y = 9, z=9z = 9.
  4. x = x + y + z gives x=10+9+9=28x = 10 + 9 + 9 = 28.
  5. y = z++ gives y=9y = 9, then z=10z = 10.
  6. The next three printf statements print xx (28), yy (9) and zz (10). The text says "value of y" each time, but the first one prints x.
  7. y > z is 9>109 > 10, which is false, so the else part runs.

Output:

The value of x = 10, y =9, z = 9
The value of y =28
The value of y =9
The value of y =10
The value of z is greater than y

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2012 AD · 1 mark

How can we return more than one value from a function?

Similar questions: Can a function return more than one (2013 AD)

Answer

A C function can return only one value through return. To get more than one value back we can:

  • pass pointers (addresses) as arguments, so the function changes the caller's variables directly (call by reference);
  • return a structure containing several members;
  • return a pointer to an array; or
  • use global variables.

Example: void calc(int a, int b, int *sum, int *diff) { *sum = a + b; *diff = a - b; }

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD

A do-while loop is useful when we want that the statements within the loop must be executed
(a) only once (b) At least once (c) more than once (d) None of the above

Similar questions: A do-while loop is useful when we (2003 AD)

Answer

(b) At least once

A do-while loop tests its condition after the body, so the body executes at least once.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2003 AD

A do-while loop is useful when we want that the statements within the loop must be executed
(a) only once (b) atleast once (c) More than once (d) one of the above

Similar questions: A do-while loop is useful when we (2002 AD)

Answer

(b) atleast once

The condition of a do-while loop is tested at the end, so the body executes at least once even if the condition is false.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2013 AD · 1 mark

Can a function return more than one value?

Similar questions: How can we return more than one (2012 AD)

Answer

No, a return statement can return only one value directly. More than one result can be sent back indirectly by using pointer (call by reference) parameters, arrays, or by returning a structure.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2013 AD · 2 marks

Differentiate between function call by reference and call by value?

Similar questions: What is a function? Differentiate between function (2016 AD)

Answer

Call by valueCall by reference
Copies of the actual arguments are passedAddresses of the actual arguments are passed
Changes in the function do not affect the original variablesChanges in the function affect the original variables
Uses ordinary parameters: swap(int a, int b)Uses pointer parameters: swap(int *a, int *b)
More memory used for copiesMemory efficient for large data

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 5 marks

What is a function? Differentiate between function call by value and call by reference in detail with example

Similar questions: Differentiate between function call by reference and (2013 AD)

Answer

A function is a self-contained block of statements that performs a specific task and can be called from other parts of the program. It gives modularity, reusability and easier debugging. Every C program has at least one function, main().

return_type function_name(parameter_list)
{
    /* body */
}

Call by value vs call by reference

Call by valueCall by reference
A copy of the actual argument is passed.The address of the actual argument is passed.
Formal parameters are ordinary variables.Formal parameters are pointers.
Changes inside the function do not affect the caller's variables.Changes made through the pointer change the caller's variables.
Needs more memory for large data copies.Saves memory, as only the address is copied.
Call: swap(a, b);Call: swap(&a, &b);

Example (call by value)

void swap(int x, int y)
{
    int t = x; x = y; y = t;
}
/* main: a=5, b=10; swap(a,b); still prints a=5, b=10 */

Example (call by reference)

void swap(int *x, int *y)
{
    int t = *x; *x = *y; *y = t;
}
/* main: a=5, b=10; swap(&a,&b); now a=10, b=5 */

In the first case only the copies x and y are swapped. In the second the original variables are swapped because their addresses were passed.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2001 AD · 2 marks

Define the flow chart and describe its geometric Symbols.

Answer

A flowchart is a diagram that shows the sequence of steps and decisions of an algorithm or program using standard geometric symbols connected by arrows.

SymbolNameUse
Oval (rounded rectangle)TerminalStart and end of the flowchart
ParallelogramInput/OutputReading data or displaying results
RectangleProcessCalculation or assignment
DiamondDecisionCondition with Yes/No (True/False) branches
ArrowFlow lineDirection of flow
Small circleConnectorJoins parts of a flowchart, on the same or another page

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2001 AD · 8 marks

Write the program
a) To find the factorial of a given number using recursive concept. b) To convert the upper case character to Lower case without using to lower ()

Answer

(a) Factorial using recursion

#include <stdio.h>

long fact(int n)
{
    if (n <= 1)
        return 1;
    return n * fact(n - 1);
}

int main()
{
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    if (n < 0)
        printf("Factorial is not defined for negative numbers\n");
    else
        printf("Factorial of %d = %ld\n", n, fact(n));
    return 0;
}

(b) Convert upper case to lower case without tolower()

In ASCII, a lower case letter is 32 greater than the matching upper case letter ('A' = 65, 'a' = 97).

#include <stdio.h>

int main()
{
    char ch;
    printf("Enter an upper case character: ");
    scanf("%c", &ch);
    if (ch >= 'A' && ch <= 'Z')
        ch = ch + 32;      /* or ch = ch - 'A' + 'a' */
    printf("Lower case: %c\n", ch);
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2001 AD · 5 marks

Write a program to generate prime numbers upto given numbers using user defined function. The value of n should be passed as argument.
Hint: if the user inputs the value of n as 10 then the output should be 1 2 3 5 7

Answer

#include <stdio.h>

int isPrime(int num)
{
    int i;
    if (num < 2)
        return 0;
    for (i = 2; i * i <= num; i++)
        if (num % i == 0)
            return 0;
    return 1;
}

void generatePrime(int n)
{
    int i;
    printf("1 ");               /* the question's sample output includes 1 */
    for (i = 2; i <= n; i++)
        if (isPrime(i))
            printf("%d ", i);
}

int main()
{
    int n;
    printf("Enter n: ");
    scanf("%d", &n);
    generatePrime(n);           /* n is passed as an argument */
    return 0;
}

For n = 10 the output is 1 2 3 5 7. (Strictly, 1 is not a prime number, but it is printed here to match the sample output in the question.)

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2001 AD · 5 marks

Write a program to generate first n numbers in Fibonacci series. (Hint: Fibonacci series 1 1 2 3 5 8 13........)

Answer

#include <stdio.h>

int main()
{
    int n, i;
    long a = 1, b = 1, next;

    printf("How many terms? ");
    scanf("%d", &n);

    for (i = 1; i <= n; i++)
    {
        printf("%ld ", a);
        next = a + b;
        a = b;
        b = next;
    }
    return 0;
}

For n = 7 the output is 1 1 2 3 5 8 13.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2001 AD · 5 marks

Explain any 5 keywords available in C with examples.

Answer

Keywords are reserved words with a fixed meaning in C that cannot be used as identifiers (variable names).

  1. int declares an integer variable.
    int age = 20;
    
  2. if (with else) executes a block only when a condition is true.
    if (marks >= 40) printf("Pass"); else printf("Fail");
    
  3. for repeats a block a known number of times.
    for (i = 1; i <= 5; i++) printf("%d ", i);
    
  4. return ends a function and optionally sends a value back to the caller.
    int square(int x) { return x * x; }
    
  5. break leaves a loop or switch immediately.
    while (1) { if (x > 10) break; x++; }
    

Other keywords include float, char, while, switch, struct, void and static.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2001 AD · 5 marks

Write a program to get the details of 5 students using structure. The following details should be read from the keyboard.
Student Name, Roll No., Total mark.
And display the details of all students.

Answer

#include <stdio.h>

struct student
{
    char name[50];
    int roll;
    float total;
};

int main()
{
    struct student s[5];
    int i;

    for (i = 0; i < 5; i++)
    {
        printf("Enter details of student %d\n", i + 1);
        printf("Name: ");
        scanf(" %[^\n]", s[i].name);
        printf("Roll No.: ");
        scanf("%d", &s[i].roll);
        printf("Total mark: ");
        scanf("%f", &s[i].total);
    }

    printf("\nName\t\tRoll No.\tTotal Mark\n");
    for (i = 0; i < 5; i++)
        printf("%-15s\t%d\t\t%.2f\n", s[i].name, s[i].roll, s[i].total);

    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD · 2 marks

Why pointers are used?

Answer

A pointer is a variable that stores the memory address of another variable. Pointers are used to:

  • Access and modify variables indirectly, and pass arguments by reference so that a function can change the caller's variables (for example swap).
  • Allocate and free memory dynamically at run time (malloc, free).
  • Handle arrays and strings efficiently, using pointer arithmetic.
  • Build dynamic data structures such as linked lists, stacks, queues and trees.
  • Return more than one value from a function.
  • Save memory and time by passing addresses instead of copying large data.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD · 8 marks

Write a menu driven program to calculate following options:
a) Factorial of a number b) Odd or Even c) Area of circle d) Exit

Answer

#include <stdio.h>
#define PI 3.14159

int main() {
    int ch, n, i;
    long fact;
    float r;
    do {
        printf("\n1. Factorial of a number\n2. Odd or Even\n3. Area of circle\n4. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &ch);
        switch (ch) {
            case 1:
                printf("Enter a number: ");
                scanf("%d", &n);
                fact = 1;
                for (i = 1; i <= n; i++)
                    fact *= i;
                printf("Factorial = %ld\n", fact);
                break;
            case 2:
                printf("Enter a number: ");
                scanf("%d", &n);
                if (n % 2 == 0)
                    printf("%d is even\n", n);
                else
                    printf("%d is odd\n", n);
                break;
            case 3:
                printf("Enter radius: ");
                scanf("%f", &r);
                printf("Area = %.2f\n", PI * r * r);
                break;
            case 4:
                printf("Exiting...\n");
                break;
            default:
                printf("Invalid choice\n");
        }
    } while (ch != 4);
    return 0;
}

The do-while loop keeps showing the menu until the user chooses option 4, and switch selects the operation.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD · 5 marks

How values can be passed to a function, explain with suitable example.

Answer

Values can be passed to a function in two ways.

(a) Call by value: the values of the actual arguments are copied into the formal parameters. Changes made inside the function do not affect the original variables.

#include <stdio.h>
void change(int x) { x = x + 10; }
int main() {
    int a = 5;
    change(a);
    printf("%d", a);   /* prints 5 */
    return 0;
}

(b) Call by reference (address): the addresses of the variables are passed using &, and the function receives them in pointer parameters. Changes made through the pointer affect the original variables.

#include <stdio.h>
void change(int *x) { *x = *x + 10; }
int main() {
    int a = 5;
    change(&a);
    printf("%d", a);   /* prints 15 */
    return 0;
}

Arrays are passed by passing the address of the first element, so the function can modify the original array.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD · 5 marks

Write a program to add first 7 terms of the following series.
1/1! + 2/2! + 3/3! + ........

Answer

The ii-th term is ii!\dfrac{i}{i!}, so the sum of the first 7 terms is

S=∑i=17ii!S = \sum_{i=1}^{7}\frac{i}{i!}

#include <stdio.h>

int main() {
    int i;
    float fact = 1, sum = 0;
    for (i = 1; i <= 7; i++) {
        fact = fact * i;          /* i! */
        sum = sum + i / fact;
    }
    printf("Sum of first 7 terms = %f\n", sum);
    return 0;
}

Here fact is updated incrementally to hold i!i! in each pass, and float is used so the division is not integer division. The sum is about 2.71812.7181.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD · 5 marks

Write a program that finds out whether user entered number is positive, negative of zero using function.

Answer

#include <stdio.h>

void check(int n) {
    if (n > 0)
        printf("%d is positive\n", n);
    else if (n < 0)
        printf("%d is negative\n", n);
    else
        printf("The number is zero\n");
}

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    check(num);
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD · 5 marks

Write a program to add two nxn matrices.

Answer

#include <stdio.h>

int main() {
    int a[10][10], b[10][10], c[10][10], n, i, j;
    printf("Enter order n (n <= 10): ");
    scanf("%d", &n);

    printf("Enter elements of first matrix:\n");
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
            scanf("%d", &a[i][j]);

    printf("Enter elements of second matrix:\n");
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
            scanf("%d", &b[i][j]);

    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
            c[i][j] = a[i][j] + b[i][j];

    printf("Sum of the matrices:\n");
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++)
            printf("%d\t", c[i][j]);
        printf("\n");
    }
    return 0;
}

Each element of the result is cij=aij+bijc_{ij} = a_{ij} + b_{ij}.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2004 AD · 2 marks

Define pointers with examples. What are the advantages of using pointers?

Answer

A pointer is a variable that stores the memory address of another variable. It is declared with *, for example int *p;. The address is obtained with & and the value at the address with * (dereferencing).

int a = 10;
int *p = &a;
printf("%d", *p);   /* prints 10 */
*p = 20;            /* a becomes 20 */

Advantages:

  • Allow call by reference, so a function can change the caller's variables.
  • Allow dynamic memory allocation (malloc, calloc).
  • Make array and string handling efficient, and a function can return more than one value.
  • Allow data structures such as linked lists and trees.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2004 AD · 8 marks

Write a menu driven program to perform following operations:
a. Find whether user inputted number is prime or not. b. Display cube of first n integers. c. Find whether the user inputted number is between 10 and 100 or not. d. Exit.

Answer

#include <stdio.h>

int main() {
    int ch, n, i, flag;
    do {
        printf("\n1. Prime or not\n2. Cubes of first n integers\n3. Between 10 and 100?\n4. Exit\n");
        printf("Enter choice: ");
        scanf("%d", &ch);
        switch (ch) {
            case 1:
                printf("Enter a number: ");
                scanf("%d", &n);
                flag = (n > 1);
                for (i = 2; i * i <= n; i++)
                    if (n % i == 0) { flag = 0; break; }
                if (flag)
                    printf("%d is prime\n", n);
                else
                    printf("%d is not prime\n", n);
                break;
            case 2:
                printf("Enter n: ");
                scanf("%d", &n);
                for (i = 1; i <= n; i++)
                    printf("Cube of %d = %d\n", i, i * i * i);
                break;
            case 3:
                printf("Enter a number: ");
                scanf("%d", &n);
                if (n > 10 && n < 100)
                    printf("%d is between 10 and 100\n", n);
                else
                    printf("%d is not between 10 and 100\n", n);
                break;
            case 4:
                break;
            default:
                printf("Invalid choice\n");
        }
    } while (ch != 4);
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2004 AD · 5 marks

Differentiate between call by values and call by addresses with example of each while passing values to function.

Answer

Call by valueCall by address (reference)
Copies of the actual arguments are passedAddresses of the actual arguments are passed
Formal parameters are ordinary variablesFormal parameters are pointers
Changes in the function do not affect the caller's variablesChanges in the function change the caller's variables
Needs extra memory for the copiesNeeds no copy of the data, so it is efficient for large data
Called as swap(a, b)Called as swap(&a, &b)

Call by value:

void swap(int x, int y) { int t = x; x = y; y = t; }
/* main: a = 1, b = 2; swap(a, b); a and b remain 1 and 2 */

Call by address:

void swap(int *x, int *y) { int t = *x; *x = *y; *y = t; }
/* main: a = 1, b = 2; swap(&a, &b); now a = 2, b = 1 */

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2004 AD · 5 marks

Write a program to calculate the factorial of integer from 4 to 7.

Answer

#include <stdio.h>

int main() {
    int n, i;
    long fact;
    for (n = 4; n <= 7; n++) {
        fact = 1;
        for (i = 1; i <= n; i++)
            fact = fact * i;
        printf("Factorial of %d = %ld\n", n, fact);
    }
    return 0;
}

Output:

Factorial of 4 = 24
Factorial of 5 = 120
Factorial of 6 = 720
Factorial of 7 = 5040

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2004 AD · 5 marks

Write a program to enter name of 10 students and display name of 5th student in reverse order.

Answer

#include <stdio.h>
#include <string.h>

int main() {
    char name[10][30];
    int i, len;
    for (i = 0; i < 10; i++) {
        printf("Enter name of student %d: ", i + 1);
        scanf("%29s", name[i]);
    }
    len = strlen(name[4]);          /* 5th student has index 4 */
    printf("5th student in reverse: ");
    for (i = len - 1; i >= 0; i--)
        printf("%c", name[4][i]);
    printf("\n");
    return 0;
}

A two-dimensional character array holds 10 names, and the 5th name is name[4] because indexing starts at 0.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2004 AD · 5 marks

Write a program to read a line of text, and determine the number of alphabets and digits it contains.

Answer

#include <stdio.h>
#include <ctype.h>

int main() {
    char line[200];
    int i, alpha = 0, digit = 0;
    printf("Enter a line of text: ");
    fgets(line, sizeof(line), stdin);
    for (i = 0; line[i] != '\0'; i++) {
        if (isalpha(line[i]))
            alpha++;
        else if (isdigit(line[i]))
            digit++;
    }
    printf("Alphabets = %d\nDigits = %d\n", alpha, digit);
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2004 AD · 5 marks

Explain the program given below written in C programming language and find the final output.
#include<stdio.h>
#include<conio.h>
void func(char *);
void print();
main()
{
  char ch8a[]="TRIBHUVAN UNIVERSITY";
  int count=0;
  clrscr();
  print();
  func(ch8a);
  print();
  getch();
}
void func(char *s)
{
  for(int i;i<strlen(s);i++)
  {
     printf("%c",s[i]);
  }
}
void print()
{
  int i;
  printf("\n");
  for(i=0;i<20;i++)
  printf("**");
  printf("\n");
}

Answer

Explanation:

  • char * parameter: func receives the address of the string ch8a.
  • print() prints a newline, then * 40 times (the loop runs 20 times and prints ** each time), then another newline. It acts as a separator line.
  • func(char *s) prints the string one character at a time with a loop over strlen(s). The loop variable i is not initialised (it should be i = 0), and strlen needs <string.h>. Assuming the intended i = 0, the whole string is printed.
  • main() calls print(), then func(ch8a), then print() again. The variable count is unused.

Output:


****************************************
TRIBHUVAN UNIVERSITY
****************************************

That is, a blank line, a line of 40 asterisks, the text TRIBHUVAN UNIVERSITY, and another line of 40 asterisks.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2007 AD · 1 mark

What is the difference between an identifier and a keyword?

Answer

An identifier is a name chosen by the programmer for a variable, function, array and so on (for example sum, main1). A keyword is a reserved word with a fixed meaning to the compiler (for example int, if, while) and cannot be used as an identifier.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2007 AD · 1 mark

If an array is passed to a function and several of its elements are altered within the function, are these changes recognized in the calling portion of the program?

Answer

Yes. An array is passed by passing the address of its first element, so the function works on the original array. Changes made to its elements inside the function are therefore visible in the calling part of the program.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2007 AD · 1 mark

What happens if we use the address-of ( & ) operator on a pointer variable?

Answer

Applying & to a pointer variable gives the address of the pointer itself (not the address it stores). The result is a pointer to a pointer, for example int **pp = &p;.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2007 AD · 1 mark

Why do we use multiple indirection operator?

Answer

Multiple indirection (**, ***) is used with pointers to pointers. It lets us reach a value through more than one level of address, for example **pp gives the value pointed to by *pp. It is used for dynamic arrays of strings, 2-D arrays and for changing a pointer inside a function.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2007 AD · 1 mark

Explain how the if-else ladder differs from a switch-case statement.

Answer

An if-else ladder can test any relational or logical condition, including ranges and floating-point values, and conditions are checked one after another. A switch-case compares one integer or character expression with constant case values only, and jumps directly to the matching case, so it is cleaner and usually faster for many fixed choices. In switch, break is needed to stop fall-through.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2007 AD · 1 mark

What is the use of preprocessor directive #include<stdio.h> in a C program?

Answer

#include<stdio.h> tells the preprocessor to insert the contents of the standard input/output header file before compilation. It provides the declarations of functions such as printf() and scanf() that the program uses.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2007 AD · 1 mark

What is a string? How do you know its length?

Answer

A string is a sequence of characters stored in a character array and terminated by the null character '\0', for example char s[] = "Nepal";.

Its length is found with the strlen() function from <string.h> (for example strlen(s) gives 5), or by counting characters until '\0' is reached.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2007 AD · 1 mark

What is the difference between literal constant and symbolic constant?

Answer

A literal constant is a fixed value written directly in the code, such as 3.14, 10 or 'A'. A symbolic constant is a name given to a constant, defined with #define PI 3.14 or const float pi = 3.14;. Symbolic constants make a program easier to read and to modify, since a value is changed in one place.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2007 AD · 1 mark

Why do you need a linker?

Answer

A linker combines the object code of the program with the object code of library functions (such as printf) and other modules, and resolves the references between them. This produces a single executable file that can be run.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2007 AD · 1 mark

Differentiate between static and automatic variable in C.

Answer

Automatic variableStatic variable
Declared with auto (the default for local variables)Declared with static
Created on entering the block, destroyed on leaving itExists for the whole program run
Not initialised automatically (holds garbage)Initialised to zero by default
Value is lost between function callsKeeps its value between function calls

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2007 AD · 2 marks

What is ternary operator? Explain with an example.

Answer

The ternary (conditional) operator ?: is the only C operator with three operands. Syntax:

condition ? expression1 : expression2;

If the condition is true, expression1 is evaluated, otherwise expression2. Example:

max = (a > b) ? a : b;

If a = 5 and b = 8, then max becomes 8. It is a short form of an if-else statement.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2007 AD · 8 marks

Write a menu driven program which performs following jobs.
a. Create a structure called employee having 3 variables: emp_no, name and salary. b. Enter 10 variables of type employee structure into a file name "c:\empinfo.txt" in binary format. c. Again read the same information from the file and print the information on the screen d. Exit.

Answer

#include <stdio.h>

struct employee {
    int emp_no;
    char name[30];
    float salary;
};

int main() {
    struct employee e;
    FILE *fp;
    int ch, i;
    do {
        printf("\n1. Create structure and write 10 records to file\n");
        printf("2. Read records from file and display\n3. Exit\nEnter choice: ");
        scanf("%d", &ch);
        switch (ch) {
            case 1:
                fp = fopen("c:\\empinfo.txt", "wb");
                if (fp == NULL) { printf("Cannot open file\n"); break; }
                for (i = 0; i < 10; i++) {
                    printf("Enter emp_no, name, salary: ");
                    scanf("%d %29s %f", &e.emp_no, e.name, &e.salary);
                    fwrite(&e, sizeof(e), 1, fp);
                }
                fclose(fp);
                break;
            case 2:
                fp = fopen("c:\\empinfo.txt", "rb");
                if (fp == NULL) { printf("Cannot open file\n"); break; }
                while (fread(&e, sizeof(e), 1, fp) == 1)
                    printf("%d\t%s\t%.2f\n", e.emp_no, e.name, e.salary);
                fclose(fp);
                break;
            case 3:
                break;
            default:
                printf("Invalid choice\n");
        }
    } while (ch != 3);
    return 0;
}

The structure employee is defined at the top (part a). Option 1 writes 10 records in binary with fwrite (part b), and option 2 reads them back with fread and displays them (part c). Option 3 exits (part d).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2007 AD · 5 marks

Write a program to read a list of strings and print them in ascending order.

Answer

#include <stdio.h>
#include <string.h>

int main() {
    char s[50][30], temp[30];
    int n, i, j;
    printf("How many strings? ");
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        printf("Enter string %d: ", i + 1);
        scanf("%29s", s[i]);
    }
    for (i = 0; i < n - 1; i++)
        for (j = i + 1; j < n; j++)
            if (strcmp(s[i], s[j]) > 0) {
                strcpy(temp, s[i]);
                strcpy(s[i], s[j]);
                strcpy(s[j], temp);
            }
    printf("Strings in ascending order:\n");
    for (i = 0; i < n; i++)
        printf("%s\n", s[i]);
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2007 AD · 5 marks

Write a program to find the sum of given series: x+x33!+x55!+⋯+n termsx + \dfrac{x^3}{3!} + \dfrac{x^5}{5!} + \dots + n \text{ terms}.

Answer

The kk-th term is x2k−1(2k−1)!\dfrac{x^{2k-1}}{(2k-1)!}. Each term is obtained from the previous one by

tk=tk−1⋅x2(2k−2)(2k−1)t_k = t_{k-1}\cdot\frac{x^2}{(2k-2)(2k-1)}

with t1=xt_1 = x.

#include <stdio.h>

int main() {
    int n, k;
    float x, term, sum;
    printf("Enter x and number of terms: ");
    scanf("%f %d", &x, &n);
    term = x;
    sum = x;
    for (k = 2; k <= n; k++) {
        term = term * x * x / ((2 * k - 2) * (2 * k - 1));
        sum = sum + term;
    }
    printf("Sum = %f\n", sum);
    return 0;
}

This series is the expansion of sinh⁡x\sinh x.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2007 AD · 5 marks

Write a program to read any two integer number n1 and n2 and develop a function that takes these two numbers as argument and print all the even integer numbers from n1 to n2. Also write a main routine to test the function.

Answer

#include <stdio.h>

void printEven(int n1, int n2) {
    int i;
    for (i = n1; i <= n2; i++)
        if (i % 2 == 0)
            printf("%d ", i);
    printf("\n");
}

int main() {
    int n1, n2;
    printf("Enter two integers n1 and n2: ");
    scanf("%d %d", &n1, &n2);
    printf("Even numbers from %d to %d:\n", n1, n2);
    printEven(n1, n2);
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2007 AD · 5 marks

Write a program using dynamically allocated array and find the average of the entered values to that array.

Answer

#include <stdio.h>
#include <stdlib.h>

int main() {
    int n, i;
    float *a, sum = 0;
    printf("How many values? ");
    scanf("%d", &n);
    a = (float *)malloc(n * sizeof(float));
    if (a == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }
    for (i = 0; i < n; i++) {
        printf("Enter value %d: ", i + 1);
        scanf("%f", &a[i]);
        sum += a[i];
    }
    printf("Average = %.2f\n", sum / n);
    free(a);
    return 0;
}

malloc reserves space for nn values at run time, and free releases it at the end.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2007 AD · 5 marks

Explain the following program and write the output when program is run.
#include <stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>

void main ()
{
  int ch;
  int arr[10];
  char str[10];
  clrscr();
  do{
    printf( "n1.Enter list numbers");
    printf( "n2.Enter word");
    printf( "n3.Exit");
    printf( "\nEnter choice:");
    scanf("%d",&ch);
    switch(ch){
      case1:
        printf("nEnter Numbers:');
        for(ch=0;ch<10;ch++)
          scanf("%d",&arr[ch]);
        for(ch=0;<10;ch++)
          printf(:%d\t",arr[ch]);
        break;
      case2:
        printf("\nEntera word:');
        scanf("%s",str);
        break;
      case3:
        exit(0);
        break;
    }
  }while(1);
}

Answer

Explanation: the program is meant to be a menu-driven program. It declares an integer ch for the choice, an integer array arr[10] and a string str[10]. Inside a do-while(1) loop it shows a menu and reads the choice with scanf:

  • Case 1: reads 10 integers into arr and then prints them separated by tabs.
  • Case 2: reads a word into str.
  • Case 3: calls exit(0) and ends the program.

The loop repeats forever until option 3 is chosen. (ch is also reused as the loop counter in case 1.)

Output: as written, the program does not compile because of errors: case1: should be case 1: (same for the others), the quote marks are mismatched ("nEnter Numbers:' and "\nEntera word:'), for(ch=0;<10;ch++) is missing the condition variable (ch<10), and printf(:%d\t",arr[ch]) has : instead of ".

If corrected, the first screen is (the \n in the first three printf calls is written as n, so those words run together):

n1.Enter list numbersn2.Enter wordn3.Exit
Enter choice:

Choosing 1 prompts Enter Numbers: and echoes the 10 numbers entered, tab separated; choosing 2 reads a word; choosing 3 ends the program.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2008 AD · 1 mark

What do you mean by variable?

Answer

A variable is a named memory location that stores a value which can change during program execution. It must be declared with a data type, for example int age;.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2008 AD · 1 mark

Write syntax of switch case statement.

Answer

switch (expression) {
    case constant1:
        statements;
        break;
    case constant2:
        statements;
        break;
    default:
        statements;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2008 AD · 1 mark

What is static variable?

Answer

A static variable is declared with the keyword static. It is initialised only once (to zero by default) and keeps its value between function calls for the whole program run, although its scope stays local to the function.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2008 AD · 1 mark

Why header files included in C are file?

Answer

Header files (with extension .h) are included to give the program the declarations of library functions, macros and constants, for example stdio.h for printf and scanf. They let us reuse standard code without writing it again.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2008 AD · 1 mark

Which character terminates a string?

Answer

The null character '\0' (ASCII value 0) terminates a string.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2008 AD · 1 mark

What is the use of address of (&) operator?

Answer

The address-of operator & gives the memory address of a variable. It is used in scanf("%d", &x) and to store addresses in pointers (p = &x;) or pass addresses to functions.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2008 AD · 1 mark

Which conversion specifies is used for string?

Answer

%s is the conversion specifier used for strings.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2008 AD · 1 mark

Which function is used to take real number as a input from user?

Answer

The scanf() function with %f (for float) or %lf (for double) is used to read a real number, for example scanf("%f", &x);.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2008 AD · 1 mark

What is global variable?

Answer

A global variable is declared outside all functions, usually at the top of the program. It is visible to all functions after its declaration, exists for the whole program run, and is initialised to zero by default.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2008 AD · 1 mark

What is structure variable?

Answer

A structure variable is a variable of a structure type, which holds all the members of the structure together. For example, with struct student { int roll; char name[20]; };, the declaration struct student s1; creates the structure variable s1, whose members are accessed as s1.roll.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2008 AD · 2 marks

Define unary operator.
If int x = 2, then (++x) + (++x) = ?

Answer

A unary operator is an operator that works on only one operand. Examples are unary minus (-x), logical NOT (!x), increment (++x, x++), decrement (--x, x--), address-of (&x), dereference (*p) and sizeof.

Value of (++x) + (++x) for x = 2: the expression modifies x twice without a sequence point, so the C standard calls it undefined behaviour. With most compilers (for example Turbo C and gcc) both increments are done first, so x becomes 4 and the sum is 4+4=84 + 4 = 8.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2008 AD · 8 marks

Write a menu driven program which perform following jobs.
I. Create a structure named student with member variables: roll, name, grade, gender, Age. II. Define ten structure variables of type student and using loop take input for all ten structure variables. III. Display names of all male students. IV. Exit.

Answer

#include <stdio.h>
#include <string.h>

struct student {
    int roll;
    char name[30];
    char grade[3];
    char gender;      /* 'M' or 'F' */
    int age;
};

int main()
{
    struct student s[10];
    int i, choice, created = 0;

    do {
        printf("\n--- MENU ---\n");
        printf("1. Input data of ten students\n");
        printf("2. Display names of all male students\n");
        printf("3. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
        case 1:
            for (i = 0; i < 10; i++) {
                printf("\nStudent %d\n", i + 1);
                printf("Roll: ");
                scanf("%d", &s[i].roll);
                printf("Name: ");
                scanf("%s", s[i].name);
                printf("Grade: ");
                scanf("%s", s[i].grade);
                printf("Gender (M/F): ");
                scanf(" %c", &s[i].gender);
                printf("Age: ");
                scanf("%d", &s[i].age);
            }
            created = 1;
            break;
        case 2:
            if (!created) {
                printf("Enter data first (option 1).\n");
                break;
            }
            printf("Male students:\n");
            for (i = 0; i < 10; i++)
                if (s[i].gender == 'M' || s[i].gender == 'm')
                    printf("%s\n", s[i].name);
            break;
        case 3:
            printf("Exiting...\n");
            break;
        default:
            printf("Invalid choice\n");
        }
    } while (choice != 3);

    return 0;
}

Option 1 fills the ten structure variables in a loop, option 2 prints the names where gender is male, and option 3 ends the do-while loop.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2008 AD · 5 marks

Write a program to display data in following format and to show their sum
1
11  21
31  41  51
61  71  81  91

Answer

The numbers start at 1 and increase by 10, arranged in rows of 1, 2, 3 and 4 numbers. The values are 1, 11, 21, ..., 91 (ten numbers). The first row prints one number, the second two, and so on.

#include <stdio.h>

int main()
{
    int row, col, n = 1, sum = 0;

    for (row = 1; row <= 4; row++) {
        for (col = 1; col <= row; col++) {
            printf("%d  ", n);
            sum += n;
            n += 10;
        }
        printf("\n");
    }
    printf("Sum = %d\n", sum);
    return 0;
}

Output:

1
11  21
31  41  51
61  71  81  91
Sum = 460

Check: the numbers are 1,11,21,…,911, 11, 21, \dots, 91, an arithmetic series of 10 terms, so the sum is 102(1+91)=460\frac{10}{2}(1+91) = 460.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2008 AD · 5 marks

Write a program to sort following list of strings: ["jump", "walk", "red", "green", "talk", "move", "look", "feel"].

Answer

The program sorts the eight strings alphabetically with bubble sort, using strcmp to compare and strcpy to swap.

#include <stdio.h>
#include <string.h>

int main()
{
    char list[8][10] = {"jump", "walk", "red", "green",
                        "talk", "move", "look", "feel"};
    char temp[10];
    int i, j, n = 8;

    for (i = 0; i < n - 1; i++)
        for (j = 0; j < n - 1 - i; j++)
            if (strcmp(list[j], list[j + 1]) > 0) {
                strcpy(temp, list[j]);
                strcpy(list[j], list[j + 1]);
                strcpy(list[j + 1], temp);
            }

    printf("Sorted list:\n");
    for (i = 0; i < n; i++)
        printf("%s\n", list[i]);
    return 0;
}

Output: feel, green, jump, look, move, red, talk, walk (one per line).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2008 AD · 5 marks

Explain following program an write the output of program
#include<stdio.h>
int main()
{
  int m,n=9,o=1;
  int sum=0;
  for(m=7;m<12;++)
  {
    Sum = n+ o;
    --n;
    o = n * 2;
  }
  printf ("\nSum is :%d", sum);
  for(o=0;o<25;o=o+5)
  {
    sum = (n + 1) + n%2+n*4;
    printf("\nSum +%d",sum);
    sm = sum * 0;
  }
  Return 0;
}

Answer

The program has typing errors (++ should be ++m, Sum should be sum, sm should be sum, Return should be return). Assuming these are corrected, it works as follows.

First loop (m = 7 to 11, so 5 iterations). The variables start as n = 9, o = 1. In each pass, sum = n + o, then n is decremented and o = n * 2.

Passsum = n + on aftero after
19 + 1 = 10816
28 + 16 = 24714
37 + 14 = 21612
46 + 12 = 18510
55 + 10 = 1548

After the loop sum = 15, and the first printf prints Sum is :15.

Second loop (o = 0, 5, 10, 15, 20, so 5 iterations). Here n = 4, so each pass computes sum=(4+1)+(4 mod 2)+4×4=5+0+16=21sum = (4+1) + (4 \bmod 2) + 4 \times 4 = 5 + 0 + 16 = 21. The statement sum * 0 is assigned to another variable, so sum stays 21.

Output:

Sum is :15
Sum +21
Sum +21
Sum +21
Sum +21
Sum +21

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2008 AD · 5 marks

What is pointer? Write a function named addarrays that accepts two integer array that are of the same size.
The function should add each element in the arrays together and place the values in a third array. That is:
First element of 3rd array = First element of 1st array + First element of 2nd array Second element of 3rd array = Second element of 1st array + First element of 2nd array And so on.

Answer

A pointer is a variable that stores the memory address of another variable. It is declared with *, for example int *p;, and p = &x; makes it hold the address of x. The value at that address is reached with *p.

The question's wording is ambiguous: it says "Second element of 3rd array = Second element of 1st array + First element of 2nd array". This is most likely a typing slip, so the function below adds corresponding elements (element ii of the 1st array plus element ii of the 2nd array), using pointers.

#include <stdio.h>

void addarrays(int *a, int *b, int *c, int size)
{
    int i;
    for (i = 0; i < size; i++)
        *(c + i) = *(a + i) + *(b + i);
}

int main()
{
    int a[5] = {1, 2, 3, 4, 5};
    int b[5] = {10, 20, 30, 40, 50};
    int c[5], i;

    addarrays(a, b, c, 5);
    for (i = 0; i < 5; i++)
        printf("%d ", c[i]);
    return 0;
}

Output: 11 22 33 44 55

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2008 AD · 5 marks

Write a function that accepts a string and a character. The function should look for the first occurrence of the character in the string and return a pointer to that location.

Answer

#include <stdio.h>

char *findchar(char *str, char ch)
{
    while (*str != '\0') {
        if (*str == ch)
            return str;      /* pointer to first occurrence */
        str++;
    }
    return NULL;             /* character not found */
}

int main()
{
    char s[100], c;
    char *p;

    printf("Enter a string: ");
    scanf("%99s", s);
    printf("Enter a character: ");
    scanf(" %c", &c);

    p = findchar(s, c);
    if (p != NULL)
        printf("Found at position %d\n", (int)(p - s));
    else
        printf("Character not found\n");
    return 0;
}

The function walks through the string with a pointer and returns the address of the first match. If the end of the string is reached it returns NULL. The position in the string is found by pointer subtraction, p - s.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2009 AD · 1 mark

What is linker?

Answer

A linker is a system program that combines the object files produced by the compiler with library routines (such as printf) to make a single executable file.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2009 AD · 1 mark

Why is stdio.h file used in C-program?

Answer

stdio.h is the standard input/output header file. It contains the declarations of functions such as printf(), scanf(), fopen(), getchar() and puts(), so the compiler can check their use.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2009 AD · 1 mark

What is preprocessor in C?

Answer

The preprocessor is the part of the C compiler that runs before actual compilation. It processes lines beginning with # (such as #include and #define), includes header files and expands macros.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2009 AD · 1 mark

Define variable.

Answer

A variable is a named memory location used to store data that can change during program execution. Each variable has a data type, for example int age;.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2009 AD · 1 mark

Why is semicolon used in C-program?

Answer

The semicolon ; is the statement terminator in C. It tells the compiler where one statement ends and the next begins.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2009 AD · 1 mark

What do you mean by prototype?

Answer

A function prototype is a declaration of a function placed before its use. It tells the compiler the function's name, return type and parameter types, for example int add(int, int);, so calls can be checked.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2009 AD · 1 mark

What is use of "\n" in C program?

Answer

\n is the newline escape sequence. When printed, it moves the cursor to the beginning of the next line, for example printf("Hello\n");.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2009 AD · 1 mark

What is use of memcpy () function?

Answer

memcpy(dest, src, n) (declared in string.h) copies n bytes from the memory area src to the memory area dest. It works on any type of data, not only strings, and the two areas should not overlap.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2009 AD · 1 mark

What is pointer variable?

Answer

A pointer variable is a variable that holds the memory address of another variable instead of an ordinary value. Example: int *p = &x;.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2009 AD · 2 marks

What is Union? How is the size of union calculated?

Answer

A union is a user-defined data type, declared with the keyword union, whose members all share the same memory location. Only one member can hold a valid value at a time.

Size: the size of a union equals the size of its largest member (rounded up for alignment if needed), because all members overlap.

union data {
    int i;       /* 2 or 4 bytes */
    float f;     /* 4 bytes */
    char s[10];  /* 10 bytes */
};

Here sizeof(union data) is 10 bytes (12 on a compiler that pads to a multiple of 4), the size of s, whereas a structure with the same members would need the sum of all member sizes (with padding).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2009 AD · 8 marks

A student records consists of its Roll, name, address, grade.
Write a program to perform following operations.
a. Read the records of 15 students. b. Display the records of all students who are from grade 10. c. Store records of all 15 students in the file "student.txt". d. Read the contents of file "student.txt" and display it.

Answer

#include <stdio.h>
#include <stdlib.h>

struct student {
    int roll;
    char name[30];
    char address[30];
    int grade;
};

int main()
{
    struct student s[15], t;
    int i;
    FILE *fp;

    /* a. read records of 15 students */
    for (i = 0; i < 15; i++) {
        printf("\nStudent %d\n", i + 1);
        printf("Roll: ");
        scanf("%d", &s[i].roll);
        printf("Name: ");
        scanf("%s", s[i].name);
        printf("Address: ");
        scanf("%s", s[i].address);
        printf("Grade: ");
        scanf("%d", &s[i].grade);
    }

    /* b. display students of grade 10 */
    printf("\nStudents of grade 10:\n");
    for (i = 0; i < 15; i++)
        if (s[i].grade == 10)
            printf("%d %s %s %d\n", s[i].roll, s[i].name,
                   s[i].address, s[i].grade);

    /* c. store all records in student.txt */
    fp = fopen("student.txt", "w");
    if (fp == NULL) {
        printf("Cannot open file\n");
        exit(1);
    }
    for (i = 0; i < 15; i++)
        fprintf(fp, "%d %s %s %d\n", s[i].roll, s[i].name,
                s[i].address, s[i].grade);
    fclose(fp);

    /* d. read the file and display it */
    fp = fopen("student.txt", "r");
    if (fp == NULL) {
        printf("Cannot open file\n");
        exit(1);
    }
    printf("\nContents of student.txt:\n");
    while (fscanf(fp, "%d %s %s %d", &t.roll, t.name,
                  t.address, &t.grade) == 4)
        printf("%d %s %s %d\n", t.roll, t.name, t.address, t.grade);
    fclose(fp);

    return 0;
}

Names and addresses are read with %s, so they must not contain spaces.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2009 AD · 5 marks

Write a program to display following 1/2, 2/3, 3/4, 4/5,......, n-1/n.

Answer

The kk-th term is kk+1\frac{k}{k+1}, for k=1k = 1 to n−1n-1.

#include <stdio.h>

int main()
{
    int n, k;

    printf("Enter n: ");
    scanf("%d", &n);

    for (k = 1; k <= n - 1; k++) {
        printf("%d/%d", k, k + 1);
        if (k < n - 1)
            printf(", ");
    }
    printf("\n");
    return 0;
}

For n=5n = 5 the output is 1/2, 2/3, 3/4, 4/5.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2009 AD · 5 marks

Write a program that reads 10 integer numbers and displays sum, average and maximum of those numbers.

Answer

#include <stdio.h>

int main()
{
    int a[10], i, sum = 0, max;
    float avg;

    printf("Enter 10 integers:\n");
    for (i = 0; i < 10; i++)
        scanf("%d", &a[i]);

    max = a[0];
    for (i = 0; i < 10; i++) {
        sum += a[i];
        if (a[i] > max)
            max = a[i];
    }
    avg = sum / 10.0;

    printf("Sum = %d\n", sum);
    printf("Average = %.2f\n", avg);
    printf("Maximum = %d\n", max);
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2009 AD · 5 marks

Write a program to take a sentence as input and display the number of white spaces in that string.

Answer

#include <stdio.h>

int main()
{
    char str[200];
    int i, count = 0;

    printf("Enter a sentence: ");
    gets(str);          /* or fgets(str, 200, stdin) */

    for (i = 0; str[i] != '\0'; i++)
        if (str[i] == ' ')
            count++;

    printf("Number of white spaces = %d\n", count);
    return 0;
}

gets() is used so that the sentence can contain spaces (scanf("%s") would stop at the first space). In modern compilers use fgets(str, 200, stdin) instead; it also stores the newline, so count only ' ' as above.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2009 AD · 5 marks

Explain the given program and write the result when the program is run.
# include<conio.h>
# include<string.h>

int main( )
{
  char aaa[ ]= "8 this is a program".
  char bbb[20];
  int l,n,t=0;
  l = atoi(aaa);
  memcpy(bbb,aaa,2);
  printf("output: %d",l);
  printf("\n output:%s",bbb);
  for (n=1; n<=l;n++)
  {
    t=t+n:
    printf("\n hello world");
  }
  printf("\n output : %d",t);
  getch( );
  return 1;
}

Answer

The program has small syntax errors (a . instead of ; after the string, t=t+n: with a colon, and stdio.h/stdlib.h are not included). Assuming they are corrected, it works as follows.

  • aaa holds the string "8 this is a program".
  • atoi(aaa) converts the leading digits to an integer, so l = 8.
  • memcpy(bbb, aaa, 2) copies the first 2 bytes ('8' and ' ') into bbb. No '\0' is copied, so printing bbb with %s is only well defined if bbb already ends with a null (assumed here).
  • The loop runs for n = 1 to 8. It prints "hello world" 8 times and adds t=1+2+⋯+8=36t = 1 + 2 + \dots + 8 = 36.

Output:

output: 8
 output:8 
 hello world
 hello world
 hello world
 hello world
 hello world
 hello world
 hello world
 hello world
 output : 36

(The second line shows 8 followed by a space.)

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2009 AD · 5 marks

Write a program that reads two string and perform following operation according to the choice inputted by the user.
a. Display larger of those two. b. Display both string in reverse.

Answer

Here "larger" is taken as the string that comes later in alphabetical order (strcmp).

#include <stdio.h>
#include <string.h>

int main()
{
    char s1[50], s2[50];
    int choice, i, len;

    printf("Enter first string: ");
    scanf("%s", s1);
    printf("Enter second string: ");
    scanf("%s", s2);

    printf("1. Display larger string\n2. Display both strings in reverse\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);

    switch (choice) {
    case 1:
        if (strcmp(s1, s2) >= 0)
            printf("Larger string: %s\n", s1);
        else
            printf("Larger string: %s\n", s2);
        break;
    case 2:
        len = strlen(s1);
        for (i = len - 1; i >= 0; i--)
            printf("%c", s1[i]);
        printf("\n");
        len = strlen(s2);
        for (i = len - 1; i >= 0; i--)
            printf("%c", s2[i]);
        printf("\n");
        break;
    default:
        printf("Invalid choice\n");
    }
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2010 AD · 1 mark

Does main function have any arguments? If yes then list the arguments.

Answer

Yes, main() can take command-line arguments: int main(int argc, char *argv[]).

  • argc (argument count): the number of command-line arguments, including the program name.
  • argv (argument vector): an array of strings holding the arguments; argv[0] is the program name.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2010 AD · 1 mark

Write the relationship between array and pointer.

Answer

The name of an array is a constant pointer to its first element, so for int a[5]; int *p = a; we have a == &a[0]. The element a[i] is the same as *(a + i), and &a[i] is the same as a + i. A pointer can therefore be used to traverse an array with pointer arithmetic, but unlike a pointer, an array name cannot be changed to point elsewhere.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2010 AD · 1 mark

Make a distinction between if statement and switch statement.

Answer

  • if can test any condition or range using relational and logical expressions; switch compares one integer or character expression against fixed constant case values.
  • if allows a different expression in each condition (else if); switch evaluates a single expression.
  • switch is cleaner for many fixed choices (such as menus), and uses break to stop falling through to the next case.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2010 AD · 1 mark

If concept of array was not present in programming language then what might be the problem?

Answer

Without arrays we would need a separate variable for every value (for example 100 variables for 100 marks), which is impractical. Related data could not be grouped and handled in loops, so programs for sorting, searching, matrices or strings would be very long and hard to write and maintain.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2010 AD · 1 mark

Define double pointer variable.

Answer

A double pointer (pointer to pointer) is a variable that stores the address of another pointer variable. It is declared with two stars: int x = 5; int *p = &x; int **pp = &p;. Then **pp gives the value of x.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2010 AD · 1 mark

Why dynamic memory allocation concept is used in programs?

Answer

Dynamic memory allocation (malloc(), calloc(), realloc(), free()) is used to allocate memory at run time, when the amount needed is not known in advance. It avoids wasting memory on fixed-size arrays, lets data structures such as linked lists grow and shrink, and lets memory be released with free() when it is no longer needed.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2010 AD · 1 mark

Write the use of fseek function with its complete argument list.

Answer

fseek() moves the file position pointer to a desired location in a file, which gives random access.

int fseek(FILE *fp, long offset, int origin);
  • fp: file pointer.
  • offset: number of bytes to move (can be negative).
  • origin: starting point, one of SEEK_SET (0, beginning of file), SEEK_CUR (1, current position) or SEEK_END (2, end of file).

It returns 0 on success.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2010 AD · 1 mark

What do you mean by format string in C language? Write any two of them.

Answer

A format string is the string given to printf()/scanf() that specifies the type and layout of the data, using conversion specifiers that begin with %. Two examples are %d (signed decimal integer) and %f (floating-point number). Others are %c (character) and %s (string).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2010 AD · 1 mark

Write the limitation of the return statement.

Answer

A return statement can send back only one value to the calling function. Several values cannot be returned directly (pointers, arrays or structures must be used instead), and any statement after return in the function is never executed.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2010 AD · 1 mark

How union helps to save memory?

Answer

All members of a union share the same memory, and the union takes only the space of its largest member. A structure allocates separate space for every member. When only one of several alternatives is needed at a time, a union saves memory.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2010 AD · 2 marks

Write the use of memmove and memcmp functions with their argument list.

Answer

memmove

void *memmove(void *dest, const void *src, size_t n);

It copies n bytes from src to dest. Unlike memcpy, it works correctly even if the two memory areas overlap.

memcmp

int memcmp(const void *s1, const void *s2, size_t n);

It compares the first n bytes of s1 and s2. It returns 0 if they are equal, a negative value if s1 is less than s2, and a positive value if s1 is greater.

Both are declared in string.h.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2010 AD · 8 marks

A book record consists of its title, author, pages and price.
Write a program to perform operations.
a. Read the records of 13 books. b. Create at least one structure pointer to display the records of 13 books. c. Store records of all 13 books in the file "booklist.dat". d. Read only the information of 9 books from "booklist.dat" skipping 2 books from first and 2 books from last and display in terminal.

Answer

#include <stdio.h>
#include <stdlib.h>

struct book {
    char title[40];
    char author[30];
    int pages;
    float price;
};

int main()
{
    struct book b[13], *p, t;
    int i;
    FILE *fp;

    /* a. read records of 13 books */
    for (i = 0; i < 13; i++) {
        printf("\nBook %d\n", i + 1);
        printf("Title: ");
        scanf(" %39[^\n]", b[i].title);
        printf("Author: ");
        scanf(" %29[^\n]", b[i].author);
        printf("Pages: ");
        scanf("%d", &b[i].pages);
        printf("Price: ");
        scanf("%f", &b[i].price);
    }

    /* b. display the records using a structure pointer */
    printf("\nAll books:\n");
    for (p = b; p < b + 13; p++)
        printf("%s | %s | %d | %.2f\n",
               p->title, p->author, p->pages, p->price);

    /* c. store all records in booklist.dat */
    fp = fopen("booklist.dat", "wb");
    if (fp == NULL) {
        printf("Cannot open file\n");
        exit(1);
    }
    fwrite(b, sizeof(struct book), 13, fp);
    fclose(fp);

    /* d. read 9 books, skipping 2 from the start and 2 from the end */
    fp = fopen("booklist.dat", "rb");
    if (fp == NULL) {
        printf("Cannot open file\n");
        exit(1);
    }
    fseek(fp, 2L * sizeof(struct book), SEEK_SET);   /* skip first 2 */
    printf("\nBooks 3 to 11:\n");
    for (i = 0; i < 9; i++) {
        fread(&t, sizeof(struct book), 1, fp);
        printf("%s | %s | %d | %.2f\n", t.title, t.author, t.pages, t.price);
    }
    fclose(fp);
    return 0;
}

The file holds 13 records. After skipping the first 2 with fseek, reading 9 records covers books 3 to 11, so the last 2 (books 12 and 13) are skipped automatically.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2010 AD · 5 marks

Write a program to find the second largest among three numbers.

Answer

#include <stdio.h>

int main()
{
    int a, b, c, second;

    printf("Enter three numbers: ");
    scanf("%d %d %d", &a, &b, &c);

    if ((a >= b && a <= c) || (a <= b && a >= c))
        second = a;
    else if ((b >= a && b <= c) || (b <= a && b >= c))
        second = b;
    else
        second = c;

    printf("Second largest = %d\n", second);
    return 0;
}

The number that lies between the other two is the second largest. For 4 9 6 the output is 6.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2010 AD · 5 marks

Write a program that reads the elements of two matrices A3×4A_{3\times4} and B4×3B_{4\times3} then perform B×A and A×B.

Answer

AA is 3×43\times4 and BB is 4×34\times3, so both products exist: A×BA\times B is 3×33\times3 and B×AB\times A is 4×44\times4.

#include <stdio.h>

int main()
{
    int A[3][4], B[4][3], AB[3][3], BA[4][4];
    int i, j, k;

    printf("Enter elements of A (3x4):\n");
    for (i = 0; i < 3; i++)
        for (j = 0; j < 4; j++)
            scanf("%d", &A[i][j]);

    printf("Enter elements of B (4x3):\n");
    for (i = 0; i < 4; i++)
        for (j = 0; j < 3; j++)
            scanf("%d", &B[i][j]);

    /* AB = A x B (3x3) */
    for (i = 0; i < 3; i++)
        for (j = 0; j < 3; j++) {
            AB[i][j] = 0;
            for (k = 0; k < 4; k++)
                AB[i][j] += A[i][k] * B[k][j];
        }

    /* BA = B x A (4x4) */
    for (i = 0; i < 4; i++)
        for (j = 0; j < 4; j++) {
            BA[i][j] = 0;
            for (k = 0; k < 3; k++)
                BA[i][j] += B[i][k] * A[k][j];
        }

    printf("\nA x B:\n");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++)
            printf("%5d", AB[i][j]);
        printf("\n");
    }

    printf("\nB x A:\n");
    for (i = 0; i < 4; i++) {
        for (j = 0; j < 4; j++)
            printf("%5d", BA[i][j]);
        printf("\n");
    }
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2010 AD · 5 marks

Write a program to display the reverse of a number using recursion.

Answer

The recursive function prints the last digit and then calls itself with the number divided by 10.

#include <stdio.h>

void reverse(int n)
{
    if (n == 0)
        return;
    printf("%d", n % 10);
    reverse(n / 10);
}

int main()
{
    int n;

    printf("Enter a number: ");
    scanf("%d", &n);

    printf("Reverse: ");
    if (n == 0)
        printf("0");
    else
        reverse(n);
    printf("\n");
    return 0;
}

For 1234 the calls print 4, 3, 2, 1, so the output is 4321. This version is for a non-negative number.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2010 AD · 5 marks

Explain the given program and write the result when the program is executed.
# include <stdio.h>
# include <conio.h>
# include <string.h>

Int main ( )
{
  char *ppp = " Programming language.";
  char **p = &ppp;
  printf("\n%s AND %s", *p, ppp);
  pp = strlen (*p);
  printf (" \n % 4 d \n % 4 d\n % 4 d\n % 4 d\n % 4 d\n", pp--,pp,++pp, pp++, --pp);

  if (strcmp ("abandon", "abandonment") <=0)
     pp = 10;
  printf ("value of pp = %d", pp);
  getch ( );
  return 0;
}

Answer

The program has typing errors (Int should be int, and pp is not declared). Assuming int pp; is declared, it works as follows.

  • ppp points to the string " Programming language." (a space, then Programming with 11 letters, a space, and language. with 9 characters).
  • p is a pointer to ppp, so *p is the same string as ppp.
  • First printf prints Programming language. AND Programming language.
  • pp = strlen(*p) gives 1+11+1+9=221 + 11 + 1 + 9 = 22.
  • The second printf uses pp with --, ++ in the same argument list. This has undefined behaviour because the order of evaluation is not specified. With compilers that evaluate arguments from right to left (such as Turbo C and gcc), the values are --pp = 21, pp++ = 21, ++pp = 23, pp = 23, pp-- = 23, and they print in order from left to right as 23, 23, 23, 21, 21 (width 4 each). The result can differ on another compiler.
  • strcmp("abandon", "abandonment") compares the strings until the first one ends, and "abandon" is shorter, so the result is negative (<= 0 is true). Therefore pp = 10.

Output (typical):

 Programming language. AND  Programming language.
  23
  23
  23
  21
  21
value of pp = 10

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2010 AD · 5 marks

Write a program that reads character and perform following operation according to the choice inputted by the user.
a. Displays "it is vowel" if entered character is vowel otherwise "it is consonant". b. Displays "it is digit" if entered character is member of digit, otherwise display the ASCII value of entered character.

Answer

The program reads a character, shows a menu, and performs operation (a) or (b) according to the user's choice.

#include <stdio.h>
#include <ctype.h>

int main() {
    char ch, choice;

    printf("Enter a character: ");
    scanf(" %c", &ch);

    printf("Choose operation:\n");
    printf("a. Check vowel or consonant\n");
    printf("b. Check digit (else show ASCII value)\n");
    printf("Enter your choice (a/b): ");
    scanf(" %c", &choice);

    switch (choice) {
    case 'a':
        if (isalpha(ch)) {
            switch (tolower(ch)) {
            case 'a': case 'e': case 'i': case 'o': case 'u':
                printf("it is vowel\n");
                break;
            default:
                printf("it is consonant\n");
            }
        } else {
            printf("it is consonant\n");
        }
        break;
    case 'b':
        if (ch >= '0' && ch <= '9')
            printf("it is digit\n");
        else
            printf("ASCII value = %d\n", ch);
        break;
    default:
        printf("Invalid choice\n");
    }
    return 0;
}

In option (a) the character is converted to lower case so that both A and a are treated as vowels. In option (b) a digit is detected by comparing the character with '0' and '9'; otherwise the character is printed with %d, which gives its ASCII value (for example A gives 65).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2012 AD · 1 mark

Why union is used in programs?

Answer

A union is used to save memory. All its members share the same memory location, whose size equals that of the largest member. So only one member can hold a value at a time. It is useful when a variable may hold different types of data at different times, for example in embedded systems or when interpreting the same bytes in different ways.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2012 AD · 1 mark

Differentiate between print and puts function.

Answer

printf() can print text and values of variables of any type using format specifiers such as %d and %f. It does not add a newline automatically.

puts() only prints a string and automatically appends a newline at the end. It has no format specifiers, so it cannot print other data types directly.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2012 AD · 1 mark

What is the use of strcat function?

Answer

strcat(s1, s2) (declared in string.h) concatenates strings. It appends the string s2 to the end of s1 and returns s1. The destination s1 must be large enough to hold both strings. For example, if s1 = "Hello " and s2 = "World", then after strcat(s1, s2), s1 becomes "Hello World".

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2012 AD · 1 mark

How command line arguments are used in programs?

Answer

Command line arguments are values passed to a program when it is run from the command prompt. They are received by main through two parameters:

int main(int argc, char *argv[])

Here argc is the number of arguments (including the program name) and argv[] is an array of strings holding them, with argv[0] the program name. For example, prog 5 10 gives argc = 3, and atoi(argv[1]) gives 5.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2012 AD · 1 mark

Make distinction between array and structure.

Answer

An array is a collection of elements of the same data type stored in contiguous memory and accessed by an index. A structure is a collection of variables (members) of different data types grouped under one name and accessed by the member name with the dot operator. Arrays are derived types, while a structure is a user-defined type.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2012 AD · 1 mark

Write the use of memset function.

Answer

memset(ptr, value, n) (declared in string.h) fills the first n bytes of the memory block pointed to by ptr with the byte value. It is commonly used to initialise an array or structure, for example memset(arr, 0, sizeof(arr)); sets every byte of arr to zero.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2012 AD · 1 mark

List binary operators.

Answer

Binary operators work on two operands. In C they are:

  • Arithmetic: +, -, *, /, %
  • Relational: <, >, <=, >=, ==, !=
  • Logical: &&, ||
  • Bitwise: &, |, ^, <<, >>
  • Assignment: =, +=, -=, *=, /=, %=

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2012 AD · 1 mark

Write about continue and goto statement.

Answer

continue: used inside loops. It skips the remaining statements of the current iteration and moves control to the next iteration (to the condition test in while/do-while, or the update expression in for).

for (i = 1; i <= 5; i++) {
    if (i == 3) continue;
    printf("%d ", i);   /* prints 1 2 4 5 */
}

goto: an unconditional jump statement. It transfers control to a labelled statement in the same function: goto label; ... label: statement;. Heavy use makes programs hard to follow, so it is generally avoided.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2012 AD · 1 mark

Write syntax to declare constant in two different ways in C programming.

Answer

(a) Using the const keyword:

const float PI = 3.14159;

(b) Using the #define preprocessor directive:

#define PI 3.14159

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2012 AD · 2 marks

Differentiate between passing parameter by value and by reference to a function with example

Answer

Call by value: a copy of the actual argument is passed, so changes made to the formal parameter do not affect the original variable.

Call by reference: the address of the argument is passed (using pointers), so the function works on the original variable and changes are visible in the caller.

void byValue(int x)  { x = 10; }     /* a stays unchanged */
void byRef(int *x)   { *x = 10; }    /* a becomes 10 */

int main() {
    int a = 5;
    byValue(a);    /* a is still 5 */
    byRef(&a);     /* a is now 10 */
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2012 AD · 8 marks

Write a single program to take n floating point numbers from user. Allocate memory for them using malloc and perform following tasks:
Display the greatest number, Display the numbers in descending order, and Write the sum and average of those numbers in file "data.tu".

Answer

#include <stdio.h>
#include <stdlib.h>

int main() {
    int n, i, j;
    float *p, temp, sum = 0, avg;
    FILE *fp;

    printf("How many numbers? ");
    scanf("%d", &n);

    p = (float *)malloc(n * sizeof(float));
    if (p == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }

    for (i = 0; i < n; i++) {
        printf("Enter number %d: ", i + 1);
        scanf("%f", &p[i]);
        sum += p[i];
    }
    avg = sum / n;

    /* greatest number */
    float max = p[0];
    for (i = 1; i < n; i++)
        if (p[i] > max)
            max = p[i];
    printf("Greatest number = %.2f\n", max);

    /* descending order (bubble sort) */
    for (i = 0; i < n - 1; i++)
        for (j = 0; j < n - 1 - i; j++)
            if (p[j] < p[j + 1]) {
                temp = p[j];
                p[j] = p[j + 1];
                p[j + 1] = temp;
            }
    printf("Numbers in descending order:\n");
    for (i = 0; i < n; i++)
        printf("%.2f\n", p[i]);

    /* write sum and average to file */
    fp = fopen("data.tu", "w");
    if (fp == NULL) {
        printf("Cannot open file\n");
        free(p);
        return 1;
    }
    fprintf(fp, "Sum = %.2f\nAverage = %.2f\n", sum, avg);
    fclose(fp);

    free(p);
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2012 AD · 5 marks

Write program to display the following pattern using while loop:
c       c       c       c
        c       c       c
                c       c
                        c

Answer

Row i (starting from 0) begins with i tab spaces and then prints 4 - i letters c, each followed by a tab. Using a while loop:

#include <stdio.h>

int main() {
    int i = 0, j, k;

    while (i < 4) {
        j = 0;
        while (j < i) {          /* leading gaps */
            printf("\t");
            j++;
        }
        k = 0;
        while (k < 4 - i) {      /* the c's */
            printf("c\t");
            k++;
        }
        printf("\n");
        i++;
    }
    return 0;
}

Output:

c       c       c       c
        c       c       c
                c       c
                        c

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2012 AD · 5 marks

Define function and also write advantage of using function. Write function which receives 15 integer numbers and returns average of them.

Answer

Function: a function is a named, self-contained block of statements that performs a specific task. It can be called from other parts of the program, and may take parameters and return a value.

Advantages of using functions:

  • Avoids repetition of code (write once, call many times).
  • Makes a large program modular, easier to understand, debug and test.
  • Allows different programmers to work on different parts.
  • Functions can be reused in other programs.

Function to find the average of 15 integers:

float average(int a[15]) {
    int i, sum = 0;
    for (i = 0; i < 15; i++)
        sum += a[i];
    return sum / 15.0;
}

It is called as avg = average(num); where num is an int array of size 15.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2012 AD · 5 marks

What is nested structure? Write program to take 28 records of cattle (cid, name, age, weight) from user and display the average weight of cattles.

Answer

Nested structure: a structure that contains another structure as one of its members. The inner structure must be defined before it is used, and its members are accessed with repeated dot operators, e.g. c.dob.year.

struct date { int day, month, year; };
struct student { int roll; struct date dob; };

Program for 28 cattle records:

#include <stdio.h>

struct cattle {
    int cid;
    char name[30];
    int age;
    float weight;
};

int main() {
    struct cattle c[28];
    int i;
    float total = 0;

    for (i = 0; i < 28; i++) {
        printf("Enter cid, name, age, weight of cattle %d: ", i + 1);
        scanf("%d %s %d %f", &c[i].cid, c[i].name, &c[i].age, &c[i].weight);
        total += c[i].weight;
    }

    printf("Average weight of cattle = %.2f\n", total / 28);
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2012 AD · 5 marks

What happens when following program is executed?
# include<stdio.h>
int main()
{
  FILE*fptr;
  char ch;
  fptr=fopen("data. dat" ,"w");
  fprintf (fptr, "facaulty of Management, Tribhuvan University");
  fclose(fptr);
  fptr = fopen ("data. dat" ,"r+");
  fseek (fptr, +11 , 0);
  fprintf (fptr , "Eng");
  fseek (fptr,+11, 1);
  fprintf (fptr , "ib");
  rewind (fptr);
  do{
    ch = fgetc (fptr);
    putchar (ch);
  }while (ch!=Eof);
  fclose(fptr);
  return 0 ;
}

Answer

Assuming the file name is data.dat and Eof is meant to be EOF (as written, Eof is an undeclared identifier and gives a compile error), the program runs as follows.

  1. The file is created and the text facaulty of Management, Tribhuvan University is written (characters are numbered from 0).
  2. In r+ mode, fseek(fptr, 11, 0) moves to position 11, the space before "Management". fprintf writes "Eng" over positions 11 to 13 ( Ma), so the text becomes facaulty ofEngnagement, Tribhuvan University.
  3. fseek(fptr, 11, 1) moves 11 bytes forward from the current position 14, to position 25. The characters ri at positions 25 and 26 (in "Tribhuvan") are overwritten with "ib", giving Tibbhuvan.
  4. rewind goes back to the start, and the file is read and displayed character by character.

Output:

facaulty ofEngnagement, Tibbhuvan University

The loop compares a char with EOF, so after the text one extra character (the value of EOF, shown as a garbage symbol) may also be printed.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD

C language has been developed by
(a) Ken Thompson (b) Bill Gate (c) Dennis Ritchie (d) Peter Norton

Answer

(c) Dennis Ritchie

C was developed by Dennis Ritchie at Bell Laboratories in the early 1970s.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD

C is a
(a) Middle level language (b) Low level language (c) High level language (d) None of the above

Answer

(a) Middle level language

C combines features of high-level languages (functions, structured control) with low-level features such as pointers and direct memory access, so it is called a middle-level language.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD

The real constant in C can be expressed in which of the following forms
(a) Fractional form only (b) exponential form only (c) ASCII form only (d) Both fractional & Exponential form

Answer

(d) Both fractional & Exponential form

A real constant can be written in fractional form (e.g. 314.5) or exponential form (e.g. 3.145e2).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD

A character variable can at a time store
(a) 1 character (b) 8 characters (c) 254 characters (d) None of the above

Answer

(a) 1 character

A char variable occupies one byte and stores a single character.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD

The maximum value that an integer constant can have is
(a) -32 767 (b) 32767 (c) 1.0701e+38 (d) None of the above

Answer

(b) 32767

For a 16-bit signed int (as in Turbo C), the range is −32768-32768 to 3276732767, so the maximum is 215−1=327672^{15}-1 = 32767.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD

If a is an integer variable, a= 5/2; will return a value
(a) 2.5 (b) 3 (c) 2 (d) 0

Answer

(c) 2

Both 5 and 2 are integers, so integer division gives 2 (the fraction is truncated) and 2 is stored in a.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD

The expression, a=7/22 * (3.14 + 2) * 3/ 5 ; evaluate to
(a) 8.28 (b) 6.28 (c) 3.14 (d) 0

Answer

(d) 0

Evaluation goes left to right for * and /. First 7/22 is integer division, which gives 0. Then 0×(3.14+2)=00 \times (3.14+2) = 0, and 0×3/5=00 \times 3 / 5 = 0.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD

A relational operator is used to
(a) combine values (b) compare values (c) Distinguish different types of variable (d) change variables

Answer

(b) compare values

Relational operators (<, >, <=, >=, ==, !=) compare two values and give true (1) or false (0).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD

Which is the correct way to define pointer?
(a) int_ptr x; (b) int *ptr; (c) *int ptr; (d) *x;

Answer

(b) int *ptr;

A pointer is declared with the data type followed by * and the name, so int *ptr; declares a pointer to an integer.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD

"A" is a ______ while 'A' is a ________.
(a) string, character (b) integer, character (c) character, character (d) None of the above

Answer

(a) string, character

"A" in double quotes is a string (it holds A and the terminating \0), while 'A' in single quotes is a character constant.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD

Which of the following is not standard keywords.
(a) goto (b) switch (c) long (d) IF

Answer

(d) IF

C keywords are in lower case, so if is a keyword but IF is not. goto, switch and long are standard keywords.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD

________ is a valid identifier.
(a) two_num (b) 4th (c) "x" (d) All of the above

Answer

(a) two_num

An identifier may contain letters, digits and underscore and must not begin with a digit. 4th starts with a digit and "x" contains quotes, so both are invalid.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD

________ statement is wrong in C programming.
(a) # define True 1 (b) int x =1; (c) # define True =1 (d) float y;

Answer

(c) # define True =1

#define takes the name and value separated by a space, with no =. With =1 the macro would expand to = 1, which is wrong. The correct form is #define True 1.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD

Suppose that I, j & k are integer values whose values are 1,2&3 respectively. Which of the following logical expression is true.
(a) (i + j)> =k (b) (j + k)>(l + 5) (c) k ! = 3 (d) i>j

Answer

(a) (i + j)> =k

With i=1,j=2,k=3i=1, j=2, k=3: (i+j)=3≥3(i+j)=3 \ge 3 is true. Also k≠3k \ne 3 is false and i>ji>j is false. Option (b) uses an undefined l and, taking it as ii, 5>65 > 6 is false.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD

If an integer is to be entered through the keyboard, Which function would you use?
(a) scan f ( ) (b) gets ( ) (c) getch ( ) (d) getcher ( )

Answer

(a) scanf()

scanf("%d", &n); reads an integer from the keyboard. gets() reads a string, and getch()/getchar() read a single character.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD

Which of the following is allowed in a C Arithmetic instruction.
(a) [ ] (b) { } (c) ( ) (d) None of the above

Answer

(c) ( )

Only parentheses are used in arithmetic expressions to group operations. Square brackets are for arrays and braces are for blocks.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD

Hierarchy decides which operator
(a) Is most important (b) Is fastest (c) Is used first (d) Operators on largest number

Answer

(c) Is used first

The hierarchy (precedence) of operators decides which operator is evaluated first in an expression.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2002 AD

Which of the following is an example of initializing a variable?
(a) int a=5 (b) a = = 5 (c) a > 6 (d) None of the above

Answer

(a) int a=5

This declares the variable and gives it an initial value at the same time. a == 5 is a comparison and a > 6 is a relational expression.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2003 AD

Which of the following is true in function definition
(a) void fun (int, int) (b) void fun (int a, int b) (c) void fun (int a, b); (d) void fun (int a, int b)

Answer

(b) void fun (int a, int b)

A function definition needs the type and name of every parameter, and no semicolon after the header. Option (c) gives no type for b.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2003 AD

Which keyword is used for unconditional branching
(a) if (b) for (c) goto (d) switch

Answer

(c) goto

goto label; transfers control to the label without testing any condition, whereas if, for and switch depend on conditions.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2003 AD

Which keys are used to compile the program in "C"?
(a) Alt+F8 (b) Ctrl+F8 (c) Alt +F9 (d) Ctrl+F9

Answer

(c) Alt+F9

In Turbo C/C++, Alt+F9 compiles the program (F9 makes it, Ctrl+F9 runs it).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2003 AD

The collection of data of same data type with one name is said to be ____________.
(a) structure (b) array (c) file (d) string

Answer

(b) array

An array is a collection of elements of the same data type stored under one name and accessed by index.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2003 AD

A function is self_contained program segment that carries out some specific, well define task.
(a) true (b) false

Answer

(a) true

A function is a self-contained block of code that performs one specific, well-defined task and can be called whenever needed.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2003 AD

Every compound statement in C is end by
(a) . (b) ; (c) : (d) ,

Answer

(b) ;

Every simple statement inside a compound statement (block) is terminated by a semicolon ;. The block itself is enclosed in braces { }, but among the given options the semicolon is the statement terminator.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2003 AD

Which header file is required for the function clrscr() and getch().
(a) stdio.h (b) math.h (c) clear.h (d) conio.h

Answer

(d) conio.h

clrscr() and getch() are console I/O functions declared in conio.h.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2003 AD

Which is the key word used to declare a structure
(a) void (b) struct (c) sizeof (d) static

Answer

(b) struct

A structure is declared with the keyword struct, e.g. struct student { int roll; char name[20]; };.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2003 AD

What will happen if the global variable is declared but not initialized?
(a) The 'garbage' value will be assigned (b) The variable will be assigned 'zero' (c) 'Error' message from the compiler (d) 'Nothing' will happen

Answer

(b) The variable will be assigned 'zero'

Global (and static) variables have static storage duration and are automatically initialised to zero if not explicitly initialised. Only local (auto) variables hold garbage values.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2003 AD

Consider the following program.
# include<stdio.h>
# include <conio.h>
void main (void)
{clrscr();
 int i,j;float s,t;
 print f ("\nCCC");
 getch() ;
}
In the above program which of the following is true
(a) The argument void in the main function is not allowed (b) The declaration of int and float are not allowed in the same line (c) The clrscr function should not be placed before the variable declaration (d) There is no error in the program

Answer

(c) The clrscr function should not be placed before the variable declaration

In (older) C, all declarations in a block must come before executable statements, so clrscr(); before int i,j; float s,t; is wrong. (void in main(void) is allowed, and int and float declarations are on separate statements.)

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2003 AD

If f is a float variable, f= 10/5; what is the value when %f is used in print f
(a) 2.0 (b) 2.00 (c) 2 (d) none of the above

Answer

(d) none of the above

10/5 is integer division giving 2, which is stored as 2.0 in f. printf("%f", f) prints six decimal places by default, i.e. 2.000000, which matches none of the options.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2003 AD

Which of the following is correct in the function call (calling function)
(a) void fun (int a, int b); (b) fun (int a, int b); (c) fun ( a, int b) (d) fun ( a, b);

Answer

(d) fun ( a, b);

In a function call only the arguments (values or variables) are passed, without data types. Options (a) and (b) are declarations/definition forms, and (c) wrongly mixes a type with an argument.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2003 AD

The output of the following statement will be
#include <stdio.h>
main ()
{ int a=5;
  print ("%.2d",a);
}
(a) 5.00 (b) 05.00 (c) Error (d) None

Answer

(d) None

The format %.2d means a minimum of two digits, so 5 would print as 05; it does not print a decimal point, so (a) and (b) are wrong. Since the standard function is printf (the code writes print), the program as written would not link; the intended output is 05, which is not listed.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2003 AD

What will be the output of the variable garbage?
int garbage = 12/6+34+21/(5*3);
(a) 37 (b) 3 (c) 7 (d) 0

Answer

(a) 37

Using integer division: 12/6=212/6 = 2, 5×3=155\times3 = 15, 21/15=121/15 = 1. So 2+34+1=372 + 34 + 1 = 37.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2003 AD

Three Parts of the loop expression in the for loop are:
(a) Initialization, testing and increment (b) Initialization, testing and condition (c) Initialization, Condition and Testing (d) Initialization, Condition and Break

Answer

(a) Initialization, testing and increment

A for loop has the form for (initialization; test condition; increment/decrement).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2003 AD

Which of the escape sequence is used for audible alert (bell sound)?
(a) \a (b) \b (c) \c (d) none of the above

Answer

(a) \a

\a is the alert (bell) escape sequence. \b is backspace.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2003 AD

What will be the output of the following program
main( )
{ float a,b,c,x;
  a=9; b=12; c=3;
  x=a-b/3+c*2-1;
  print("x = %.2f

",x);
}
(a) x=3.00 (b) x=7.00 (c) x=10.00 (d) x=12.00

Answer

(c) x=10.00

x=9−12/3+3×2−1=9−4+6−1=10x = 9 - 12/3 + 3\times 2 - 1 = 9 - 4 + 6 - 1 = 10. Printed with %.2f it gives x = 10.00.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2003 AD

"C" language has been developed by
(a) Ken Thompson at AT & T in 1970 (b) Dennish Ritchie at AT & T in 1972 (c) Cambridge University in 1970 (d) None of the above

Answer

(b) Dennish Ritchie at AT & T in 1972

C was developed by Dennis Ritchie at AT&T Bell Laboratories in 1972.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2013 AD · 1 mark

Consider array int test[2][5]. How many int data the array test can contain?

Answer

The array test[2][5] can hold 2×5=102 \times 5 = 10 integers.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2013 AD · 1 mark

When you pass an array name as an argument to a function, what is actually being passed?

Answer

The base address of the array, i.e. the address of its first element, is passed. No copy of the array is made, so changes made in the function affect the original array.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2013 AD · 1 mark

When void keyboard used?

Answer

The void keyword is used to show that a function returns no value (void f()), or that it takes no parameters (int main(void)). It is also used for generic pointers (void *).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2013 AD · 1 mark

What is type conversion?

Answer

Type conversion is the change of a value from one data type to another. It can be implicit (done automatically by the compiler, e.g. int promoted to float in a mixed expression) or explicit (type casting by the programmer, e.g. (float) a / b).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2013 AD · 1 mark

What are command line arguments?

Answer

Command line arguments are values given to a program on the command line when it is executed. They are received by main as int main(int argc, char *argv[]), where argc is the number of arguments and argv is an array of strings holding them (argv[0] is the program name).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2013 AD · 1 mark

While compiling a C program it is showing that "Unable to open STDIO.H". What is the reason?

Answer

The compiler cannot find the header file stdio.h in its include directory. The reason may be a wrong include path setting, a missing or misinstalled header file, or an incorrect file name or case (it should be #include <stdio.h>). Setting the correct include directory fixes it.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2013 AD · 1 mark

How does a constant defined by const differ from the constant define by the pre-processor statement # define?

Answer

  • A const constant is a typed variable handled by the compiler. It has a data type, occupies memory, obeys scope rules and can be seen by a debugger, e.g. const int max = 10;.
  • A #define constant is a macro replaced by the preprocessor with text before compilation. It has no type, no memory and no scope, e.g. #define MAX 10.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2013 AD · 1 mark

What is the advantage of using the function malloc()?

Answer

malloc() allocates memory dynamically at run time, so the exact amount needed can be decided during execution. This avoids wasting memory on fixed-size arrays, and the memory can be released with free() when no longer needed.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2013 AD · 1 mark

Differentiate between append mode and write mood specified in fopen().

Answer

  • Write mode ("w") creates a new file, or if the file exists, erases its old contents and writes from the beginning.
  • Append mode ("a") opens the file (creating it if absent) and keeps existing contents; new data is added at the end of the file.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2013 AD · 8 marks

Write a menu driven program to do the following over a list of numbers
a. Find the smallest number of the list. b. Compute the frequency of given number X. c. Find the sum of positive and negative numbers. d. Find the number of even elements.

Answer

#include <stdio.h>

int main(void)
{
    int a[100], n, i, ch, x, count, even;
    int small, sumPos, sumNeg;

    printf("Enter number of elements: ");
    scanf("%d", &n);
    printf("Enter %d numbers:\n", n);
    for (i = 0; i < n; i++)
        scanf("%d", &a[i]);

    do {
        printf("\n----- MENU -----\n");
        printf("1. Smallest number\n");
        printf("2. Frequency of a number X\n");
        printf("3. Sum of positive and negative numbers\n");
        printf("4. Number of even elements\n");
        printf("5. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &ch);

        switch (ch) {
        case 1:
            small = a[0];
            for (i = 1; i < n; i++)
                if (a[i] < small)
                    small = a[i];
            printf("Smallest number = %d\n", small);
            break;
        case 2:
            printf("Enter X: ");
            scanf("%d", &x);
            count = 0;
            for (i = 0; i < n; i++)
                if (a[i] == x)
                    count++;
            printf("Frequency of %d = %d\n", x, count);
            break;
        case 3:
            sumPos = sumNeg = 0;
            for (i = 0; i < n; i++) {
                if (a[i] > 0)
                    sumPos += a[i];
                else if (a[i] < 0)
                    sumNeg += a[i];
            }
            printf("Sum of positive numbers = %d\n", sumPos);
            printf("Sum of negative numbers = %d\n", sumNeg);
            break;
        case 4:
            even = 0;
            for (i = 0; i < n; i++)
                if (a[i] % 2 == 0)
                    even++;
            printf("Number of even elements = %d\n", even);
            break;
        case 5:
            break;
        default:
            printf("Invalid choice\n");
        }
    } while (ch != 5);

    return 0;
}

The program reads the list into an array and repeatedly shows a menu inside a do-while loop. A switch runs the chosen operation: linear search for the minimum, counting matches of XX, adding positive and negative values separately, and counting elements with a[i] % 2 == 0.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2013 AD · 5 marks

Write a program to store records of N employees in a file named employee. Dat. These records contain name, identification number, office name and occupation of the employee. Now display name of those employees whose office name is "Nepal Bank" and occupation is "Manager".

Answer

#include <stdio.h>
#include <string.h>

struct employee {
    char name[30];
    int id;
    char office[30];
    char occupation[30];
};

int main(void)
{
    struct employee e;
    FILE *fp;
    int n, i;

    fp = fopen("employee.dat", "wb");
    if (fp == NULL) {
        printf("Cannot open file\n");
        return 1;
    }
    printf("Enter number of employees: ");
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        printf("Employee %d\n", i + 1);
        printf("Name: ");
        scanf(" %[^\n]", e.name);
        printf("ID: ");
        scanf("%d", &e.id);
        printf("Office name: ");
        scanf(" %[^\n]", e.office);
        printf("Occupation: ");
        scanf(" %[^\n]", e.occupation);
        fwrite(&e, sizeof(e), 1, fp);
    }
    fclose(fp);

    fp = fopen("employee.dat", "rb");
    if (fp == NULL) {
        printf("Cannot open file\n");
        return 1;
    }
    printf("\nManagers of Nepal Bank:\n");
    while (fread(&e, sizeof(e), 1, fp) == 1) {
        if (strcmp(e.office, "Nepal Bank") == 0 &&
            strcmp(e.occupation, "Manager") == 0)
            printf("%s\n", e.name);
    }
    fclose(fp);
    return 0;
}

The records are written to employee.dat using a structure and fwrite. The file is then reopened for reading, and fread loads each record; the name is displayed only if the office is "Nepal Bank" and the occupation is "Manager".

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2013 AD · 5 marks

Write a program that lets user to enter the total rainfall for each month. The program should calculate and display the total rainfall for the year, the average monthly rainfall and the months with the highest and lowest amount of rainfall.

Answer

#include <stdio.h>

int main(void)
{
    char *month[12] = {"January", "February", "March", "April", "May",
                       "June", "July", "August", "September", "October",
                       "November", "December"};
    float rain[12], total = 0, avg;
    int i, hi = 0, lo = 0;

    for (i = 0; i < 12; i++) {
        printf("Enter total rainfall for %s: ", month[i]);
        scanf("%f", &rain[i]);
        total += rain[i];
        if (rain[i] > rain[hi]) hi = i;
        if (rain[i] < rain[lo]) lo = i;
    }
    avg = total / 12;

    printf("Total rainfall for the year = %.2f\n", total);
    printf("Average monthly rainfall = %.2f\n", avg);
    printf("Highest rainfall: %s (%.2f)\n", month[hi], rain[hi]);
    printf("Lowest rainfall: %s (%.2f)\n", month[lo], rain[lo]);
    return 0;
}

The yearly total is accumulated while reading, the average is total/12, and the indices hi and lo track the months with the largest and smallest values.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2014 AD · 1 mark

When will you use structure variable?

Answer

A structure variable is used when related data items of different data types must be grouped under one name, e.g. a student record with name, roll number and marks, or an employee record stored in a file.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2014 AD · 1 mark

Is the use of ternary operator (? :) alternative option of using if...else statement?

Answer

Yes, the ternary operator ?: is a compact alternative to a simple if...else. For example, max = (a > b) ? a : b; is equivalent to if (a > b) max = a; else max = b;. It is suited to simple conditional assignments, while if...else is better for multiple statements.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2014 AD · 1 mark

Write syntax of fopen function.

Answer

FILE *fp;
fp = fopen("filename", "mode");

Here filename is the name of the file and mode is one of "r", "w", "a", "r+", "w+", "a+" (add b for binary). It returns a FILE pointer, or NULL if the file cannot be opened.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2014 AD · 1 mark

What is the problem/error in following part of the program?
main()
{
  int array_size=200;
  float marks (array_size);
  return 0;
}

Answer

There are two errors. First, the array is declared with parentheses, marks (array_size), instead of square brackets; it should be marks[...]. Second, the size of an array in C (static declaration) must be a constant expression, but array_size is a variable. Correct forms are float marks[200]; or #define SIZE 200 with float marks[SIZE]; (or malloc for dynamic size).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2014 AD · 1 mark

What is the output of the following program?
main(
{
  float a[]={10.5 ,2.6,3.7,4.6,5.1,6.6};
  float *ptr = a+2;
  printf("\n*ptr=%f and * (ptr+1)=%f", *ptr,* (ptr+1));
  return 0;
}

Answer

ptr = a + 2 points to a[2], so *ptr is 3.7 and *(ptr+1) is a[3] = 4.6. Output:

*ptr=3.700000 and * (ptr+1)=4.600000

(main( has a missing ), which should be main().)

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2014 AD · 1 mark

What is the output of the following program?
main()
{
  int n=10;
  printf("\n n=%d", n);
  printf("\n n=%d", n++);
  printf("\n n=%d", ++n);
  printf("\n n=%d", n) ;
  return 0;
}

Answer

Post-increment prints the old value and then increments; pre-increment increments first.

 n=10
 n=10
 n=12
 n=12

The second printf prints 10 and makes n equal to 11, the third makes it 12 before printing, and the last prints 12.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2014 AD · 1 mark

When is static variable used?

Answer

A static variable is used when a local variable must keep its value between function calls. It is initialised only once, lives for the entire program run, and is visible only inside its function (or file, if declared globally). Example: counting how many times a function is called.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2014 AD · 1 mark

What happens when you assign a fractional number to an integer variable?

Answer

The fractional part is truncated (not rounded) and only the integer part is stored. For example, int x = 7.9; stores 7.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2014 AD · 1 mark

Why is else statement used?

Answer

The else statement gives the alternative block to be executed when the condition of the if statement is false. It lets the program choose between two paths: if (cond) {...} else {...}.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2014 AD · 1 mark

List out two main functions of return statement in function definition.

Answer

  1. It terminates the execution of the function and transfers control back to the calling function.
  2. It sends a value (the result) back to the calling function, e.g. return a + b;.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2014 AD · 3 marks

Write program that takes an integer number from user, and displays message "welcome" if the given integer number is of four digits only otherwise displays "rejected".

Answer

#include <stdio.h>

int main()
{
    int n;
    printf("Enter an integer: ");
    scanf("%d", &n);
    if (n < 0)
        n = -n;
    if (n >= 1000 && n <= 9999)
        printf("welcome\n");
    else
        printf("rejected\n");
    return 0;
}

A number has exactly four digits when its absolute value lies between 1000 and 9999. The sign is removed first so that negative four-digit numbers are also accepted.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2014 AD · 3 marks

Write function that displays factorial of number 7 only.

Answer

#include <stdio.h>

void factorial7(void)
{
    int i;
    long f = 1;
    for (i = 1; i <= 7; i++)
        f = f * i;
    printf("Factorial of 7 = %ld\n", f);
}

int main()
{
    factorial7();
    return 0;
}

The function multiplies 1×2×⋯×71 \times 2 \times \dots \times 7 and prints 7!=50407! = 5040.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2014 AD · 3 marks

Write program that takes a string from user and displays the length of that string.

Answer

#include <stdio.h>

int main()
{
    char str[100];
    int len = 0;
    printf("Enter a string: ");
    gets(str);
    while (str[len] != '\0')
        len++;
    printf("Length of the string = %d\n", len);
    return 0;
}

The loop counts characters until the null character '\0' is reached. The library function strlen(str) from <string.h> gives the same result.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2014 AD · 3 marks

Write a program to display following pattern.
*    #
*    #
*    ?    ?
*    ?    ?    +

Answer

#include <stdio.h>

int main()
{
    printf("*    #\n");
    printf("*    #\n");
    printf("*    ?    ?\n");
    printf("*    ?    ?    +\n");
    return 0;
}

Each printf prints one row, with four spaces between the symbols, exactly as in the required pattern.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2014 AD · 3 marks

What do you understand by function call by value and call by reference? Write programs to illustrate both types of function call.

Answer

Call by value: The values of the actual arguments are copied into the formal parameters. The function works on the copies, so changes made inside the function do not affect the original variables.

Call by reference: The addresses of the actual arguments are passed (using pointers). The function works on the original variables through their addresses, so changes made inside the function affect the originals.

Program for call by value:

#include <stdio.h>

void swap(int a, int b)
{
    int t = a;
    a = b;
    b = t;
}

int main()
{
    int x = 5, y = 10;
    swap(x, y);
    printf("x = %d, y = %d\n", x, y);   /* x = 5, y = 10 : unchanged */
    return 0;
}

Program for call by reference:

#include <stdio.h>

void swap(int *a, int *b)
{
    int t = *a;
    *a = *b;
    *b = t;
}

int main()
{
    int x = 5, y = 10;
    swap(&x, &y);
    printf("x = %d, y = %d\n", x, y);   /* x = 10, y = 5 : swapped */
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2014 AD · 4 marks

Write function that takes a string from user, if the length of string is 7 then it displays the middle character of that string and function returns, otherwise user is asked to input another string.

Answer

#include <stdio.h>
#include <string.h>

void middleChar(void)
{
    char str[100];
    while (1)
    {
        printf("Enter a string: ");
        scanf("%s", str);
        if (strlen(str) == 7)
        {
            printf("Middle character = %c\n", str[3]);
            return;
        }
        printf("Length is not 7. Try again.\n");
    }
}

int main()
{
    middleChar();
    return 0;
}

For a string of length 7 the indices run from 0 to 6, so the middle character is str[3]. The loop keeps asking for input until a string of length 7 is entered, and then the function returns.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2014 AD · 4 marks

Write program that stores information of students in a file. In this file new records of students can be added without affected existing records of students. Students have name, age and address as attributes.

Answer

#include <stdio.h>

struct student
{
    char name[30];
    int age;
    char address[40];
};

int main()
{
    FILE *fp;
    struct student s;
    char ch;

    fp = fopen("student.dat", "ab");   /* append mode keeps old records */
    if (fp == NULL)
    {
        printf("Cannot open file\n");
        return 1;
    }
    do
    {
        printf("Enter name: ");
        scanf("%s", s.name);
        printf("Enter age: ");
        scanf("%d", &s.age);
        printf("Enter address: ");
        scanf("%s", s.address);
        fwrite(&s, sizeof(s), 1, fp);
        printf("Add another record (y/n)? ");
        scanf(" %c", &ch);
    } while (ch == 'y' || ch == 'Y');
    fclose(fp);
    return 0;
}

The file is opened in append binary mode ("ab"), so every run adds new records at the end of the file and the existing records are not erased or changed.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2014 AD · 4 marks

Write program which displays sum of all elements of two different 3-D integer arrays.

Answer

#include <stdio.h>

int main()
{
    int a[3][3][3], b[3][3][3];
    int i, j, k, sumA = 0, sumB = 0;

    printf("Enter 27 elements of first array:\n");
    for (i = 0; i < 3; i++)
        for (j = 0; j < 3; j++)
            for (k = 0; k < 3; k++)
            {
                scanf("%d", &a[i][j][k]);
                sumA += a[i][j][k];
            }

    printf("Enter 27 elements of second array:\n");
    for (i = 0; i < 3; i++)
        for (j = 0; j < 3; j++)
            for (k = 0; k < 3; k++)
            {
                scanf("%d", &b[i][j][k]);
                sumB += b[i][j][k];
            }

    printf("Sum of elements of first array = %d\n", sumA);
    printf("Sum of elements of second array = %d\n", sumB);
    printf("Sum of all elements of both arrays = %d\n", sumA + sumB);
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 1 mark

Write steps for executing a C program.

Answer

  1. Write the source code in an editor and save it with the .c extension.
  2. The preprocessor processes the directives (#include, #define).
  3. The compiler translates the source code into object code (.obj or .o) and reports syntax errors.
  4. The linker combines the object code with library functions to produce the executable file (.exe).
  5. Run the executable file and check the output.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 1 mark

What statements are used for conditional compilation?

Answer

The preprocessor directives #if, #else, #elif, #endif, #ifdef and #ifndef are used for conditional compilation. They decide which part of the source code is compiled depending on a condition or on whether a macro is defined.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 1 mark

Why is it incorrect to declare: int *p=55;?

Answer

A pointer stores a memory address, not an ordinary value. In int *p=55; the integer constant 55 is assigned to the pointer as if it were an address, which is a type mismatch and may point to an invalid memory location. A pointer must be initialised with the address of a variable, for example int x = 55; int *p = &x;.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 1 mark

What is the difference between double and float variables?

Answer

floatdouble
Single precision, 4 bytesDouble precision, 8 bytes
About 6 to 7 digits of precisionAbout 15 to 16 digits of precision
Range about 3.4×10±383.4 \times 10^{\pm 38}Range about 1.7×10±3081.7 \times 10^{\pm 308}
Format specifier %fFormat specifier %lf (for scanf)

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 1 mark

How does variable definition differ from variable declaration?

Answer

A declaration tells the compiler the name and type of a variable but does not necessarily reserve memory, for example extern int x;. A definition creates the variable and reserves memory for it, for example int x; or int x = 5;.

A variable may be declared many times but defined only once.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 1 mark

Why n++ executes faster than n=n+1?

Answer

n++ is a single increment operation that most compilers translate directly into one machine instruction (INC). In n = n + 1, the expression is evaluated first, then the result is assigned to n, which may need an extra load, an addition and a store. Hence n++ is generally faster (modern optimising compilers often generate the same code for both).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 1 mark

Is it possible to put default: anywhere in the switch statement?

Answer

Yes. The default: label may be placed anywhere among the case labels of a switch statement, not necessarily at the end. It is executed only when no case matches. If it is not the last label and has no break, control falls through to the statements that follow it.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 1 mark

Define Enumerated data type.

Answer

An enumerated data type is a user-defined data type made of a set of named integer constants, declared with the keyword enum. By default the names get the values 0, 1, 2, and so on.

enum day {SUN, MON, TUE, WED, THU, FRI, SAT};
enum day today = TUE;   /* value 2 */

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 1 mark

Define functions: fseek() and ftell().

Answer

fseek(): Moves the file position pointer to a desired location in a file.

fseek(fp, offset, origin);

Here offset is the number of bytes to move and origin is SEEK_SET (beginning), SEEK_CUR (current position) or SEEK_END (end of file). It returns 0 on success.

ftell(): Returns the current position of the file pointer as a number of bytes from the beginning of the file.

long pos = ftell(fp);

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 1 mark

Differentiate between malloc and calloc functions.

Answer

malloc()calloc()
Allocates a single block of memoryAllocates multiple blocks of memory
malloc(size) takes one argument, the total bytescalloc(n, size) takes two arguments, number of blocks and size of each
The memory contains garbage valuesThe memory is initialised to zero

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 3 marks

Write function that takes list of integers of size ten as parameter and displays all the prime numbers of the list.

Answer

#include <stdio.h>

void showPrimes(int list[10])
{
    int i, j, isPrime;
    printf("Prime numbers in the list: ");
    for (i = 0; i < 10; i++)
    {
        if (list[i] < 2)
            continue;
        isPrime = 1;
        for (j = 2; j * j <= list[i]; j++)
            if (list[i] % j == 0)
            {
                isPrime = 0;
                break;
            }
        if (isPrime)
            printf("%d ", list[i]);
    }
    printf("\n");
}

int main()
{
    int a[10] = {2, 4, 7, 9, 11, 15, 17, 20, 23, 1};
    showPrimes(a);
    return 0;
}

For the sample list the output is 2 7 11 17 23.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 3 marks

Write program to display a line of text "Best of Luck" 5 times using a function.

Answer

#include <stdio.h>

void show(void)
{
    printf("Best of Luck\n");
}

int main()
{
    int i;
    for (i = 0; i < 5; i++)
        show();
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 3 marks

What will be output of following program?
#include<stdio.h>
int main()
{
  char string[]="I know what you did last vacation";
  int m;
  for (m=0; string [m] != '\0'; m++)
  if( (m % 2 ) == 0 && string[m] != ' ')
  printf("%d", string [m]);
  return 0;
}

Answer

The program prints the ASCII code (because of %d) of each non-space character at an even index mm of the string.

The characters at even positions that are not spaces are: I (0), k (2), o (4), h (8), t (10), y (12), u (14), d (16), d (18), l (20), s (22), a (26), a (28), i (30), n (32).

Their ASCII values are 73, 107, 111, 104, 116, 121, 117, 100, 100, 108, 115, 97, 97, 105, 110, printed without separators.

Output:

731071111041161211171001001081159797105110

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 3 marks

Write program to read a line of text and display number of vowels in it.

Answer

#include <stdio.h>

int main()
{
    char str[200];
    int i, count = 0;

    printf("Enter a line of text: ");
    gets(str);
    for (i = 0; str[i] != '\0'; i++)
    {
        switch (str[i])
        {
            case 'a': case 'e': case 'i': case 'o': case 'u':
            case 'A': case 'E': case 'I': case 'O': case 'U':
                count++;
        }
    }
    printf("Number of vowels = %d\n", count);
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 3 marks

Write program that prints three different texts in three different lines.

Answer

#include <stdio.h>

int main()
{
    printf("Welcome to C programming\n");
    printf("This is the second line\n");
    printf("This is the third line\n");
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 5 marks

Write program that writes only those words which starts with 'a' to the file named "a.txt". The content of the file "a.txt" must not be erased with new entry.

Answer

#include <stdio.h>

int main()
{
    FILE *fp;
    char word[50];
    char ch;

    fp = fopen("a.txt", "a");   /* append mode: old content is kept */
    if (fp == NULL)
    {
        printf("Cannot open file\n");
        return 1;
    }
    do
    {
        printf("Enter a word: ");
        scanf("%s", word);
        if (word[0] == 'a' || word[0] == 'A')
            fprintf(fp, "%s\n", word);
        printf("Continue (y/n)? ");
        scanf(" %c", &ch);
    } while (ch == 'y' || ch == 'Y');
    fclose(fp);
    return 0;
}

Only words whose first letter is a (or A) are written. Opening the file with mode "a" appends the new words at the end, so the earlier content of a.txt is never erased.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 5 marks

Write program to display following:
          1
        2 1 2
      3 2 1 2 3
    4 3 2 1 2 3 4

Answer

Row ii (for i=1i = 1 to 4) has 4−i4 - i leading blank cells (two spaces each), then the numbers i,i−1,…,1i, i-1, \dots, 1 and then 2,3,…,i2, 3, \dots, i.

#include <stdio.h>

int main()
{
    int i, j;
    for (i = 1; i <= 4; i++)
    {
        for (j = 1; j <= 4 - i; j++)
            printf("  ");
        for (j = i; j >= 1; j--)
            printf("%d ", j);
        for (j = 2; j <= i; j++)
            printf("%d ", j);
        printf("\n");
    }
    return 0;
}

Output:

      1
    2 1 2
  3 2 1 2 3
4 3 2 1 2 3 4

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2015 AD · 5 marks

Write program to perform following:
Define structure named employee with name and age as member variables, make array of size five of employee structure, take values of name and age for all the five employees. Display average age of those five employees.

Answer

#include <stdio.h>

struct employee
{
    char name[30];
    int age;
};

int main()
{
    struct employee e[5];
    int i, sum = 0;
    float avg;

    for (i = 0; i < 5; i++)
    {
        printf("Enter name and age of employee %d: ", i + 1);
        scanf("%s %d", e[i].name, &e[i].age);
        sum += e[i].age;
    }
    avg = sum / 5.0;
    printf("Average age = %.2f\n", avg);
    return 0;
}

Dividing by 5.0 (not 5) avoids integer division.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 1 mark

Define Linker.

Answer

A linker is a system program that combines the object code produced by the compiler with the object code of library functions and other modules to produce a single executable file.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 1 mark

Differentiate between literal and symbolic constant.

Answer

A literal constant is a fixed value written directly in the program, such as 25, 3.14 or 'A'. A symbolic constant is a name given to a constant value, defined with #define (for example #define PI 3.14159) or const. Symbolic constants make a program easier to read and to change, because the value is altered in one place only.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 1 mark

What is type casting?

Answer

Type casting is the explicit conversion of a value from one data type to another using the cast operator (type).

int a = 7, b = 2;
float r = (float)a / b;   /* r = 3.5 */

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 1 mark

What do you mean by precedence rule?

Answer

The precedence rule defines the order in which operators are evaluated in an expression that has more than one operator. An operator with higher precedence is evaluated before one with lower precedence (for example * before +). When operators have equal precedence, their associativity (left to right or right to left) decides the order. Parentheses can be used to override it.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 1 mark

Why is getch function used?

Answer

getch() (from <conio.h>) reads a single character from the keyboard without displaying it on the screen and without waiting for the Enter key. It is often used at the end of a program to hold the output screen until a key is pressed, and to read passwords.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 1 mark

When is continue statement used?

Answer

The continue statement is used inside a loop to skip the remaining statements of the current iteration and move directly to the next iteration (the condition test in while/do-while, or the increment step in for).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 1 mark

What does double pointer mean?

Answer

A double pointer (pointer to pointer) is a pointer that stores the address of another pointer variable. It is declared with two asterisks.

int x = 10;
int *p = &x;
int **pp = &p;   /* **pp is 10 */

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 1 mark

What is linked list?

Answer

A linked list is a dynamic data structure made of a collection of nodes, where each node contains data and a pointer to the next node. The last node points to NULL. Nodes are created at run time with malloc(), so the list can grow or shrink easily and its nodes need not be stored in contiguous memory.

struct node
{
    int data;
    struct node *next;
};

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 1 mark

List the arguments given to function strcmp().

Answer

strcmp(s1, s2) takes two arguments: the two strings s1 and s2 to be compared. It returns 0 if they are equal, a negative value if s1 is less than s2, and a positive value if s1 is greater than s2.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 1 mark

Define nested structure with example.

Answer

A nested structure is a structure that contains another structure as one of its members.

struct date
{
    int day, month, year;
};

struct student
{
    char name[30];
    struct date dob;   /* nested structure */
};

struct student s = {"Ram", {5, 10, 2000}};
printf("%d", s.dob.year);   /* member access using two dots */

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 3 marks

Display the following pattern using loop
BBA
BIM
BIM
BBA
BBA
BIM

Answer

The rows follow the repeating sequence BBA, BIM, BIM, so the pattern is printed by choosing one of three strings in a loop.

#include <stdio.h>

int main()
{
    char *t[2] = {"BBA", "BIM"};
    int order[6] = {0, 1, 1, 0, 0, 1};   /* 0 = BBA, 1 = BIM */
    int i;
    for (i = 0; i < 6; i++)
        printf("%s\n", t[order[i]]);
    return 0;
}

Output:

BBA
BIM
BIM
BBA
BBA
BIM

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 3 marks

Write a program to count white spaces in a given sentence.

Answer

#include <stdio.h>

int main()
{
    char s[200];
    int i, count = 0;

    printf("Enter a sentence: ");
    gets(s);
    for (i = 0; s[i] != '\0'; i++)
        if (s[i] == ' ')
            count++;
    printf("Number of white spaces = %d\n", count);
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 3 marks

Write a program to calculate sum of the following numbers: -3, -4, 8, 9, 10, -17

Answer

#include <stdio.h>

int main()
{
    int a[6] = {-3, -4, 8, 9, 10, -17};
    int i, sum = 0;
    for (i = 0; i < 6; i++)
        sum += a[i];
    printf("Sum = %d\n", sum);
    return 0;
}

The sum is −3−4+8+9+10−17=3-3 - 4 + 8 + 9 + 10 - 17 = 3.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 3 marks

Write a function to check if the entered prime number is greater than 90 and is odd.

Answer

#include <stdio.h>

int check(int n)
{
    int i;
    if (n <= 90 || n % 2 == 0)
        return 0;
    for (i = 3; i * i <= n; i += 2)
        if (n % i == 0)
            return 0;      /* not prime */
    return 1;              /* prime, greater than 90 and odd */
}

int main()
{
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    if (check(n))
        printf("%d is a prime number greater than 90 and odd\n", n);
    else
        printf("Condition not satisfied\n");
    return 0;
}

For example, 97 returns 1, while 91 (=7×13=7 \times 13) and 100 return 0.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 3 marks

Write a function that uppercase string to lowercase string.

Answer

#include <stdio.h>
#include <ctype.h>

void toLower(char str[])
{
    int i;
    for (i = 0; str[i] != '\0'; i++)
        str[i] = tolower(str[i]);   /* A-Z becomes a-z, others unchanged */
}

int main()
{
    char s[50] = "HELLO World";
    toLower(s);
    printf("%s", s);   /* hello world */
    return 0;
}

Without ctype.h, the conversion can be written as if (str[i] >= 'A' && str[i] <= 'Z') str[i] = str[i] + 32;.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 5 marks

What is file? Explain the modes in which file can be opened.

Answer

A file is a named collection of related data stored permanently on secondary storage (disk). Unlike variables in RAM, its contents remain after the program ends. In C, a file is accessed through a pointer of type FILE * using fopen():

FILE *fp;
fp = fopen("data.txt", "r");

File opening modes

ModeMeaning
rOpen an existing text file for reading. Returns NULL if the file does not exist.
wOpen for writing. Creates the file, or erases the old contents if it exists.
aOpen for appending. Data is added at the end; file is created if absent.
r+Open an existing file for both reading and writing.
w+Open for reading and writing; the file is created or truncated.
a+Open for reading and appending; the file is created if absent.

Adding b (for example rb, wb, ab) opens the file in binary mode.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2016 AD · 5 marks

Write the output generated by following program.
#include<stdio.h>
#include<conio.h>

main(){
  int a,b,c;
  clrsc();
  for(a=1;a<=3;a++){
    for (b=1;b<=3;b++){
      for(c=1;c<=3;c++){
        if(a==b || b==c || a==c){
          continue ;
        }else{
          printf("%d%d%d\n" ,a,b,c);
        }
      }
    }
  }
  getch();
}

Answer

The innermost printf runs only when aa, bb, cc are all different (otherwise continue skips it). So the program prints every permutation of 1, 2, 3 in the order of the loops:

123
132
213
231
312
321

(clrsc() is a misspelling of clrscr() and would give a compile or link error in a strict compiler; assuming it is intended as clrscr(), the output is as above.)

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 1 mark

What is constant?

Answer

A constant is a fixed value that cannot be changed during program execution, for example 10, 3.14, 'A' or a variable declared as const int max = 100;.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 1 mark

Does a break required by default case in switch statement?

Answer

No. The default case is normally written last, so control simply leaves the switch after executing it. A break is needed only if default is placed before other cases, otherwise execution would fall through into the following cases.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 1 mark

What is type casting in C Programming?

Answer

Type casting is converting a value from one data type to another, either implicitly by the compiler or explicitly by the programmer using (type) expression. For example, float avg = (float) sum / n; converts sum to float so that the division is not an integer division.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 1 mark

Define structure.

Answer

A structure is a user-defined data type that groups variables of different data types under one name using the struct keyword, for example struct student { char name[20]; int roll; float marks; };.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 1 mark

What is the use of gets function?

Answer

gets() reads a whole line of text, including spaces, from standard input (keyboard) into a character array until the Enter key is pressed. The newline is replaced by the null character '\0'. It does no bounds checking, so fgets() is safer.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 1 mark

What is double pointer?

Answer

A double pointer (pointer to pointer) is a pointer variable that stores the address of another pointer variable. It is declared with two asterisks, for example int **pp; where pp = &p; and p = &x;. Then **pp gives the value of x.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 1 mark

From which function C program starts?

Answer

A C program starts executing from the main() function.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 1 mark

What is use of == operator?

Answer

== is the equality (relational) operator. It compares two operands and gives 1 (true) if they are equal and 0 (false) otherwise, for example if (a == b). It is different from =, which is the assignment operator.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 1 mark

What is wrong with this statement? scanf("%", iden 1);

Answer

The statement scanf("%", iden 1); has two errors. First, the format specifier is incomplete: a conversion character such as d, f or c must follow % (for example %d). Second, iden 1 is not a valid variable name, and the argument must be the address of a variable, so it needs the & operator. A correct form is scanf("%d", &iden1);.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 3 marks

Write a program to add four integers taken from user.

Answer

#include <stdio.h>

int main()
{
    int a, b, c, d, sum;
    printf("Enter four integers: ");
    scanf("%d %d %d %d", &a, &b, &c, &d);
    sum = a + b + c + d;
    printf("Sum = %d", sum);
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 3 marks

Explain dynamic memory allocation with example.

Answer

Dynamic memory allocation means allocating memory at run time from the heap, so the size need not be fixed in advance. The functions are in <stdlib.h>:

  • malloc(size) allocates size bytes (uninitialised).
  • calloc(n, size) allocates memory for n elements and sets them to zero.
  • realloc(ptr, newsize) changes the size of a block already allocated.
  • free(ptr) releases the memory.

Example

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n, i, *p;
    printf("How many numbers? ");
    scanf("%d", &n);
    p = (int *) malloc(n * sizeof(int));
    if (p == NULL) {
        printf("Memory not available");
        return 1;
    }
    for (i = 0; i < n; i++)
        scanf("%d", &p[i]);
    for (i = 0; i < n; i++)
        printf("%d ", p[i]);
    free(p);
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 3 marks

List and explain different file opening modes.

Answer

A file is opened with fp = fopen("name", "mode");. The modes are:

  • r: open an existing text file for reading; returns NULL if it does not exist.
  • w: open for writing; creates the file or erases existing contents.
  • a: open for appending; new data is written at the end of the file.
  • r+: open an existing file for reading and writing.
  • w+: open for reading and writing; creates or truncates the file.
  • a+: open for reading and appending.

Adding b (rb, wb, ab, ...) selects binary mode.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 3 marks

Write a function to check whether a given number is 987 or not.

Answer

#include <stdio.h>

int is987(int n)
{
    if (n == 987)
        return 1;   /* the number is 987 */
    else
        return 0;
}

int main()
{
    int x;
    printf("Enter a number: ");
    scanf("%d", &x);
    if (is987(x))
        printf("The number is 987");
    else
        printf("The number is not 987");
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 3 marks

Write a function to copy content of one array to another array. You can consider any type of arrays.

Answer

#include <stdio.h>

void copyArray(int src[], int dest[], int n)
{
    int i;
    for (i = 0; i < n; i++)
        dest[i] = src[i];
}

int main()
{
    int a[5] = {1, 2, 3, 4, 5}, b[5], i;
    copyArray(a, b, 5);
    for (i = 0; i < 5; i++)
        printf("%d ", b[i]);   /* 1 2 3 4 5 */
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 5 marks

List any five string handling function. Write a program to swap contents of two string variables with each other.

Answer

Five string handling functions (in <string.h>)

  1. strlen(s) returns the length of the string (without '\0').
  2. strcpy(d, s) copies string s into d.
  3. strcat(d, s) appends s to the end of d.
  4. strcmp(s1, s2) compares two strings; returns 0 if equal, negative or positive otherwise.
  5. strrev(s) reverses a string (non-standard); strupr()/strlwr() or strncpy() may also be used.

Program to swap two strings

#include <stdio.h>
#include <string.h>

int main()
{
    char s1[50], s2[50], temp[50];
    printf("Enter first string: ");
    gets(s1);
    printf("Enter second string: ");
    gets(s2);

    strcpy(temp, s1);
    strcpy(s1, s2);
    strcpy(s2, temp);

    printf("After swapping:\nFirst = %s\nSecond = %s", s1, s2);
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 5 marks

Write a program to print following pattern using for loop:
2
4
8
14
22

Answer

The terms are 2, 4, 8, 14, 22. The differences are 2, 4, 6, 8, so each term is the previous term plus 2i2i (where ii is the step number).

#include <stdio.h>

int main()
{
    int i, n = 2;
    for (i = 1; i <= 5; i++) {
        printf("%d\n", n);
        n = n + 2 * i;
    }
    return 0;
}

Output: 2, 4, 8, 14, 22 (check: 2+2=42+2=4, 4+4=84+4=8, 8+6=148+6=14, 14+8=2214+8=22).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2017 AD · 5 marks

Write a program to store value of two integer variables in a file named "Two_Rupees.txt"

Answer

#include <stdio.h>

int main()
{
    int a, b;
    FILE *fp;

    printf("Enter two integers: ");
    scanf("%d %d", &a, &b);

    fp = fopen("Two_Rupees.txt", "w");
    if (fp == NULL) {
        printf("File cannot be opened");
        return 1;
    }
    fprintf(fp, "%d %d", a, b);
    fclose(fp);
    printf("Values stored in file.");
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 1 mark

Why is if statement used?

Answer

The if statement is used for decision making: it executes a statement or block only when a given condition is true, so the program can choose between different paths.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 1 mark

What is unary operator?

Answer

A unary operator works on a single operand. Examples are ++, --, unary -, !, &, * and sizeof, as in -a, i++ or !flag.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 1 mark

What is the difference between float and double data type?

Answer

float is single precision: 4 bytes, about 6 to 7 significant digits. double is double precision: 8 bytes, about 15 to 16 significant digits, with a larger range.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 1 mark

Define pointer variable.

Answer

A pointer variable is a variable that stores the memory address of another variable. It is declared with *, for example int *p = &x;.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 1 mark

Write the syntax of gets() function.

Answer

char *gets(char *str);

Example: gets(name); where name is a character array.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 1 mark

What are the arguments passed to main () function?

Answer

main() can receive command-line arguments through two parameters: int main(int argc, char *argv[]). Here argc is the number of arguments (including the program name) and argv is an array of strings holding the arguments.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 1 mark

What are the different meanings of symbol ** in C language?

Answer

The symbol * has these meanings in C:

  1. Multiplication operator, as in a * b.
  2. Pointer declaration, as in int *p;.
  3. Indirection (dereference) operator, giving the value at an address, as in *p.

A double ** is used for a pointer to pointer, as in int **pp; (and **pp is the value reached through two levels). It is not an exponent operator in C.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 1 mark

How union saves memory?

Answer

In a union all members share the same memory location, and the size of the union is that of its largest member. Only one member can hold a value at a time, so memory is saved compared with a structure, which allocates separate space for every member.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 1 mark

How file is opened in C language?

Answer

A file is opened using the fopen() function, which returns a FILE pointer:

FILE *fp;
fp = fopen("filename.txt", "r");
if (fp == NULL)
    printf("Cannot open file");

The second argument is the mode (r, w, a, etc.). The file is closed with fclose(fp).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 1 mark

What does free() function do?

Answer

free(ptr) releases a block of memory previously allocated by malloc(), calloc() or realloc() back to the system so that it can be reused, preventing memory leaks.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 3 marks

Write function which accepts two integers as argument and displays sum those numbers.

Answer

#include <stdio.h>

void showSum(int a, int b)
{
    printf("Sum = %d\n", a + b);
}

int main()
{
    showSum(12, 8);   /* Sum = 20 */
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 3 marks

Write program to write 'hello world' to file named hello.dat.

Answer

#include <stdio.h>

int main()
{
    FILE *fp;
    fp = fopen("hello.dat", "w");
    if (fp == NULL) {
        printf("Cannot open file");
        return 1;
    }
    fprintf(fp, "hello world");
    fclose(fp);
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 3 marks

Create nested structure and do I/O for members of it.

Answer

A nested structure has a structure as a member of another structure. Here date is nested in student.

#include <stdio.h>

struct date {
    int day, month, year;
};

struct student {
    char name[30];
    int roll;
    struct date dob;      /* nested structure */
};

int main()
{
    struct student s;

    printf("Enter name: ");
    scanf("%s", s.name);
    printf("Enter roll: ");
    scanf("%d", &s.roll);
    printf("Enter date of birth (dd mm yyyy): ");
    scanf("%d %d %d", &s.dob.day, &s.dob.month, &s.dob.year);

    printf("\nName: %s\nRoll: %d\nDOB: %d/%d/%d\n",
           s.name, s.roll, s.dob.day, s.dob.month, s.dob.year);
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 3 marks

Write function, which accepts one integer as argument and returns factorial number one less than that argument.

Answer

The function returns (n−1)!(n-1)!.

#include <stdio.h>

long factOfPrevious(int n)
{
    long f = 1;
    int i;
    for (i = 2; i <= n - 1; i++)
        f = f * i;
    return f;
}

int main()
{
    printf("%ld", factOfPrevious(6));   /* 5! = 120 */
    return 0;
}

For n≤2n \le 2 the loop does not run and the function returns 1, which is correct since 0!=1!=10! = 1! = 1.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 3 marks

Write program to add numbers at even position of integer array of size 50.

Answer

Taking positions as 1, 2, 3, ... (so even positions are 2, 4, ..., 50), these are array indices 1, 3, 5, ..., 49.

#include <stdio.h>

int main()
{
    int a[50], i, sum = 0;
    printf("Enter 50 integers:\n");
    for (i = 0; i < 50; i++)
        scanf("%d", &a[i]);

    for (i = 1; i < 50; i += 2)    /* positions 2, 4, ..., 50 */
        sum += a[i];

    printf("Sum at even positions = %d", sum);
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 5 marks

What is linked list? Explain in which situations linked list is preferred over array.

Answer

A linked list is a dynamic data structure made of nodes. Each node holds data and a pointer to the next node; the last node points to NULL. A head pointer holds the address of the first node. Nodes are created with malloc() and are not stored contiguously.

struct node {
    int data;
    struct node *next;
};

Situations where a linked list is preferred over an array

  1. The number of elements is not known in advance or changes often, since a list grows and shrinks at run time while an array has a fixed size.
  2. Frequent insertion or deletion in the middle or at the front: only pointers change, whereas an array must shift elements.
  3. Memory should be used efficiently: no unused reserved space, and no need for one large contiguous block.
  4. Implementing stacks, queues and similar structures that change size.

The drawback is that elements cannot be accessed directly by index, and each node needs extra memory for the pointer.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 5 marks

Why files are used in program? Describe sequential and random access of file and also explain any two file handling functions.

Answer

Why files are used

Variables live in RAM and are lost when the program ends. Files store data permanently on disk, allow large volumes of data to be handled, let data be shared between programs, and let input and output be kept without typing it again.

Sequential access reads or writes data in order from the beginning to the end. To reach the 10th record, the first nine must be passed. Functions such as fscanf(), fgets() and fgetc() are used in this way. It is simple, suitable for processing all records.

Random (direct) access allows moving straight to any position in the file without reading what comes before, using fseek(), ftell() and rewind(). It is faster for retrieving or updating a particular record.

Two file handling functions

  1. fopen(filename, mode) opens a file and returns a FILE pointer, or NULL on failure. Example: fp = fopen("a.txt", "r");.
  2. fprintf(fp, format, ...) writes formatted data into a file, for example fprintf(fp, "%d", x);. (Similarly fclose(fp) closes the file and saves the data.)

fseek(fp, offset, origin) moves the file position indicator: fseek(fp, 0, SEEK_END); goes to the end of the file.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2018 AD · 5 marks

Differentiate between switch..case and if...else statement. Write importance of conditional statements.

Answer

Difference between switch..case and if...else

switch..caseif...else
Selects one of many alternatives by comparing one expression with constant values.Tests any logical or relational condition.
The expression must be an integer or character type.Can test floats, ranges and compound conditions (&&, `
Uses case labels, break and default.Uses else if and else.
Cannot test ranges such as x > 10.Can test ranges.
More readable and often faster for many fixed choices (menus).Better for few choices or complex conditions.

Importance of conditional statements

Conditional statements let a program take decisions and choose different paths of execution depending on the data. Without them a program would execute the same sequence every time. They are used for validating input, handling errors, building menus, comparing values, and implementing the logic of nearly every real program.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2021 Aug · 1 mark

What is a preprocessor?

Answer

The preprocessor is a program that processes the source code before compilation. It handles directives that begin with #, such as #include, #define and #ifdef, performing file inclusion, macro expansion and conditional compilation.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2021 Aug · 1 mark

Differentiate between linker and compiler.

Answer

A compiler translates source code (.c) into object code (.obj/.o) and reports syntax errors. A linker combines one or more object files with library code (e.g. printf from the standard library) to produce a single executable file.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2021 Aug · 1 mark

Write the syntax of do...while loop.

Answer

do {
    statement(s);
} while (condition);

The body executes first and the condition is tested afterwards, so the body runs at least once.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2021 Aug · 1 mark

Give an example of implicit type casting.

Answer

Implicit type casting is done automatically by the compiler when types are mixed in an expression.

int a = 5;
float b = 2.5, c;
c = a + b;   /* a is converted to float automatically; c = 7.5 */

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2021 Aug · 1 mark

What is the use of fread()?

Answer

fread() reads a block of data from a file into memory. Syntax: fread(&ptr, size, count, fp); It reads count items, each of size bytes, from the file pointed to by fp into ptr, and is commonly used to read structures from binary files.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2021 Aug · 1 mark

Write the syntax for two dimensional integer array.

Answer

int array_name[rows][columns];

Example: int a[3][4]; declares a 2-D integer array with 3 rows and 4 columns.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2021 Aug · 1 mark

How do you pass the reference of a variable in a function as parameter?

Answer

Pass the address of the variable using the address-of operator & in the call, and receive it in a pointer parameter in the function (call by reference).

void change(int *p) { *p = 10; }

int main() {
    int x = 5;
    change(&x);   /* x becomes 10 */
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2021 Aug · 1 mark

List the uses of %d and %c.

Answer

  • %d is the format specifier for an integer (int); it is used in printf to print an integer and in scanf to read one, e.g. scanf("%d", &n);.
  • %c is the format specifier for a single character (char); it is used to print or read one character, e.g. printf("%c", ch);.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2021 Aug · 1 mark

How is structure different from union?

Answer

StructureUnion
Declared with the keyword struct.Declared with the keyword union.
Each member has its own memory; size is the sum of all members (with padding).All members share the same memory; size is that of the largest member.
All members can hold values at the same time.Only one member holds a valid value at a time.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2021 Aug · 1 mark

Write the syntax of conditional operator.

Answer

variable = (condition) ? expression1 : expression2;

If the condition is true, expression1 is evaluated, otherwise expression2. Example: max = (a > b) ? a : b;

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2021 Aug · 3 marks

Write a program to display all the numbers from 100 to 200 that are divisible by 4.

Answer

#include <stdio.h>

int main() {
    int i;
    printf("Numbers from 100 to 200 divisible by 4:\n");
    for (i = 100; i <= 200; i++) {
        if (i % 4 == 0)
            printf("%d ", i);
    }
    return 0;
}

The loop checks every number from 100 to 200 and prints those whose remainder on division by 4 is zero (100, 104, ..., 200).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2021 Aug · 3 marks

Create a function int greater (int, int) that returning the greater number between them.

Answer

#include <stdio.h>

int greater(int a, int b) {
    if (a > b)
        return a;
    else
        return b;
}

int main() {
    int x, y;
    printf("Enter two numbers: ");
    scanf("%d %d", &x, &y);
    printf("Greater number = %d", greater(x, y));
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2021 Aug · 3 marks

Write a program that illustrates the use of pointers.

Answer

A pointer is a variable that stores the address of another variable; * dereferences it.

#include <stdio.h>

int main() {
    int a = 10;
    int *p;

    p = &a;                       /* p stores address of a */
    printf("Value of a = %d\n", a);
    printf("Address of a = %p\n", (void *)p);
    printf("Value through pointer = %d\n", *p);

    *p = 20;                      /* changes a through the pointer */
    printf("New value of a = %d\n", a);
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2021 Aug · 3 marks

Write a program to input a string and check whether it is palindrome or not.

Answer

#include <stdio.h>
#include <string.h>

int main() {
    char str[100];
    int i, len, flag = 1;

    printf("Enter a string: ");
    scanf("%s", str);
    len = strlen(str);

    for (i = 0; i < len / 2; i++) {
        if (str[i] != str[len - 1 - i]) {
            flag = 0;
            break;
        }
    }

    if (flag)
        printf("%s is a palindrome", str);
    else
        printf("%s is not a palindrome", str);
    return 0;
}

The first character is compared with the last, the second with the second last, and so on; any mismatch means it is not a palindrome.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2021 Aug · 3 marks

A data file "student.txt" contains name, age, address, cell number of some students. Write a program to list all students whose address is "Pokhara".

Answer

Assuming each line of the file holds name age address cellnumber separated by spaces:

#include <stdio.h>
#include <string.h>

int main() {
    FILE *fp;
    char name[50], address[50], cell[20];
    int age;

    fp = fopen("student.txt", "r");
    if (fp == NULL) {
        printf("Cannot open file");
        return 1;
    }

    while (fscanf(fp, "%s %d %s %s", name, &age, address, cell) == 4) {
        if (strcmp(address, "Pokhara") == 0)
            printf("%s %d %s %s\n", name, age, address, cell);
    }

    fclose(fp);
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2021 Aug · 5 marks

Write a program to input 10 integers in an array and find the maximum and minimum

Answer

#include <stdio.h>

int main() {
    int a[10], i, max, min;

    printf("Enter 10 integers:\n");
    for (i = 0; i < 10; i++)
        scanf("%d", &a[i]);

    max = min = a[0];
    for (i = 1; i < 10; i++) {
        if (a[i] > max) max = a[i];
        if (a[i] < min) min = a[i];
    }

    printf("Maximum = %d\n", max);
    printf("Minimum = %d\n", min);
    return 0;
}

Both max and min start with the first element and are updated while scanning the remaining nine.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2021 Aug · 5 marks

Create a structure named Employee with data members name, salary and address. Take the data of 5 employees and display the name of employee whose salary>10,000.

Answer

#include <stdio.h>

struct Employee {
    char name[50];
    float salary;
    char address[50];
};

int main() {
    struct Employee e[5];
    int i;

    for (i = 0; i < 5; i++) {
        printf("Enter name, salary and address of employee %d: ", i + 1);
        scanf("%s %f %s", e[i].name, &e[i].salary, e[i].address);
    }

    printf("Employees with salary > 10000:\n");
    for (i = 0; i < 5; i++) {
        if (e[i].salary > 10000)
            printf("%s\n", e[i].name);
    }
    return 0;
}

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2021 Aug · 5 marks

Illustrate the importance of switch case with an example.

Answer

The switch statement selects one of many blocks of code according to the value of an expression. It is more readable and easier to maintain than a long if...else if ladder when one variable is compared against many constant values, and the compiler can often execute it faster.

Syntax:

switch (expression) {
    case value1: statements; break;
    case value2: statements; break;
    default: statements;
}

Example: a simple calculator.

#include <stdio.h>

int main() {
    float a, b;
    char op;

    printf("Enter expression (e.g. 5 + 3): ");
    scanf("%f %c %f", &a, &op, &b);

    switch (op) {
        case '+': printf("Result = %.2f", a + b); break;
        case '-': printf("Result = %.2f", a - b); break;
        case '*': printf("Result = %.2f", a * b); break;
        case '/':
            if (b != 0) printf("Result = %.2f", a / b);
            else printf("Division by zero");
            break;
        default: printf("Invalid operator");
    }
    return 0;
}

The break statement stops execution from falling through into the next case, and default handles values that match no case.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

Questions from TU BIM Business Communication (MGT 204) 2001 AD (Board exam paper, 2001 AD), TU BIM Business Communication (MGT 204) 2002 AD (Board exam paper, 2002 AD), TU BIM Business Communication (MGT 204) 2003 AD (Board exam paper, 2003 AD), TU BIM Business Communication (MGT 204) 2005 AD (Board exam paper, 2005 AD), TU BIM Business Communication (MGT 204) 2007 AD (Board exam paper, 2007 AD), TU BIM Business Communication (MGT 204) 2008 AD (Board exam paper, 2008 AD), TU BIM Business Communication (MGT 204) 2009 AD (Board exam paper, 2009 AD), TU BIM Business Communication (MGT 204) 2010 AD (Board exam paper, 2010 AD), TU BIM Business Communication (MGT 204) 2011 AD (Board exam paper, 2011 AD), TU BIM Business Communication (MGT 204) 2012 AD (Board exam paper, 2012 AD), TU BIM Business Communication (MGT 204) 2013 AD (Board exam paper, 2013 AD), TU BIM Business Communication (MGT 204) 2014 AD (Board exam paper, 2014 AD), TU BIM Business Communication (MGT 204) 2015 AD (Board exam paper, 2015 AD), TU BIM Business Communication (MGT 204) 2016 AD (Board exam paper, 2016 AD), TU BIM Business Communication (MGT 204) 2017 AD (Board exam paper, 2017 AD), TU BIM Business Communication (MGT 204) 2018 AD (Board exam paper, 2018 AD), TU BIM Business Communication (MGT 204) 2019 AD (Board exam paper, 2019 AD), TU BIM Business Communication (MGT 204) 2021 AD (Board exam paper, 2021 AD), TU BIM Discrete Mathematics (MTH 202) 2005 AD (Board exam paper, 2005 AD), TU BIM Discrete Mathematics (MTH 202) 2007 AD (Board exam paper, 2007 AD), TU BIM Discrete Mathematics (MTH 202) 2008 AD (Board exam paper, 2008 AD), TU BIM Discrete Mathematics (MTH 202) 2009 AD (Board exam paper, 2009 AD), TU BIM Discrete Mathematics (MTH 202) 2010 AD (Board exam paper, 2010 AD), TU BIM Discrete Mathematics (MTH 202) 2011 AD (Board exam paper, 2011 AD), TU BIM Discrete Mathematics (MTH 202) 2012 AD (Board exam paper, 2012 AD), TU BIM Discrete Mathematics (MTH 202) 2013 AD (Board exam paper, 2013 AD), TU BIM Discrete Mathematics (MTH 202) 2014 AD (Board exam paper, 2014 AD), TU BIM Discrete Mathematics (MTH 202) 2016 AD (Board exam paper, 2016 AD), TU BIM Discrete Mathematics (MTH 202) 2017 AD (Board exam paper, 2017 AD), TU BIM Discrete Mathematics (MTH 202) 2018 AD (Board exam paper, 2018 AD), TU BIM Discrete Mathematics (MTH 202) 2019 AD (Board exam paper, 2019 AD), TU BIM Discrete Mathematics (MTH 202) 2021 AD (Board exam paper, 2021 AD), TU BIM Data Communication and Computer Network (ITC 214) 2001 AD (Board exam paper, 2001 AD), TU BIM Data Communication and Computer Network (ITC 214) 2002 AD (Board exam paper, 2002 AD), TU BIM Data Communication and Computer Network (ITC 214) 2004 AD (Board exam paper, 2004 AD), TU BIM Data Communication and Computer Network (ITC 214) 2005 AD (Board exam paper, 2005 AD), TU BIM Data Communication and Computer Network (ITC 214) 2009 AD (Board exam paper, 2009 AD), TU BIM Data Communication and Computer Network (ITC 214) 2010 AD (Board exam paper, 2010 AD), TU BIM Data Communication and Computer Network (ITC 214) 2011 AD (Board exam paper, 2011 AD), TU BIM Data Communication and Computer Network (ITC 214) 2012 AD (Board exam paper, 2012 AD), TU BIM Data Communication and Computer Network (ITC 214) 2013 AD (Board exam paper, 2013 AD), TU BIM Data Communication and Computer Network (ITC 214) 2014 AD (Board exam paper, 2014 AD), TU BIM Data Communication and Computer Network (ITC 214) 2015 AD (Board exam paper, 2015 AD), TU BIM Data Communication and Computer Network (ITC 214) 2016 AD (Board exam paper, 2016 AD), TU BIM Data Communication and Computer Network (ITC 214) 2017 AD (Board exam paper, 2017 AD), TU BIM Data Communication and Computer Network (ITC 214) 2018 AD (Board exam paper, 2018 AD), TU BIM Data Communication and Computer Network (ITC 214) 2019 AD (Board exam paper, 2019 AD), TU BIM Data Communication and Computer Network (ITC 214) 2021 AD (Board exam paper, 2021 AD), TU BIM Sociology for Business (SOC 201) 2001 AD (Board exam paper, 2001 AD), TU BIM Sociology for Business (SOC 201) 2002 AD (Board exam paper, 2002 AD), TU BIM Sociology for Business (SOC 201) 2003 AD (Board exam paper, 2003 AD), TU BIM Sociology for Business (SOC 201) 2005 AD (Board exam paper, 2005 AD), TU BIM Sociology for Business (SOC 201) 2008 AD (Board exam paper, 2008 AD), TU BIM Sociology for Business (SOC 201) 2009 AD (Board exam paper, 2009 AD), TU BIM Sociology for Business (SOC 201) 2011 AD (Board exam paper, 2011 AD), TU BIM Sociology for Business (SOC 201) 2012 AD (Board exam paper, 2012 AD), TU BIM Sociology for Business (SOC 201) 2013 AD (Board exam paper, 2013 AD), TU BIM Sociology for Business (SOC 201) 2014 AD (Board exam paper, 2014 AD), TU BIM Sociology for Business (SOC 201) 2015 AD (Board exam paper, 2015 AD), TU BIM Structured Programming (ITC 213) 2001 AD (Board exam paper, 2001 AD), TU BIM Structured Programming (ITC 213) 2002 AD (Board exam paper, 2002 AD), TU BIM Structured Programming (ITC 213) 2004 AD (Board exam paper, 2004 AD), TU BIM Structured Programming (ITC 213) 2007 AD (Board exam paper, 2007 AD), TU BIM Structured Programming (ITC 213) 2008 AD (Board exam paper, 2008 AD), TU BIM Structured Programming (ITC 213) 2009 AD (Board exam paper, 2009 AD), TU BIM Structured Programming (ITC 213) 2010 AD (Board exam paper, 2010 AD), TU BIM Structured Programming (ITC 213) 2012 AD (Board exam paper, 2012 AD), TU BIM Structured Programming (ITC 213) 2002 AD (Objective question paper, 2002 (20 MCQs, 0.5 marks each, full marks 10)), TU BIM Structured Programming (ITC 213) 2003 AD (Objective question paper, 2003 (20 MCQs, 0.5 marks each, full marks 10); item 10 as printed in the scan), TU BIM Structured Programming (ITC 213) 2013 AD (Board exam paper, 2013 (the scan of this paper ends after question 5)), TU BIM Structured Programming (ITC 213) 2014 AD (Board exam paper, 2014 AD), TU BIM Structured Programming (ITC 213) 2015 AD (Board exam paper, 2015 (the line "if( (m 2 ) == 0" is printed with a modulus sign that is illegible in the scan; read as m % 2)), TU BIM Structured Programming (ITC 213) 2016 AD (Board exam paper, 2016 (the first figure in question 2 is transcribed as printed)), TU BIM Structured Programming (ITC 213) 2017 AD (Board exam paper, August-September 2017), TU BIM Structured Programming (ITC 213) 2018 AD (Board exam paper, September-October 2018), TU BIM Structured Programming (ITC 213) 2021 Aug (Board exam paper, August 2021) and TU BIM Second Semester Model Questions 2022/23 (IT 232, IT 233, IT 235, IT 234, MGT 241) (Faculty of Management model question set, BIM second semester (new course), via EducateNepal). Answers are written for this site; check them against your class notes.