Download as pdf
Download as pdf
You are on page 1of 13
COMPUTER SCIENCE CLASS-XII COMPOSEDBY:SAJIDURREHMAN CONTACT: salid@office.com.pk | MOBILE; +92 345 528 26 25 WEBSITE: www.office.com.pk = (aloes Q.1: Answer the following as True or False. (a) You can return as many data items as you like from a function to the calling program, using the keyword retumn(). (b) The variables commonly used in C functions are accessible to all other function in the program. (c) A global variable is defined outside of any function. (d) A global variable can be used in main() only. (e) In C langtiage, you can place the function main() any Where in your program but it will still be executed first, Answers: (a) False (b) False (c)True (d)False (e) True Q2. Write a program that prints the larger of two numbers entered from the keyboard. Use a function to do the actual comparison of the two numbers. Pass the two numbers to the function as arguments and have the function return the answer with return (). Answer: PROGRAM #include int fcomp(int x, int y); void main(void) { int numi, num2; printf("Enter the first number: "); scanf("%d", &numl); printf("Enter the second number: "); scanf("%d", &num2); printf ("The * largest number is a", fcomp (num, num2) ) ; } int fcomp(int x, int y) { Page 1 of 13 www. facebook.com/office.com.pk COMPUTERSCIENCE CLASS-XII COMPOSED BY: SAJID UR REHMAN CONTACT: sajid@office.com.pk | MOBILE: +92 345 528 26 25 ae WEBSITE: www.office.com.pk int largest; if (x>y) ‘ largest=x; else - largest=y; return(largest)7 } OUTPUT Enter the first number: 10 Enter the second number: 50 The largest number is 50 Q.3: Write a program using a function to calculate the area of a rectangle. Answer: PROGRAM #include int rarea(int width, int length); void main(void) { z int x, y? printf("Enter rectangle width: ")7 scanf("%d", &)7 printf£("Enter rectangle length: ")7 scanf("%d", &y)i printf("Area of rectangle is wa", rarea(x, y))i a int rarea(int width, int length) £ int area; area=width*length; return(area); + OUTPUT Page 2of 13 www.facebook.com/office.com.pk COMPUTER SCIENCE CLASS-XII COMPOSED BY: SAJIDURREHMAN — CONTACT: sajid@office.com.pk MOBILE: +92 345 528 26 25 WEBSITE: www.office.com.pk Enter rectangle width: 10 Enter rectangle length: 50 Area of rectangle is 500 Q.4: Write a program that produces the following table of temperature in Centigrade and Fahrenheit from 0 degrees Centigrade. Use a function for conversion. Centigrade Fahrenheit 0 32 5 10 50 Answer: PROGRAM #include int ctemp(int temp_c); void main(void) t int x; printf ("Centigrade\tFahrenheit"); for(x=0; *<=50; x=x+5) “a printf ("\n%d\t\t%d", x, ‘Scemp (x) oF } int ctemp(int ‘temp_c) € int temp_f; temp_f=(1.8*temp_c) +327 return(temp_f); OUTPUT Centigrade Fahrenheit Page3of13 ——winp. facebook. com/office.com. pk COMPUTER SCIENCE CLASS-XII COMPOSED BY: SAJIDURREHMAN — CONTACT: sajid@office.com.pk | MOBILE: +92 345 528 26 25 WEBSITE: www.office.com.pk oO 32 5 41 10 50 15 59 20 68 25 7 30 86 “ 35 95 40 104 45 113 50 122 Q.5: Write a program that reads a phrase and prints the number of lower-case letters in it using a function for counting. Answer: PROGRAM #include #include int flowercase(char fphrase[50]); void main(void) { char phrase[50]; printf("Enter phrase: "); gets (phrase); printf ("Lower+case letters=%d", flowercase(phrase)); } int flowercase(char fphrase[50]) t int x, low=0; for(x=0; x<=strlen(fphrase); x++) if ((fphrase[x])>='a' && (fphrase[x]<='z')) t es lowslow+1; : . ? + ‘xreturn(low); , Page 4 of 13 www. facebook. com/office.com.pk COMPUTERSCIENCE CLASS-XII COMPOSED BY:SAJIDURREHMAN CONTACT: sajid@office.com.pk | MOBILE: +92 345 528 26 25 WEBSITE: www.office.com.pk a OUTPUT Enter phrase: Hello World Lower-case letters = 8 Q.6: Write a program that reads n floating-point numbers in an array and prints their product using a function. Answer: PROGRAM #include void fproduct (float fnum[5]) 7 void main(void) t float num[5]; int x; for (x=0; x<=4; ate) { printf("Enter floating-point no.%d: ", x)# scanf("%£", &num[x]) + 3 fproduct (num) ; 3 — void fproduct(float fnum[5]) float fpro; . fpro=fnum[0]*fnum[1] *fnum[2]*fnum[3] *fnum[4] 7 print£("Product is %f", fpro); OUTPUT Enter floating-point no.0: 1. Enter floating-point no.1: 2. Enter floating-point no.2: 3 i 3 bee = Enter floating-point no.3: Enter floating-point no.4: Page 5 of 13 www, fasstook. convoffice.compk COMPUTERSCIENCE CLASS-KII COMPOSED BY:SAJIDURREHMAN CONTACT: sajid@office.com.pk | MOBILE: +92 345 528 26 25 WEBSITE: www.office.com.pk Product is 3622.819092 Q.7:. Write a program that reads numbers in two integers arrays x and y of size m and n and prints them in ascending order using a function. Answer: PROGRAM #include void fasc(int array1[3], int array2[3])7 void main(void) { int x[3], y[3], oF for(c=0; c<=2; c++) t printé("Enter number for array xI%d]: ",c)7 scant ("%d",&x[¢])7 i ua printf("Enter number for array y[%d]:.",¢)7 scanf ("%a",&y[c])7 ? $ fasc(x, y)i } void fasc(int arrayl[3], int array2[3]) ; ix int a, b, holderl, holder2, asort; flor(a=0;\a<=1; a++) for(b=0; b<1y b++) { if (arrayl[b]>arrayl[b+1]) holderl=arrayi[b]l; arrayi[a]=arrayl1[at+1]; arrayl[a+i]=holder1; } if (array2 [b] >array2[b+1]) = holder2=array2[b]7 array2([a]=array2 [atl]; array2[a+1]=holder2; Page’6 of 13 www. facebook. convoffice.com.pk COMPUTER SCIENCE CLAS: COMPOSED BY: SAJID UR REHMAN www.office.com.pk printf£("\nArray x\tArray y")? for(asort=0; asort<=2; asort++) { print£("\n%d\t%d",arrayl[asort], array2[asort]); ? } OUTPUT Enter number for array x[0]: 3 Enter number for array y[0]: 8 Enter number for array x[1]: 5 Enter number for array y[1]: 23 ~ Enter number for array x[2]: 45 «~ Enter number for array y[2]: 12)" “' Array x Array y 3 8 5. 23 45 12 L. C programs are made more fléxible through the use of (a) Calculations (6) Data (c) Functions (@) Folders x is a program unit or module that is designed to perform some particular task. (a) File (b) Statement : (c) Folder (@) Function Page 7 of 13 www, facebook. convoffice.com.pk - COMPUTER SCIENCE CLASS-XII COMPOSED BY: SAJID UR REHMAN ‘salid@office.com.pk | Rrfatis iene oh CONTACT: sajid@office.com.pk WEBSITE: www.office.com.pk 3. Functions are written only but may be referenced at several points in a program. (a) Once (b) Twice . © Fourth (d) Several 4. Functions are of one another. (a) Logical (b) Independent (c) Dependent (a) None ofall 5. is a predefined function of C language. (a) printinQ) (b) scanf() (©) streamp() (@) None of them - 6. These functions are also known as C library functions. (a) stremp() (b) pow() (©) sart() @ Allab&c 7. The function is always executed first. (a) top) (b) main (c) initial() (@) first) 8, All C programs must have a function called (a) first() (b) main() (©) start() (a) scan() 9. Function declaration also known as. (a) Monotype (b) Prototype (c) Cynotype (4) Sypotype 10. Function declaration is very similar to declaration of before they ware. * (a) Procedures (b) File (c) Variables (d) Graphics 11. Just like variables, function declaration tells compiler the of the function. (a) Solution (b) Name (c) Property (d) Purpose Page 8 of 13 www.facebook. conv/office.com.pk - ! COMPOSED BY: SAJIDURREHMAN — CONTAC 345 528 2625 www.office.com.pl COMPUTER SCIENCE CLASS: sajid@office.com.pk 12. The function definition is the itself, (a) Function (b) Statement (c) Prototype (d) Similar i 2 13. Function declaration does not end with (a) colon (:) (b) comma (,) (c) semicolon (;) (@) question mark (?) 14, The body of the function, the statements that do the work, follow the function (@) Relation .(b). Statement (c) Structure (d) Definition 15. The body of the function is enclosed within. (a) Brackets (b) Parenthesis (c) Braces (d) Commas 16. The brackets followed by function name tell the compiler that you are referr, to a function and not to a m (a) Constant (b) Exponent (©) Variable (d) Reciprocal 17. Variables defitied in a function are not known outside the (a) Program (b) Function (c) Message (d) Folder 18. Using of variable declared in user defined function in the function main() the compiler will give an message. (a) Erase (b) Error (©) Constant (d) Variable 19. What does “void” do before the function name? (a) that this function does not have any statement (b) that this function does not have any name (©) that this function does not retum anything (d) that this function should call before main() function Page 9 of 13 www. facebook. con/office.com.pk | COMPUTER SCIENCE CLASS-KII COMPOSEDBY: SAJIDURREHMAN CONTACT: sajid@office.com.pk | MOBILE: +92 345 528 26 25 WEBSITE: www.office.com.pk 20. How many values the “return” statement can return? (a) One _ (b) Two (c) Three * (@) Four 21. Variables declared within a function are called (a) Convenient (6) Hard to call (©) Local, - (4) Global 22. Global variables must be declared (a) outside of function (b) ‘inside of function (c) inside of main() function (d) inside of user-defined function 23. The function fact() is (a) used to calculate the factorial GPa: number (/ (b) used to calculate the facts of errors (c) auser defined function (d) None of all 24, Header files in C language contain (a) Compiler commands (b) Library functions (c) Header information of C programs (d) Operators for files 25. In C, if you pass an array as an argument to a function, what actually gets passed? (a) Value of elements in array (b) First element of the array (c) Base address of the array (a) Address of the last element of array 26. The keyword used to transfer control from a function back to the calling function is : (a) switch (b) goto (c) go back (d) retum Page 100f13 - WWW.facebook.convoffice.compk COMPUTERSCIENCE CLASS-XII COMPOSEDBY: SAJIDURREHMAN CONTACT: sajid@office.com.pk MOBILE: +92 345 528 26 25 WEBSITE: www.office.com.pk fee Cue Q.1. What is the fundamental to programming in every language? Ans: Dividing your program into manageable chunks of code is an idea that is fundamental to programming in every language. Q.2. What is the importance of function in “C” language? Ans: A function is a basic building block in C language. C programs are made more flexible through the use of function. -Q.3. Define a function. Ans: A function is a program unit or module that is designed to’ perform sore particular task. Q.4. Write down any two reasons for partitioning your programs. Ans: First, makes programs much easier to read and to manage. Second, make functions thggan be reused. Q.5. Write any one example of the benefits of reusing function. Ans: Standard library is a good example of the benefits of reusing function. Q.6. Why are the functions written individually? ‘Ans: Individual functions are written to carry out ‘each of the tasks defined in the analysis of the problem Q.7. What is the benefit of independency of function? Ans: Because the functions are independent of one another the programmer can write each function and test it without worrying about the details of other function. Page 11 0f13. - #WW.facebook.conv/office.com.pk | ‘COMPUTER SCIENCE CLASS-XII COMPOSED BY: SAJIDURREHMAN — CONTACT: sajid@office.com.pk MOBILE: +92 345 528 26 25 WEBSITE: www.office.com.pk -Q.8. Write some “C” library functions? Ans: printf(), scanf(), stremp(), pow() and sqrt() are known as “C” library functions. Q.9. Define the Function declaration or prototype. ‘Ans: The function declaration also known as prototype is a statement that describes a function sufficiently for the compiler to be able to compile calls to it. It declares the name of the function its return type and the type of its parameters. Q.10. Explain “void main(void);”. Ans: The first void before the function name means that this function does not return anything and the second void in the brackets means it has no arguments. § Q.11. What do you say about the defining a function? Ans: We specify what a function does in a function definition. There are two parts in the definition, first is the function header, which is the first line of the definition before the opening brace and the function body which lies between the braces: Q.12. What do you know about the function header? Ans: This is the first line of the function. Q.13. Write down the general form of a function header. Ans: The general form of a function bee can be written as fellows: type Function Name (Par: — list) uo Q.14. What do you know about the Function body? Ans: A function performs its computations by executing the statements in its body. The variables declared within the body of a function and all the functions parameters are local to the function. A variable is created at the point at which it is defined and ceases to exist at the end of the block containing it. ots. ‘Explain the return values. ~ ‘Ans: When a function with a return type other than void is called, it must retum a single value of the type specified in the function header. The return value is calculated with in the body of the function and is returned when execution of the function is complete. Q.16. Write down the methods that we can use to return more than two values. ‘Ans: There are two methods that we'can use to return more than two values one is using global variables and the second is specifying the values to be returned as arguments in the function. Page 120f13. #ww.facebook.com/office.com pk { COMPUTERSCIENCE CLASS-xII COMPOSED BY: SAJIDURREHMAN — CONTACT: sajid@office.com.pk | u MOBILE: +92 345 528 26 25 WEBSITE: www.office.com.pk Q.17. Define a global variable. Ans: A variable that is known to all the functions in a program. Q.18. What is called mainQ? Ans: All “C” program must have a function called main(). Q.19. What is meant by field? Ans: Data item about an entity such as name, address, date of birth etc is known field. Q.20. What is an entity? An: thing of interest to an organization about which data is to be held is known as Page 130f13 = www.facebook.convoffice.com.pk

You might also like