22POP13set3 Answers
22POP13set3 Answers
Model Question Paper-III with effect from 2022-23 (CBCS Scheme) - 22POP13
Principles of Programming Using C
1a. Explain the components of a computer with a neat diagram
The brain of the computer is the Central Processing Unit (CPU) represented by a single chip
on a PC. The CPU carries out every instruction stored in a program while interacting with other
agencies as and when necessary.
Most of the work is done by the Arithmetic and Logic unit (ALU) which is the integral part
of the CPU The CPU needs both fast and slow memory to work with. Fast memory is
represented by primary memory known as Random Access Memory (RAM). It is divided into
number of contiguously numbered cells. The number represents the address of the cell. The
primary memory is used for storing instructions and data of the program currently in execution.
Secondary memory is used to store the data not required currently. Data in secondary memory
are stored as files having unique names. The program is executed by loading instructions and
data from secondary memory to primary memory. User interact with the computer system using
input and output devices
THE CENTRAL PROCESSING UNIT (CPU):- A central processing unit (CPU), also
called a central processor or main processor, is the electronic circuitry within a computer that
executes instructions that make up a computer program . The CPU performs basic airthmetic ,
logic, controlling, and input/output (I/O) operations specified by the instructions in the
program. CPU itself has following three components.
Memory or Storage Unit Control Unit ALU(Arithmetic Logic Unit) Memory or Storage
Unit
This unit can store instructions, data, and intermediate results. This unit supplies information
to other units of the computer when needed. It is also known as internal storage unit or the main
memory or the primary storage or Random Access Memory (RAM). Its size affects speed,
power, and capability.
2a. Define Identifiers and explain its rules. State whether the following identifiers are valid or
invalid with justification. i. $roll no ii. _name123 iii. If iv. Name_ _123
Rules to define an Identifiers
1. The first character of the identifier must always be a letter or an underscore followed by any
number of letters digits or underscore.
2. Keywords cannot be used as identifiers or variables.
3. An Identifier or a variable should not contain two consecutive underscores
4. Whitespaces and special symbols cannot be used to name the identifiers.
5. Identifiers are case sensitive (A same variable name declared in uppercase letters and lower
case letters are two different variables in C program)
i. $roll no- invalid because The first character of the identifier must always be a letter or an
underscore followed by any number of letters digits or underscore.
ii. _name123- valid because the first character of the identifier is an underscore followed by
any number of letters digits
iii. Name_ _123- invalid because The first character of the identifier must always be a letter
with single underscore
iv. If – invalid because identifier cannot be a keyword.
2b. Summarize the formatted input and output statements with suitable syntax and example
2c. Explain the SDLC life cycle for the efficient design of a program with a neat diagram.
2) Expanded source code is sent to compiler which compiles the code and converts it into
assembly code.
3) The assembly code is sent to assembler which assembles the code and converts it into object
code. Now a simple.obj file is generated.
4) The object code is sent to linker which links it to the library such as header files. Then it is
converted into executable code. A simple.exe file is generated.
5) The executable code is sent to loader which loads it into memory and then it is executed.
After execution, output is sent to console.
Relational operators in C are commonly used to check the relationship between the two
variables
Logical operators perform logical operations on a given expression by joining two or more
expressions or conditions.
The bitwise operators are the operators used to perform the operations on the data at the
bitlevel. When we perform the bitwise operations, then it is also known as bit-level
programming. It consists of two digits, either 0 or 1
Type casting
A data type is converted to another data type using the casting operator by the
developer.
It can be applied to any compatible data types and incompatible data types.
The casting operator is required to cast a data type to another type.
The destination data type could be smaller than the source data type.
It happens during the program design.
It is also known as narrowing conversion since the destination data type may be
smaller than the source data type.
It is generally used in coding and competitive programming.
It is efficient.
It is reliable.
Type conversion
3c. Write a program to compute the roots of quadratic equation by accepting coefficients
4a. Define looping. Explain while and do-while with suitable example
• A set of statements have to be repeatedly executed for a specified number of times until
a condition is satisfied.
• The statements that help us to execute the set of statements repeatedly are called as
looping condition.
5a. Explain the syntax of Function Declaration and Function Definition with example.
5b. Write a C program to swap two integers using call by value method of passing arguments
to a function.
#include <stdio.h>
void swap(int, int);
int main()
{
int x, y;
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
swap(x, y);
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}
void swap(int a, int b)
{
int temp;
temp = b;
b = a;
a = temp;
printf("Values of a and b is %d %d\n",a,b);
}
6a. What is an array? Explain how two dimensional arrays are declared and initialized.
Array is a collection of elements of same data type. The elements are stored sequentially one
after the other in memory.
6c. Discuss any three operations that can be performed on arrays with example.
1. Searching: It is used to find out the location of the data item if it exists in the given
collection of data items.
2. Sorting: The Process of arranging the elements in ascending or descending order is called
sorting
3. Merging: It is used to combine the data items of two sorted files into single file in the sorted
form.
#include<stdio.h>
void main()
{
int a[10],b[10],c[20],i;
clrscr();
printf("Enter Elements in 1st Array: ");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("Enter Elements in 2nd Array: ");
for(i=0;i<10;i++)
{
scanf("%d",&b[i]);
}
printf("\nElements of Array After Merge: ");
for(i=0;i<10;i++)
{
c[i]=a[i];
c[i+10]=b[i];
}
for(i=0;i<20;i++)
{
printf(" %d",c[i]);
}
}
7a. What are strings? Mention different operations that can be performed on strings? Explain
any two with an example?
String constant or a string literal can be defined as a sequence of characters enclosed in double
quotes that will be treated as a single data element followed by a null character ‘\0’ (Null
character indicates the end of string).
7c. Discuss the working of the following string manipulation functions with suitable code: i)
strchr ii) strspn iii) strcmp iv) strcpy
i) strchr
The C library function char *strchr(const char *str, int c) searches for the first occurrence of
the character c (an unsigned char) in the string pointed to by the argument str.
ii) strspn
The function strspn() searches specified string in the given string and returns the number of
the characters that are matched in the given string.
#include <stdio.h>
#include <string.h>
int main () {
int len;
const char str1[] = "abcdefgh";
const char str2[] = "abXXcdeZZh";
/* Searching the string str2 in the string str1 * It returns the count of characters of str2 that
* are matched in the str1 */
len = strspn(str1, str2);
printf("Number of matched characters: %d\n", len );
return 0;
}
iii) strcmp:
String Compare:- The function strcmp() is used to compare the string data every character of
one string is compared with the corresponding position character of second string
Syntax: strcmp(str1,str2);
iv) strcpy:
String Copy:- The function strcpy() copies the content from one string to another string.
Syntax: strcpy(str2,str1);
8a. Define pointers and explain how to declare a pointer variable? Differentiate null pointer
and void pointer with suitable example?
The pointers in C language refer to the variables that hold the addresses of different variables
of similar data types.
We use pointers to access the memory of the said variable and then manipulate their addresses
in a program.
Every variable is a memory location and every memory location has its address defined which
can be accessed using ampersand (&) operator, which denotes an address in memory.
8b. Write a program to add two integers by passing pointer variable as parameters to functions?
8c. Write a program to print all even numbers from m to n using pointers?
#include<stdio.h>
void main()
{
int a[10],n,i;
printf(“enter size of the array”);
scanf(“%d”, &n);
printf(“enter the elements”);
for(i=0; i<n;i++)
scanf(“%d”, a[i]);
printf(“even numbers are \n”);
for (i=0; i<n;i++)
{
if (*(a+i)%2==0)
{
printf(“%d\n”, *(a+i));
}
}
}
9a. What is structure? Explain the C syntax of structure declaration with example
9c. Write a C program to maintain a record of “n‟ students details using an array of structures
with four fields (roll no, name, marks and grade).Assume appropriate data type for each field.
Print the names of the student with marks>=70.
10a. Explain I/O stream and Data I/P stream used in files.
Stream
A stream can be defined as a sequence of data. There are two kinds of Streams −
This stream is used for reading data from the files. Objects can be created using the
keyword new and there are several types of constructors available.
During stream I/O operations a pointer is maintained for each stream. The pointer references
the current position in a stream where a read operation or a write operation takes place. The
position in a stream is relative to its beginning, counted as number of characters. Position 1 is
always the first character. Pointers increase automatically during read operations and write
operations. This eases the sequential reading from or writing to streams. The stream I/O
functions can modify the position pointers by specifying explicit character or line positions to
be read or written.
When streams are declared, they are given names. A stream can be any source or destination
of external data that a program uses. Typical streams are files and data sets, and consoles for
interactive input and output. The stream I/O concept also allows to view other sources and
destinations as streams, for example, a reader, puncher, printer, program stack, queue, or a
communication path. Programming environments that support stream I/O usually provide
a default input stream, which is often the terminal input buffer, and a default output stream,
often the display.
10b. Define Enumerated data type. Explain the declaration and access of enumerated data types
with a code in C.
10c. Write a function to perform the following i) Read data from a fileii) Write data to a
file