Programming Fundamentals CMP-2122 Solved Past Paper 2017

University of Sargodha

M.S.c.I.T. First Term Exam 2017

Subject : I.T                    Paper: Programming Fundamentals   (CMP: 2122)

Time Allowed: 2:30 Hour

                                                                     Objective Part                 (Compulsory)

Q01.     Note: Attempt all questions each question carry equal marks?  (12*2=24)

1.      What is preprocessor directive?

ANS: Preprocessor are used to include header files in the program. The preprocessor includes the specified header file into the source code before compiling.

2.      What are comments and how do you insert it in a C program?

ANS: Comments are the textual note in code for the programmer that is ignored by the compiler while compiling. It has no effect on object code.

3.      Why we use do-while loop in C?

ANS: do-while loop is used to execute a block of statements continuously until the condition is true. A do while loop executes once even condition is false in start. Because it test the condition after executing the body of loop.

4.      What is variable initialization and why is it important?

ANS: Variable initialization means assigning a value to the variable at the time of variable declaration. It is important because without initialization a variable may have an unknown value, which can lead to unpredictable outputs when used in computations or other operation.

5.      What is the low level language?

ANS: In low level language Instruction are near to computer hardware and far from human understanding. A low level language is a programming language that is machine dependent.

6.      Describe the importance of default statement in switch statement?.

ANS: The default case in a switch statement will be activated if none of the other case values match. It is used exactly for this purpose – if nothing else matches in the switch then this one will always match. Without a default case value, if nothing matched in the switch then nothing will be done. Sometimes it is necessary to know that nothing matched.

7.      What are syntax errors? Why does it occur?

ANS: The rules of writing statements in a computer programming language are called syntax of the language. All program statements are written according to these rules. A single mistake in these rules causes an error. This error is called syntax error.

8.      What is the meaning of prototype of a function?

ANS: A function prototype  is a declaration of  function that specifies the function’s name and return type  and no of parameters(return type , arity and data types of parameters). In prototype function body is omitted. It is also called function header of function signature.

 

9.      It is necessary to initialize the const variable in C?

ANS:  A const variable is constant throughout its life. A value can’t be assigned to the const variable. so it must be initialize at the time of declaration otherwise there may be some garbage value in const variable which can’t be changed.

10.  Define a structure?

ANS: Structures are the collections of related variables under one name. Structure may contain variables of many different data type in contrast to array.

11.   What is the advantage of an array over individual variable?

ANS: An array is a homogenous collection of data. It helps in storing multiple values which are of same type. If the user wishes to store multiple values of similar type, an array is the best option which can be used, creating so much variable is a tedious and time consuming. Moreover a loop can be handled using a loop easily, which is impossible in case of individual variables. 

12.  What does the character “r” and ‘’w” mean when writing programs that will make use of files?

ANS: “r” and “w” are used to set file opening mode of file in fopen() function. “r” means that file will be open in reading mode and “w” indicates that file will be open in writing mode.

Subjective Part (9*4=36)

Q02.   Write a program that accept your age in years and then display age in days and months.

Program:

#include<stdio.h>

#include<conio.h>

main(){

            int years , days , months;

            printf(“Please enter your age in years: “);

            scanf(“%d”,&years);

            days = years * 365;

            months = years * 12;

            printf(“Your age in Dasys: %d\n”, days);

            printf(“Your age in Months: %d\n”, months);

            getch();           

}

 

 Q03.   Write a program that displays the following output using nest loops.

      *

    * * *

  * * * * *

* * * * * * *

Program:

 #include <stdio.h>

int main()

{

    int i, space, rows = 4, k=0;

  

    for(i=1; i<=rows; ++i, k=0)

    {

        for(space=1; space<=rows-i; ++space)

        {

            printf(”  “);

        }

        while(k != 2*i-1)

        {

            printf(“* “);

            ++k;

        }

        printf(“\n”);

    }

   }

 

Q04.   Write a C program that copies contents of a text file to another file.

Program:

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

main()

{

            FILE *out , *in;

            char ch;

if((in = fopen(“input.txt” , “r”))==NULL)

puts(“File in not opened”);

            out = fopen(“output.txt”, “w”);

            while((ch = fgetc(in))!=EOF)

{

                        fputc(ch, out);

}

            fclose(out);

            fclose(in);

            getch();

 }

 

Q05.   Write a program that takes ten elements (array) from user. Using bubble sort, sort the array in ascending order.

Program:

#include <stdio.h>

#include <conio.h>

#define SIZE 10

 main( void )

{

 // initialize a

 int a[ SIZE ];

 int pass , i, hold;

puts( “Please Enter 10 number to sort ” );

 

 for ( i = 0; i < SIZE; ++i ) {

 scanf( “%d”,&a[ i ] );

 }

for ( pass = 1; pass < SIZE; ++pass ) {

for ( i = 0; i < SIZE – 1; ++i )

{

if ( a[ i ] > a[ i + 1 ] ) {

hold = a[ i ];

a[ i ] = a[ i + 1 ];

a[ i + 1 ] = hold;

}

} }

puts( “\n Data items in ascending order” );

for ( i = 0; i < SIZE; ++i ) {

 printf( “%4d”, a[ i ] );

 }

 getch();

}

  06.   Write a C program to find factorial of number using function recursion.

Program:

#include <stdio.h>

#include <conio.h>

 int fictroial(int n);

int main()

{

    int n;

    printf(“Enter a positive integer: “);

    scanf(“%d”, &n);

    printf(“Factorial of %d = %d”, n, fictroial(n));

    getch();;

}

int fictroial(int n)

{

    if (n >= 1)

        return n*fictroial(n-1);

    else

        return 1;

}

Q07.   Write a four function calculator that performs addition, subtraction, multiplication and division that takes two operands and an operator from user. Perform this logic using switch statement.   


Program:

# include <stdio.h>

# include <conio.h>

 main() {

    char op;

    double firstNumber,secondNumber;

    printf(“Enter an operator (+, -, *,/): “);

    scanf(“%c”, &op);

    printf(“Enter First Number: “);

    scanf(“%lf”,&firstNumber);

    printf(“Enter Second Number: “);

    scanf(“%lf”,&secondNumber);

    switch(op)

    {

        case ‘+’:

            printf(“%.1lf + %.1lf = %.1lf”,firstNumber, secondNumber, firstNumber + secondNumber);

            break;

        case ‘-‘:

            printf(“%.1lf – %.1lf = %.1lf”,firstNumber, secondNumber, firstNumber – secondNumber);

            break;

        case ‘*’:

            printf(“%.1lf * %.1lf = %.1lf”,firstNumber, secondNumber, firstNumber * secondNumber);

            break;

        case ‘/’:

            printf(“%.1lf / %.1lf = %.1lf”,firstNumber, secondNumber, firstNumber / secondNumber);

            break;

  

        default:

            printf(“Error! operator is not correct”);

    }

        getch();

}

You May Like Also

2 thoughts on “Programming Fundamentals CMP-2122 Solved Past Paper 2017”

Leave a Comment

Talk with Admin
Join Rising Education Whatsapp Group