Search Placementyogi.com
 

C Functions Objective Questions and Answers

  :: Topics ::

1

What is the output of below c program?

#include<stdio.h>
int main()
{
  displayOut();
  return 0;
}

int displayOut()
{
  printf("Hi from placementyogi.com\n");
}
A.

Compiles with no warning and error messages

B.

Compiles with warning: conflicting types for dispalyOut

C.

Does not compile, gives error : dispalyOut not declared

D.

Compile but throws error while linking

See Answer & Explanation Lets Discuss
Correct answer is : A
Explanation

 

Compiler by default assumes return type of undeclared function to int and accept unspecified number of argument.

 

Here displayOut is called before it is defined. In this case the compiler assumes that the function dispayOut() prototype is declared as int displayOut(); but when in definition compiler sees that it is returning int which it has assumed so no error and warning.

 

See below example

/* program1.c*/
#include<stdio.h>
int main()
{

 displayOut();

 return 0;
}

void dispalyOut()
{
 printf("Hi from placementyogi.com\n");
}

In above program( program1.c) return type of displayOut() is void but the datatype assumed by compiler is int so conflicting data type warning.

 
2

What is the output of below c program?

#include<stdio.h>
main()
{
  printf("Hello from main,");

  int inside()
  {
     printf("inside,");

     int deepinside()
     {
       printf("and deepinside function\n");
     }

  }
}
A.

Hello from main,inside,and deepinside function

B.

Hello from main,inside

C.

Function definition inside another function is not allowed in C.

D.

None

See Answer & Explanation Lets Discuss
Correct answer is : C
Explanation

Yes, it's true. Definition of a function is not allowed inside body of any other function in Standard C .

If you are using gcc compiler then program compiles.  

 
3

What is the ouput of below c program?

#include<stdio.h>
int main()
{
    printf("PlacementYogi");
    main();
    return 0;
}
A.

Infinite loop

B.

Error because of stack overflow

C.

PlacementYogi is printed 65535 times

D.

Calling main() function is not allowed

See Answer & Explanation Lets Discuss
Correct answer is : B
Explanation

 

A stack(call stack) is used to store information about the active subroutines of a computer program. So whenever control transfer from one function(subroutine) to another function, return address and other parameter are stored on stack.

In above program main is calling itself repeatedly, it creates an infinite loop. Each call pushes return address and other parameters to call stack and it continues till stack is full. When stack is full it terminates with error : Segmentation fault (core dumped)

 

 
4

What is the output of below c program?

#include<stdio.h>
int main()
{
  void swap(int,int);
  int a=10,b=20;
  swap(a,b);               //Line 6
  printf("%d,%d",a,b);

}

void swap( int x, int y)  //Line 9
{
  int temp=x;
      x=y;
      y=temp;
}
A.

10, 20

B.

20, 10

C.

10, 10

D.

20, 20

See Answer & Explanation Lets Discuss
Correct answer is : A
Explanation

In above program, function is called by value. In "call by value"  parameter's copies are made. 

 

Initially at Line 6 from where swap function is called,  a = 10, b=20.  

 a --> | 10 |   b-->| 20

         

Now control goes to Line 9 and values of a,b are copied to x,y. Now x=10, y=20 and a = 10, b= 20

 a --> | 10 |   b-->| 20

 x --> | 10 |   y-->| 20

 

After this values of x and y are swapped, Now x=20, y=10 but still values of a and b are 10 and 20.   

 a --> | 10 |   b-->| 20

 x --> | 20 |   y-->| 10

 

So whatever changes we have done in swap function is on x and y which is a separate copy of a and b. 

 
5

What is the output of below c program?

#include<stdio.h>
int main()
{
  void swap(int*,int*); //Line 4
  int a=10,b=20;
  swap(&a,&b);        //Line 6
  printf("%d,%d",a,b);

}

void swap( int* x, int* y) //Line 9
{
  int temp=*x;    //Line 10
      *x=*y;      //Line 11
      *y=temp;    //line 12
}
A.

10, 20

B.

20, 10

C.

10, 10

D.

20, 20

See Answer & Explanation Lets Discuss
Correct answer is : B
Explanation

Here we are calling swap() by passing address of the parameters. This is called "call by reference".

 

Line 4 : void swap(int*,int*); <= this is the prototype of function and parameters are type of int* (Integer pointer), it stores the address of integer variable.

 

Line 6 : swap(&a,&b); <= Calling swap function by passing address of  a and b using '&' operator.

 a | 10 |   

 b | 20

 

Line 9:  Now memory address of a and b are copied to x and y, so x and y too start pointing same memory as a and b

a | 10 | <--x

b | 20 | <--y

 

Now values at memory location where x and y are pointing are swapped( Line 10, 11 and 12)

[*x => value at memory location where x is pointing.]

a | 20 | <--x

b | 10 | <--y

 

Now, you can see a and b memory location is same but values are changed.

 
 

Banner to LogicGuns.com

Hide Page Information

C Functions Objective Questions and Answers for Written Round Interview

Here you can find C Functions Objective Questions with answers and explanation. We have a vast collection of Objective Questions on Functions in C that will help you a lot!

Why learn C Functions?

Functions are an integral part of C and many Objective Questions in the written round are asked from this topic.In this section you can learn and practice C Functions (Objective Questions with Answers) to improve your skills in order to face the interview and clear campus and off-campus selection of TCS, Infosys, Wipro etc.

Where can I get C Functions Objective Questions and Answers with Explanation?

PlacementYogi provides you lots of fully solved C Functions Objective Questions and answers with explanation. Fully solved examples with detailed answer description, explanation are given and it would be easy to understand.Though we do not have pdf file for C Functions, We have quiz Objective Questions with answers to help you test yourself..

\n

Where can I get C Functions Interview Objective Questions and Answers (objective type, multiple choice)?

Here you can find objective type C Functions Objective Questions and answers for interview and entrance examination.Multiple Answers and true or false type Objective Questions are also provided.

How to solve C Functions problems?

You can easily solve all kind of Objective Questions based on C Functions by practicing the exercises (including shortcut Functions to solve problems) given below.