GkSeries.com

Functions in C Programming - MCQ Questions and Answers

(1) Consider the following program:

	main()
	{
		char *x="xyz;
		f(k);
		printf("%s\n",k);
	}
	f(char *k)
	{
		k=malloc(4);
		strcpy(k,"pq");
	}
	

What will be the output?

[A] pq
[B] xyz
[C] syntax error
[D] none of these

Comment

Answer: Option [C]

There is an opening quote in the third statement but no closing. So syntax error occurs.

mcq on c programming functions 01

(2) What does the following function print?

	func(int i)
	{
		if(i%2) return 0;
		else return 1;
	}
	main()
	{
		int i=3;
		i=func(i);
		i=func(i);
		printf("%d", i);
	}
	
[A] 3
[B] 1
[C] 0
[D] 2

Comment

Answer: Option [B]

(3) What is wrong with the following function?

	int Main(int ac, char *av[])
	{
		if(ac==0) return 0;
		else
	{
	printf("%s", av[ac-1]);
	Main(ac-1, av);
	}
		return 0;
	}
	
[A] Function cannot have name as Main, it should be main only
[B] The arguments' name must be argc and argv, respectively
[C] There cannot be two return statements in the function
[D] There error in the function

Comment

Answer: Option [D]

There is no error in the function. Here the Main() function differenciate with the main(). In the given problem the Main() has two arguments as int ac, char *av[]

(4) What is the following function determining?

	int fn(int a, int b)
	{
		if (b==0) return 0;
		if (b==1) return a;
		return a+fn(a, b-1);
	}
	
[A] a+b where a and b are integers
[B] a+b where a and b are non-negative integers
[C] a*b where a and b are integers
[D] a*b where a and b are non-negative integers

Comment

Answer: Option [B]

The above function is a recursive function. The function will return a+b where a and b are non-negative integers

(5) What is the output of the following code?

	main()
	{
		int a=1, b=10;
		swap(a,b);
		printf("\n%d%d", a,b);
	}
	swap(int x, int y)
	{
		int temp;
		temp=x;
		x=y;
		y=temp;
	}
	
[A] 1 1
[B] 1 10
[C] 10 1
[D] None of these

Comment

Answer: Option [B]

The 'call by value' method is applied in this program. Here the data is passed by value in the main(). So the variables are not changed.

Chapters