C | Functions
1. What is the output of this C code?
int main()
{
void foo(), f();
f();
}
void foo()
{
printf("2 ");
}
void f()
{
printf("1 ");
foo();
}
A. Compile time error as foo is local to main
B. 1 2
C. 2 1
D. Compile time error due to declaration of functions inside main
Answer: Option B
2. What is the output of this C code?
int main()
{
void foo();
void f()
{
foo();
}
f();
}
void foo()
{
printf("2 ");
}
A. 2 2
B. 2
C. Compile time error
D. Depends on the compiler
Answer: Option D
Explanation:
Even though the answer is 2, this code will compile fine only with gcc. GNU C supports
nesting of functions in C as a language extension where as standard C compiler doesn’t.
3. What is the output of this C code?
void foo();
int main()
{
void foo();
foo();
return 0;
}
void foo()
{
printf("2 ");
}
A. Compile time error
B. 2
C. Depends on the compiler
D. Depends on the standard
Answer: Option B
4. What is the output of this C code?
void foo();
int main()
{
void foo(int);
foo(1);
return 0;
}
void foo(int i)
{
printf("2 ");
}
A. 2
B. Compile time error
C. Depends on the compiler
D. Depends on the standard
Answer: Option A
5. What is the output of this C code?
void foo();
int main()
{
void foo(int);
foo();
return 0;
}
void foo()
{
printf("2 ");
}
A. 2
B. Compile time error
C. Depends on the compiler
D. Depends on the standard
Answer: Option B
6. What is the output of this C code?
void m()
{
printf("hi");
}
void main()
{
m();
}
A. hi
B. Run time error
C. Nothing
D. Varies
Answer: Option A
7. What is the output of this C code?
void m();
void n()
{
m();
}
void main()
{
void m()
{
printf("hi");
}
}
A. hi
B. Compile time error
C. Nothing
D. Varies
Answer: Option B
8. What is the output of this C code?
void main()
{
m();
void m()
{
printf("hi");
}
}
A. hi
B. Compile time error
C. Nothing
D. Varies
Answer: Option B
9. What is the output of this C code?
void main()
{
m();
}
void m()
{
printf("hi");
m();
}
A. Compile time error
B. hi
C. Infinite hi
D. Nothing
Answer: Option C
10. What is the output of this C code?
void main()
{
static int x = 3;
x++;
if (x <= 5)
{
printf("hi");
main();
}
}
A. Run time error
B. hi
C. infinite hi
D. hi hi
Answer: Option D
11. Which of the following is a correct format for declaration of function?
A. return-type function-name(argument type);
B. return-type function-name(argument type) {}
C. return-type (argument type)function-name;
D. Both (a) and (b)
Answer: Option A
12. Which of the following function declaration is illegal?
A. int 1bhk(int);
B. int 1bhk(int a);
C. int 2bhk(int*, int []);
D. All of the mentioned
Answer: Option D
13. Which function definition will run correctly?
A. int sum(int a, int b)
return (a + b);
B. int sum(int a, int b)
{return (a + b);}
C. int sum(a, b)
return (a + b);
D. Both (a) and (b)
Answer: Option B
14. Can we use a function as a parameter of another function? [ Eg: void wow(int
func()) ]
A. Yes, and we can use the function value conveniently
B. Yes, but we call the function again to get the value, not as convenient as in
using variable
C. No, C does not support it.
D. This case is compiler dependent
Answer: Option C
15. The value obtained in the function is given back to main by using ________
keyword?
A. return
B. static
C. new
D. volatile
Answer: Option A
16. What is the return-type of the function sqrt()
A. int
B. float
C. double
D. Depends on the data type of the parameter
Answer: Option C
17. Which of the following function declaration is illegal?
A. double func();
int main(){}
double func(){}
B. double func(){};
int main(){}
C. int main()
{
double func();
}
double func(){//statements}
D. None of the mentioned
Answer: Option D
18. What is the output of this code having void return-type function?
void foo()
{
return 1;
}
void main()
{
int x = 0;
x = foo();
printf("%d", x);
}
A. 1
B. 0
C. Runtime error
D. Compile time error
Answer: Option D
19. What will be the data type returned for the following function?
int func()
{
return (double)(char)5.0;
}
A. char
B. int
C. double
D. multiple type-casting in return is illegal
Answer: Option B
20. What is the problem in the following declarations?
int func(int);
double func(int);
int func(float);
A. A function with same name cannot have different signatures
B. A function with same name cannot have different return types
C. A function with same name cannot have different number of parameters
D. All of the mentioned
Answer: Option D
21. The output of the code below is
void main()
{
int k = m();
printf("%d", k);
}
void m()
{
printf("hello");
}
A. hello 5
B. Error
C. Nothing
D. Junk value
Answer: Option A
22. The output of the code below is
int *m()
{
int *p = 5;
return p;
}
void main()
{
int *k = m();
printf("%d", k);
}
A. 5
B. Junk value
C. 0
D. 5
Answer: Option D
23. The output of the code below is
int *m();
void main()
{
int *k = m();
printf("hello ");
printf("%d", k[0]);
}
int *m()
{
int a[2] = {5, 8};
return a;
}
A. hello 5 8
B. hello 5
C. hello followed by garbage value
D. Compilation error
Answer: Option C
24. The output of the code below is
int *m();
void main()
{
int k = m();
printf("%d", k);
}
int *m()
{
int a[2] = {5, 8};
return a;
}
A. 5
B. 8
C. Nothing
D. Varies
Answer: Option D
25. The output of the code below is
void m(int k)
{
printf("hi");
}
void m(double k)
{
printf("hello");
}
void main()
{
m(3);
}
A. hi
B. hello
C. Compile time error
D. Nothing
Answer: Option C
26. What is the default return type if it is not specified in function definition?
A. void
B. int
C. double
D. short int
Answer: Option B
27. What is the output of this C code?
int foo();
int main()
{
int i = foo();
}
foo()
{
printf("2 ");
return 2;
}
A. 2
B. Compile time error
C. Depends on the compiler
D. Depends on the standard
Answer: Option A
28.What is the output of this C code?
double foo();
int main()
{
foo();
return 0;
}
foo()
{
printf("2 ");
return 2;
}
A. 2
B. Compile time error
C. Depends on the compiler
D. Depends on the standard
Answer: Option B
29. functions can return structure in c?
A. true
B. false
C. Depends on the compiler
D. Depends on the standard
Answer: Option A
30. functions can return enumeration constants in c?
A. true
B. false
C. depends on the compiler
D. depends on the standard
Answer: Option A
31. What is the output of code given below?
enum m{JAN, FEB, MAR};
enum m foo();
int main()
{
enum m i = foo();
printf("%d\n", i);
}
int foo()
{
return JAN;
}
A. Compile time error
B. 0
C. Depends on the compiler
D. Depends on the standard
Answer: Option A
32. What is the output of this C code?
void main()
{
m();
printf("%d", x);
}
int x;
void m()
{
x = 4;
}
A. 4
B. Compile time error
C. 0
D. Undefined
Answer: Option B
33. What is the output of this C code?
int x;
void main()
{
printf("%d", x);
}
A. Junk value
B. Run time error
C. 0
D. Undefined
Answer: Option C
34. What is the output of this C code?
int x = 5;
void main()
{
int x = 3;
printf("%d", x);
{
x = 4;
}
printf("%d", x);
}
A. Run time error
B. 3 3
C. 3 5
D. 3 4
Answer: Option D
35. What is the output of this C code?
int x = 5;
void main()
{
int x = 3;
printf("%d", x);
{
int x = 4;
}
printf("%d", x);
}
A. 3 3
B. 3 4
C. 3 5
D. Run time error
Answer: Option A
36. Functions in C are ALWAYS:
A. Internal
B. External
C. Both Internal and External
D. External and Internal are not valid terms for functions
Answer: Option B
37. Global variables are:
A. Internal
B. External
C. Both (a) and (b)
D. None of the mentioned.
Answer: Option B
38.
Which of the following are an external variable?
int func (int a)
{
int b;
return b;
}
int main()
{
int c;
func (c);
}
int d;
A. a
B. b
C. c
D. d
Answer: Option D
39.
What will be the output?
int main()
{
printf("%d", d++);
}
int d = 10;
A. 9
B. 10
C. 11
D. Compile time error
Answer: Option D
40. What will be the output?
double var = 8;
int main()
{
int var = 5;
printf("%d", var);
}
A. 5
B. 8
C. Compile time error due to wrong format identifier for double
D. Compile time error due to redeclaration of variable with same name
Answer: Option A
41. What is the output of this C code?
double i;
int main()
{
printf("%g\n",i);
return 0;
}
A. 0
B. 0.000000
C. Garbage value
D. Depends on the compiler
Answer: Option B
42. Which part of the program address space is p stored in the code given below?
int *p = NULL;
int main()
{
int i = 0;
p = &i;
return 0;
}
A. Code/text segment
B. Data segment
C. Bss segment
D. Stack
Answer: Option B
43. Which part of the program address space is p stored in the code given below?
int *p;
int main()
{
int i = 0;
p = &i;
return 0;
}
A. Code/text segment
B. Data segment
C. Bss segment
D. Stack
Answer: Option C
44. Can variable i be accessed by functions in another source file?
int i;
int main()
{
printf("%d\n", i);
}
A. 0
B. false
C. Only if static keyword is used
D. Depends on the type of the variable
Answer: Option A
45. Property of external variable to be accessed by any source file is called by C90
standard as:
A. external linkage
B. external scope
C. global scope
D. global linkage
Answer: Option A
46. What is the output of this C code?
int *i;
int main()
{
if (i == NULL)
printf("true\n");
return 0;
}
A. true
B. true only if NULL value is 0
C. Compile time error
D. Nothing
Answer: Option A
47. What is the output of this C code?
int *i;
int main()
{
if (i == 0)
printf("true\n");
return 0;
}
A. true
B. true only if NULL value is 0
C. Compile time error
D. Nothing
Answer: Option B
48. What is the output of this C code?
static int x = 5;
void main()
{
x = 9;
{
int x = 4;
}
printf("%d", x);
}
A. 9
B. 4
C. 5
D. 0
Answer: Option A
49. What is the output of this C code?
void main()
{
m();
m();
}
void m()
{
static int x = 5;
x++;
printf("%d", x);
}
A. 6 7
B. 6 6
C. 5 5
D. 5 6
Answer: Option A
50. What is the output of this C code?
void main()
{
static int x;
printf("x is %d", x);
}
A. 0
B. 1
C. Junk value
D. Run time error
Answer: Option A
51. What is the output of this C code?
static int x;
void main()
{
int x;
printf("x is %d", x);
}
A. 0
B. Junk value
C. Run time error
D. Nothing
Answer: Option B
52. What is the output of this C code?
void main()
{
static double x;
int x;
printf("x is %d", x);
}
A. 0
B. Nothing
C. Compile time error
D. Junk value
Answer: Option C
53. What is the output of this C code?
void main()
{
static int x;
if (x++ < 2)
main();
}
A. Infinite calls to main
B. Run time error
C. Varies
D. main is called twice
Answer: Option D
54. Which of following is not accepted in C?
A. static a = 10; //static as
B. static int func (int); //parameter as static
C. static static int a; //a static variable prefixed with static
D. All of the mentioned
Answer: Option C
55. Which of the following cannot be static in C?
A. Variables
B. Functions
C. Structures
D. None of the mentioned
Answer: Option D
56. The scope of an automatic variable is:
A. Within the block it appears
B. Within the blocks of the block it appears
C. Until the end of program
D. Both (a) and (b)
Answer: Option D
57. Automatic variables are allocated space in the form of a:
A. stack
B. queue
C. priority queue
D. random
Answer: Option A
58. Which of the following is a storage specifier?
A. enum
B. union
C. auto
D. volatile
Answer: Option C
59. Default storage class if not any is specified for a local variable, is auto:
A. true
B. false
C. Depends on the standard
D. None of the mentioned
Answer: Option A
60. Automatic variables are stored in:
A. stack
B. data segment
C. register
D. heap
Answer: Option A
61. What linkage does automatic variables have?
A. Internal linkage
B. External linkage
C. No linkage
D. None of the mentioned
Answer: Option C
62. What is the output of this C code?
int main()
{
auto i = 10;
const auto int *p = &i;
printf("%d\n", i);
}
A. 10
B. Compile time error
C. Depends on the standard
D. Depends on the compiler
Answer: Option A
63. Automatic variables are variables that are?
A. Declared within the scope of a block, usually a function
B. Declared outside all functions
C. Declared with auto keyword
D. Declared within the keyword extern
Answer: Option A
64. Automatic variables:
A. Exist only within that scope in which it is declared
B. Cease to exist after the block is exited
C. Both (a) & (b)
D. Only 1
Answer: Option C
65. Automatic variables are allocated memory in?
A. heap
B. Data segment
C. Code segment
D. stack
Answer: Option D
66. What is the output of this C code?
void main()
{
int x;
}
here x is?
A. automatic variable
B. static variable
C. global variable
D. register variable
Answer: Option A
67. Automatic variables are initialized to?
A. 0
B. Junk value
C. Nothing
D. Both (a) & (b)
Answer: Option B
68. Which of the following storage class supports char data type?
A. register
B. static
C. auto
D. All of the mentioned
Answer: Option D
69. The variable declaration with no storage class specified is by default:
A. auto
B. extern
C. static
D. register
Answer: Option A
70. What is the output of this C code?
int x;
void main()
{
printf("%d", x);
}
A. Junk value
B. Run time error
C. 0
D. Undefined
Answer: Option C
71. What is the output of this C code?
int x = 5;
void main()
{
int x = 3;
printf("%d", x);
{
x = 4;
}
printf("%d", x);
}
A. Run time error
B. 3 3
C. 3 5
D. 3 4
Answer: Option D
72. What is the scope of an external variable?
A. Whole source file in which it is defined
B. From the point of declaration to the end of the file in which it is defined
C. Any source file in a program
D. From the point of declaration to the end of the file being compiled
Answer: Option D
73. What is the scope of a function?
A. Whole source file in which it is defined
B. From the point of declaration to the end of the file in which it is defined
C. Any source file in a program
D. From the point of declaration to the end of the file being compiled
Answer: Option D
74. Which variable has the longest scope?
int b;
int main()
{
int c;
return 0;
}
int a;
A. a
B. b
C. c
D. Both (a) and (b)
Answer: Option B
75. Array sizes are optional during array declaration by using ______ keyword.
A. auto
B. static
C. extern
D. register
Answer: Option C
76. What is the output of this C code?
void main()
{
int x = 3;
{
x = 4;
printf("%d", x);
}
}
A. 4
B. 3
C. 0
D. Undefined
Answer: Option A
77. What is the output of this C code?
int x = 5;
void main()
{
int x = 3;
m();
printf("%d", x);
}
void m()
{
x = 8;
n();
}
void n()
{
printf("%d", x);
}
A. 8 3
B. 8 5
C. 3 8
D. 5 3
Answer: Option A
78. What is the output of this C code?
void main()
{
m();
m();
}
void m()
{
static int x = 5;
x++;
printf("%d", x);
}
A. 6 7
B. 6 6
C. 5 5
D. 5 6
Answer: Option A
79. What is the output of this C code?
static int x;
void main()
{
int x;
printf("x is %d", x);
}
A. 0
B. Junk value
C. Run time error
D. Nothing
Answer: Option B
80. What is the output of this C code?
void main()
{
static double x;
int x;
printf("x is %d", x);
}
A. Nothing
B. 0
C. Compile time error
D. Junk value
Answer: Option C
81. What is the output of this C code?
void main()
{
static int x;
if (x++ < 2)
main();
}
A. Infinite calls to main
B. Run time error
C. Varies
D. main is called twice
Answer: Option D
82. Which of the following is true for static variable?
A. It can be called from another function.
B. It exists even after the function ends.
C. It can be modified in another function by sending it as a parameter.
D. All of the mentioned
Answer: Option B
83. Assignment statements assigning value to local static variables are executed only
once?
A. true
B. false
C. Depends on the code
D. None of the mentioned
Answer: Option B
84. What is the format identifier for “static a = 20.5;”?
A. %s
B. %d
C. %f
D. Illegal declaration due to absence of data type
Answer: Option B
85. Functions have static qualifier for its declaration by default.
A. true
B. false
C. Depends on the compiler
D. Depends on the standard
Answer: Option B
86. Is initialization mandatory for local static variables?
A. YES
B. NO
C. Depends on the compiler
D. Depends on the standard
Answer: Option B
87.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
Answer: (D)
Explanation: When parameters are passed to a function, the value of every parameter is
evaluated before being passed to the function.
88.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.
88.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
Answer: (A)
Explanation: In C, function parameters are always passed by value. Pass-by-reference is
simulated in C by explicitly passing pointer values.
89.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
Answer: (B)
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.
90.
#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
Answer: (A)
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,
};
91.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
Answer: (C)
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 ");
}
92.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
Answer: (A)
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.
93.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
Answer: (B)
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.
94.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.
Answer: (B)
Explanation: extern keyword is used for global variables. Functions are global
anyways, so adding extern doesn’t add anything.
95.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
Answer: (C)
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.
96.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.
Answer: (B)
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.
97.
#include <stdio.h>
#include <stdarg.h>
int fun(int n, ...)
{
int i, j = 1, val = 0;
va_list p;
va_start(p, n);
for (; j < n; ++j)
{
i = va_arg(p, int);
val += i;
}
va_end(p);
return val;
}
int main()
{
printf("%d\n", fun(4, 1, 2, 3));
return 0;
}
(A) 3
(B) 5
(C) 6
(D) 10
Answer: (C)
Explanation: The function receives variable number of arguments as there are three
dots after first argument. The firs argument is count of all arguments including
first. The function mainly returns sum of all remaining arguments.
See https://www.geeksforgeeks.org/how-to-count-variable-numbers-of-arguments-i
n-c for details
98.What’s going to happen when we compile and run the following C program snippet?
#include "stdio.h"
int main()
{
int a = 10;
int b = 15;
printf("=%d",(a+1),(b=a+2));
printf(" %d=",b);
return 0;
}
(A) =11 15=
(B) =11 12=
(C) Compiler Error due to (b=a+2) in the first printf().
(D) No compile error but output would be =11 X= where X would depend on compiler
implementation.
Answer: (B)
Explanation: As per C standard C11, all the arguments of printf() are evaluated irrespective of
whether they get printed or not. That’s why (b=a+2) would also be evaluated and value of b
would be 12 after first printf(). That’s why correct answer is B.
99.Assuming int size is 4 bytes, what is going to happen when we compile and run the following
program?
#include “stdio.h”
int main()
{
printf(“GeeksQuiz\n”);
main();
return 0;
}
(A) We can’t use main() inside main() and compiler will catch it by showing compiler error.
(B) GeeksQuiz would be printed in 2147483647 times i.e. (2 to the power 31) – 1.
(C) It’ll print GeeksQuiz infinite times i.e. the program will continue to run forever until it’s
terminated by other means such as CTRL+C or CTRL+Z etc.
(D) GeeksQuiz would be printed only once. Because when main() is used inside main(), it’s
ignored by compiler at run time. This is to make sure that main() is called only once.
(E) GeeksQuiz would be printed until stack overflow happens for this program.
Answer: (E)
Explanation: First of all, there’s no restriction of main() calling main() i.e. recursion can happen
for main() as well. But there’s no explicit termination condition mentioned here for this recursion.
So main() would be calling main() after printing GeeksQuiz. This will go on until Stack of the
program would be filled completely. Please note that stack (internal to a running program) stores
the calling function sequence i.e. which function has called which function so that the control can
be returned when called function returns. That’s why here in program, main() would continue to
call main() until complete stack is over i.e. stack-overflow occurs.
100.Both of the following declarations for function pointers are equivalent. Second one (i.e. with
typedef) looks cleaner.
/* First Declaration */
int (*funPtr1)(int), (*funPtr2)(int);
/* Second Declaration*/
typedef int (*funPtr)(int);
funPtr funPtr1, funPtr2;
(A) TRUE
(B) FALSE
Answer: (A)
Explanation: Usually data type of function pointers tends to be cryptic and that’s why it’s used in
conjunction with typedef. Think of a function pointer which is pointing to a function that accepts
a function pointer and that returns a function pointer. This can be used simplified using typedef
otherwise it’s going to very difficult to read/understand!
101.Pick the best statement for the following program.
#include "stdio.h"
int foo(int a)
{
printf("%d",a);
return 0;
}
int main()
{
foo;
return 0;
}
(A) It’ll result in compile error because foo is used without parentheses.
(B) foo to be executed with output “garbage integer”.
(C) No compile error but foo function wouldn’t be executed. The program wouldn’t print
anything.
(D) No compile error and ZERO (i.e. 0) would be passed to foo function. This would make foo to
be executed with output 0.
Answer: (C)
Explanation: In C, if a function name is used without parentheses, the reference to the function
name simply generates a pointer to the function, which is then discarded. So the above program
would compile but won’t print anything.
ISRO
102.Consider the function
int fun(x: integer)
{
If x > 100 then fun = x – 10;
else
fun = fun(fun(x + 11));
}
For the input x = 95, the function will return
(A) 89
(B) 90
(C) 91
(D) 92
Answer: (C)
Explanation: f(95)–>f(f(106)) = f(96)–>f(f(107)) = f(97)–>f(f(108)) = f(98)–>f(f(109)) =
f(99)–>f(f(110)) = f(100)–>f(f(111)) = f(101) = 91
So the correct option is (C)
103.Consider the function
int func(int num) {
int count = 0;
while(num) {
count++;
num >>= 1;
}
return(count) ;
}
For func(435) the value returned is
(A) 9
(B) 8
(C) 0
(D) 10
Answer: (A)
Explanation: For detailed solution refer GATE-CS-2014-(Set-2)
So option (A) is correct.
104.Consider the following C function
void swap ( int x, int y )
{
int tmp;
tmp = x;
x= y;
y = tmp;
}
In order to exchange the values of two variables a and b:
(A) Call swap (a, b)
(B) Call swap (&a, &b)
(C) swap(a, b) cannot be used as it does not return any value
(D) swap(a, b) cannot be used as the parameters passed by value
Answer: (D)
Explanation: The code will not work because the parameters are passed by value. In order
to swap the values of x and y the parameters should be passed with reference.The correct
code is:
void swap ( int &x, int &y )
{
int tmp;
tmp = x;
x= y;
y = tmp;
}