Functions

123
Question 1
Output of following program?
#include <stdio.h>
int main()
{
    int i = 5;
    printf("%d %d %d", i++, i++, i++);
    return 0;
}
A
7 6 5
B
5 6 7
C
7 7 7
D
Compiler Dependent
Functions    
Discuss it


Question 1 Explanation: 
When parameters are passed to a function, the value of every parameter is evaluated before being passed to the function. What is the order of evaluation of parameters - left-to-right or right-to-left? If evaluation order is left-to-right, then output should be 5 6 7 and if the evaluation order is right-to-left, then output should be 7 6 5. Unfortunately, there is no fixed order defined by C standard. A compiler may choose to evaluate either from left-to-right. So the output is compiler dependent.
Question 2
In C, parameters are always
A
Passed by value
B
Passed by reference
C
Non-pointer variables are passed by value and pointers are passed by reference
D
Passed by value result
Functions    
Discuss it


Question 2 Explanation: 
In C, function parameters are always passed by value. Pass-by-reference is simulated in C by explicitly passing pointer values.
Question 3
Which of the following is true about return type of functions in C?
A
Functions can return any type
B
Functions can return any type except array and functions
C
Functions can return any type except array, functions and union
D
Functions can return any type except array, functions, function pointer and union
Functions    
Discuss it


Question 3 Explanation: 
In C, functions can return any type except arrays and functions. We can get around this limitation by returning pointer to array or pointer to function.
Question 4
#include <stdio.h>
int main()
{
  printf("%d", main);  
  return 0;
}
A
Address of main function
B
Compiler Error
C
Runtime Error
D
Some random value
Functions    
Discuss it


Question 4 Explanation: 
Explanation: Name of the function is actually a pointer variable to the function and prints the address of the function. Symbol table is implemented like this.
struct
{
   char *name;
   int (*funcptr)();
}
symtab[] = {
   "func", func,
   "anotherfunc", anotherfunc,
};
Question 5
Output?
#include <stdio.h>

int main()
{
    int (*ptr)(int ) = fun;
    (*ptr)(3);
    return 0;
}

int fun(int n)
{
  for(;n > 0; n--)
    printf("GeeksQuiz ");
  return 0;
}
A
GeeksQuiz GeeksQuiz GeeksQuiz
B
GeeksQuiz GeeksQuiz
C
Compiler Error
D
Runtime Error
Functions    
Discuss it


Question 5 Explanation: 
The only problem with program is fun is not declared/defined before it is assigned to ptr. The following program works fine and prints "GeeksQuiz GeeksQuiz GeeksQuiz "
int fun(int n);

int main()
{
    // ptr is a pointer to function fun()
    int (*ptr)(int ) = fun;

    // fun() called using pointer 
    (*ptr)(3);
    return 0;
}

int fun(int n)
{
  for(;n > 0; n--)
    printf("GeeksQuiz ");
}
Question 6
Output of following program?
#include<stdio.h>

void dynamic(int s, ...)
{
    printf("%d ", s);
}

int main()
{
    dynamic(2, 4, 6, 8);
    dynamic(3, 6, 9);
    return 0;
}
A
2 3
B
Compiler Error
C
4 3
D
3 2
Functions    
Discuss it


Question 6 Explanation: 
In c three continuous dots is known as ellipsis which is variable number of arguments of function. The values to parameters are assigned one by one. Now the question is how to access other arguments. See this for details.
Question 7
Predict the output?
#include <stdio.h>
int main()
{
    void demo();
    void (*fun)();
    fun = demo;
    (*fun)();
    fun();
    return 0;
}

void demo()
{
    printf("GeeksQuiz ");
}
A
GeeksQuiz
B
GeeksQuiz GeeksQuiz
C
Compiler Error
D
Blank Screen
Functions    
Discuss it


Question 7 Explanation: 
This is a simple program with function pointers. fun is assigned to point to demo. So the two statements "(*fun)();" and "fun();" mean the same thing.
Question 8
What is the meaning of using extern before function declaration? For example following function sum is made extern
extern int sum(int x, int y, int z)
{
    return (x + y + z);
}
A
Function is made globally available
B
extern means nothing, sum() is same without extern keyword.
C
Function need not to be declared before its use
D
Function is made local to the file.
Functions    
Discuss it


Question 8 Explanation: 
extern keyword is used for global variables. Functions are global anyways, so adding extern doesn't add anything.
Question 9
What is the meaning of using static before function declaration? For example following function sum is made static
static int sum(int x, int y, int z)
{
    return (x + y + z);
}
A
Static means nothing, sum() is same without static keyword.
B
Function need not to be declared before its use
C
Access to static functions is restricted to the file where they are declared
D
Static functions are made inline
Functions    
Discuss it


Question 9 Explanation: 
In C, functions are global by default. Unlike global functions, access to static functions is restricted to the file where they are declared. We can have file level encapsulation using static variables/functions in C because when we make a global variable static, access to the variable becomes limited to the file in which it is declared.
Question 10
In C, what is the meaning of following function prototype with empty parameter list
void fun()
{
   /* .... */
}
A
Function can only be called without any parameter
B
Function can be called with any number of parameters of any types
C
Function can be called with any number of integer parameters.
D
Function can be called with one integer parameter.
Functions    
Discuss it


Question 10 Explanation: 
Empty list in C mean that the parameter list is not specified and function can be called with any parameters. In C, to declare a function that can only be called without any parameter, we should use "void fun(void)" As a side note, in C++, empty list means function can only be called without any parameter. In C++, both void fun() and void fun(void) are same.
There are 26 questions to complete.
123

Company Wise Coding Practice    Topic Wise Coding Practice