0% found this document useful (0 votes)
1K views14 pages

Q.1 Fill in The Blanks:: (Ch#13) Functions Computer Science Part-II

This document contains questions and answers about functions in C programming. It covers key topics like function prototypes, parameters, return types, scope of variables, and built-in vs user-defined functions. Multiple choice and true/false questions are provided to test understanding of functions, with answers filled in for some questions.

Uploaded by

hammad haris
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)
1K views14 pages

Q.1 Fill in The Blanks:: (Ch#13) Functions Computer Science Part-II

This document contains questions and answers about functions in C programming. It covers key topics like function prototypes, parameters, return types, scope of variables, and built-in vs user-defined functions. Multiple choice and true/false questions are provided to test understanding of functions, with answers filled in for some questions.

Uploaded by

hammad haris
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/ 14

[Ch#13] Functions 380 Computer Science Part-II

Q.1 Fill in the blanks:


1. A function is a self contained piece of code.
2. Pre-defined functions are packaged in libraries.
3. A prototype provides basic information about the function to the compiler.
4. The duration for which a variable exists in memory is called its lifetime.
5. Scope of a variable refers to the region of the program where it can be referenced.
6. Global variables are declared outside all blocks.
7. A function can not return more than one value (s) through return statement.
8. The parameters specified in the function header are called formal parameters.
9. The parameters passed to a function in the function call are called actual
parameters.
10. Functions help to achieve modular programming.
Q.2 Choose the correct option:
1. Functions prototype for built-in functions are specified in:
a) source files b) header files
c) object files d) image files
2. Global variables are created in
a) RAM b) ROM
c) Hard Disk d) cache
3. Which of the following is true about a function call?
a) Stops the execution of the program.
b) Transfers control to the called function.
c) Transfers control to the main function.
d) Resumes the execution of the program.
4. The predefined functions that are part of C-language are called:
(a) user-defined (b) subprograms
(c) subroutines (d) built-in functions
5. The functions that are defined by programmer are called:
(a) user-defined (b) subprograms
[Ch#13] Functions 381 Computer Science Part-II
(c) subroutines (d) built-in functions
6. Another name for built-in function is:
(a) user-defined function (b) library function
(c) ready-made function (d) None
7. A function is called with the reference of its:
(a) name (b) parameter
(c) definition (d) None
8. A function:
(a) may return more than one values.
(b) may return only one value.
(c) cannot return any value. (d) None of these
9. The actual values are passed to the function in:
(a) function declaration (b) function call
(c) function definition (d) All
10. Which of the following looks for the prototype of functions used in a program:
a) linker b) loader
c) compiler d) parser
11. The name of actual and formal parameters:
a) May or may not be same b) Must be same
c) Must be different d) Must be in lowercase
12. Formal arguments are also called
a) Actual arguments b) Dummy arguments
c) Original arguments d) Referenced arguments
13. The printf() is a:
a) Built-in function b) User defined function
c) Local function d) keyword
14. The actual body of the function is defined in:
(a) function declaration (b) function call
(c) function definition (d) None
15. The last statement of the body of the function is:
(a) break; (b) continue;
(c) return (d) shift
16. A built-in function:
a) Can not be redefined b) Can be redefined
[Ch#13] Functions 382 Computer Science Part-II
c) Can not return a value d) Should be redefined
17. In a C program two functions can have:
a) Same name b) Same parameters
c) Same name and same parameters d) Same name but different parameters
18. The name of actual and formal parameters:
(a) may or may not be same (b) must be same
(c) must be different (d) must be in lowercase
19. In C-language, subprograms are referred to as:
(a) Methods (b) Procedure
(c) functions (d) modules
20. The predefined functions that are part of the programming language and can be
used for different purposes are called:
(a) Library functions (b) Built-in functions
(c) user-defined functions (d) Both a & b
21. The parameters specified in the function header are called:
(a) formal parameters (b) actual parameters
(c) default parameters (d) command line parameters
22. __________ perform tasks that may need to be repeated many times.
a) Condition b) Module
c) Program d) Function
23. If the whole logic of program is contained in main function, it is called
____________.
a) Structured Programming b) Un structured Programming
c) Object Oriented Programming d) Modular Programming
24. Built in functions make our task ____________.
a) Complex b) Lengthy
c) Simple d) Technical
25. The parameters passed to a function in the function call are called:
(a) formal parameters (b) actual parameters
(c) default parameters (d) command line parameters
26. Which of the following is the advantage of function?
(a) Easy to write program (b) Eliminate duplicate code
(c) Re-usability (d) All of these
27. Function definition consists of:
(a) function header (b) Function body
[Ch#13] Functions 383 Computer Science Part-II
(c) Both a & b (d) None
28. The first line of function definition is called _________.
a) Function Face b) Function Definition
c) Function Header d) Function Name
29. The region of a program in which a variable is accessible, refers to its
a) Area b) Scope
c) Function d) Use
30. As soon as the control moves outside of their scope, local variable are
a) Need to declare again b) Accessible to only a limited set of instructions
c) Accessible d) Destroyed
31. Function declaration consists of:
(a) function name
(b) Function return type
(c) number and types of parameters
(d) All
32. What is true about a function prototype?
(a) It is also referred to as function declaration
(b) It is terminated with a semicolon (;)
(c) It is a single statement.
(d) All of these
33. A function that does not return anything has return type:
(a) nothing (b) float
(c) void (d) null
34. The variables that are declared outside all blocks is called
a) General Variables b) Variables
c) Global variables d) Global Data Items
35. What is the return type of the function with prototype: “int func(char x, float v,
double t );”
a) char b) double
c) float d) int
36. The function declaration statement int abc (void); indicates that:
(a) Return data type of function is void.
(b) return data type of function is int.
(c) function take no argument.
(d) function takes one argument of int type.
[Ch#13] Functions 384 Computer Science Part-II
(e) Both (b) and (c)
37. The variables declared inside any function are known as:
(a) global variable (b) private variable
(c) external variable (d) local variable
38. Which of the following is a valid function call (assuming the function exists)?
a) funct; b) funct x,y;
c) funct(); d) int funct();
39. Which of the following can return a value?
a) Function b) Procedure
c) Both a and b d) None of Above
40. Data can be shared between functions using:
(a) local variable (b) static variable
(c) global variable (d) register variable
41. Local variables are also called:
(a) Automatic (b) Normal
(c) Global (d) None
Q.3 Write T for True and F for false Statements.

1. In C arguments can be passed to a function by value (T)

2. There can be multiple main functions in a C program (F)

3. A function can be called anywhere in the program (T)

4. In C, every function must return a value (F)

5. A user defined function can not be called in another user defined (F)
function.

6. A function can be called only once in a program (F)

7. Scope of a local variable is the block in which it is defined. (T)

8. Global variables exist in memory till the execution of the program. (T)

9. An unstructured program is more difficult to debug than a structured (T)


program.
[Ch#13] Functions 385 Computer Science Part-II

10. Function body is an optional part of the function. (F)


Q4. Write a program that call two functions draw_horizontal and draw_vertical
to construct a rectangle. Also write functions draw_horizontal to draw
parallel horizontal lines and draw_vertical to draw parallel vertical lines.
Answer:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void draw_horizontal (int x, int y, int w, int h);
void draw_vertical (int x, int y, int w, int h);
void main( )
{
int x, y, w, h;
int gdriver=DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode,”c:\\tc\\bgi”);
printf(“\n Enter Width “);
scanf(“%d”, &w);
printf(“\n Enter Height “);
scanf(“%d”, &h);
printf(“\n Enter X Value “);
scanf(“%d”, &x);
printf(“\n Enter Y Value “);
scanf(“%d”, &y);
draw_horizontal (x, y, w, h);
draw_vertical (x, y, w, h);
}
void draw_horizontal (int x, int y, int w, int h)
{
line (x, y, x + w, y);
line (x, y + h, x + w, y + h);
}
void draw_vertical (int x, int y, int w, int h)
{
line (x, y, x , y + h);
[Ch#13] Functions 386 Computer Science Part-II
line (x + w, y, x + w, y + h);
}
Q5. Write a program that gets two points (x1, y1) and (x2, y2). Displays the
distance between them using distance formula in a function.
Answer:
#include<stdio.h>
#include<math.h>
float distance(float, float, float, float);
void main( )
{
float x1, x2, y1, y2, d;
printf(“\n Enter Co-ordinates of First Point x1 and y1”);
scanf(“%f %f”, &x1, &y1);
printf(“\n Enter Co-ordinates of Second Point x2 and y2”);
scanf(“%f %f”, &x2, &y2);
d = distance(x1, y1, x2, y2);
printf(“\n Distance Between Two Points = %.2f”, d);
}
float distance(float x1, float y1, float x2, float y2)
{
float dis, x, y;
x = x2 - x1;
y = y2 - y1
dis = sqrt(x*x + y*y);
return dis;
}
Q6. Write a program hat prompts the user to enter a number and then reverse it
through function.
Answer:
#include<stdio.h>
int reverse(int);
void main( )
[Ch#13] Functions 387 Computer Science Part-II
{
int n, rvrs;
printf(“\n Enter 4 digit Number”);
scanf(“%d” , &n);
rvrs = reverse(n);
printf(“\n Reverse Number = %d”, rvrs);
}
int reverse(int n) //1234
{
int a, b, c, d, e, f, rvrs;
a = n/1000; //a=1
b = n%1000; //b=234
c = b/100; //c=2
d = b%100; //d=34
e = d/10; //e=3
f = d%10; //f=4
rvrs = f*1000 + e*100 + c*10 + a;
return rvrs;
}
Q7. Produce the following output using draw_astrisk function
*******
******
*****
****
***
**
*
Answer:
#include<stdio.h>
void draw_astrisk(void);
void main()
{
draw_astrisk();
[Ch#13] Functions 388 Computer Science Part-II
}
void draw_astrisk(void)
{
int r, c;
for (c=7;c>=1; c--)
{
for (r=1;r<=c;r++)
{
printf(“*”);
}
printf(“\n”);
}
}
Q8. Write a function isPrime that input a number and return 1 if number is
prime otherwise return 0.
Answer:
#include<stdio.h>
int isPrime(int);
void main()
{
int n, res;
printf(“\n Enter any Number : “);
scanf(“%d”, &n);
res = isPrime(n);
if(res==1)
printf(“\n Number is prime and returned value is 1”);
else
printf(“\n Number is Not prime and returned value is 0”);
}
int isPrime(int n)
{
int k =2;
while (k<=n-1)
{
if(n%k==0)
return 0;
else
[Ch#13] Functions 389 Computer Science Part-II
k++;
}
return 1;
}
Q9. Perform arithmetic operations using functions
Answer:
#include<stdio.h>
float add(float,float);
float sub(float,float);
float mul(float,float);
float div(float,float);
void main ( )
{
float a,b, res;
int op;
printf(“\n Enter two Numbers : “);
scanf(“%f %f”, &a,&b);
printf(“\n Enter 1 for Addition : “);
printf(“\n Enter 2 for Subtraction : “);
printf(“\n Enter 3 for Multiplication : “);
printf(“\n Enter 4 for Division : “);
printf(“\n Now please press 1 2 3 or 4 : “);
scanf(“%d”, &op);
switch(op)
{
case 1:
res=add(a,b);
break;
case 2:
res=sub(a,b);
break;
case 3:
res=mul(a,b);
break;
[Ch#13] Functions 390 Computer Science Part-II
case 4:
res=div(a,b);
break;
default:
printf(“ Choice is invalid “);
}
printf(“\n Result = %.2f”,res);
}
float add(float x, float y)
{
return x+y;
}
float sub(float x, float y)
{
return x-y;
}
float mul(float x,float y)
{
return x*y;
}
float div(float x, float y)
{
if (x==0 || y ==0 )
{
printf(“\n Do Not Use Zero in Division”);
return 0;
}
if(x>y)
return x/y;
else
return y/x;
}
Q10. Program of Factorial using function:
Answer:
#include<stdio.h>
int fact(int);
void main()
[Ch#13] Functions 391 Computer Science Part-II
{
int n, res;
printf(“\n Enter any Number range [1-7] : “);
scanf(“%d”, &n);
res = fact(n);
printf(“\n Factorial of %d is %d”, n, res);
}
int fact(int n)
{
int a=1,i;
for(i=1; i<=n; i++)
a *= i;
return a;
}
Q11. Calculate GCD of two Numbers through function:
Answer:
#include<stdio.h>
int gcd(int, int);
void main()
{
int a,b, res;
printf(“\n Enter Two Positive Numbers: “);
scanf(“%d %d”, &a, &b);
res = gcd(a,b);
printf(“\n GCD = %d”, res);
}
int gcd(int a, int b)
{
int gcd, r, tmp;
c: r=a%b;
if(r==0)
gcd=b;
else
{
a=b;
[Ch#13] Functions 392 Computer Science Part-II
b=r;
goto c;
}
return gcd;
}
[Ch#13] Functions 393 Computer Science Part-II

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy