Sei sulla pagina 1di 6

UNIT V 2 mark question UNIT V FOCP 2 Mark Questions (with Answers) 1. Write definition of function.

. Indicate types of functions available in C. A function is a self-contained block or a sub-program of one or more statements that performs a special task when called. Types of functions - Without arguments or return values. Eg. abc() - With arguments but without return values. Eg. abc(int x) - With arguments and return values. Eg. int abc(int x) - Without argument but with return values. Eg. int abc(). 2. How do functions help to reduce program size? If a task is to be performed repetitively, then it is not necessary to rewrite the particular block of program again and again. Writing the block of statements in a user-defined function helps to reduce program size. 3. Differentiate between library and user-defined functions. Library User defined - pre-defined set of functions - user-defined - use cannot understand internal working - user can understand internal working bcos source code is visible - user cannot modify the function. - user is able to modify the function 4. Give the syntax of the function definition. function_type function_name(parameter_list){ /*local variable declaration */ Stmt 1; Stmt 2; return statement } 5. How does a function work? - function is defined and called - control passes ot called function, working of calling function is stopped. - Values of actual arguments are received by formal argument of called function. - Function operates on formal arguments and send back result to calling function. 6. List any five-library functions. - ceil(x), sqrt(x), log(x), pow(x,y), sin(x). 7. What are actual and formal arguments? Actual arguments are arguments of calling functions whereas formal argument are the arguments in the called function. main(){ -----------------

UNIT V 2 mark question ----------------Abc(x, y, z); // Function call, Actual argument -------------------------------} Abc(l, k, z){ // function definition, formal argument ------------------------------------------------} 8. What are local and global variables? Local variables are defined within the body of the function or block whereas global variables are defined outside the main() function. Local variables scope is only within the function it is declared whereas global variables can be used by multiple functions. 9. What are the uses of return() statements? - to return the value to the calling function. - To exit from the called function 10. Write a program to display Hello 10 times. Create user-defined function message(). #include<stdio.h> void message(); main(){ int i=0; for(i=0; i<5; i++) message(); } void message(){ printf("\n Hello"); } 11. Write a program to pass an array of values to function max to find the maximum number. #include<stdio.h> float max(float a[], int); main(){ float val[5] = {2.3, 5.3, 1.2, 6.3, 6.1}; int n = 5; printf("\n Max element = %.1f \n", max(val, n)); } float max(float arr[], int n){ int i; float max = arr[0]; for(i=1; i<n; i++){ if(arr[i]>max) max = arr[i];

UNIT V 2 mark question } return max; } 12. Write a program to calculate average temperature of five days. User temp() function. 13. What are the parts of a function declaration? Function Type(return type), function name, parameter list and terminating semicolon. 14. What are the elements of user-defined functions? Function definition, Function call and Function declaration. 15. Mention difference between character array and integer array. In a character array, the compiler automatically puts the null character, \0 at end of character array. In integer array this is not done. 16. Give syntax for declaring and initializing array during compile time. data_type variable_name[size]; /* Declaration */ data_type array_name[size] = {list of values}; /* Initialization during compile time*/ 17. Give syntax of two dimensional array. data_type array_name[row_size][column_size]. 18. What are strings? How are they declared? A string is a group of characters, digits, and symbols enclosed within quotation marks. 19. What is a NULL character? Why is it important? The NULL character is \0. It serves as an end-of-string marker in character arrays. 20. What is the difference between strcmp() and stricmp()? strcmp() compares two strings and distinguishes between upper and lower case whereas stricmp() compares two strings but does not discriminate between upper and lower case. 21. Write a program to conduct a simple quiz. #include<stdio.h> #include<string.h> main(){ char ans1[50] = "assembler"; char ans2[50] = "FORTRAN"; char ans[50]; printf("\n What translates assembly language to machine language?"); scanf("%s", &ans); if(stricmp(ans1, ans)==0) // returns 0 if identical printf("\n Correct answer! \n"); else // returns other value if not identical printf("\n Wrong answer... \n");

UNIT V 2 mark question

printf("\n What is Formula Translator?"); scanf("%s", &ans); if(stricmp(ans2, ans)==0) // returns 0 if identical printf("\n Correct answer! \n"); else // returns other value if not identical printf("\n Wrong answer... \n"); } 22. What are pointers? Why are they important? A pointer is a memory variable that stores a memory address. It is important as - it saves memory space - execution time with pointer is faster due to direct access to memory location - dynamically memory can be allocated. 23. What is the use of (*) indirection operator? It is used for declaration and dereference. When a pointer is dereferenced, the value stored at that address by the pointer is retrieved. 24. Give syntax for declaring pointer. data_type *pt_name; 25. What is the relation between array and pointer? An array name by itself is an address or pointer. It points to the address of the first element(0th element) in an array. 26. What is a structure in C? How is a structure declared? A structure is a collection of one or more variables of different data types grouped together under a single name. 27. Give syntax of structure declaration. struct struct_type{ type variable1; type variable2; } 28. What is the use of struct keyword and use of dot operator. struct keyword is used to declare a structure. The dot operator is used to access the structure members. 29. How are arrays of structure variables defined? struct time{ int sec; int min; int hr; } t[3]; /* t[3] is an array of 3 elements containing 3 objects of time structure. 30. How are user-defined data types defined? By using typedef, the syntax is typedef type dataname; Eg. typedef int hours; hours hrs;

UNIT V 2 mark question 31. What is enumerated data type? enum is used for creating our own data type and define what values the variables of these data types can hold. Eg. enum month{Jan, Feb, Mar, Apr}; 32. What is a union in C? Union is similar to structure but member of unions have same memory locations. 33. What is the difference between union and structure? In structure, each member has its own storage location whereas in union, all members have the same memory location. In union, the compiler allocates memory that is large enough to hold the largest variable type. 34. Mention some guidelines in developing a C program. - coding character set use ASCII character set - identifiers follow standard naming conventions - declaration all variables and identifiers must be declared before they are used. - naming conventions - naming restrictions - complexity excessive use of nested expressions can lead to unreadability. - Modularity use concept of modularity to manage complexity of code - Buffer overflow of buffer can cause corruption to adjacent data - data type - goto avoid the use of goto - array make sure it doesnt run out of bounds. - arithmetic expressions - check to avoid division by zero condition. Part B Functions 1. Write a user defined function to perform following tasks. a. Square of number b. Area of square c. Conversion of decimal number to binary d. Reverse of number 2. Write a program to count for how many times a function is called. 3. Write a program to add return values of three functions 4. Write a program to increment and display the return value of function. 5. Write a program to calculate the result of the following with recursive calls of function. a. X= 1+2+3+4+n b. X=1!+2!+3!+n c. 1+3+5+n 6. Write a program to compute the total amount payable with annual interest for a given number of years. The inputs are principal amount, rate of interest and number of years. Attempt the program with and without recursion method. 7. Write a function that will generate and print the first n Fibonacci numbers.

UNIT V 2 mark question 8. Write a function prime that returns 1 if the argument is a prime number and returns zero otherwise. Arrays 9. Write a program to read 10 integers in an array. Find the largest and smallest number. 10. Write a program to read a text. Find out the number of lines in it. 11. Write a program to read the marks of five subjects obtained by five students in an examination.. Display the top two student codes and their marks. 12. Evaluate the following series. Calculate every term and store its result in an array. Using the array calculate the final result a. X=11+22+33+n b. X=1!+2!+3!+.n 13. Write a program to display the items that have been exported between 1995 and 2000. Input the amount of various items described as under a. Find the total and average export value of all the items b. Find the year in which minimum and maximum export was made. Working with strings 14. Write a program to arrange a set of fruit names given below in descending order. 15. Write a program to enter some text and display the text in reverse order. 16. Write a program to enter text through keyboard. Convert first character to capital and display the text. 17. Write a program that reads a string from keyboard and determines whether it is a palindrome or not. Structure and Unions 18. Write a program to declare a structure of students and calculate sum, avg and grade of 6 subjects for an array of students. 19. Write a program to declare a structure of employees and calculate gross salary using basic, da, hra, and tax for an array of employees. 20. Define a structure called cricket that will describe the player name, team name and batting average. Using cricket, declare an array player with 50 elements and write a program to read the information about all the 50 players and print a team-wise list containing names of players with their batting average.

Potrebbero piacerti anche