BLOG DETAILS

Solved Paper PPL Through C | 2016 | Rajasthan University (RU) | BCA Part-I | By Suresh Kulahry

  • By Suresh Kulahry
  • |
  • February 28, 2020

In this blog, we provide the solution of BCA Part-I Previous Year Paper of subject Principle of Programming Languge Through C (PPL Through C) of Year 2016. The solution of this paper is written in EASY and EXAM ORIENTED Language that helps student to how actually written solution in main Rajasthan University (RU) exam.

B.C.A. (Part-I) EXAMINATION, 2016

(Faculty of Science)

(Three-Year scheme of 10+2+3 Pattern)

Paper-134

PRINCIPLES OF PROGRAMMING LANGUAGE (THROUGH ‘C’)

Time Allowed: Three Hours

Maximum Marks – 100

PART-I: (Very Short Answer) consists of 10 questions of 2 marks each. Maximum limit for each questions is up to 40 words.

PART-II:  (Short Answer) consists of 5 questions of 4 marks each. Maximum limit of each question is up to 80 words.

PART-III: (Long Answer) consists of 5 questions of 12 marks each with internal choice.

PART-I

  1. Define pseudo code.
  2. What do you understand by machine level languages?
  3. Give syntax of switch statement.
  4. What is header file?
  5. Define arrays.
  6. Give syntax and purpose of strcat().
  7. Why do we use malloc()? Give syntax of malloc().
  8. Differentiate between local variable and global variable.
  9. Differentiate between union and structures.
  10. Give syntax and purpose of fprintf().

PART-II

  1. Draw a flow chart to find out maximum of 3 numbers.
  2. Explain different logical operators of ‘C’ with suitable examples.
  3. Explain while and do-while loops with examples.
  4. Write a code in ‘C’ to compute factorial of a number using recursion.
  5. Give ‘C’ functions (any one) to:-
    1. Open a file.
    2. Close a file.
    3. Reading data from a file.
    4. Writing data to a file.

PART-III

  1. Write a note on evaluation of programming languages in details.
                                    OR
    Write a pseudo code to display the following series:-
    1. 0, 1, 1, 2, 3, 5,.............n
    2. 1, 4, 9, 16, 25,..............n
  2. With the help of suitable examples, discuss the various decision making statements of ‘C’.
                                    OR
    Explain the different data types of ‘C’ in brief.
  3. What do you mean by iterative statements? Explain different types of loops supported by ‘C’ with use of break and continue keywords.
                                    OR
    What are arrays? Write a program in ‘C’ to find out sum of 10 numbers using arrays.
  4. What is the need of user defined functions? How do we create functions in ‘C’? explain in detail.
                                    OR
    What are pointer variables? Write a ‘C’ code to illustrate the use of indirection operator (*) to access the values pointed by a pointer.
  5. Discuss different formatted and unformatted I/O functions of ‘C’.
                                    OR
    Write a note on ‘file handling in C’ in brief.

SOLUTION PART-I

Define pseudo code.

Pseudo code is one of the methods that could be used to represent an algorithm. It is not written in a specific syntax that is used by a programming language and therefore cannot be executed in a computer. It just used for human understand. Basically it acts as a bridge between the program and the algorithm or flowchart.

What do you understand by machine level languages ?

Machine language is the only language that is directly understood by the computer. The machine language code is written as string of 1’s and 0’s. In this not need any translator program.

Give syntax of switch statement.

When one of the many alternatives is to be selected the switch statement allows a program to select one statement for execution out of a set of alternatives and the remaining statements will be skipped.
Syntax:
switch(expression)
{
     case choice1:
     statements;
     break;
     case choice2:
     statements;
     break;
     case choice N:
     statements;
     break;
     default:
     statements;
}
The expression must be any integer or character type. The choice 1, choice 2 and choiceN are the possible values which we are going to test with the expression value.

What is header file?

The C program depends upon some header files for function declaration that are used in the program. So, header file is collection of identifiers and generally contains declaration of predefined function. For example, the function printf() and scanf() are declared in stdio.h header file.

Define arrays.

An array is finite collection of homogeneous (same type) elements. An array is a data structure that can store multiple elements of same type under one name.
“So, an array is a linear collection of similar data type elements, stored in unique and successive memory locations.”
DECLARATION:
              Data_Type Variable_Name [Size];
EXAMPLE:
              int arr[7];

Give syntax and purpose of strcat().

The strcat() function joins two strings together. When we combine two strings, this function adds the character of one string to the end of other string.
Syntax:
     strcat(String1, String2);
When the function strcat() is executed String2 is appended to String1. The string String2 remains unchanged.

Why do we use malloc()? Give syntax of malloc().

The malloc() function is used to allocate memory space in bytes to the variable of different data types. The function reserves bytes of determined size and return the base address to pointer variable.
Syntax:
     Pointer_Variable= (Data_Type *) malloc (Block Size);

Differentiate between local variable and global variable.

The variable which is created or declared inside the any block is called local variable. The local variables only exist inside the specific function that creates them and they are unknown to other functions. The scope of local variable is limited to the block in which it is declared.
The variable which is declared outside the any block is called global variable. The scope of global variable is global and exists throughout the program.

int a=45;//Global Variable
void main()
{
int b=15;//Local Variable
}

Differentiate between union and structures.

First of all, both structure and union are user defined data type but there are few differences between them, they are:

  • The structure holds all the members while union holds one member at a time.
  • The size of structure type variable is equal to sum of size of all the members while size of union type variable is equal to size of largest member.

Give syntax and purpose of fprintf().

This function is used for writing characters, strings, integers, float etc. to the file. The fprintf()function contains one more parameter that is file pointer, which points the opened file.

Syntax:
     fprintf (file_pointer, “control_string”, variables);

 

SOLUTION PART-II

Draw a flow chart to find out maximum of 3 numbers.

Graphical representation of any problem is called flowchart. A flowchart is a visual representation of the sequence of steps and decisions needed to perform a process. In flowchart various types of graphical symbol is used for represent different activities.

Suraku Academy

Explain different logical operators of ‘C’ with suitable examples.

Logical operators are used to check logical relation between two expressions. The expressions may be variables, constants.

OPERATOR

NAME

EXAMPLE

RESULT

&&

Logical AND

5>3 && 5<10

1

||

Logical OR

8>5 || 8<2

1

!

Logical NOT

!(8!=9)

0

  • The logical AND and logical OR operator are binary operator while logical NOT operator is unary operator.
  • The logical AND (&&) operator provides true result when both expressions are true, otherwise 0.
  • The logical OR (||) operator provides true result when one of the expressions is true, otherwise 0.
  • The logical NOT operator provide 0 if the condition is true and vice-versa.

Explain while and do-while loops with examples.

The loop is a block of statements that are executed again and again till a specific condition is satisfied. On the basis of condition checking, the loops are divided into two types, entry-control and exit-control loop. The while loop is entry-control and do-while is exit-control loop.
THE while LOOP:
                            In while loop the test condition is indicated at the top and it tests the value of the expression before processing the body of the loop.

Syntax:
while(condition)
{
     Statement;
     Statement;//Body of Loop.
     Statement;
}
Example:
//WAP TO PRINT “Suraku Academy” 5 TIMES.
void main()
{
     int i=1;
     while(i<=5)
     {
          printf("Suraku Academy ");
          i++;
     }
}

THE do-while LOOP:
                                   In this the given test condition is evaluated after the execution of the body statement. The do-while loop will execute at least one time even if the condition is false initially.

Syntax:
do
{
     Statement;
     Statement;//Body of Loop.
     Statement;
} while (condition);
Example:
//WAP TO PRINT “Suraku Academy” 5 TIMES.
void main()
{
     int i=1;
     do
     {
          printf("Suraku Academy ");
          i++;
     } while(i<=5);
}

 

Write a code in ‘C’ to compute factorial of a number using recursion.

//WAP to Calculate Factorial of Given Number Using Recursion.
long int fact(int);//Function Declaration.
void main()
{
     int num;
     long int f;
     clrscr();
     printf("Enter The Number: ");
     scanf("%d",&num);
     f=fact(num);//Function Calling.
     printf("Factorial of Given Number: %ld",f);
     getch();
}
long int fact(int n)//Function Definition.
{
     long int f;
     if(n>0)
     {
          f=n*fact(n-1);
          return(f);
     }
     return(1);
}

Give ‘C’ functions (any one) to:

OPEN A FILE:

A file has to be opened before read and write operations. Opening of file means load the file in RAM. For opening the file, we use the predefined function fopen(). In fopen() function we pass name of the file and its mode of operation as an argument.

Syntax:
     fp = fopen (“File_name”,“Mode_of_operation”);
Example:
     FILE *fp;
     fp = fopen(“store.txt”, “r”);

CLOSE A FILE:

The file is opened by the fopen() should be closed after the work is over, i.e. we need to close the file after reading and writing operations.

Syntax:
     fclose(file_pointer);
     fcloseall();

 

SOLUTION PART-III

Write a note on evaluation of programming languages in details.

Programming languages exist to enable programmers to develop software effectively. But how efficiently programmers can write software depends on the usability of the languages and tools that they develop with. In order to understand the various constructs of a programming language and its capabilities, it is useful to know some evaluation criteria. Some of the language criterias to evaluate a programming language are:

  • Readability.
  • Writability.
  • Reliability.
  • Cost.
  • Generality.
  • Extensibility.
  • Standardability.

Readability:
The first and most obvious criteria is certainly readability. Coding should be simple and clear to understand.

  • Simplicity : Should not involve complex syntax, many ways to perform a single task,overloading of methods and operator etc.
  • Orthogonality : This means relatively small set of primitive constructs can be combine. For example, int *count; Here pointer and integer is combined.
  • Control Statements : There should be adequate control statements. Use of for loop, while loop, do while loop is adequate. Using of go to statements causes poor readability.
  • Data Types and Structures : Language should involve adequate facilities for defining data types and data structure. For example, timeout = 1; is unclear as compare to timeout = true;.

Writability:
Writability is a measure of how easily language can be used to code.Most of the language characteristics that affect readability also affect writability.

Reliability:
It refers to type checking (It is testing for type error, either at compile or run time. For example, float height; is more desirable as compare to int height;.), exception handling (It is the ability of program to handle run time error. Remember, handling runtime error are more expensive than compile errors.), aiasing (It is same memory location having more than one name. Which is causes confusion), etc.

Cost:
Total cost of programming should be minimum. For example, cost of trainer, Cost of writing algorithm, Cost of compiling program in the language, Cost of hardware required for program, Cost of maintenance, etc.

Generality:
Language should not be limited to specific application only.

Extensibility:
The programming language should be flexible, must be able to add new constructs.

Standardability:
The programming language should be platform independent.

 

Write a pseudo code to display the series 0, 1, 1, 2, 3, 5, ……………n

 

Pseudo Code:
Step 1 : Start.
Step 2 : Declare Variable a,b,next,n,i
Step 3 : Initialize Variable a=0, b=1, i=3
Step 4 : Read value of n from user.
Step 5 : Print a and b
Step 6 : Repeat until i<=n
               (A)  next=a+b
               (B)  print next
               (C)  a=b, b=next
               (D)  i=i+1
Step 7 : Stop.
C Program:
void main()
{
     int a=0,b=1,next,i=3,n;
     printf("Enter Value of N : ");
     scanf("%d",&n);
     printf("The Series ");
     printf("%d    %d ",a,b);
     while(i<=n)
     {
          next=a+b;
          printf("%d    ",next);
          a=b;
          b=next;
          i++;
     }
     getch();
}
OUTPUT
Enter Value of N : 8
The Series
0     1     1     2     3     5     8     13

 

Write a pseudo code to display the series 1, 4, 9, 16, 25, ……………n

 

Pseudo Code:
Step 1 : Start.
Step 2 : Declare Variable n,a,square
Step 3 : Initialize Variable a=1
Step 4 : Read value of n from user.
Step 5 : Repeat until a<=n
               (A) square=a*a
               (B) Print square
               (C) a=a+1
Step 6 : Stop.
C Program:
void main()
{
     int n,a,square;
     printf("Enter Value of N : ");
     scanf("%d",&n);
     printf("The Series ");
     for(a=1;a<=n;a++)
     {
          square=a*a;
          printf("%d ",square);
     }
     getch();
}
OUTPUT
Enter Value of N : 7
The Series
1     4     9     16     25     36     49

 

With the help of suitable examples, discuss the various decision making statements of ‘C’.

The C program statements generally execute sequentially in the order in which they are written in the program but in some cases, we may have to change the order of execution of statements.
Decision making statements in a programming language help the programmer to transfer the control from one part to other part of the program. Thus, these decision-making statements facilitate the programmer to determining the flow of control. There are five (5) decision control or selection statements.

  1. The simple if statement.
  2. The if else statement.
  3. The Nested if else statement.
  4. The if else if ladder statement.
  5. The switch case statement.

The Simple if Statement :
The ‘C’ language uses the keyword if to execute a set of statements or block of statements when the logical condition is true. It has only one option that means it cares only true part.

Syntax:
if(condition)
{
       Statement-1;
       Statement-2;
       Statement-N;
}
Programming Example : 1
//DEMONSTRATION OF SIMPLE IF STATEMENTS.
void main()
{
     int num;
     printf("Enter The Number : ");
     scanf("%d",&num);
     if(num>100)
     printf("The Given Number is Greater Than 100");
     getch();
}
Programming Example : 2
//WAP TO CHECK GIVEN NUMBER IS POSITIVE, NEGATIVE OR ZERO.
void main()
{
     int num;
     printf("Enter A Number : ");
     scanf("%d",&num);
     if(num>0)
          printf("Given Number is Positive");
     if(num<0)
          printf("Given Number is Negative");
     if(num==0)
          printf("Given Number is Zero");
     getch();
}

 

THE if else STATEMENT :
The if else statement takes care of the true and false conditions. It has two blocks. One block is for if and it is executed when the condition is true. The other block is for else and it is executed when the condition is false.

Syntax:
if(condition)
{
     Statement-1;
     Statement-2;
     Statement-N;
}
else
{
     Statement-1;
     Statement-2;
     Statement-N;
}
Note : If there is only one statement in if block and else block then there is no need of curly braces.
Programming Example : 3
//WAP FOR CHECK THE GIVEN NUMBER IS EVEN OR ODD.
void main()
{
     int num;
     printf("Enter The Number : ");
     scanf("%d",&num);
     if(num%2==0)
          printf("The Number %d is Even",num);
     else
          printf("The Number %d is Odd",num);
     getch();
}

 

THE NESTED if else STATEMENTS :
In this kind of statement, we check condition within the condition for executing various statements.

Syntax:
if(condition)
{
     if(condition)
     {
          Statements-1;
          Statements-2;
          Statements-N;
     }
     else
     {
          Statements-1;
          Statements-2;
          Statements-N;
     }
}
else
{
     if(condition)
     {
          Statements-1;
          Statements-2;
          Statements-N;
     }
     else
     {
          Statements-1;
          Statements-2;
          Statements-N;
     }
}
Programming Example : 4
//WAP TO FIND LARGEST AMONG THREE NUMBER.
void main()
{
     int a,b,c;
     printf("Enter Any Three Number : ");
     scanf("%d%d%d",&a,&b,&c);
     if(a>b)
     {
          if(a>c)
               printf("A is Largest");
          else
               printf("C is Largest");
     }
     else
     {
          if(b>c)
               printf("B is Largest");
          else
               printf("C is Largest");
     }
     getch();
}

 

THE if else if LADDER STATEMENT :
The if else if ladder statement helps user to decide one option from multiple options. The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the else if ladder is skipped. If none of the conditions is true, then the final else statement will be executed.

Syntax:
if(condition)
     Statements;
else if(condition)
     Statements;
else if(condition)
     Statements;
………………...
………………...
else
     Statements;
Programming Example : 5
//WAP TO CHECK GIVEN YEAR IS LEAP YEAR OR NOT.
void main()
{
     int year;
     printf("Enter Year: ");
     scanf("%d",&year);
     if(year%100!=0&&year%4==0)
          printf("Given Year is Leap Year");
     else if(year%400==0)
          printf("Given Year is Leap Year");
     else
          printf("Given Year is Not Leap Year");
     getch();
}

 

THE switch case STATEMENT :
When one of the many alternatives is to be selected the switch statement allows a program to select one statement for execution out of a set of alternatives and the remaining statements will be skipped.

Syntax:
switch(expression)
{
     case choice1:
     statements;
     break;
     case choice2:
     statements;
     break;
     case choiceN:
     statements;
     break;
     default:
     statements;
}
Note: The expression must be any integer or character type. The choice 1, choice 2 and choiceN are the possible values which we are going to test with the expression value. In case none of the values from choice1 to choose N are matches with the expression value then default statement is executed.
Programming Example : 6
//DEMONSTRATION OF SWITCH CASE STATEMENT.
void main()
{
     int choice;
     printf("Enter the Number (1-7) : ");
     scanf("%d",&choice);
     switch(choice)
     {
          case 1:
          printf("Sunday");
          break;
          case 2:
          printf("Monday");
          break;
          case 3:
          printf("Tuesday");
          break;
          case 4:
          printf("Wednesday");
          break;
          case 5:
          printf("Thursday");
          break;
          case 6:
          printf("Friday");
          break;
          case 7:
          printf("Saturday");
          break;
          default:
          printf("Enter the Valid Choice");
     }
     getch();
}

 

Write a note on ‘file handling in C’ in brief.

A collection of data or information that has a name, called the file. Almost all information stored in a computer must be in a file. File is a collection of numbers, symbols and text placed onto the disk.
To work with file handling in C or wants to save program data permanently in secondary storage, C provides the new data type called FILE.
There are four steps to process a file in 'C' language.

  1. Declaration of FILE Type Pointer.
  2. Open the File.
  3. Operation With File.
  4. Closing the File.

DECLARATION OF FILE TYPE POINTER :
The 'C' communicates with files using a new data type called a FILE.

Syntax:
     FILE *Variable_Name;
Example:
     FILE *fp, *fpp;

 

OPEN THE FILE :
A file has to be opened before read and write operations. Opening of file means load the file in RAM. For opening the file we use the predefined function fopen(). In fopen () function we pass name of the file and its mode of operation as an argument.

 

Syntax:
     fp = fopen (“File_name”,“Mode_of_operation”);
Example:
     FILE *fp;
     fp = fopen (“Store.txt”, “r”);

 

OPERATION WITH FILE :
Once the file is opened using fopen() function, then file is ready for operation. The FILE type pointers point to the first character of the file. Basically, three types of operation perform with file.

 

  1. Reading from file.
  2. Writing to file.
  3. Append content to file.

CLOSING A FILE :
The file is opened by the fopen() should be closed after the work is over, i.e. we need to close the file after reading and writing operations.

Syntax:
     fclose(File_Pointer);
     fcloseall();

 

Programming Examples:
//WAP TO WRITE "SURAKU ACADEMY" IN A FILE.
void main()
{
     int length,i;
     char std[]="SURAKU ACADEMY";
     FILE *fp;
     fp=fopen("save.txt ","w");
     if(fp==NULL)
     {
          printf(“File not created”);
          exit(1);
     }
     length=strlen(std);
     for(i=0;i fputc(std[i],fp);
     fclose(fp);
     getch();
}
//WAP TO READ CONTENT FROM "save.txt" FILE.
void main()
{
     FILE *fp;
     char ch;
     fp=fopen("save.txt","r");
     if(fp==NULL)
     {
          printf("File Not Found");
          exit(1);
     }
     while(!feof(fp))
     {
          ch=fgetc(fp);
          printf("%c",ch);
     }
     fclose(fp);
     getch();
}
OUTPUT
SURAKU ACADEMY

 

What are pointer variables? Write a ‘C’ code to illustrate the use of indirection operator (*) to access the values pointed by a pointer.

In C, variables are used to hold data values during the execution of the program. Every variable when declared occupies certain memory location. For example, integer-type variable takes two bytes of memory, character type one byte and float type four bytes of memory.

Example:
     int a;
     float per;
     char ch;
Memory Representation:
About-Base-Address
The starting address of a is 45891, per is 65120 and ch is 28562. The starting address of variable is also known as base address.

 

A pointer is a special variable that stores memory address rather than value. It is called pointer because it points to a particular location in memory by storing the address of that location.

POINTER DECLARATION :
The declaration of pointer variable is similar to normal variable declaration but preceding with asterisk (*) symbol.

Syntax:
     Data_Type *Pointer_Name;
Example:
     int *ptr;
Here type of variable ptr is ‘pointer to int’ or (int *), or we can say that base type of ptr is int.

INITIALIZATION OF POINTER :
Providing address (memory location) to the pointer is known as initialization of pointer. The Address of (&) operator is used for initialize the pointer variable.

Example:
     int *ptr;
     int num=26;
     ptr=&num ;

INDIRECTION OPERATOR :

 

  • The operator * is known as indirection operator.
  • It is also known as dereferencing operator.
  • If the operator * is put at the time of declaration then it indicate the variable is pointer variable.
  • If the operator * put other than declaration statement then work is dereferencing the memory location or give the value that store in that location.
Programming Examples:
//DEMONSTRATION OF POINTER VARIABLE-1.
void main()
{
     int num=20,*ptr;
     ptr=&num ;
     printf("Value of NUM: %d ",num);
     printf("Address of PTR: %u ",&ptr);
     printf("Address of NUM: %u ",&num);
     printf("Value of PTR: %u ",ptr);
     printf("Value of NUM: %d ",*ptr);
     *ptr=26;
     printf("Value of NUM: %d ",num);
     printf("Value of NUM: %d ",*&num);
     getch();
}
OUTPUT
Value of NUM: 20
Address of PTR: 65522
Address of NUM: 65524
Value of PTR: 65524
Value of NUM: 20
Value of NUM: 26
Value of NUM: 26
//DEMONSTRATION OF POINTER VARIABLE-2.
void main()
{
     int a=10,b=20, *x,*y;
     x=&a;
     y=&b;
     *y=*x;
     *x=*y-6;
     a=20+*y-8;
     x=a+*y;
     printf("%d %d %d %d",a,*x,b,*y);
     getch();
}
OUTPUT
32
32
10
10

 

Click Here to Know More About Pointer in Details.