Programming In C 2017-18 Previous Year Paper Solution
Short Questions

Q2. Answer the following questions: Short answer type [Marks:2×10]

a) C is a structured programming language? Justify.

Ans: C is called a structured programming language because it solves a larger problem dividing into smaller structural blocks(functions, loops, if-else, switch-case, etc) each of which handles a particular responsibility. The program which solves the entire problem is a collection of such structural blocks.

b) Write a ‘C’ program to find the largest among two numbers using Conditional Operator.

Ans: C program code:

#include <stdio.h>

int main()
{
    int num1, num2, max;

    /*
     * Take Input two number from user
     */
    printf("Enter two numbers: ");
    scanf("%d%d", &num1, &num2);

    max = (num1 > num2) ? num1 : num2;

    printf("Maximum between %d and %d is %d", num1, num2, max);

    return 0;
}

c) What is the difference between while and do-while loops?

Ans: Difference between while and do-while loops:

while Loopdo-while loop
1. while loop is defined as per below syntax:
while ( condition)
{
//loop body
}
1. do-while is defined as per below syntax:
do{
//loop body.
} while( Condition );
2. In while loop, the condition is checked first then statements are executed.2. In the do-while loop, statements are executed atleast once, thereafter condition is checked.
3. The iterations do not occur if, the condition in the first iteration fails.3. The iteration occurs at least once even if the condition fails in the first iteration.
4. while loop is called ‘Entry-controlled’ loop.4. do-while loop is called as ‘Exit-controlled’ loop.
5. Semicolon is not required in the syntax.5. Semicolon is used at the end of the loop in the syntax.

d) Discuss about switch statement with an example?

Ans: The switch case statement is used when we have multiple scenarios and we need to perform a different task for each scenario.

The general syntax to define a switch case statement in C programming is as follow:

switch (expression)
{
     case constant1:
                     //C Statements;
                     break;
     case constant2:
                     //C Statements;
                     break;
     default:
                     //C Statements;
                     break;
}

Example: Building a Calculator

// Program to create a simple calculator
#include <stdio.h>

int main() {
    char opt;
    double num1, num2;

    printf("Enter an Operation (+, -, *, /): ");
    scanf("%c", &opt);
    printf("Enter two number: ");
    scanf("%lf %lf",&num1, &num2);

    switch(opt)
    {
        case '+':
            printf("%.1lf + %.1lf = %.1lf",num1, num2, num1+num2);
            break;

        case '-':
            printf("%.1lf - %.1lf = %.1lf",num1, num2, num1-num2);
            break;

        case '*':
            printf("%.1lf * %.1lf = %.1lf",num1, num2, num1*num2);
            break;

        case '/':
            printf("%.1lf / %.1lf = %.1lf",num1, num2, num1/num2);
            break;

        // Unkonow operation
        default:
            printf("Kindly provide a valid operation");
    }

    return 0;
}

e) Differentiate between call by value and call by reference.

Ans:

call by valuecall by reference
1. In call by value, a copy of the variable is passed1. In call by reference,  the variable itself is passed
2. Does not allow you to make any changes in the actual variables.2. Allows you to make changes in the values of variables by using function calls.
3. In call by value, different memory is allocated for actual and formal parameters.3. In call by reference, the memory allocation is same for both formal parameters and actual parameters.

f) What is the difference between assignment and equality operation?

Ans:

Assignment OperationEquality Operation
1. = is used in an Assignment Operation.1. == is used in an Equality Operation.
2. It assigns the value of right side expression’s or variable’s value to the left side variable.2. It compares value of left and side expressions, return 1 if they are equal other will it will return 0.

g) What are the limitation of array?

Ans: Limitation of array:

  1. The size of an array is determined at the moment the array is declared, and cannot be changed later on.
  2. Memory Allocated during Compile-time and Once allocated at it Cannot be Changed during Run-time.
  3. An array can hold data belonging to the same Data types.
  4. Wastage of Memory, if an array of large size is defined
  5. Searching and insertion operations on an array must be linear. For example, if we want to insert 9 into an array (10, 1, 0, 5) before 1, we must move 1 and 0 backward, remove 4, and get an array(10, 9, 1, 0).

h) Give an example of structure inside another structure.

Ans: C provides the feature of nesting one structure within another structure to create complex data types. For example, employee structure can contain another structure type for department details shown below:

 

#include<stdio.h>  
struct department   
{  
    char deptname[30];  
    char deptcode[14];  
};  
struct employee  
{  
    char name[20];  
    struct department dept;  
};  
void main ()  
{  
    struct employee emp;  
    printf("Enter employee information : \n");  
    scanf("%s %s %s",emp.name,emp.dept.deptname, &emp.dept.deptcode);  
    printf("Displaying the employee information : \n");  
    printf("Full Name: %s\nDepartmentName: %s\nDepartmentCode:%s\n",emp.name,emp.dept.deptname,emp.dept.deptcode);  
}  

i) How is a file pointer declared?

Ans: In C , to declare a file, we use a file pointer. A file pointer is a pointer variable that specifies the next byte to be read or written to. Every time a file is opened, the file pointer points to the beginning of the file. A file pointer is declared as follows:

FILE *fp;
//fp is the name of the file pointer

j) Differentiate between binary and text file?

Ans:

Text FileBinary File
1.  A text file contains textual information in the form of alphabets, digits and special characters or symbols1. binary file contains bytes or a compiled version of a text file.
2. An error in a textual file can be recognized and eliminated when seen.2. An error in a binary file corrupts the file and is not easy to detect.