Professional Documents
Culture Documents
Paper-2-BPOPS103 - Model Paper Solution 2022-23
Paper-2-BPOPS103 - Model Paper Solution 2022-23
main( )
{
Local declaration
Statements;
}
Function1( )
{
Local declaration
Statements;
}
Function2( )
{
Local declaration
Statements;
}
………………….
………………….
FunctionN( )
{
Local declaration
Statements;
}
A C program is composed of pre-processor commands, a global declaration section, and
one or more functions.
The pre-processor directives contain special instructions that indicate how to prepare the
program for compilation. One of the most important and commonly used pre-processor
commands is include which tells the compiler that to execute the program, some
information is needed from the specified header file.
The statements in a C program are written in a logical sequence to perform a specific
task.
Execution of a C program begins at the main() function.
You can choose any name for the functions.
Every program must contain one function that has its name as main().
.
Types of Errors
Run-time Errors As the name suggests, run-time errors occur when the program is
being run executed. Such errors occur when the program performs some illegal
operations like
• Dividing a number by zero
• Opening a file that already exists
• Lack of free memory space
• Finding square or logarithm of negative numbers
Run-time errors may terminate program execution, so the code must be written in such a
way that it handles all sorts of unexpected errors rather terminating it unexpectedly. This
ability to continue operation of a program despite of run-time errors is called robustness.
Compile-time Errors Again as the name implies, compile-errors occur at the time of
compilation of the program. Such errors can be further classified as follows:
Syntax Errors Syntax error is generated when rules of C programming language
are violated. For example, if we write int a: then a syntax error will occur since the
correct statement should be int a;
Semantic Errors Semantic errors are those errors which may comply with rules of
the programming language but are not meaningful to the compiler. For example, if
we write, a * b = c; it does not seem correct. Rather, if written like c = a * b would
have been more meaningful.
Logical Errors Logical errors are errors in the program code that result in unexpected
and undesirable output which is obviously not correct. Such errors are not detected by
the compiler, and programmers must check their code line by line or use a debugger to
locate and rectify the errors. Logical errors occur due to incorrect statements. For
example, if you meant to perform c = a + b; and by mistake you typed c = a * b; then
though this statement is syntactically correct, it is logically wrong.
Linker Errors These errors occur when the linker is not able to find the function
definition for a given prototype. For example, if you write clrscr(); but do not include
conio.h then a linker error will be shown. Similarly, even if you have defined a function
display_data() but while calling if you mistakenly write displaydata() then again a linker
error will be generated.
Solution:
Explain 4 major errors in C program 6 Marks
Scheme [6 = 6 Marks]
OR
2. (a). Explain the various rules for forming identifiers names. Give examples for
valid and invalid identifiers for the same. 8 Marks
Answer:
● Identifiers are names given to program elements such as variables, arrays and
functions. Identifiers may consist of sequence of letters, numerals, or underscores.
Rules for forming identifier name
● it cannot include any special characters or punctuation marks (like #, $, ^, ?, .,
etc) except the underscore"_".
● There cannot be two successive underscores
● Keywords cannot be used as identifiers
● The names are case sensitive. So, example, “FIRST” is different from “first” and
“First”.
● It must begin with an alphabet or an underscore.
● It can be of any reasonable length. Though it should not contain more than 31
characters.
Example:
Valid variable invalid variable
num2 $num1
_apple +add
a_2 199_space
#12
Solution:
Rule for forming identifier 5 Marks
Classify valid and invalid variable 3 Marks
Scheme [5 + 3 = 8 Marks]
(b). Mention various output devices and explain hardcopy devices. 6 Marks
Answer:
Any device that outputs/gives information from a computer is called an output device.
Output devices are electromechanical devices which accept digital data from the
computer and converts them into human understandable language.
(c). Discuss the variants of microcomputer that are widely used today 6 Marks
Answer:
Microcomputers, commonly known as PCs, are very small and cheap. The first
microcomputer was designed by IBM in 1981 and was named IBM-PC.
Later on, many computer hardware companies copied this design and termed their
microcomputers as PC-compatible, which refers to any PC that is based on the original
IBM PC design.
Another type of popular PC is designed by Apple. PCs designed by IBM and other PC-
compatible computers have a different architecture from that of Apple computers.
Laptop: Laptops are small microcomputer that can easily fit inside a briefly.
Desktop PCs: A desktop PC is the most popular model of PCs. The system unit of the
desktop PC can be placed flat on a desk or table.
Workstation: Workstation are single-user computer that have the same features of PCs,
but their processing speed matches that of a minicomputer or mainframe computer.
Network Computer: Network computer have less processing power, memory, and
storage than a desktop computer. These computers are specifically designed to be used
as terminals in a networked environment.
Handheld Computers: The mid-1990s witnessed a range of small personal computing
devices that are commonly known as handheld computers, or mobile computers. (Smart
phones and Tablet PCs)
Solution:
Explain various microcomputers 6 Marks
Scheme [6 = 6 Marks]
MODULE-2
Continue Statement:
● The continue statement can only appear in the body of a loop.
● When the compiler encounters a continue statement then the rest of the
statements in the loop are skipped and the control is unconditionally transferred
to the loop-continuation portion of the nearest enclosing loop.
● Its syntax is quite simple, just type keyword continue followed with a semi-colon.
continue;
● If placed within a for loop, the continue statement causes a branch to the code
that updates the loop variable.
● For example,
Solution:
Explain difference 6 Marks
Scheme [6 = 6 Marks]
OR
4. (a). Illustrate Nested loops in C with suitable example. 6 Marks
Answer:
C allows its users to have nested loops, i.e., loops that can be placed inside other loops.
Although this feature will work with any loop such as while, do-while, and for, it is most
commonly used with the for loop, because this is easiest to control.
A for loop can be used to control the number of times that a particular set of statements
will be executed. Another outer loop could be used to control the number of times that a
whole loop is repeated.
Syntax
The syntax for a nested for loop statement in C is as follows: −
for ( initialization; condition; increment )
{
for ( initialization; condition; increment )
{
statement(s);
}
statement(s);
}
Programs to print triangles using *, numbers and characters
*
* *
* **
* ***
* ****
#include <stdio.h>
void main()
{
int i, j, rows;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=1; i<=rows; ++i)
{
for(j=1; j<=i; ++j)
{
printf("* ");
}
printf("\n");
}
}
Solution:
Explain nested loop with syntax 3 Marks
Write example Program to illustrate nested loop 3 Marks
Scheme [3 + 3 = 6 Marks]
(c) Explain switch statement with syntax. Write a C program to simulate calculator.
Answer: 7 Marks
● A switch case statement is a multi-way decision statement that is simplified
version of an if-else block that evaluates only one variable.
The general form of the switch statement is as shown below:
switch (variable)
{
case value1:
Statement block-1;
break;
case value2:
Statement block-2;
break;
case value-3:
Statement block-3;
break;
……………………
……………………
case value N:
Statement block-N;
break;
default:
Statement default-block;
break;
}
Statement-x;
Here, statement blocks refer to statement lists contain zero or more statements. These
statements in the block are not enclosed within opening and closing braces.
Switch statements are mostly used in two situations:
1. When there is only one variable to evaluate in the expression.
2. When many conditions are being tested for.
The switch case statement compares the value of the variable given in the switch
statement with value of each case statement that follows. When the value of the switch
and the case statement matches, the statement block of that particular case is executed.
Default is also a case that is executed when no match is found between the values of
switch and case statements and thus there are no statements to be executed.
default is also a case that is executed when the value of the variable does not match
with any of the values of the case statement. That is, the default case is executed when
no match is found between the values of switch and case statements and there are no
statements to be executed.
The break statement must be used at the end of each case because if it is not used, then
the case that matched and all the following cases will be executed.
Program:
Base case is when n=1, because if n = 1, the result is known to be 1
Recursive case of the factorial function will call itself but with a smaller value of n, this
case can be given as : factorial(n) = n X factorial (n-1)
Solution:
Explain the concept of recursion 3 Marks
Write example program 3 Marks
Scheme [3 + 3 = 6 Marks]
Variable Scope
Function Program
Block scope File scope
scope scope
Block Scope:
A statement block is a group of statements enclosed within an opening and closing curly
brackets ({ }). If a variable is declared within a statement block then, as soon as the
control exits that block, the variable will cease to exist. Such a variable also known as a
local variable is said to have a block scope.
Blocks of statements may be placed one after the other in a program; such blocks that
are placed at the same level are known as parallel blocks.
Function Scope:
Function Scope indicates that a variable is active and visible from the beginning to the
end of a function.
● Function scope is applicable only with goto label names. That is the programmer
cannot have the same label name inside a function.
Using goto statement is not recommended as it is not considered to be a good
programming practice.
Program Scope:
● If you want that functions should be able to access some variables which are not
passed to them as arguments, then declare those variables outside any function
blocks. Such variables are commonly known as global variables. Hence, global
variables are those variables that can be accessed from any point in the program.
File Scope:
● When a global variable is accessible until the end of the file, the variable is said
to have file scope.
● To allow a variable to have file scope, declare that variable with the static keyword
before specifying its data type, like this:
static int x = 10;
● A global static variable can be used any-where from the file in which it is declared
but it is not accessible by any other files.
● Such variables are useful when the programmer writes his own header files.
Solution:
Explain four scope variable 6 Marks
Scheme [6 = 6 Marks]
OR
6.(a). Differentiate between call by value and call by reference. Using suitable
example.
Answer: 8 Marks
Call by Value
● In the Call by Value method, the called function creates new variables to store
the value of the arguments passed to it. Therefore, the called function uses a copy
of the actual arguments to perform its intended task.
● If the called function is supposed to modify the value of the parameters
passed to it, then the change will be reflected only in the called function. In the
calling function no change will be made to the value of the variables.
#include<stdio.h>
void add( int n);
void main()
{
int num = 2;
printf("\n The value of num before calling the function = %d", num);
add(num);
printf("\n The value of num after calling the function = %d", num);
}
void add(int n)
{
n = n + 10;
printf("\n The value of num in the called function = %d", n);
}
The output of this program is:
The value of num before calling the function = 2
The value of num in the called function = 20
The value of num after calling the function = 2
Call By Reference:
● When the calling function passes arguments to the called function using call by
value method, the only way to return the modified value of the argument to the caller is
explicitly using the return statement. The better option when a function can modify the
value of the argument is to pass arguments using call by reference technique.
● In call by reference, we declare the function parameters as references rather than
normal variables. When this is done any changes made by the function to the arguments
it received are visible by the calling program.
● To indicate that an argument is passed using call by reference, an ampersand sign (&)
is placed after the type in the parameter list. This way, changes made to that
parameter in the called function body will then be reflected in its value in the calling
program.
#include<stdio.h>
void add( int &n);
void main()
{
int num = 2;
printf("\n The value of num before calling the function = %d", num);
add(num);
printf("\n The value of num after calling the function = %d", num);
}
void add( int &n)
{
n = n + 10;
printf("\n The value of num in the called function = %d", n);
}
strcpy Function
Syntax:
char *strncat(char * str1, char *str2);
This function copies the string pointed to by str2 to str1 including the null character of
str2. It returns the argument str1. Here str1 should be big enough to store the content of
str2.
#include<string.h>
#include<stdio.h>
void main()
{
char str1[20],str2[20];
printf("Enter string: ");
gets(str2);
strcpy(str1, str2); //Content of string src is copied to string dest
printf("Copied string: ");
puts(str1);
}
Output:
Enter string: mitmysore
Copied string: mitmysore
Solution:
List different string handling functions 2 Marks
Explain any 2 string handling functions 6 Marks
Scheme [2 + 6 = 8 Marks]
(b). Develop a program using pointer to compute the sum, mean and standard
deviation of all element stored in array of N real number 8 Marks
Answer:
/* Program to find sum, mean, and Standard Deviation using pointer */
#include<stdio.h>
#include<math.h>
void main()
{
float a[10],*ptr,mean,std,sum=0,sumstd=0;
int n,i;
printf("Enter the no of elements n =");
scanf("%d",&n);
printf(" Enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
ptr=a;
for(i=0;i<n;i++)
{
sum=sum+ *ptr;
ptr++;
}
mean=sum/n;
ptr=a;
for(i=0;i<n;i++)
{
sumstd=sumstd+ pow((*ptr-mean),2);
ptr++;
}
std=sqrt(sumstd/n);
printf("Sum=%.3f\t",sum);
printf("Mean=%.3f\t",mean);
printf("Standard Deviation=%.3f\n",std);
}
Solution:
Develop a C program to compute Standard Deviation 8 = 8 Marks
Scheme [8 = 8 Marks]
(b). What is pointer? Discuss pointer arithmetic with suitable C code. 6 Marks
Answer:
● Actually pointers are nothing but memory addresses.
● A pointer is a variable that contains the memory location of another variable.
Another definition of pointer is: “Pointer is a variable which holds the address of
another variable of same type.”
● The general syntax of declaring pointer variable is
data_type *ptr_name;
Like other variables, pointer variables can also be used in expressions.
For example, if ptr1 and ptr2 are pointers, then the following statements are valid.
For ex,
int num1=2, num2= 3, sum=0, mul=0, div=1;
int *ptr1, *ptr2;
ptr1 = &num1, ptr2 = &num2;
When using pointers, unary increment (++) and decrement (--) operators have greater
precedence than the dereference operator (*). Therefore, the expression
*ptr++ is equivalent to *(ptr++). So the expression will increase the value of ptr so that it
now points to the next element.
In order to increment the value of the variable whose address is stored in ptr, write
(*ptr)++
Example Programs:
#include <stdio.h>
const int MAX = 3;
void main ()
{
int var[] = {10, 100, 200};
int i, *ptr;
/* let us have array address in pointer */
ptr = var;
for ( i = 0; i < MAX; i++)
{
printf("Address of var[%d] = %x\n", i, ptr );
printf("Value of var[%d] = %d\n", i, *ptr );
/* move to the next location */
ptr++;
}
}
Solution:
Define Pointer 2 Marks
Explain pointer expression with example 4 Marks
Scheme [2 + 4 = 6 Marks]
Solution:
Explain different file opening modes 7 Marks
Scheme [7 = 7 Marks]
(b). Implement structure to read, write and compute average marks of the students.
List the students scoring above and below the average marks for a class of n
students. 8 Marks
Answer:
/* Program to Maintain Student information */
#include<stdio.h>
#include<string.h>
struct student
{
int roll_num, marks;
char name[20];
};
void main()
{
int i, n;
struct student s[10];
int total = 0;
float avg =0.0;
printf("Enter the students n = ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf(" Enter the %d student details\n",i+1);
printf(" Enter the roll number:");
scanf("%d",&s[i].roll_num);
printf(" Enter the student name without spaces:");
scanf("%s",s[i].name);
printf(" Enter the marks:");
scanf("%d",&s[i].marks);
}
printf("\n The student details are \n");
printf("------------------------------------------------\n");
printf(" Roll Num\tName\t\tMarks\n");
printf("------------------------------------------------\n");
for(i=0;i<n;i++)
{
printf("\t%d\t%s\t\t%d\n",s[i].roll_num,s[i].name,s[i].marks);
}
printf("------------------------------------------------\n");
for(i=0;i<n; i++)
{
total = total + s[i].marks;
}
avg = total/n;
printf("Average marks of the class is = %f\n",avg);
printf("\n List of students who have above the average marks\n");
printf("------------------------------------------------\n");
printf(" Roll Num\tName\t\tMarks\n");
printf("------------------------------------------------\n");
for(i=0;i<n;i++)
{
if(s[i].marks >= avg)
{
printf("\t%d\t%s\t\t%d\n",s[i].roll_num,s[i].name,s[i].marks);
}
}
printf("\n List of students who have below the average marks\n");
printf("------------------------------------------------\n");
printf(" Roll Num\tName\t\tMarks\n");
printf("------------------------------------------------\n");
for(i=0;i<n;i++)
{
if(s[i].marks < avg)
{
printf("\t%d\t%s\t\t%d\n",s[i].roll_num,s[i].name,s[i].marks);
}
}
}
Solution:
Develop a C program to display student information 8 Marks
Scheme [8 = 8 Marks]
(c). What are enumeration variable? How are they declared? 5 Marks
Answer:
● The enumerated data type is a user defined type based on the standard integer type.
● An enumeration consists of a set of named integer constants. That is, in an
enumerated type, each integer value is assigned an identifier. This identifier (also known
as an enumeration constant) can be used as symbolic names to make the program more
readable.
● To define enumerated data types, enum keyword is used.
● Enumerations create new data types to contain values that are not limited to the
values fundamental data types may take. The syntax of creating an enumerated data
type can be given as below.
enum enumeration_name { identifier1, identifier2, …..., identifiern };
● Consider the example given below which creates a new type of variable called COLORS
to store colors constants.
● enum COLORS {RED, BLUE, BLACK, GREEN, YELLOW, PURPLE, WHITE};
In case you do not assign any value to a constant, the default value for the first one in
the list - RED (in our case), has the value of 0. The rest of the undefined constants have a
value 1 more than its previous one. So in our example,
● RED = 0, BLUE = 1, BLACK = 2, GREEN = 3, YELLOW = 4, PURPLE = 5, WHITE =6
● If you want to explicitly assign values to these integer constants then you should
specifically mention those values as shown below.
● enum COLORS {RED = 2, BLUE, BLACK = 5, GREEN = 7, YELLOW, PURPLE ,
WHITE = 15};
● As a result of the above statement, now
● RED = 2, BLUE = 3, BLACK = 5, GREEN = 7, YELLOW = 8, PURPLE = 9, WHITE = 15
Solution:
Explain enumeration 5 Marks
Scheme [5 = 5 Marks]
OR
10. (a). Write a short note on functions used to
Read data from a file
Write data to a file.
Answer: 8 Marks
● A file must be first opened before data can be read from it or written to it. In order
to open a file and associate it with a stream, the fopen() function is used.
● The prototype of fopen() can be given as:
FILE *fopen(const char *file_name, const char *mode);
● Using the above prototype, the file whose pathname is the string pointed to by
file_name is opened in the mode specified using the mode.
● If successful, fopen() returns a pointer-to-structure and if it fails, it returns NULL.
● When opening a file in read purpose, file must be open in read mode. (r)
● When opening a file in write purpose, file must be open in write mode. (w)
(b). Differentiate structures and unions with syntax and example. 6 Marks
Answer: