CP QuetionBank Answers
CP QuetionBank Answers
COMPUTER PROGRAMMING-
2DEC0101
1
Sem.: 1st C.E.
Subject: C.P.
Start/End Symbol
The terminator symbol marks the starting or ending point of the system. It usually contains the
word "Start" or "End."
Decision Symbol
FALSE TRUE
A decision or branching point. Lines representing different decisions emerge from different points
of the diamond.
Input/Output Symbol
Represents material or information entering or leaving the system, such as customer order (input)
or a product (output).
Connector Symbol
Indicates that the flow continues where a matching symbol (containing the same letter) has been
placed.
1
Sem.: 1st C.E.
Subject: C.P.
Process / Rectangle
Flowchart Process object is used to illustrate a process, action or an operation. These are represented by
rectangles; and the text in the rectangle mostly includes a verb. Examples include 'Edit video', 'Try Again',
'Choose your Plan'.
Loop
It provides the flow of program in simplified form but do not have any syntax.
Types of Algorithm:
1) Divide and Conquer
2) Greedy Method
3) Branch and Bound
4) Recursion
2
Sem.: 1st C.E.
Subject: C.P.
It is used to solve complex problem easily. Complex problem are divided into several
steps which make problem easy to solve.
b. Greedy Method:-
It is used to solve optimization problem. We can select one best solution from
several solutions by using this method.
When several statements are executed repeatedly at that time this algorithm is used.
d. Recursion:-
There are two types of header files: the files that the programmer writes and the files that come
with your compiler.
You request the use of a header file in your program by including it, with the C
preprocessing directive #include like you have seen inclusion of stdio.h header file, which comes
along with your compiler.
Both user and system header files are included using the preprocessing directive #include.
1. STDIO
2. CONIO
3. MATH
4. STRING
3
Sem.: 1st C.E.
Subject: C.P.
5. CTYPE
✓ Easier to learn
✓ Require less time to write
✓ Provide better documentation
✓ Easier to maintain
✓ Rich set of operator
✓ Readability
✓ File handling
✓ Flexible control structure
C is known as structured language because C program can be divided into different parts.
Some of the parts are optional and some parts are compulsory.
Optional part is written as per requirement of program and compulsory part is written
necessary. If compulsory part is not written then program is not compile or run.
Answer:
C is known as middle level language because it provides functionality of both low level and
high level language. Binary or assembly functionality of low level language and modular
programming, backup functionality of high level language. That both functionality provided
by C.
C provides good programming compared to low level language and good machine efficiency
as compared to high level language.
4
Sem.: 1st C.E.
Subject: C.P.
C is stable language
C is standard language and not revised so that it is called stable language
C is modular Language
Large program of C is divided into small program so that it is reduce program size
C is portable language
Program of c language written for one computer can be run on another computer.
C is small language
C has only 32 keywords.
C has a data types.
C support many data types with various sizes.
C has operators
Different types of like arithmetic, logical, relational etc are supported by C.
C is quick language
Program run in C is faster than other language.
C is core language
It is common and popular language.
Comment Section:-
It is optional part. It is used to display the name of program or any massage related to
program.
Link Section:-
It is compulsory part. It is used to link with library. It includes header files.
Definition Section:-
It is optional part. It is used to define symbolic constant if required.
Global Declaration Section:-
It is optional part. It is used to declare global variable if required.
5
Sem.: 1st C.E.
Subject: C.P.
Main () function:-
It is compulsory part. It is very important part of program. It contains syntax and
logic of your program. It is enclosed by opening and closing curly brackets.
Algorithm Flowchart
Represent problem solution step by step in Represent problem solutions in graphical
descriptive format format
Written using special words are used like Drawn using some shapes or symbols like
start, stop, input, print rectangle, diamond etc
Easy to write even problem solution is Difficult to draw if problem solution is
complex complex.
Not easy to understand as compared to Easy to understand as compared to
flowchart algorithm
There is no need to write algorithm again if Generally it is required to redraw again if
minor change are occurred minor change are occurred
Int char float double If else do while For break continue switch
(b) Identifier:
6
Sem.: 1st C.E.
Subject: C.P.
Both uppercase and lowercase letters are allowed in the name of identifier.
•The value of the variable may changes during execution of the program.
I. The variable name may consist of letters (A-Z, a-z), digits (0-9) and underscore symbol (_).
7
Sem.: 1st C.E.
Subject: C.P.
The value of the constant cannot change during execution of the program.
(1) Integer constant: Integer constant is a sequence of digits. There are 3 types of integer.
(ii) Octal: An octal integer constant consists of any combination of digits from
(2) Real constant: Real constant is a sequence of digits with decimal points. Such a constant
are used to represent weight, height etc.
Example: 12.34.
Example: ‘a’
(4) String Constant: String constant is a sequence enclosed in a double quotation mark.
Example: ‘’Hello’’
Constant can be declared in a program using count keyword. For Example: const PIE = 3.14;
Q.13 List basic types available in ‘C’ along with modifier and size in byte? OR
Give Classification of data type & explain fundamental data type?
Answer:
List basic types available in ‘C’ along with modifier and size in byte
8
Sem.: 1st C.E.
Subject: C.P.
Data Types
int short int long int float double long double signed unsigned
Floating Types: floating is used to store only real number. It can be store
only 32 bits.
By default it can be used 6 digits after floating point. To used real number in
program declare variable as float.
9
Sem.: 1st C.E.
Subject: C.P.
The value of the constant cannot change during execution of the program.
(1) Integer constant: Integer constant is a sequence of digits. There are three types of integer.
(ii) Octal: An octal integer constant consists of any combination of digits from
(2) Real constant: Real constant is a sequence of digits with decimal points. Such a constant
are used to represent weight, height etc.
Example: 12.34.
(3) Character constant: Character constant is a single character enclosed in a single quotation
mark.
Example: ‘a’
(4) String Constant: String constant is a sequence enclosed in a double quotation mark.
Example: ‘’Hello’’
10
Sem.: 1st C.E.
Subject: C.P.
(3) Value of the constant represents the value, which we want to assign to the
Example:
1) The rules for naming symbolic constant are same as the rules for naming variables.
Symbolic names are written in capital letters to distinguish them from the normal variable.
2) There should be no blank space between the pound sign ‘#’ and the word defines.
4) A blank space is required between #define and symbolic name and between the symbolic
name and the value.
6) After defining the symbolic constant they should not be assigned any other value in the
program.
7) There is no need to declare data type for the symbolic constant. The data type depends on
the type of the constant.
8) #define statement may appear anywhere in the program but before it is referenced in the
program.
11
Sem.: 1st C.E.
Subject: C.P.
Register The variable with the register storage class are stored in computer register
rather than in the memory so we can fast access this variables.
It can be declared as register int x;
Extern Global variables are declared in this storage class.
It can be declared as extern int x;
An operator is a symbol that tell the computer the perform certain Mathematical or logical
manipulation.
Operators are always associated with operands. Depending upon number of operator there are
basically three types of operators.
1) Binary operator: The operator which has two operands is known as binary operator.
Example :a+b
2) Unary operator: The operator which has one operand is known as unary operator.
Example : -a
3) Ternary operator: The operator which has three operands is known as ternary operator.
Example: (a>b)?a:b
1) Arithmetic operator
2) Relational operator
3) Logical operator
4) Assignment operator
6) Conditional (ternary)operator
7) Bitwise operator
8) Special operator
12
Sem.: 1st C.E.
Subject: C.P.
➢ Addition, subtraction, multiplication, division in when the both operands are integers the
result is always integers. if one or both operands are real the result is real.
2) Unary minus(-):
➢ It multiplier the operand by +1
➢ Example: -a
3) Binary plus(+):
➢ It add two operands.
➢ Example : a+b
4) Binary minus(-):
➢ It subtracts two operands.
➢ Example :a-b
5) Multiplication(*):
➢ It multipliers two operands
➢ Example :a*b
6) Division(/) :
➢ It division one operand by another operand.
➢ Example: a/b
7) Modulo operator (%):
➢ It divides one operand by another operand and produce reminder of the
division.
➢ Example :a%b
➢ Modulo operator does not work with floating point data. It only works with
the integer data.
Now assume that a=5 and b=2 then
1) +a=5
2) –b=-2
3) a+b =7
4) a-b =3
5) a*b =10
6) a/b =2
7) a%b =1
13
Sem.: 1st C.E.
Subject: C.P.
1) a= =b false
2) a!=b true
3) a<b true
4) a<=b true
5) a>b false
6) a>=b false
14
Sem.: 1st C.E.
Subject: C.P.
2) Logical OR(||):
➢ The truth table logical OR (||) operator is given below:
False False False
False True True
True False True
True True True
False True
True False
➢ Examples :
15
Sem.: 1st C.E.
Subject: C.P.
➢ Examples:
1) X = x+1 can be return as x+=1
2) X = x*(a+b) can be return as x*=(a+b)
3) A= a%10 can be return as a%=10
➢ Advantage of short hand assignment operator are:
1) The variable on the left hand side need not be repeated so it is easy write.
2) The statement is more efficient.
3) The statement is more concise and easier to read.
16
Sem.: 1st C.E.
Subject: C.P.
m=5
y = ++m
m=5
y = m++
2) Decrement operator:
➢ Decrement operators subtract one from its operand.
➢ Example: m=5 then m-- will Decrement the value of m by one. Thus
the value of m becomes 4 now.
➢ There are two type of Decrement operator available.
i) Prefix Decrement operator:
• When prefix Decrement operator is used in expression then it will
first Decrement the value of its operand and then assign it to
variable.
Example:
m=5
y = --m
m=5
17
Sem.: 1st C.E.
Subject: C.P.
y = m--
18
Sem.: 1st C.E.
Subject: C.P.
Examples:
Syntax:
If(condition)
19
Sem.: 1st C.E.
Subject: C.P.
else
20
Sem.: 1st C.E.
Subject: C.P.
#include <stdio.h>
int main(){
int num;
scanf("%d",&num);
printf("%d is even.",num);
else
printf("%d is odd.",num);
return 0;
Syntax:
If(condition 1)
If(condition 2)
Statement-1;
21
Sem.: 1st C.E.
Subject: C.P.
Else
Statement-2;
Else
Statement-3;
2. if the condition 1 is true then condition 2 is checked. If condition 2 is true then statement-1
is executed.
22
Sem.: 1st C.E.
Subject: C.P.
#include <stdio.h>
int main(){
int numb1, numb2;
printf("Enter two integers to check\n");
scanf("%d %d",&numb1,&numb2);
if(numb1==numb2) //checking whether two integers are equal.
printf("Result: %d = %d",numb1,numb2);
else
if(numb1>numb2) //checking whether numb1 is greater than numb2.
printf("Result: %d > %d",numb1,numb2);
else
printf("Result: %d > %d",numb2,numb1);
return 0;
}
Enter two integers to check.
5
3
Result: 5 > 3
23
Sem.: 1st C.E.
Subject: C.P.
Syntax:
If(condition-1)
Statement(s)-1;
Else if(condition-2)
24
Sem.: 1st C.E.
Subject: C.P.
Statement(s)-2;
Else if(condition-3)
Statement(s)-3;
Else if(condition-N)
Statement(s)-N;
Else
Default statement(s)-N;
1. first condition-1 is executed , if the condition-1 is true then the statement-1 is executed.
2. if the condition-1 is false then condion-2 is checked. If condition-2 is true then statement-2
is executed.
3. This procedure repeated until all the condition is checked. if all the condition became false
then the default statement is executed.
25
Sem.: 1st C.E.
Subject: C.P.
• Writing the code using the multiple if..else becomes lengthy and also difficult to manage.
Using switch statement it is done by easy.
• The switch statement test the value of a given variable against a list of case values and
when the match is found, a statement associated with the case is executed.
Case 1:
statement(s)1;
Break;
Case 2:
statement(s)2;
Break;
Case N:
statement(s)N;
Break;
Default :
default_statement(s);
• Value-1,value-2 is constant or known as case labels ,each case lable value must be unique
with a switch statement. each case lable must end with (;).
26
Sem.: 1st C.E.
Subject: C.P.
1. the value of the expression is compared against the value of case lable.
2. if the case is found whose value match with value of the expression ,then then the
statement associated with the case is executed. there is a single statement or multiple statement.
There is break which send the control to the next statement.
3. if the value of the expression does not match with any case value then the statement
associated with the default case is executed.
The ‘c’ is a structural programming language where use of goto statement is a dangerous
because the use of the goto statement in the program makes it difficult to understand and debug.
Syntax:
Goto lable;
Here , lable is the lable to the statement to which goto transfer control.
1. forward reference
2. backward reference
1) difference in flowchart:
27
Sem.: 1st C.E.
Subject: C.P.
2) In while loop the condition is tested first and then the statements are executed if the
condition turns out to be true.
In do while the statements are executed for the first time and then the conditions are
tested, if the condition turns out to be true then the statements are executed again.
3) In a while loop the condition is first tested and if it returns true then it goes in the loop.
In a do-while loop the condition is tested at the last.
4) In while loop the block of code is never execute if first time condition is false.
In do while loop the block of code that must be executed at least once.
6) Syntax:
while loop : Do...While loop:
while (condition) do
{ {
Statements; Statements;
} }while(condition);
28
Sem.: 1st C.E.
Subject: C.P.
• The initialization step occurs one time only, before the loop begins.
o If the condition is true ( non-zero ), then the body of the loop is executed next.
29
Sem.: 1st C.E.
Subject: C.P.
o If the condition is false ( zero ), then the body is not executed, and execution
continues with the code following the loop.
• The incrementation happens AFTER the execution of the body, and only when the body is
executed.
Voidmain()
{ int i;
For( i=0; i<=10; i++)
{
printf(“hello /n”);
}
Getch(); }
□ The above example of for loop print 10 times hello word in output.
30
Sem.: 1st C.E.
Subject: C.P.
Q.34 What is array? Explain 1-D array with the help of example.
Answer:
o An array is collection of elements of same data type that is referred by a
single name.
o The elements of an array are referred by their index or subscript value.
Salary[0] Salary[1] Salary[2] Salary[3] Salary [4] Salary [5] Salary [6] Salary [7] Salary [8]
Salary [9]
23 54 62 32 01 2 15 41 22 32
Index 0 1 2 3 4 5 6 7 8 9
31
Sem.: 1st C.E.
Subject: C.P.
32