C MCQ Questions and Answers on Functions and Pointers
Table of Contents
Learn C MCQ Questions and Answers on Functions in C and Pointers in c on Basics to attend job placement exams, interview questions, college viva and Lab Tests
C language is built up of a number of functions. A function is a group of statements or block of statements that servers a specific purpose.
Type of Functions in C
There are two types of functions in a C language.
- Library Functions
- User Defined Functions
1. Library Functions
As the name suggests, Library functions are predefined functions written for specific purposes like printing, scanning input and more. To use library functions, you have to include the header files with the file name extension DOT-H (.h) in your C program using a Preprocessor Directive.
2. User Defined Functions
User defined functions are the functions defined or written by you in your C program. You can define any number of functions in a C program.
Syntax of a Function
RETURN-TYPE FUNCTION_NAME(PARAMERTER1, PARAMETER2,..) { STATEMENTS; // FUNCTION LOGIC return SOME_VALUE; }
Notes about Writing and Working of a Function in C
A function name should follow the same guidelines of choosing a Variable name
function name can not start with a number. It can start with an Underscore ( _ ) symbol.
A function name can contain Alphabets, numbers and Special character like Underscore.
Same function name should repeated again in the same C program.
Popular ‘return’ keyword is used most with functions.
Default Return type of a function is a Garbage Integer value if not specified explicitly.
A function can return only one value at a time.
You can put any number of RETURN statements in a c program.
You need not write a RETURN statement. It is not suggested.
main() is also a function which is invoked or called or started automatically on execution.
Any executable C program must contain main() function.
Other programming languages call functions as methods.
Arguments are nothing but Parameters a function accepts.
And Arguments received in a function are called FORMAL ARGUMENTS.
Arguments passed to a function are called ACTUAL ARGUMENTS.
Variables defined in a C program are called Local Variables. Local variables die after execution of the function.
Static Storage Class Variables in a function Retain their value among multiple calls.
Functions accept all types of data i.e int, long, float, double, char and pointers.
Arguments are values a function accepts from a calling function. Accepting arguments is not mandatory.
If you call a function SOMEFUNCTION() from main() function, then the main function is called CALLING FUNCTION and SOMEFUNCTION() is called CALLED FUNCTION.
There is no limit on the number of different functions being defined in a C program.
A function may called any number of times.
Nesting of functions or defining a function inside another function is not allowed.
Passing or arguments are processed from Right to Left in C Language.
A function can call itself. This process is called Recursion.
Functions in C
Example 1: Area of Rectangle Function
int area(int,int); int main() { int x=5, y=6; int z = area(x,y); int p = area(2,6); printf("%d, %d", z, p); return 9; } int area(int a, int b) { return (a*b); } //output //30, 12
In the above program, we passes arguments x and y to the function area(). Function area() returned calculated area which is assigned to the variable Z. You can directly call a function with constants without using any variables like area(2,6).
Functions in C
Example 2: Using Library functions
#include<math.h> int main() { float x=1; float a = sin(x); printf("%f", a); return 9; }
We have to INCLUDE the Header file of the Library we want to use. Different functions are included in different libraries. As we are dealing with Trigonometric functions, we have included MATH.H file.
Recursion or Recursive Functions
A function which calls itself is called a Recursive Function and the process is called Recursion.
- To create a recursive function, exit condition must be specified without which it becomes never ending recursion and the program crashes.
- Recursion is slow compared to normal WHILE, FOR and DO WHILE loops.
- Recursion logic by one developer is difficult to understand for other developers.
- Usually, IF or ELSE conditions are used to END recursion.
Example 1:
int main() { int a = 4335; int no_of_digits = func(a); printf("NO=%d", no_of_digits); return 9; } int func(int p) { int k; if(p<10) return 1; else k = 1 + func(p/10); return k; } //output //NO=4 //Analysis //Step1: 1 + func(433) //Step2: 1 + (1 + func(43)) //Step3: 1 + (1 + (1 + func(4))) //Step4: 1 + (1 + (1 + 1) )
Above recursive function takes the input number a P. Using recursion, it counts number of digits present in the given number.
Pointers in Functions
When passing variables and variable addresses from one function to another, pointers are also used.
There are two ways of calling a function with arguments.
- Call By Value
- Call By Reference
1. Call By Value / Pass by Reference
If you pass only variables or constants to a function, it is called Call By Value. Because you do not use any Pointers or Addresses. Advantage of Call By Value is that the original values of passing variables do not change when you make changes in the called function.
Example
void show(int); int main() { int a=10; show(a); //CALL BY VALUE printf("%d", a); } void show(int x) { x = x + 1; //Here it is not a. printf("%d,", x); return; } //output: //11,10 //Value of a is not changed.
2. Call By Reference / Pass by Reference
Call By Reference passes addresses of variables to another function. So whatever changes you make to the variables in the CALLED function reflect in the CALLING function. It has side effects.
Addresses are passed by call by reference using two operators: ADDRESS OF (&) and VALUE AT ADDRESS (*).
C MCQ Questions and Answers on Functions and Pointers
Example
void show(int*); int main() { int a=10; show(&a); //CALL BY Reference printf("%d", a); } void show(int *x) { *x = *x + 1; //Here it is A only. printf("%d,", *x); return; } //output: //11,11 //Value of a is CHANGED.
&a is the address of the Variable ‘a’. *x is the value at the address passed. So you are changing the original copy of data in memory location passed. This is called CALL BY REFERENCE.
[WpProQuiz 31]
Questions and Answers on Functions in C
1) Choose correct statement about Functions in C Language.
A) A Function is a group of c statements which can be reused any number of times.
B) Every Function has a return type.
C) Every Function may no may not return a value.
D) All the above.
Answer [=] D
2) Choose a correct statement about C Language Functions.
A) A function name can not be same as a predefined C Keyword.
B) A function name can start with an Underscore( _ ) or A to Z or a to z.
C) Default return type of any function is an Integer.
D) All the above.
Answer [=] D
3) Choose a correct statement about C Function.?
main() { printf("Hello"); }
A) “main” is the name of default must and should Function.
B) main() is same as int main()
C) By default, return 0 is added as the last statement of a function without specific return type.
D) All the above
Answer [=] D
4) A function which calls itself is called a ___ function.
A) Self Function
B) Auto Function
C) Recursive Function
D) Static Function
Answer [=] C
5) What is the output of C Program with Functions.?
int main() { void show() { printf("HIDE"); } show(); return 0; }
A) No output
B) HIDE
C) Compiler error
D) None of the above
Answer [=] B
Explanation:
Take note that the main() function contains the definition of the show() function. There won’t be a compile error as a result. However, it is not advisable to define a function inside of another function.
6) What is the output of C Program with functions.?
void show(); int main() { show(); printf("ARGENTINA "); return 0; } void show() { printf("AFRICA "); }
A) ARGENTINA AFRICA
B) AFRICA ARGENTINA
C) ARGENTINA
D) Compiler error
Answer [=] B
Explanation:
First show() function is called. So it prints AFRICA first.
7) What is the output of C Program with functions.?
int main() { show(); printf("BANK "); return 0; } void show() { printf("CURRENCY "); }
A) CURRENCY BANK
B) BANK CURRENCY
C) BANK
D) Compiler error
Answer [=] D
Explanation:
Yes. compiler mistake. Its Function Prototype should be declared before outside of main() and before the show(); function is called ().
void show(); int main() { show(); printf("BANK "); return 0; }
8) How many values can a C Function return at a time.?
A) Only One Value
B) Maximum of two values
C) Maximum of three values
D) Maximum of 8 values
Answer [=] A
Explanation:
Using a return val; statement, you can return only one value.
9) What is the output of a C program with functions.?
void show(); void main() { show(); printf("RAINBOW "); return; } void show() { printf("COLOURS "); }
A) RAINBOW COLOURS
B) COLOURS RAINBOW
C) COLOURS
D) Compiler error
Answer [=] B
Explanation:
VOID functions should not return anything. RETURN; is returning nothing.
1. First void main() return; nothing. Still it is valid.
2. Second void show() function is NO RETURN statement. It is also valid.
10) What is the output of C Program.?
void show(); void main() { printf("PISTA "); show(); } void show() { printf("CACHEW "); return 10; }
A) PISTA CACHEW
B) CASHEW PISTA
C) PISTA CASHEW with compiler warning
D) Compiler error
Answer [=] C
Explanation:
void show() function should not return anything. So return 10; is not recommended.
11) What is the output of C Program with functions.?
int show(); void main() { int a; printf("PISTA COUNT="); a=show(); printf("%d", a); } int show() { return 10; }
A) PISTA COUNT=
B) PISTA COUNT=0
C) PISTA COUNT=10
D) Compiler error
Answer [=] C
Explanation:
int show() function returns TEN (10). 10 is assigned to a at a=show().
12) What is the output of C Program with functions.?
void main() { int a; printf("TIGER COUNT="); a=show(); printf("%d", a); } int show() { return 15; return 35; }
A) TIGER COUNT=15
B) TIGER COUNT=35
C) TIGER COUNT=0
D) Compiler error
Answer [=] A
Explanation:
More than one return statement will not cause Compiler Error. But only FIRST return STATEMENT is executed. Anything after return 15; is not reachable.
13) What are types of Functions in C Language.?
A) Library Functions
B) User Defined Functions
C) Both Library and User Defined
D) None of the above
Answer [=] C
14) What is the output of C program with functions.?
int show(); void main() { int a; a=show(); printf("%d", a); } int show() { return 15.5; return 35; }
A) 15.5
B) 15
C) 0
D) Compiler error
Answer [=] B
Explanation:
It is perfectly Okay to return a float number 15.5 as an Integer inside int show() function. 15.5 is demoted to integer as 15 and returned.
15) What is the output of C Program.?
int myshow(int); void main() { myshow(5); myshow(10); } int myshow(int b) { printf("Received %d, ", b); }
A) Received 5, Received 10,
B) Received 10, Received 5,
C) Received 0, Received 0,
D) Compiler error
Answer [=] A
Explanation:
Notice the function prototype declaration int myshow(int). If you declare wrong either Compiler warning or error is thrown. myshow(5) passes number 5. 5 is received as variable int b.
16) What is the output of C Program with functions and pointers.?
int myshow(int); void main() { int a=10; myshow(a); myshow(&a); } int myshow(int b) { printf("Received %d, ", b); }
A) Received 10, Received 10,
B) Received 10, Received RANDOMNumber,
C) Received 10, Received RANDOMNumber, with a compiler warning
D) Compiler error
Answer [=] C
Explanation:
a is 10. &a is the address of the variable a which is a random memory location. To receive an address, int myshow(int b) should be rewritten as int myshow(int *k).
17) What is the output of C Program with functions and pointers.?
int myshow(int *); void main() { int a=10; myshow(&a); } int myshow(int *k) { printf("Received %d, ", *k); }
A) Received RANDOMNumber,
B) Received 10,
C) Received 10,
D) Compiler error
Answer [=] C
Explanation:
It is called Passing a variable by reference. You are passing &a instead of a. Address of a or &a is received as int *k. Observe the function prototype declaration before main(), int myshow(int *).
18) What is the output of C Program with functions and pointers.?
void myshow(int *); void main() { int a=10; printf("%d ", a); myshow(&a); printf("%d", a); } void myshow(int *k) { *k=20; }
A) 10 10
B) 20 20
C) 10 20
D) Compiler error
Answer [=] C
Explanation:
You passed &a instead of a into myshow(int) function. *k=20 changes the valued of passed variable passed by reference.
19) What is the output of C Program with functions.?
void myshow(int); void main() { int a=10; printf("%d ", a); myshow(a); printf("%d", a); } void myshow(int k) { k=20; }
A) 10 10
B) 20 20
C) 10 20
D) Compiler error
Answer [=] A
Explanation:
You passed variable a directly by value. myshow(a). k=20 will not actually change the variable a as variable k and variable a are completely different. It is called Pass By Value.
20) Choose correct statements about C Language Pass By Value.
A) Pass By Value copies the variable value in one more memory location.
B) Pass By Value does not use Pointers.
C) Pass By Value protects your source or original variables from changes in outside functions or called functions.
D) All the above
Answer [=] D