Model Answer Paper Summer 2018

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

MODEL ANSWER
SUMMER – 2018 EXAMINATION
Subject: Programming in „C‟ Subject Code: 17212

Important Instructions to examiners:


1) The answers should be examined by key words and not as word-to-word as given in the model
answer scheme.
2) The model answer and the answer written by candidate may vary but the examiner may try to
assess the understanding level of the candidate.
3) The language errors such as grammatical, spelling errors should not be given more Importance
(Not applicable for subject English and Communication Skills).
4) While assessing figures, examiner may give credit for principal components indicated in the
figure. The figures drawn by candidate and model answer may vary. The examiner may give
credit for any equivalent figure drawn.
5) Credits may be given step wise for numerical problems. In some cases, the assumed constant
values may vary and there may be some difference in the candidate‟s answers and model
answer.
6) In case of some questions credit may be given by judgement on part of examiner of relevant
answer based on candidate‟s understanding.
7) For programming language papers, credit may be given to any other program based on
equivalent concept.

Q. Sub Answer Marking


No Q.N. Scheme
.
1. Attempt any TEN of the following: 20
(a) Define following terms: 2M
(i) Variable (ii) Constant
Ans. (i) Variable: Variable is a symbolic name given to a memory
location which holds some value inside it. Each
(ii) Constant: definitio
A constant is a value inside an identifier which cannot be changed in n 1M
the program.
(b) Define the term loop. 2M
Ans. Loop: In any programming language, a loop can be defined as a Correct
block in which certain number of statements can be sequentially definitio
executed in repetition until a condition becomes false. n 2M

(c) State the use of strlen( ). Also give its syntax. 2M


Ans. Strlen( ):
strlen() is a string function which is used to find length of the string. Use of
strlen ( )
1M

Page 1 / 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

MODEL ANSWER
SUMMER – 2018 EXAMINATION
Subject: Programming in „C‟ Subject Code: 17212

Syntax : Correct
strlen(string) Syntax
1M
(d) Write any two advantages of using function. 2M
Ans. Advantages of using function:
1) It can reduce length of the program. Any two
2) Easy to locate errors and debug. advanta
3) It implements top down approach. ges 1M
4) It provides reusability i.e. once a function is written, it can be each
used at many places, even in other programs.
(e) List two operators used with pointer. 2M
(Note: Any other relevant operators shall be considered) Any two
Ans. Operators used with pointers: operator
& - address of the variable s 1M
* - value at the address each
(f) State any four relational operators. 2M
Ans. Relational operators:
< Less than Any
> Greater than four
<= Less than equal to operator
>= Greater than equal to s-½M
== Equal to each
!= Not equal to
(g) Write syntax for scanf( ) function. Give one example. 2M
Ans. Syntax of scanf():
scanf("format specifier1 format specifier2,..format specifier n", & Syntax
variable1, &variable2… &variable n); 1M
Example:
Assuming a as an integer variable and b as a float variable, scanf() Example
can be used to input values into a and b as : 1M
scanf("%d %f",&a,&b);
(h) State the use of break statement. 2M
Ans. Use of break statement:
break statement is used to early exit from the loop/block. After Correct
exiting it transfers the control out of the loop/ block. use 2M
(i) Define the term character array. 2M
Ans. Character array is the one which can be used to store sequence of Definitio
characters inside it and can share a common name. n 2M

Page 2 / 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

MODEL ANSWER
SUMMER – 2018 EXAMINATION
Subject: Programming in „C‟ Subject Code: 17212

(j) Write syntax to define function in „C‟ program. 2M


Ans. Syntax:
function_return_type <function name>([parameter list])
{
local variable declaration; Correct
executable statements; Syntax
. 2M
.
return[(variable)];
}
(k) Give output for following program: 2M
#include<stdio.h>
void main( )
{
char ch = „e‟;
switch (ch)
{
case „a‟:
printf(“I am in case a”);
case „b‟:
printf(“I am in case b”);
default:
printf(“I am in default case”);
}
Ans. Output:
I am in default case 2M
OR
Error
Code needs one curly brace bracket ( } ) at the end otherwise there
will be an error showing missing curly brace bracket ( } ).
(l) Write syntax of for loop. 2M
Ans. Syntax of for loop:
for(initialization; condition; increment/decrement )
{
executable statements; Correct
. syntax
. 2M
}

Page 3 / 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

MODEL ANSWER
SUMMER – 2018 EXAMINATION
Subject: Programming in „C‟ Subject Code: 17212

2. Attempt any FOUR of the following: 16


(a) Describe use of any two bit wise operators with suitable example. 4M
Ans. Bitwise operators:
| – Bitwise OR
& – Bitwise AND
~ – One‟s complement Descript
^ – Bitwise XOR ion of
any two
<< – left shift bit wise
>> – right shift operator
s
Description:
Bitwise OR – | 2M each
It takes 2 bit patterns, and performs OR operations on each pair of
corresponding bits. The following example will explain it.
1010
1100
--------
OR 1110
Bitwise AND – &
It takes 2 bit patterns, and perform AND operations with it.
1010
1100
-------
AND 1000
-------
The Bitwise AND will take pair of bits from each position, and if
only both the bit is 1, the result on that position will be 1. Bitwise
AND is used to Turn-Off bits.
Bitwise NOT: One‟s complement operator (Bitwise NOT) is used to
convert each “1-bit to 0-bit” and “0-bit to 1-bit”, in the given binary
pattern. It is a unary operator i.e. it takes only one operand.

1001
---- NOT
0110
-------

Page 4 / 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

MODEL ANSWER
SUMMER – 2018 EXAMINATION
Subject: Programming in „C‟ Subject Code: 17212

Bitwise XOR ^
Bitwise XOR ^, takes 2 bit patterns and perform XOR operation with
it.
0101
0110
------
XOR 0011
------
Left shift Operator – <<
The left shift operator will shift the bits towards left for the given
number of times.
int a=2<<1;
Right shift Operator – >>
The right shift operator will shift the bits towards right for the given
number of times.
int a=8>>1;
(b) Write a „C‟ program to calculate and display multiplication of 1 4M
to 7 numbers using for loop.
(E.g.: 1 * 2 * 3…. * 7 = 5040)
(Note: Any other relevant logic shall be considered)
Ans. #include<stdio.h> Correct
#include<conio.h> logic 2M
main()
{
int i,p=1; Correct
clrscr(); syntax
for(i=1;i<=7;i++) 2M
{
p=p*i;
}
printf("Multiplication of numbers from 1 to 7 is = %d",p);
}
(c) Describe following functions with its syntax and example: 4M
(i) strcmp( ) (ii) strcat( )
Ans. (i) strcmp( ): Descript
It is a string function, which is used to compare the contents of two ion with
strings. Syntax
It returns 0 if both string are equal. Otherwise it returns the numerical of
difference between the ascii values of the first non matching pair of strcmp

Page 5 / 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

MODEL ANSWER
SUMMER – 2018 EXAMINATION
Subject: Programming in „C‟ Subject Code: 17212

characters. ( ) 1M
Syntax:
strcmp(string1,string2);

Eg : if s1="there" and s2="their" Example


the output of strcmp(s1,s2) will be 9 as the difference between ascii 1M
values of 'r' and 'i' is 9.

(ii) strcat( ): Descript


It is string function which is to use concatenate second string at the ion with
end of the first string. Syntax
Syntax: of Strcat
strcat(string1,string2); ( ) 1M

Eg: if s1="Msbte" and s2="Exam" Example


strcat(s1,s2) will store value in s1 as "MsbteExam" 1M
(d) Write a program to accept a number and display its cube using 4M
function.
(Note: Any other relevant logic shall be considered)
Ans. #include<stdio.h>
#include<conio.h>
void cube(int n) Correct
{ logic 2M
printf("%d",n*n*n);
}
void main() Correct
{ Syntax
int no; 2M
clrscr();
printf("\n Enter number:");
scanf("%d",&no);
cube(no);
getch();
}
(e) Write a „C‟ program to print length of accepted string using 4M
pointer.
(Note: Any other relevant logic shall be considered)
Ans. #include<stdio.h>
#include<conio.h>

Page 6 / 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

MODEL ANSWER
SUMMER – 2018 EXAMINATION
Subject: Programming in „C‟ Subject Code: 17212

void main()
{
char str[10],*ptr;
int l=0; Correct
clrscr(); logic 2M
printf("\n Enter string:");
scanf("%s",&str);
ptr=str; Correct
while(*ptr!='\0') Syntax
{ 2M
l=l+1;
ptr=ptr+1;
}
printf("\n Length of string=%d",l);
getch();
}
(f) Describe the use of continue statement with example. 4M
Ans. Continue statement:
Continue statement is used to continue the loop with the next iteration Use of
after skipping any statement in between. The continue statement tells Continu
the compiler that “skip the following statements and continue with e
the next iteration”. Stateme
Syntax: nt 2M
continue;
Example:
for (int j=0; j<=8; j++)
{ Example
if (j==4) 2M
continue;
printf("%d ", j);
}
In the above example, Value 4 is not displayed because when j=4
continue statement skips printf() statement and continues with next
iteration of for.
Output: 0 1 2 3 5 6 7 8
3. Attempt any FOUR of the following: 16
(a) Describe with suitable example the concept of formatted output. 4M
(Note: Example can be a program or a code snippet)
Ans. Formatted output:

Page 7 / 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

MODEL ANSWER
SUMMER – 2018 EXAMINATION
Subject: Programming in „C‟ Subject Code: 17212

printf( ) is used for formatted output to standard output depending on


the format specification. Format specifiers, along with the data to be
output are the parameters to the function. The different format
specifiers used are: Descript
%d-int values ion 3M
%f-float values
%c-char values
%s-string
General syntax:
printf(“control string/format specifier”,data1,data2,…data n);
control string indicates how many arguments follow and their data
types.
data1,data2 are the variables whose data are formatted and printed
according to the specifications of the control string.

Example:
printf(“%d %d”,no1,no2); Example
In the above example %d specify format and no1, no2 are the 1M
variables whose value will be display on the screen.
(b) Write a „C‟ program to find gross salary of employee. Accept 4M
basic salary from user. If basic salary is less than 2000 then
calculate HRA = 11% and DA = 80% of basic salary. If basic
salary is equal or greater than 2000 then HRA = 600 and
calculate DA = 95% of basic salary. Display gross salary. (gross
salary = basic salary + HRA + DA)
(Note: Any other relevant logic shall be considered)
Ans. #include<stdio.h>
#include<conio.h>
void main() Correct
{ syntax
float g_sal=0; 2M
float b_sal;
float hra;
float da;
clrscr() ; Correct
printf("Enter the basic salary"); logic 2M
scanf("%f",&b_sal);
if(b_sal < 2000)
{

Page 8 / 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

MODEL ANSWER
SUMMER – 2018 EXAMINATION
Subject: Programming in „C‟ Subject Code: 17212

hra=b_sal*0.11;
da=b_sal*0.80;
g_sal=b_sal+hra+da;
}
else if(b_sal >=2000)
{
hra=600;
da=b_sal*0.95;
g_sal=b_sal+hra+da;
}
else
{
printf("Not valid");
}
printf("Gross salary is %f",g_sal);
getch();
}
(c) Describe „No argument with return value‟ category of function 4M
with example.
Ans. No argument with return value‟ category function does not accept any
argument but returns a value as a result of function execution. This Descript
function is declared and defined with data type of return value and ion 2M
function name in a prototype.
Syntax:
Return type function_name( )
{
Body of the function;
}

Example:
#include<stdio.h>
#include<conio.h>
int printNum(); Example
void main() 2M
{
int i = printNum();
printf("%d",i);
getch();
}

Page 9 / 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

MODEL ANSWER
SUMMER – 2018 EXAMINATION
Subject: Programming in „C‟ Subject Code: 17212

int printNum()
{
int i = 10;
clrscr();
return i;
}
(d) Write a „C‟ program to find factorial of a number using 4M
recursion.
(Note: Any other relevant logic shall be considered)
Ans. #include<stdio.h>
#include<conio.h>
int factorial(int num)
{
if( num==1)
{ Correct
return 1; syntax
} 2M
else
{
return(num*factorial(num-1)); Correct
} logic 2M
}
void main()
{
int num;
int result;
clrscr();
printf("Enter a number");
scanf("%d",&num);
result=factorial(num);
printf("Factorial of %d is %d",num,result);
getch();
}
(e) With suitable example, describe any two operations on pointer. 4M
(Note: Code snippet shall be considered).
Ans. The pointer arithmetic is done as per the data type of the pointer. The
basic operations on pointers are
Increment:
It is used to increment the pointer. Each time a pointer is

Page 10 / 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

MODEL ANSWER
SUMMER – 2018 EXAMINATION
Subject: Programming in „C‟ Subject Code: 17212

incremented, it points to the next location.


Example: For an int pointer variable, if the current position of pointer
is 1000, when it is incremented, it points to 1002 because for storing Any 2
an int value it takes 2 bytes of memory. operator
int *ptr; s
ptr++; descripti
on 1M
Decrement: each
It is used to decrement the pointer. Each time a pointer is and
decremented, it points to the previous location. example
Example: if the current position of pointer is 1002, then decrement 1M each
operation results in the pointer pointing to the location 1000.
int *ptr;
ptr--;

Addition and subtraction


C Allows to add integers to or subtract integers from pointers as well
as to subtract one pointer from another.
Example:
int*p1,*p2;
p1+4;
p2-2;
p1-p2;

Comparison operators
Pointers may be compared by using relational operators, such as ==,
<, and >. If p1 and p2 point to variables that are related to each other,
such as elements of the same array, then p1 and p2 can be compared
using the comparison operators.
(f) Write a „C‟ program to accept a string from user and copy it into 4M
another string. Display both the strings.
(Note: Any other relevant logic shall be considered)
Ans. #include<stdio.h>
#include<conio.h>
void main()
{
int i; Correct
char str[20]; syntax
char dest[20]; 2M

Page 11 / 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

MODEL ANSWER
SUMMER – 2018 EXAMINATION
Subject: Programming in „C‟ Subject Code: 17212

clrscr();
printf("Enter a string");
scanf("%s",str); Correct
for(i=0;str[i]!='\0';i++) logic 2M
{
dest[i]=str[i];
}
dest[i]='\0';
printf("The source string is %s",str);
printf("\nThe copied string is %s",dest);
getch();
}
OR
#include<stdio.h>
#include<conio.h>
void main()
{
char source[20];
char dest[20];
clrscr();
printf("enter the string");
scanf("%s",source);
strcpy(dest,source);
printf("Source string is %s",source);
printf("destination string is %s",dest);
getch();
}
4. Attempt any FOUR of the following: 16
(a) Describe conditional operator with syntax and example. 4M
Ans. Conditional operator:
Conditional operators return one value if condition is true and returns Descript
another value if condition is false. ion with
This operator is also called as ternary operator as it takes three syntax
arguments. 2M
Syntax: (Condition? true_value: false_value);
Example:
#include<stdio.h> Example
#include<conio.h> 2M
void main()

Page 12 / 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

MODEL ANSWER
SUMMER – 2018 EXAMINATION
Subject: Programming in „C‟ Subject Code: 17212

{
int a,b,max;
clrscr();
printf("Enter the value of a & b:");
scanf("%d%d",&a,&b);
max=(a>b)?a:b;
printf(“%d is large”,max);
getch();
}
(b) Describe with example in which case do-while loop is most 4M
suitable than while loop.
Ans. Do-while is most suited when some statements of a program to be
executed at least once even if the condition is false. When while loop Descript
is used, if the condition is false then the statements will not be ion 1M
executed even once.
In menu driven programs do while loop is most suitable than while
loop as do loop displays menu options at least once without checking
any condition which is not possible with while loop.
Example:-
do Any
{ suitable
printf("Menu"); example
printf("\n1. Add \n2.Subtract"); with
printf("\n Enter your choice:"); descripti
scanf("%d",&ch); on 3M
switch(ch)
{
case 1:
...
...
}
printf("\n do you want to continue:(1/2)");
scanf("%d",&ch);
}while(ch==1);
In the above example, menu will be displayed without checking any
condition. Depending upon user's choice a case from switch will
execute. If user wish to continue then while loop takes the control
back to do statement.

Page 13 / 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

MODEL ANSWER
SUMMER – 2018 EXAMINATION
Subject: Programming in „C‟ Subject Code: 17212

Displaying menu with while condition must have a condition to enter


inside loop.
(c) Write a „C‟ program to calculate and display sum of five elements 4M
from array.
(Note: Any other relevant logic shall be considered)
Ans. #include<stdio.h>
#include<conio.h>
void main() Correct
{ logic 2M
int arr[5];
int i;
int sum=0; Correct
clrscr(); syntax
for(i=0;i<5;i++) 2M
{
printf("Enter the nos of the array");
scanf("%d",&arr[i]);
}
for(i=0;i<5;i++)
{
sum=sum+arr[i];
}
printf("Sum of the elements :%d",sum);
getch();
}
(d) Write a „C‟ program to define a structure „Bank‟ with members 4M
as branchno and bankname. Accept and display data for one
bank.
(Note: Any other relevant logic shall be considered)
Ans. #include<stdio.h>
#include<conio.h> Correct
void main() syntax
{ 2M
struct bank
{
int branchno;
char branchname[20];
}b; Correct
clrscr(); logic 2M

Page 14 / 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

MODEL ANSWER
SUMMER – 2018 EXAMINATION
Subject: Programming in „C‟ Subject Code: 17212

printf("enter the branchno, branchname");


scanf("%d%s",&b.branchno,&b.branchname);
printf("The details of bank are\nNo: %d\nName:%s", b.branchno,
b.branchname);
getch();
}
(e) Differentiate between call by value and call by reference methods. 4M
(Any four points)
(Note: Any other relevant points shall be considered)
Ans.
Call by value Call by reference
In call by value, a copy of actual In call by reference, the
arguments is passed to location, that is, the address of
respective formal arguments. actual arguments is passed to
formal arguments Any
Actual arguments will remain Alteration to actual arguments four
safe, they cannot be modified in is possible within called points
the called function. function. 1M each
Address of the actual and formal Address of the actual and formal
arguments are different arguments are the same
Changes made inside the Changes made in the function is
function is not reflected in other reflected outside also.
functions
(f) Define the terms pointer and pointer expression. Also write two 4M
advantages of using pointer.
Ans. Pointer: Pointer is a variable that stores the address of another Definitio
variable which is of similar data type. n of
Eg: int i=3; pointer
int *ptr = &i; here the address of i is stored in the pointer variable 1M
ptr.

Pointer expression: Definitio


When a pointer variable is used in side an expression then it is called n of
a pointer expression. pointer
Eg: int *ptr; expressi
int i = 3; on 1M
ptr=&i;
ptr=ptr+3;
.

Page 15 / 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

MODEL ANSWER
SUMMER – 2018 EXAMINATION
Subject: Programming in „C‟ Subject Code: 17212

Advantages of using pointer:


(i) It allows passing of arrays and strings to functions more
efficiently.
(ii) It makes possible to pass address of structure instead of entire Any 2
structure to the functions. advanta
(iii) It makes possible to return more than one value from the ges 1M
function. each
(iv) It supports dynamic memory management.
5. Attempt any FOUR of the following: 16
(a) Write an algorithm and draw a flowchart to add two numbers. 4M
Ans. Algorithm:
step 1: Start
step 2: Input values for variables no1 and no2.
step 3: Calculate addition of two values using formula as Correct
sum = no1 + no2. algorith
step 4: Display addition m 2M
step 5: Stop

Flowchart:

Correct
flowchar
t 2M

(b) Describe importance of break statement in switch case statement. 4M


Ans. Break statement:
The break statement transfers the control out of loop/ block such as
for, while or switch case. When a break statement is encountered, it

Page 16 / 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

MODEL ANSWER
SUMMER – 2018 EXAMINATION
Subject: Programming in „C‟ Subject Code: 17212

skips the remaining part of current iteration of the loop and passes
control out of the block or loop. The break will exit only a single
loop.
Syntax: Relevant
break; descripti
Break statement is important in switch case statement to allow only on 4M
one case to execute from multiple cases. After executing statements
from a single case, control must be pass outside the switch so that
other cases cannot execute. If break statement is not given after any
case then all cases after that case will also execute.
Example:
switch(choice)
{
case 1:
printf(“welcome to case 1”);
break;
case 2:
printf(“welcome to case 2”);
case 3:
printf("welcome to case 3");
}
In the above example, if choice is 1 then first case will execute and
then break statement will transfer the control out of the block.
If choice is 2 then first case will be ignored and second case executes.
For second case break statement is not given so after executing
second case, third case will also execute.
(c) Write a 'C' program to read two matrices of 3 x 3. Calculate and 4M
display their addition.
(Note: Any other relevant logic shall be considered)
Ans. #include<stdio.h>
#include<conio.h> Correct
void main() logic 2M
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("Enter first matrix elements:\n");
for(i=0;i<3;i++) Correct
{ syntax
for(j=0;j<3;j++) 2M

Page 17 / 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

MODEL ANSWER
SUMMER – 2018 EXAMINATION
Subject: Programming in „C‟ Subject Code: 17212

{
scanf("%d",&a[i][j]);
}
}
printf("\nEnter second matrix elements:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\n\nAddition of two matrices is:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
}
getch();
}
(d) Write a 'C' program to implement a menu for following : 4M
(i) To find whether the number is even or odd.
(ii) To find whether the number is positive or negative.
(Note: Any other relevant logic shall be considered)
Ans. #include<stdio.h>
#include<conio.h>
void main()
{
int choice,no;
clrscr();

Page 18 / 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

MODEL ANSWER
SUMMER – 2018 EXAMINATION
Subject: Programming in „C‟ Subject Code: 17212

printf("\n Enter Number:");


scanf("%d",&no); Correct
printf("\n Menu \n 1. Find whether number is even or odd"); logic 2M
printf("\n 2. Find whether number is positive or negative");
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(no%2==0) Correct
printf("\nNumber is Even"); syntax
else 2M
printf("\nNumber is Odd");
break;
case 2:
if(no>0)
printf("Numer is Positive");
else
printf("Number is Negative");
}
getch();
}
(e) Describe register and static storage classes with example. 4M
Ans. Register storage class: These variables are stored in the CPU
registers instead of memory. Since the register access is much faster
compared to the memory, frequently used variables can be stored this Descript
way. It is local to the block in which the variable is defined. It exists ion 1M
till the control remains within the block in which the variable is each
defined.
Example:
void main() Example
{ 1M each
register int count=0;
count++;
printf(“%d”,count);
}

Static storage class: The value of the static variable persists until the
end of the program execution. A variable can be declared as a static

Page 19 / 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

MODEL ANSWER
SUMMER – 2018 EXAMINATION
Subject: Programming in „C‟ Subject Code: 17212

using the keyword static. The static variable is initialized to zero. It is


local to the block in which the variable is defined. Value of the
variable persists between different function calls.

Example:
void func1()
{
static int x=0;
x= x+1;
printf(“x=%d”,x);
}
(f) State the meaning of each statement : 4M
int *ptr, no;
no = 5;
ptr = &no;
printf("%d %d", no,*ptr);
Ans. int *ptr,no; -> This statement declares a pointer variable 'ptr' and a
variable 'no' with data type as integer.

no=5; -> This statement initializes value 5 to variable „no‟ i.e. value Meanin
5 is stored in variable 'no' g of
each
ptr=&no; -> This statement initializes pointer variable 'ptr' with the step 1M
address of variable 'no' i.e it stores the address of variable 'no' in
pointer variable ptr.

printf("%d%d",no,*ptr); ->This statement displays value of no as


5 and value 5 stored in the address of „no‟ within pointer variable.
6. Attempt any FOUR of the following: 16
(a) Write a 'C' program to accept radius of circle and calculate area 4M
of circle. Display calculated area.
(Note: Any other relevant logic shall be considered)
Ans. #include<stdio.h>
#include<conio.h> Correct
void main() logic 2M
{
float radius,area; Correct
clrscr(); Syntax
printf("\n Enter radius:"); 2M

Page 20 / 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

MODEL ANSWER
SUMMER – 2018 EXAMINATION
Subject: Programming in „C‟ Subject Code: 17212

scanf("%f",&radius);
area=3.14*radius*radius;
printf("\n Area =%f",area);
getch();
}
(b) Write a 'C' program to print sum of digits in the number. 4M
(e.g. number = 2134, sum = 2 + l + 3 + 4 = l0)
(Note: Any other relevant logic shall be considered)
Ans. #include<stdio.h>
#include<conio.h> Correct
main() logic 2M
{
int no,q,r,sum=0;
clrscr();
printf("enter a number:");
scanf("%d",&no);
while(no!=0)
{ Correct
r=no%10; syntax
sum=sum+r; 2M
q=no/10;
no=q;
}
printf("sum of digits=%d",sum);
getch();
}
(c) Define array. With suitable example, describe how to declare and 4M
initialize one dimensional array.
Ans. Array: Definitio
An array is a collection of data elements of similar data type. The n 2M
values in an array are stored in continuous memory locations.

Declare and initialize one dimensional array:


syntax:- datatype arr_name[size] ={values}; Declarat
ion 1M
In the above syntax data type specify type of all data element stored
inside an array. arr_name specify name of the array variable. Size Initializ
specify number of data elements that can be stored inside it. Values ation
are the data elements stored in an array. 1M

Page 21 / 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

MODEL ANSWER
SUMMER – 2018 EXAMINATION
Subject: Programming in „C‟ Subject Code: 17212

Example:-
int a[10] = {10, 20, 5, 3, 55, 45, 15, 7, 30, 52};
In the above example, an array variable a stores 10 integer values
inside it.

(d) Describe how to access and initialize structure members with 4M


example.
Ans. Accessing member:
Accessing structure members:-
Structure members are accessed with structure variable and dot
operator. Descript
Syntax:- structure_variable.structure_member ion of
Example:- access
struct student to
{ int rollno; structur
char name[10]; e
}S1; member
void main() 2M
{
printf("%d",S1.rollno);
printf("%s",S1.name);
}
In the above example, structure student has two members as rollno
and name.'S1‟ is a structure variable. To access members of structure
student,'S1' variable is used inside main().Variable 'S1' followed by
dot operator and member name is used to access members of
structure.

Initialization of structure members:-


Structure members can be initialized while creating structure variable.
All the values as per number of members are specified inside a curly Descript
bracket along with comma as a separator. ion of
Initializ
Example:- ation of
struct student S1={1,"abc"}; structur
e
In the above example, S1 is a structure variable. Value 1 is initialized member
to rollno and "abc" is initialized to name stored inside structure 2M
variable S1.

Page 22 / 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

MODEL ANSWER
SUMMER – 2018 EXAMINATION
Subject: Programming in „C‟ Subject Code: 17212

(e) Explain local and global variable with example. 4M


Ans. Local variable is a variable that is declared inside a specific function.
It is available and used only inside the function. Only the function in
which it is declared can access it. It exists until end of function or
block in which it is declared.
void function1()
{ Local
int number1; variable
} 2M
void function2()
{
int number2;
}
In above example, variable number1 is accessed only inside function1
not in function2.

Global variable is a variable that is declared outside all functions. It


is available and used throughout the entire program. It can be
accessed by any function in the program. It exists as long as the
program's execution doesn't come to an end.
Example:
int number; Global
void main() variable
{ 2M
}
void function1()
{
}
In the above example 'number' is a global variable.
(f) Describe use of if-else statement with syntax and example. 4M
Ans. Use:
If-else statement is a decision making statement and is used to control
the flow of execution of statements. It allows the computer to
evaluate the expression first and then depending on whether the value Descript
of the expression is true or false, it transfers the control to the ion 2M
particular statement block.
Syntax of if-else statement:
if (test expression) Syntax
{ 1M

Page 23 / 24
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

MODEL ANSWER
SUMMER – 2018 EXAMINATION
Subject: Programming in „C‟ Subject Code: 17212

True-block statement (s)


}
else
{
False-block statement (s)
}
Statement-x;
Example:-
int no;
no=2;
if(no>0) Example
{ 1M
printf("Number is positive");
}
else
{
printf("Number is negative");
}

Page 24 / 24

You might also like