0% found this document useful (0 votes)
84 views28 pages

22POP13set3 Answers

The document is a model question paper for the course "Principles of Programming Using C". It contains questions on various topics related to C programming. The first section contains questions about computer components, input devices, and an algorithm to compute the area of a circle. The second section defines identifiers and validates some examples, summarizes input/output statements, and explains the software development life cycle with a diagram. The third section describes operator types in C with examples, differentiates between type conversion and casting, and provides a program to compute quadratic equation roots. The final section defines looping and explains while and do-while loops with examples.

Uploaded by

alsonmathias1209
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views28 pages

22POP13set3 Answers

The document is a model question paper for the course "Principles of Programming Using C". It contains questions on various topics related to C programming. The first section contains questions about computer components, input devices, and an algorithm to compute the area of a circle. The second section defines identifiers and validates some examples, summarizes input/output statements, and explains the software development life cycle with a diagram. The third section describes operator types in C with examples, differentiates between type conversion and casting, and provides a program to compute quadratic equation roots. The final section defines looping and explains while and do-while loops with examples.

Uploaded by

alsonmathias1209
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

22POP13/23 Principles of Programming using C

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.

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 1


22POP13/23 Principles of Programming using C

1b. Describe any three input devices


The Keyboard: A keyboard is defined as the set of typewriter-like keys that enables you to
enter data into a computer or other devices. Computer keyboards are similar to
electrictypewriters but contain additional typing keys. The standard selection of keys can be
classified as Alphanumeric keys, Punctution keys and special keys. The standard layout of
letters, numbers, and punctuation is known as QWERTY because the first six typing keys on
the top row of letters spell QWERTY. The QWERTY keyboard was designed in the 1800s for
mechanical typewriters and was actually designed to slow typists down to avoid jamming the
keys on mechanical units.
Pointing Devices: A pointing device, or sometimes called a pointing tool, is a hardware input
device that allows the user to move the mouse cursor in a computer program or GUI operating
system. Using a pointing device, you can point at or manipulate any object or text on the screen.
For example, using a pointing device you could point at and select an icon from a list of icons.
The Scanner: A scanner is an input device that scans documents such as photographs and
pages of text. When a document is scanned, it is converted into a digital format. This creates
an electronic version of the document that can be viewed and edited on a computer.
1c. Design an algorithm, flowchart and program to compute area of a circle.

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 2


22POP13/23 Principles of Programming using C

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

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 3


22POP13/23 Principles of Programming using C

2c. Explain the SDLC life cycle for the efficient design of a program with a neat diagram.

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 4


22POP13/23 Principles of Programming using C

1) C program (source code) is sent to preprocessor first. The preprocessor is responsible to


convert preprocessor directives into their respective values. The preprocessor generates an
expanded source code.

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.

3a. Describe any 4 types of operators in C with example


C language has built-in operators & provides the following types of operators
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Misc Operators

An arithmetic operator performs mathematical operations such as addition, subtraction,


multiplication, division etc. on numerical values (constants and variables).

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 5


22POP13/23 Principles of Programming using C

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

3b. Differentiate between type conversion and type casting in C

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 6


22POP13/23 Principles of Programming using C

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

 A data type is converted into another data type by the compiler.


 It can be applied to compatible data types only.
 There is no need to use a casting operator.
 The destination data type can’t be smaller than source data type.
 It is done at compile time.
 It is also known as widening conversion since the destination data type can’t be
smaller than the source data type.
 It is not used frequently in coding and competitive programming since it may
result in a wrong answer.
 It is less efficient.
 It is less reliable.

3c. Write a program to compute the roots of quadratic equation by accepting coefficients

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 7


22POP13/23 Principles of Programming using C

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.

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 8


22POP13/23 Principles of Programming using C

4b. Implement a C program to simulate a simple calculator that performs arithmetical


operations using switch statements.

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 9


22POP13/23 Principles of Programming using C

4c. Explain unconditional statements with example


• An unconditional statements are the statements which transfer the control or flow of
execution unconditionally to another block of statements. They are also called jump
statements.
• There are four types of unconditional control transfer statements.
(i) break
(ii)continue
(iii) goto
(iv)return
(i)break Statement: A break statement terminates the execution of the loop and the control is
transferred to the statement immediately following the loop. i.e., the break statement is used to
terminate loops or to exit from a switch.
Syntax :
Jump-statement;
break;
(ii) continue statement: The continue statement is used to bypass the remainder of the current
pass through a loop.
The loop does not terminate when a continue statement is encountered.
Instead, the remaining loop statements are skipped and the computation proceeds directly to
the next pass through the loop.
Syntax :
Jump-statement;
Continue;
(iii)goto statement :
The goto requires a label in order to identify the place where the branch is to be made. A label
is any valid variable name and must be followed by a colon (: ).
The label is placed immediately before the statement where the control is to be transferred.
The label can be anywhere in the program either before or after the goto label statement.
Syntax:
goto label;
.............
.............
.............
label: statement;
(iv)return statement :
• The return statement terminates the execution of a function and returns control to the
calling function.
• Execution resumes in the calling function at the point immediately following the call.
A return statement can also return a value to the calling function.
• Syntax :
Jump-statement:
return expression;

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 10


22POP13/23 Principles of Programming using C

5a. Explain the syntax of Function Declaration and Function Definition with example.

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 11


22POP13/23 Principles of Programming using C

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

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 12


22POP13/23 Principles of Programming using C

5c. Describe different types of storage classes with example.

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 13


22POP13/23 Principles of Programming using C

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.

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 14


22POP13/23 Principles of Programming using C

6b. Write a C program to find transpose of a 3x3 matrix.


#include<stdio.h>
int main()
{
int mat[3][3], i, j, matTrans[3][3];
printf("Enter 3*3 Matrix Elements: ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
scanf("%d", &mat[i][j]);
}
// Transposing the Matrix...
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
matTrans[j][i] = mat[i][j];
}
printf("\nTranspose of given Matrix is:\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
printf("%d ", matTrans[i][j]);
printf("\n");
}
return 0;
}

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.

/*C program to search an element in an array using linear search*/

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 15


22POP13/23 Principles of Programming using C

2. Sorting: The Process of arranging the elements in ascending or descending order is called
sorting

/*C program to sort n numbers using Bubble sort*/

3. Merging: It is used to combine the data items of two sorted files into single file in the sorted
form.

/*C program to merge two arrays*/

#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: ");

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 16


22POP13/23 Principles of Programming using C

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).

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 17


22POP13/23 Principles of Programming using C

7b. Explain array of strings with an example?


The array of strings is an array of one dimensional character array which consist of strings as
its individual elements.
Syntax: char string_name[size1][size2];
where, char is a datatype of strings string_name is a valid identifier which is the name of the
string variable
size1 indicates the number of strings in the array
size 2 indicates the maximum length of each string.
Static Initialization of strings:-
char str1[3][10]={“Thomas”,“Bob ”,“Alice”};

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 18


22POP13/23 Principles of Programming using C

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.

 str − This is the C string to be scanned.


 c − This is the character to be searched in str.
#include <stdio.h>
#include <string.h>
int main ()
{
char string[55] ="This is a string for testing";
char *p;
p = strchr (string,'i');

printf ("Character i is found at position %d\n",p-string+1);


printf ("First occurrence of character \"i\" in \"%s\" is" \
" \"%s\"",string, p);
return 0;
}

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>

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 19


22POP13/23 Principles of Programming using C

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

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 20


22POP13/23 Principles of Programming using C

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.

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 21


22POP13/23 Principles of Programming using C

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?

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 22


22POP13/23 Principles of Programming using C

#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

9b. Differentiate between Structure and Union

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 23


22POP13/23 Principles of Programming using C

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.

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 24


22POP13/23 Principles of Programming using C

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 25


22POP13/23 Principles of Programming using C

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 −

 InPutStream − The InputStream is used to read data from a source.


 OutPutStream − The OutputStream is used for writing data to a destination.
FileOutputStream.:which makes use of these two classes to copy an input file into an output
file
FileInputStream:

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.

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 26


22POP13/23 Principles of Programming using C

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

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 27


22POP13/23 Principles of Programming using C

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 28

You might also like