Programming Fundamentals CMP-2122 Solved Past Paper 2015

                                              University of Sargodha

                                            M.s.c.I.T, First Term Exam 2015

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

Time Allowed: 2:30 Hour

                                           Objective Part                 (Compulsory)

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

1.      What are comments in C?

Answer:

Comments are the textual note in code for the programmer. Comments explain what the code does, and ignored by the compiler while compiling. It has no effect on object code.

2.      What are escape sequence?

Answer:

A combination of two or more characters in formatted string to control the display of string on output device is called Escape sequence. i.e. \n , \t.

3.      What is the use of preprocessor in C language?

Answer:

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

4.      What is the right declaration for main function? Is void main () correct?

Answer:

The right declaration is

void main(){

}

Yes, void main is correct.

 

5.      Define the valid identifier?

Answer:

There are certain rules of Identifier and a valid identifier is one which follow all the rules. Some of these rules are:

1.      An Identifier must start from character or under score.

2.      An Identifier may contain up to 31 characters.

3.      There must be no white space in Identifier.

4.      Reserve Words can’t be used as Identifier.

6.      What is flow-chart?

Answer:

A flow chart is a pictorial representation of a program. There are labeled geometrical symbols, together with the arrows connecting one symbol with other. A flow chart helps in correctly designing the program by visually showing the sequence of instructions to be executed.

7.      Where we use break statement in a C program?

Answer:

In the switch statement, the break statement is normally used at the end of statement in each case. The break statement is used to exit from the body of the switch structure. break statement is also used in loop to terminate early.

8.      What is the difference between ++i and i++?

Answer:

i++: It first adds 1 to the value of the”i” and then the value of the “i” is used in the expression.

i++:  It first used the current value of the post-fixed variable “i”  in the expression and then adds 1to the value of the variable “i”.

9.      Describe the function header in C?

Answer:

The first line of the function is called function header. The function header consists of return type, function name and list of parameter.

10.   What is the difference between syntax error and logical error?

Answer:

Syntax Error: 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.

Logical error: The error in the logic of the program is called logical error. The compiler cannot detect these errors.

 

11.   What is meant by the “equivalence of pointers and array” in C?

Answer:

An array name is a constant pointer that contains the address of first element of array. To access any random element in the array pointer arithmetic is applied on array name and required element is access. It is called equivalence of pointers and array.

12.   What are counter-controlled repetition requirement?

Answer:

The number of iteration must be known in advance for counter controlled repetition and there must be counter variable to count no of iterations.

 

Subjective (9×4=36)

 

Q02.        Develop a C program to sort array by using bubble sort method.

Answer:

Program

 

#include <stdio.h>

#define SIZE 10

void main( void )

{

             int a[ SIZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };

 int pass;

 int i;

 int hold;

puts( “Data items in original order” );

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

 printf( “%4d”, 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 ] );

}

 

Q03.        Write a C program to find the smallest of ten integers.

Answer:

Program

#include <stdio.h>

#include <conio.h>

main()

{

int no, smallest;

printf(“please enter  a number :”);

scanf(“%d”,&no);

smallest = no;

for(int i=1;i<10;i++)

{

printf(“please enter  next number :”);

scanf(“%d”,&no);

if (no<smallest)

smallest= no;

}

printf(“smallest number in 10  integers :%d”, smallest)

}

 

Q04.        The Greatest Common Devisor (GCD) of two integers is the largest integer that evenly divides each of the two numbers. Write function GCD that returns the greatest common devisor of two integers.

Answer:

Function:

int GCD(int no1 , int no2)

{

int gcd,p,q;

p=no1;

 q=no2;

 if(p==0)

 {

            gcd=q;

 }

 else if(q==0)

 {

            gcd=p;

 }

 else

 {

  while(q!=0)

  {

             if(p>q)

             {

                         p=p-q;

              }

              else

             {

                         q=q-p;

             }

             gcd=p;

  }

  }

return gcd;

}

 

Q05.        Develop a C program to find class grade average with counter controlled repetition.

Answer:

Program

#include <stdio.h>

#include <conio.h>

main()

{

int no = 0, grade=0 , total=0 , average=0;

printf(“Please Enter the Number of Students”);

scanf(“%d”,&no);

for(int i=0;i<no; i++)

{

   printf(“Please enter  Grade of Student:”);

   scanf(“%d”,&grade);

   total = total+grade;

}

average = total /no;

printf(“ Average Grade of Class  : %d”, average);

}

Q06.        Write a C program that asks user to enter two numbers, obtain these two numbers from the user and print their sum, product, difference, quotient and remainder.

Answer:

Program

#include <stdio.h>

#include <conio.h>

main()

{

int no1 , no2, sum , product , diff , quot , rem;

printf(“please enter  a number :”);

scanf(“%d”,&no1);

printf(“please enter  second number :”);

scanf(“%d”,&no2);

sum= no1+no2;

product= no1+no2;

diff= no1+no2;

quot = no1+no2;

rem= no1+no2;

printf( “sum of  %d and %d = %d” no1 , no2 , sum);

printf( “Product of  %d and %d = %d” no1 , no2 , product);

printf( “Diff of  %d and %d = %d” no1 , no2 , diff);

printf( “Quotient of  %d and %d = %d” no1 , no2 , quot);

printf( “Remainder of  %d and %d = %d” no1 , no2 , rem);

}

 

Q07.        Write a program to copy the contents of one text file into another text file.

Answer:

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();

}

 

 

 

3 thoughts on “Programming Fundamentals CMP-2122 Solved Past Paper 2015”

Leave a Comment

Talk with Admin
Join Rising Education Whatsapp Group