0% found this document useful (0 votes)
221 views20 pages

22POP13set1 Answers

The document provides information about different types of computers based on speed, memory and cost. It discusses supercomputers, mainframes, minicomputers, microcomputers, smartphones and embedded computers. It also provides examples of algorithms to find the area and perimeter of a circle and a C program to calculate simple interest. Additionally, it discusses variables and identifiers in C, relational, logical and conditional operators, the goto and switch statements, and formatted input/output functions like scanf() and printf().

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)
221 views20 pages

22POP13set1 Answers

The document provides information about different types of computers based on speed, memory and cost. It discusses supercomputers, mainframes, minicomputers, microcomputers, smartphones and embedded computers. It also provides examples of algorithms to find the area and perimeter of a circle and a C program to calculate simple interest. Additionally, it discusses variables and identifiers in C, relational, logical and conditional operators, the goto and switch statements, and formatted input/output functions like scanf() and printf().

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/ 20

22POP13/23 Principles of Programming using C

Model Question Paper-I with effect from 2022-23 (CBCS Scheme) - 22POP13

Principles of Programming Using C


1a. Define computer. Describe the various types of computers based on speed, memory and cost.
Computer is an electronic device which accepts input, processes data, stores information and produces
output. Data: Raw facts/ figures, Information: Processed data.
The various types of computers based on speed, memory and cost:
Apart from being classified by generations, computers can also be categorized by their size. The size of
a computer is often an indirect indication of its capabilities.
Supercomputers: These are huge machines having most powerful and fastest processors. It uses
multiple CPUs for parallel data processing. Speeds are measured in flops (floating point operations per
second). The fastest operates at 34 petaflops. They are used for weather forecasting, analysis of
geological data. They have enormous storage, uses more power and generate lot of heat. They are used
by government agencies.
Mainframes: These are multi-user machines that support many users using the feature of time sharing.
It can run multiple programs even with a single CPU. The processor speed is measured in MIPS (Million
instructions per second). It is used to handle data, applications related to organization and online
transactions in banks, financial institutions and large corporations.
Minicomputers/Midrange computers: It was introduced by DEC (Digital Equipment Corporation).
They can serve hundreds of users and are small enough to partially occupy a room. They are used in
smaller organizations or a department of a large one. They are not affordable to be used in home.
Microcomputers: The microcomputer or PC is introduced by Apple and endorsed by IBM. This is a
single-user machine powered by a single-chip microprocessor. They are very powerful machines having
gigabytes of memory. They are both used in standalone mode and in a network. A microcomputer takes
the form of desktop, notebook (laptop) or a netbook (smaller laptop). PCs today are powered by 3 types
of OS – windows (7, 8 or 10), Mac OS X (Apple) and Linux. They are used for engineering and
scientific applications and for software development.
Smartphones and Embedded Computers: The smartphone is a general purpose computer i.e.,
capable of making phone calls. It has a powerful processor, with multiple cores, supports GBs of
memory, and runs developed OS (Android or iOS). It can be operated with keyboard, touch or stylus.
Embedded Computers or micro-controllers are very small circuits containing a CPU, non-volatile
memory, input and output handling facilities. They are embedded into many machines that we use –
cars, washing machines, cameras etc. The processor here runs a single unmodifiable program stored in
memory.
1b. Develop an algorithm to find the area and perimeter of a circle. Also define an algorithm

Step 1: Start
Step 2: Input radius
Step 3: let pi = 3.14
Step 4: area = pi * radius * radius
Step 5: perimeter = 2 * pi * radius
Step 6: print area, perimeter Step 7: stop

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


22POP13/23 Principles of Programming using C

Algorithm: In its purest sense, an algorithm is a mathematical process to solve a problem using a finite
number of steps. In the world of computers, an algorithm is the set of instructions that defines not just
what needs to be done but how to do it. In an algorithm,

• Each step should be numbered in a hierarchical manner,

• Every step must be complete, unambiguous and error free


1c. Write a short note on the characteristics of a computer
1. Speed: – As you know computer can work very fast. It takes only few seconds for calculations that
we take hours to complete.
• Computer can perform millions (1,000,000) of instructions and even more per second.
2. Accuracy: – The degree of accuracy of computer is very high and every calculation is performed with
the same accuracy.
3. Diligence: – A computer is free from tiredness, lack of concentration, low energy, etc. It can work
for hours without creating any error.
4. Versatility: – It means the capacity to perform completely different type of work
5. Power of Remembering: – Computer has the power of storing any amount of information or data.
Any information can be stored and recalled as long as you require it, for any numbers of years.
6. No IQ: – Computer is a dumb machine and it cannot do any work without instruction from the user.
7. No Feeling: – It does not have feelings or emotion, taste, knowledge and experience.
8. Storage: – The Computer has an in-built memory where it can store a large amount of data.
2a . What is variable? What are the rules to construct variable? Classify the following as valid/invalid
Identifiers. i) num2 ii) $num1 iii) +add iv) a_2 v) 199_space vi) _apple vii)#12
• A variable is nothing but a name given to a storage area that our programs can manipulate.
• Each variable in C has a specific type, which determines the size and layout of the variable's
memory.
Rules to construct variable:
• Every variable name should start with alphabets or underscore (_).
• No spaces are allowed in variable declaration.
• Except underscore (_) no other special symbol are allowed in the middle of the variable
declaration (not allowed -> roll-no, allowed -> roll_no).
• Maximum length of variable is 8 characters depend on compiler.
• Every variable name always should exist in the left hand side of assignment operator (invalid -
> 10=a; valid -> a=10;).
• No keyword should access variable name (int for <- invalid because for is keyword).
• i) num2- valid ii) $num1- invalid iii) +add- invalid iv) a_2- valid v) 199_space- invalid vi)
_apple- valid vii)#12- invalid

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


22POP13/23 Principles of Programming using C

2b. Draw a flowchart and C program which takes as input p,t,r. Compute the simple interest
and display the result

2c. Write a note on the following operators. i) Relational ii) Logical iii) Conditional
i) Relational: Relational operators in C are commonly used to check the relationship between the
two variables.

ii) Logical : These operators are used to perform logical operations on the given expressions.

iii) Conditional: The conditional operator is also known as a ternary operator. The conditional
statements are the decision-making statements which depends upon the output of the expression. It is
represented by two symbols, i.e., '?' and ':'.

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


22POP13/23 Principles of Programming using C

3a. Develop a C program that takes three coefficients (a, b, and c) of a quadratic
equation;(ax2+bx+c) as input and compute all possible roots and print them with appropriate
messages.

3b. Explain the working of goto statement in C with example.

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


22POP13/23 Principles of Programming using C

If the label statement is below the goto statement then it is called forward jump. if the label statement
is above the goto statement then it is called backward jump.
3c. Explain switch statement with syntax and example
A switch statement tests the value of a variable and compares it with multiple cases. Once the case
match is found, a block of statements associated with that particular case is executed. Each case in a
block of a switch has a different name/number which is referred to as an identifier. The value provided
by the user is compared with all the cases inside the switch block until the match is found. If a case
match is not found, then the default statement is executed, and the control goes out of the switch block.
The break statement is used at the end of each case to come out of the switch block.

4a. Develop a simple calculator program in C language to do simple operations like addition,
subtraction, multiplication and division. Use switch statement in your program

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


22POP13/23 Principles of Programming using C

4b. Explain with examples formatted input output statements in C


Formatted Input and Output statements
scanf( ):
scanf() function reads all type of data value from input device or from a file. the address operator
“&” is used to indicate the memory location of the variable. This memory location is used to store
the data which is read through the keyboard.
Syntax: scanf(“format specifier”,addresslist);
where: format specifier indicates the type of data to be stored in the variable. address list indicates
the location of the variable where the value of the data is to be stored. the address list is usually
prefixed with an ”&”(ampersand) operator for each variable.
Example: if we want to store the values 50 and 31from the keyboard in variables num1 and num2
then the input function is read as scanf(“%d%d”,&num1,&num2); the value 50 will be assigned to
num1 and value 31 will be assigned to num2.
printf( ): In C programming language, printf() function is used to print the “character, string, float,
integer, octal and hexadecimal values” onto the output screen. The features of printf() can be
effectively exploited to control the alignment and spacing of printouts on terminals.

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


22POP13/23 Principles of Programming using C

Syntax: printf(“Text Message”); OR printf(“format specifier”,variablelist);


where: format specifier indicates the type of data to be displayed variable list indicates the value
present in the variable. the number of format specifier must match the number of variables in the
variablelist.
Example: if we want to display the values stored in variables num1 and num2 then the printf
statement can be written as printf(“The Value of num1 = %d and The value of num2 =
%d\n”,num1,num2); This statement will display the values stored in the respective variables

4c. Explain with syntax, if and if-else statements in C program


(i) simple if: This is a one way selection statement which helps the programmer to execute or skip
certain block of statements based on the particular condition.
Syntax: if(conditional_expression) { True block statements; }

(ii) if-else statement: This is a two way selection statement which executes true block or false block
of statements based on the given condition. The keyword “else” is used to shift the control when
the condition is evaluated to false.
Syntax: if(conditional_expression) { } else { } True block statements; False block statements;

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


22POP13/23 Principles of Programming using C

5a. Write a C program to swapping of 2 numbers using call by reference and call by value.
Call by value. Call by reference

5c. Discuss the implementation of user defined function with suitable example.
The user defined function is defined by the user according to its requirements instead of relying
only on the built-in functions C allows us to create our own function called user defined function.
Parts of user defined function. (i)Function Declaration or Function prototype (ii)Function call or
calling Function (iii)Function Definition or defining a function.
i)Function Declaration or Function prototype :- It will inform the compiler about the return type,
function name and number of arguments along with the data types.
syntax: return_type function_name(argument _list);
ii) Function call or calling function :- invoking the function with valid number of arguments and
valid data type is called as function call.
Syntax: function_name(argumement_list);
iii) Function definition or defining a function :- The declared function must define the same to
perform the specific task.

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


22POP13/23 Principles of Programming using C

Syntax :return_type function_name(argument _list) { local_variable_declaration; Body of the


function; }

6a. Write a C program to find the product of two given matrix.

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


22POP13/23 Principles of Programming using C

6b. Explain the working of recursion with suitable example.


when a function calls itself again and again it is called as recursion
• Here the calling function and called function are same
• It is a powerful technique of writing complex algorithms in an easier way
• The recursive function must always have a stopping condition or an exit condition in
the function else the program will go into infinite loop

6b. Explain the declaration and initialization of one dimensional and two dimensional arrays with
an example.
Single Dimensional Array :- An Array which has only one subscript is known as Single
dimensional array or One dimensional array The individual array elements are processed by using
a common array name with different index values that start with Zero and ends with array_size-1.
Syntax of Declaring Single Dimensional Arrays: data_type array_name[array_size];
where data_type: can be int, float or char array_name: is name of the array
array_size : an integer constant indicating the maximum number of data elements to be stored.
Example: int a[5];
The general syntax of initialization of array is
data_type array_name[array_size]= {List of values};
Example: int b[4]={10,12,14,16};
Here each value will be stored in respective index values of the array.

Two dimensional array: The simplest form of multidimensional array is two dimensional array.
Arrays with two or more dimensions are called multi-dimensional arrays. (in terms of rows and
columns) of same data type or An array which has two subscripts are known as two dimensional
arrays.
The first subscript represents rows and the second subscript represent column.

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


22POP13/23 Principles of Programming using C

Syntax: data_type array_name[size1][size2];


where, data_type: is the type of data to be stored and processed in the computer’s memory
array_name: is a valid identifier representing name of the array.
[size1]: indicates number of rows in the array
[size2]: indicates the number of columns in the array.
Example: int a[2][3];
Initialization of two dimensional array :
1)Initializing all elements row wise:- A multidimensional array can be initialized by specifying
bracketed values for each row.
Example: int[2][3]={{5,3,4} {6,1,2}} ;
this initialization can also be written as int a[2][3] ={5,3,4,6,1,2}

7a. Develop a C program to concatenate 2 strings without using built-in function

7b. Define String. Explain any 4 string manipulation function with suitable 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. 11


22POP13/23 Principles of Programming using C

1.
2.

3.

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


22POP13/23 Principles of Programming using C

4.

7c. Explain the difference between gets() and scanf() functions

8a. Develop a C program to find the largest of three numbers using pointer.

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


22POP13/23 Principles of Programming using C

8b. Define Pointer. Explain pointer variable declaration and initialization 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.
Declaration of a pointer is done before using it to store any variable address.
The general form of a pointer variable declaration is − type *var-name; type is the pointer's base type;
it must be a valid C data type and var-name is the name of the pointer variable.
The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this
statement the asterisk is being used to designate a variable as a pointer.

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


22POP13/23 Principles of Programming using C

8c. Explain the difference between a null pointer and a void pointer

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


22POP13/23 Principles of Programming using C

9a. Discuss the general syntax of structure variable declaration of structure to store book information.
A Structure variable declaration is similar to the declaration of variables of any other data types.
It includes the following elements.

1. The keyword struct.


2. The structure tag name.
3. List of variable names separated by commas.
4. A terminating semicolon

• Keyword struct: The keyword struct is used at the beginning while defining a
structure in C. Similar to a union, a structure also starts with a keyword.

• structName: This is the name of the structure which is specified after the keyword
struct.

• data_Type: The data type indicates the type of the data members of the
structure. A structure can have data members of different data types.

• member_name: This is the name of the data member of the structure. Any number
of data members can be defined inside a structure. Each data member is allocated a
separate space in the memory.

Syntax: struct book_bank book1, book2, book3;


Declaration

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


22POP13/23 Principles of Programming using C

9b. Differentiate between structure and union.

9c. Write a program to write employees details in a file called employee.txt. Then read the record of
the nth employee and calculate his salary.

#include<stdio.h>
typedef struct
{
char name[30];
int id;
int salary;
}employee;
int main()
{
int n,i,j;
FILE *fptr;
employee e[10], temp;
/* Reading file in binary read mode */
fptr = fopen("employee.txt","rb");
if(fptr == NULL)
{
printf("File error!");
exit(1);
}
printf("Enter how many records:\n");
scanf("%d",&n);

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


22POP13/23 Principles of Programming using C

/* Reading from file and storing them in structure array */


for(i=0;i < n;i++)
{
fread(&e[i],sizeof(e[i]),1, fptr);
} /* Sorting */
for(i=0;i< n-1;i++)
{
for(j=i+1;j< n;j++)
{
if(e[i].salary>e[j].salary)
{
temp = e[i];
10a. Discuss the different modes of operation on files with suitable example.

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


22POP13/23 Principles of Programming using C

10b. Differentiate between gets() and fgets().


gets()

gets() is used to read string from the standard input device until newline character not found,
use of gets() may risky because it does not check the array bound.

For example: if you have a character array with 20 characters and input is more than 20
characters, gets() will read all characters and store them into variable. Since, gets() does not
check the maximum limit of input characters, so any time compiler may return buffer overflow
error.

fgets()

fgets() is used to read string till newline character or maximum limit of the character array, use
of fgets() is safe as it checks the array bound.

fgets() has following parameters: buffer, maximum length, and input device reference.

10c.Implement structures 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

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


22POP13/23 Principles of Programming using C

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

You might also like