Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
20 views33 pages

CP QuetionBank Answers

The document is a question bank for a Computer Programming course, covering fundamental concepts such as flowcharts, algorithms, header files, and the C programming language. It includes definitions, advantages of high-level languages, and distinctions between flowcharts and algorithms, along with details on data types, constants, and operators in C. Additionally, it discusses storage classes and provides rules for naming variables and constants.

Uploaded by

parmarrutika1
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)
20 views33 pages

CP QuetionBank Answers

The document is a question bank for a Computer Programming course, covering fundamental concepts such as flowcharts, algorithms, header files, and the C programming language. It includes definitions, advantages of high-level languages, and distinctions between flowcharts and algorithms, along with details on data types, constants, and operators in C. Additionally, it discusses storage classes and provides rules for naming variables and constants.

Uploaded by

parmarrutika1
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/ 33

QUESTION BANK WITH ANSWERS

COMPUTER PROGRAMMING-
2DEC0101

1
Sem.: 1st C.E.
Subject: C.P.

Q.1 What is Flowchart? Draw Symbol for Flowchart.


Ans. It is graphical representation of an Algorithm is called Flowchart.

There are various symbols are used to represent logic of program.

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

DIRECTION OF FLOW OF DATA

Indicating flow of control in our problem.

Loop

Q.2 What is Algorithm? Explain it in detail.


Answer:

It is stepwise program written in our routine language.

It provides the flow of program in simplified form but do not have any syntax.

It provides the idea and logic of program.

Types of Algorithm:
1) Divide and Conquer
2) Greedy Method
3) Branch and Bound
4) Recursion

2
Sem.: 1st C.E.
Subject: C.P.

a. Divide and Conquer:-

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.

c. Branch and Bound:-

When several statements are executed repeatedly at that time this algorithm is used.

d. Recursion:-

When procedure call itself is called recursion.

Q-3 What is Header file? List out different header files.


A header file is a file with extension .h which contains C function declarations to be shared
between several source files.

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.

List of Header Files Are:

1. STDIO

2. CONIO

3. MATH

4. STRING

3
Sem.: 1st C.E.
Subject: C.P.

5. CTYPE

Q.4 List the major advantages of high level language.


Answer:

✓ Easier to learn
✓ Require less time to write
✓ Provide better documentation
✓ Easier to maintain
✓ Rich set of operator
✓ Readability
✓ File handling
✓ Flexible control structure

Q.5 why C is known as structured language.


Answer:

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.

In short compulsory part is written in proper structure and it is given by C


language so that it is known as structured language.

Q.6 Why C is known as middle level language.

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.

Q.7 Give the advantages of ‘C’ language?


Answer:

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.

Q.8 Explain Basic Structure of ‘C’


Answer:

1. Comment Section (Optional)


2. Link Section (Compulsory)
3. Definition Section (Optional)
4. Global Declaration Section (Optional)
5. Main () function (Compulsory)
{
Declaration and
Execution portion
}
6. Sub program/ user defined function (Optional)
Function 1
Function 2

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.

Sub program/ user defined function


It is optional part. It is depend upon the requirement of main () part where the user
defined functions are to be utilized.

Q.9 Differentiate Flowchart and Algorithm


Answer:

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

Q.10 Define following terms.


(a) Keyword:

• Keywords are those words whose meaning is predefined.

• The programmer cannot change the meaning of keywords.

• There are 32 keywords available in c.

• All the keywords must be written in lowercase.

• Following is the list of keywords:

Int char float double If else do while For break continue switch

(b) Identifier:

6
Sem.: 1st C.E.
Subject: C.P.

• Identifier is user define function.

• Identifier refers to the name of variable, array, function etc.

• It consists of letters, digits and underscore.

Both uppercase and lowercase letters are allowed in the name of identifier.

Q. 11 What is variable? List rules naming variable.


•A variable is a named location in computer memory.

•It is used to store the data value.

•The value of the variable may changes during execution of the program.

• Rules for naming variable:

Example: int 1NO; is not valid

Example: int continues is not valid

Example: int NO1; is not valid.

Example: int MAX;int max;

Here both MAX and max are treated as different variable.

I. The variable name may consist of letters (A-Z, a-z), digits (0-9) and underscore symbol (_).

Example: int no_1; / int no1; / int NO1;

II. The variable name cannot start with digit.

III. Keywords cannot be used as a name of variable.

IV. A white space is not allowed in the name of variable.

V. Variable is case sensitive. Uppercase and lowercase are different.

Q. 12 What is constant? Explain different types of constant.


Ans. Constant means fixed value.

7
Sem.: 1st C.E.
Subject: C.P.

The value of the constant cannot change during execution of the program.

C supports following types of constant:

(1) Integer constant: Integer constant is a sequence of digits. There are 3 types of integer.

(i) Decimal: A decimal integer constant consist of any combination of digits

(ii) Octal: An octal integer constant consists of any combination of digits from

(iii) Hexadecimal: A hexadecimal integer constant consists of any combination

Example: 0xA23, 0XBCD.

(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’’

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

Data Type Keyword Used Purpose Size (Byte) Range of


values
Character char For storing 1 Byte -128 to +127
char and string
Integer int For storing 2 Byte -32768 to
integer +32767
Floating point float For storing real 4 Byte 3.4e – 38 to
number 3.4e + 38
Large floating double For storing 8 Byte 1.7e – 308 to

8
Sem.: 1st C.E.
Subject: C.P.

point double 1.7 + 308


accuracy
floating
number

Classification of data type

Data Types

integer floating character

int short int long int float double long double signed unsigned

Fundamental data type

Integer Types: integer means it can be store only 16 or 32 bits data. If


we can store more than that data then we can used short or long int.
▪ To used integer number in program declare variable as int.
▪ Long or short are used to increase the range of values.
▪ A signed integer used for one bit for sign and 15 bits for value.

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.

Double Types: double is used to store double accuracy floating number. It


can be store only 64 bits.
By default it can be used 14 digits after floating point. To used real number in
program declare variable as float.

Character Types: Character is used to store only single character. It can be


store only 8 bits.
When you can declare a variable as a char than value of variable must be
written in single or double quote.

9
Sem.: 1st C.E.
Subject: C.P.

Q. 14 What is constant? Explain different types of constant.


Constant means fixed value.

The value of the constant cannot change during execution of the program.

C supports following types of constant:

(1) Integer constant: Integer constant is a sequence of digits. There are three types of integer.

(i) Decimal: A decimal integer constant consist of any combination of digits

(ii) Octal: An octal integer constant consists of any combination of digits from

(iii) Hexadecimal: A hexadecimal integer constant consists of any combination

Example: 0xA23, 0XBCD.

(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’’

Constant can be declared in a program using count keyword.

For Example: const PIE = 3.14;

Q. 15 What is Symbolic constant? How to define it?


The general syntax of defining symbolic constant is given below:

10
Sem.: 1st C.E.
Subject: C.P.

(1) #define is a preprocessor directive which is used to defined symbolic

(2) Symbolic name is a name of the constant, which we want to define.

(3) Value of the constant represents the value, which we want to assign to the

Example:

#define PIE 3.14

#define symbolic-name value of the constant

Rules of defining symbolic constant:

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.

3) ‘#’ must be the first character in the line.

4) A blank space is required between #define and symbolic name and between the symbolic
name and the value.

5) #define statement must not end with a semicolon.

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.

Q.16 Explain storage classes.


Storage Meaning
Class
Auto Local variable are stored in auto storage class. By default all variables are
having auto storage class when they are declared.
It can be declared as int x, y, z; OR auto int x, y, z;
Static It retains the value even if the control is transferred to the function when it is
called. Local variables are stored.
It can be declared as static int x;

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;

Q.17 What is operator? List various operators available in c.


Various operator available in C are as follows:

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

5) Increment and decrement operator

6) Conditional (ternary)operator

7) Bitwise operator

8) Special operator

Q.18 Explain Arithmetic operators with example ?

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.

The arithmetic operator support by c language as bellow.


1) Unary plus(+):
➢ It multiplier the operand by +1
➢ Example :+a

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

Q.19 Explain Relational operators with example?

13
Sem.: 1st C.E.
Subject: C.P.

➢ Relational operators are used for comparisons.


➢ If we want to take decision based on certain condition at that time we have to use
relational operator.
➢ An expression, which contains relational operator, is known as relational expression.
➢ The relational expression returns either TRUE(1) or FALSE(0) based on result of
condition

➢ The relational operator support by c language is given bellow.


i) Equal to(= =):
• It compares two operands and return TRUE (1) if both operands are equal.
ii) Not equal to (! =)
• It compares two operands and return TRUE (1) if both operands are not equal.
iii) Less than(<):
• It compares two operands and return TRUE (1) if operand1 is less than operand2.
iv) Less than equal to(<=):
• It compares two operands and return TRUE (1) if operand1 is less than or equal
to operand2.
v) Greater than(>):
• It compares two operands and return TRUE (1) if operand1 is greater than
operand2.
vi) Greater than equal to(>=):
• It compares two operands and return TRUE (1) if operand1 is greater than or
equal to operand2.
Examples:

Suppose a=5, b=7

1) a= =b false
2) a!=b true
3) a<b true
4) a<=b true
5) a>b false
6) a>=b false

Q.20 Explain Logical operators with example?


➢ Logical operators are used when we want to combine more than one condition.
➢ An expression, which combines more than one condition, is known as logical
expression or compound relational expression.
➢ The logical expression returns either true (1) or false (0) value based on conditions and
truth table.

14
Sem.: 1st C.E.
Subject: C.P.

➢ The logical operator supported by c language is given bellow:


1) Logical AND(&&):
➢ The truth table logical AND (&&) operator is given below:

False False False


False True False
True False False
True True True

➢ If all the condition is true then it returns true otherwise false.

2) Logical OR(||):
➢ The truth table logical OR (||) operator is given below:
False False False
False True True
True False True
True True True

➢ If all the condition is false then it returns false otherwise true.

3) Logical NOT (!):


➢ The truth table logical NOT (!) operator is given below:

False True
True False

➢ Examples :

15
Sem.: 1st C.E.
Subject: C.P.

Suppose a=5, b=3, c=6;

1) (a>b) && (a>c) -false


2) (a>b) || (a>c) -true
3) !(a>b) -false

Q.21 Explain Assignment operators with example?


➢ The assignment operator assigns the result of an expression to a variable.
➢ Example: a=5 will assign the value 5 to variable a.
➢ In addition to this assignment operator c has a set of short hand assignment operator.
➢ The short hand operator takes the following form:
➢ V op = exp.
➢ Where v is a variable, exp is an expression and op is a binary arithmetic operator.
➢ Here op = is know as short hand assignment operator.
➢ Thus the statement v op = exp is same as v = v op exp.

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

Q.22 Explain Increment & Decrement operators with example?


➢ Increment and decrement operators are unary operator because they have only one operand.
1) Increment operator:
➢ Increment operator adds one to its operand.
➢ Example: m=5 then m++ will increment the value of m by one. Thus
the value of m becomes 6 now.
➢ There are two type of increment operator available.
i) Prefix increment operator:
• When prefix increment operator is used in expression then it will
first increment the value of its operand and then assign it to
variable.
Example:

16
Sem.: 1st C.E.
Subject: C.P.

m=5

y = ++m

Will gives m=6 and y=6

ii) Postfix increment operator:


• When postfix increment operator is used in expression then it will
first assign the value of its operand to the variable and then
Decrement the value of its operand.
Example:

m=5

y = m++

Will gives m=6 and y=5

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

Will gives m=4 and y=4

ii) Postfix Decrement operator:


• When postfix Decrement operator is used in expression then it will
first assign the value of its operand to the variable and then
Decrement the value of its operand.
Example:

m=5

17
Sem.: 1st C.E.
Subject: C.P.

y = m--

Will gives m=4 and y=5

Q.23 Explain conditional operators with example?


➢ The conditional operator is also known as ternary operator. It is known as ternary operator
because it has three operands.

➢ The general form of ternary operator is follow:


▪ (Condition)? Statement-1: statement-2;
➢ First the condition is checked. If the condition is true then the statement is followed the? Is
executed otherwise the statement followed the : is executed.
➢ Example: (a>b)? printf(“a is max”): printf(“b is max”);
▪ Here if the value of a greater than b then it prints a is max otherwise it print b
is max.
Q.24 Explain Bitwise operators with example?
➢ The bitwise operators are used to manipulate data at bit level.
➢ These operators are used for testing bits or shifting them left or right.
➢ They can not be applied to float and double.
➢ The bitwise operators supported by c language is given below:
1) Bitwise AND(&):
2) Bitwise OR(|):
3) Bitwise Exclusive OR(^):
4) Shift left(<<):
5) Shift right(>>):
6) One ‘s complement(~):

Q.25 Explain special operators with example?


➢ The special operator supported by c language is given below:
1) Comma operator(,):
The comma operator can be used to link related expression together.

➢ The expressions are evaluated left to right.


➢ The value of the right most expression is the value of the combined
expression.
➢ Example :

18
Sem.: 1st C.E.
Subject: C.P.

Z= (x=10, y=5, x+y);

➢ First the value of 10 is assigned to x, then value of 5 is assigned to y. finally


the value of x+y (10+5=15) is assigned to z.
2) Sizeof operator:
➢ The sizeof operator is used to find how many bytes the operand occupies.
➢ The operand may be a variable, a constant or data type.
➢ The general syntax of sizefof operator is:
Sizeof (operand)

Examples:

Sizeof (a); // here it return 2 bytes if a is of type int.

Sizeof (2.5) // here it return 4 bytes because 2.5 is float.

Sizeof (double) // here it return 8 bytes.

➢ The sizeof operator is a compile time operator.


➢ The common use of sizeof operator are as given below:
i) To determine the length of array and structure when their sizeof
are not known to the programmer.
ii) To allocate memory space dynamically to variables during
execution of the program.
3) Pointer operator(&,*):
➢ Pointer operator are used to declared the pointer variable and access the value
stored at particular memory location.

4) Member selection operator (. , ->):


The member selection operators are used to access the member of structure.

Q.26 Explain If..Else statement with flow chart and example.


The general syntax of if..else statement is as follow:

Syntax:

If(condition)

True Block statement(s);

19
Sem.: 1st C.E.
Subject: C.P.

else

False block statement(s);

The if..else statement is executed in the following order:

1. first the condition is checked.

2. if condition is true the true statement is executes.

3. if condition is false the false statement is executed.

20
Sem.: 1st C.E.
Subject: C.P.

#include <stdio.h>

int main(){

int num;

printf("Enter a number you want to check.\n");

scanf("%d",&num);

if((num%2)==0) //checking whether remainder is 0 or not.

printf("%d is even.",num);

else

printf("%d is odd.",num);

return 0;

Enter a number you want to check.


25
25 is odd.

Q.27 Explain Nesting if-else statement with flowchart and Example.


• When the more then one condition is to be checked then we can use nesting of if..else first
the condition is checked.

• The syntax of nesting if..else statement is as follow:

Syntax:

If(condition 1)

If(condition 2)

Statement-1;

21
Sem.: 1st C.E.
Subject: C.P.

Else

Statement-2;

Else

Statement-3;

• The nesting if-else statement is executed in the following manner.

1. First the condition 1 is checked.

2. if the condition 1 is true then condition 2 is checked. If condition 2 is true then statement-1
is executed.

3. but if the condition 2 is false the statement-2 is executed.

4. if condition 1 is false the statement-3 is executed. Statement-3;

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

Q. 28 Explain if-Else-if ladder(Multiple if..else) with flow chart and example.


• The if..else ladder statement provide two-way decision where we select one of the
alternative.

• It is used for multiple choice.

23
Sem.: 1st C.E.
Subject: C.P.

• The two way decision is done by nested if..else is not sufficient.

• Following is the syntax of the if..else ladder.

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;

• if-else-if ladder is executed in the following order:

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.

Q. 29 Explain Switch ..case statement with flowchart and example.


• The switch statement is also known as multi-choice or multi decision

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.

• It provide the choice for each value of variable or expression.

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

• General syntax of switch case statement is as follow:

Switch(variable name or expression)

Case 1:

statement(s)1;

Break;

Case 2:

statement(s)2;

Break;

Case N:

statement(s)N;

Break;

Default :

default_statement(s);

• The expression or variable name is an integer or characters.

• 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 (;).

• The switch..case statement is executed in the following order:

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.

Q.30 Explain goto statement with example.


Use: the goto statement in the c programming is used to transfer the control unconditionally
from one part of the program to other part of the program.

‘c’ language provide a unconditional branching mechanism called as goto statement.

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.

The syntax of the goto statement is as follow:

Syntax:

Goto lable;

Here , lable is the lable to the statement to which goto transfer control.

Following shows the two possible use of goto statement.

1. forward reference

2. backward reference

Q.31 Difference between While and Do..while loop.

1) difference in flowchart:

27
Sem.: 1st C.E.
Subject: C.P.

Flowchart of While Loop Flowchart of Do...While Loop

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.

5) While loop is entry control loop


Where as do while is exit control loop.

6) Syntax:
while loop : Do...While loop:
while (condition) do
{ {
Statements; Statements;
} }while(condition);

Q. 32 Difference between Break & Continue statement in C.

28
Sem.: 1st C.E.
Subject: C.P.

Q.33 Explain For Loop with flowchart & example.


for-loops are counter-controlled, meaning that they are normally used whenever the number of
iterations is known in advance.

• The initialization step occurs one time only, before the loop begins.

• The condition is tested at the beginning of each iteration of the loop.

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.

Flowchart of For Loop:

Syntex or Example of For Loop:

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.

✓ The syntax for declaring one dimension array is:


datatype array_name[size];
✓ Here datatype defines the type of array elements. It can be char, int, float,
long int etc. the array_name is the name of a variable represented the array,
while size which represented in [] symbols represented size of array.
For Example:
✓ If we have a one array name salary with integer values of 10 elements. Than
those array declared as follows:
int salary[10];
Datatype int
Name of Array salary Size
of Array 10
Number of Elements in array 10
✓ The elements of array must be started with 0. In above example the first array
element is salary [0] and last element is salary [9]. So total 10 elements in
this array.

✓ The graphically represented of array is:

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

✓ Another way to initialization of one dimension array is give below:

31
Sem.: 1st C.E.
Subject: C.P.

Int number [3] = {12, 4, 78};


Float total [2] = {2.5, 6, 6.23};
Char name [] = {‘p’,’n’,’c’};

32

You might also like