C Functions
C Functions
int main()
{
void foo(), f();
f();
}
void foo()
{
printf("2 ");
}
void f()
{
printf("1 ");
foo();
}
B. 1 2
C. 2 1
Answer: Option B
int main()
{
void foo();
void f()
{
foo();
}
f();
}
void foo()
{
printf("2 ");
}
A. 2 2
B. 2
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.
void foo();
int main()
{
void foo();
foo();
return 0;
}
void foo()
{
printf("2 ");
}
B. 2
Answer: Option B
void foo();
int main()
{
void foo(int);
foo(1);
return 0;
}
void foo(int i)
{
printf("2 ");
}
A. 2
Answer: Option A
void foo();
int main()
{
void foo(int);
foo();
return 0;
}
void foo()
{
printf("2 ");
}
A. 2
Answer: Option B
6. What is the output of this C code?
void m()
{
printf("hi");
}
void main()
{
m();
}
A. hi
C. Nothing
D. Varies
Answer: Option A
void m();
void n()
{
m();
}
void main()
{
void m()
{
printf("hi");
}
}
A. hi
C. Nothing
D. Varies
Answer: Option B
void main()
{
m();
void m()
{
printf("hi");
}
}
A. hi
C. Nothing
D. Varies
Answer: Option B
void main()
{
m();
}
void m()
{
printf("hi");
m();
}
C. Infinite hi
D. Nothing
Answer: Option C
void main()
{
static int x = 3;
x++;
if (x <= 5)
{
printf("hi");
main();
}
}
B. hi
C. infinite hi
D. hi hi
Answer: Option D
Answer: Option A
12. Which of the following function declaration is illegal?
A. int 1bhk(int);
Answer: Option D
13. Which function definition will run correctly?
C. int sum(a, b)
return (a + b);
Answer: Option B
14. Can we use a function as a parameter of another function? [ Eg: void wow(int
func()) ]
B. Yes, but we call the function again to get the value, not as convenient as in
using variable
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
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}
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
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
Answer: Option B
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
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
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
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
void m(int k)
{
printf("hi");
}
void m(double k)
{
printf("hello");
}
void main()
{
m(3);
}
A. hi
B. hello
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
int foo();
int main()
{
int i = foo();
}
foo()
{
printf("2 ");
return 2;
}
A. 2
Answer: Option A
double foo();
int main()
{
foo();
return 0;
}
foo()
{
printf("2 ");
return 2;
}
A. 2
Answer: Option B
A. true
B. false
Answer: Option A
A. true
B. false
Answer: Option A
31. What is the output of code given below?
B. 0
Answer: Option A
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
int x;
void main()
{
printf("%d", x);
}
A. Junk value
C. 0
D. Undefined
Answer: Option C
int x = 5;
void main()
{
int x = 3;
printf("%d", x);
{
x = 4;
}
printf("%d", x);
}
C. 3 5
D. 3 4
Answer: Option D
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
Answer: Option A
A. Internal
B. External
Answer: Option B
37. Global variables are:
A. Internal
B. External
Answer: Option B
38.
Which of the following are an external variable?
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
Answer: Option D
double var = 8;
int main()
{
int var = 5;
printf("%d", var);
}
A. 5
B. 8
Answer: Option A
double i;
int main()
{
printf("%g\n",i);
return 0;
}
A. 0
B. 0.000000
C. Garbage value
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
int i;
int main()
{
printf("%d\n", i);
}
A. 0
B. false
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
int *i;
int main()
{
if (i == NULL)
printf("true\n");
return 0;
}
A. true
D. Nothing
Answer: Option A
int *i;
int main()
{
if (i == 0)
printf("true\n");
return 0;
}
A. true
D. Nothing
Answer: Option B
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
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
Answer: Option A
static int x;
void main()
{
int x;
printf("x is %d", x);
}
A. 0
B. Junk value
D. Nothing
Answer: Option B
void main()
{
static double x;
int x;
printf("x is %d", x);
}
A. 0
B. Nothing
D. Junk value
Answer: Option C
void main()
{
static int x;
if (x++ < 2)
main();
}
C. Varies
Answer: Option D
Answer: Option C
A. Variables
B. Functions
C. Structures
Answer: Option D
Answer: Option D
A. stack
B. queue
C. priority queue
D. random
Answer: Option A
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
Answer: Option A
A. stack
B. data segment
C. register
D. heap
Answer: Option A
A. Internal linkage
B. External linkage
C. No linkage
Answer: Option C
int main()
{
auto i = 10;
const auto int *p = &i;
printf("%d\n", i);
}
A. 10
Answer: Option A
Answer: Option A
D. Only 1
Answer: Option C
A. heap
B. Data segment
C. Code segment
D. stack
Answer: Option D
void main()
{
int x;
}
here x is?
A. automatic variable
B. static variable
C. global variable
D. register variable
Answer: Option A
A. 0
B. Junk value
C. Nothing
Answer: Option B
68. Which of the following storage class supports char data type?
A. register
B. static
C. auto
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
int x;
void main()
{
printf("%d", x);
}
A. Junk value
C. 0
D. Undefined
Answer: Option C
int x = 5;
void main()
{
int x = 3;
printf("%d", x);
{
x = 4;
}
printf("%d", x);
}
B. 3 3
C. 3 5
D. 3 4
Answer: Option D
B. From the point of declaration to the end of the file in which it is defined
D. From the point of declaration to the end of the file being compiled
Answer: Option D
B. From the point of declaration to the end of the file in which it is defined
D. From the point of declaration to the end of the file being compiled
Answer: Option D
int b;
int main()
{
int c;
return 0;
}
int a;
A. a
B. b
C. c
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
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
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
static int x;
void main()
{
int x;
printf("x is %d", x);
}
A. 0
B. Junk value
D. Nothing
Answer: Option B
void main()
{
static double x;
int x;
printf("x is %d", x);
}
A. Nothing
B. 0
D. Junk value
Answer: Option C
void main()
{
static int x;
if (x++ < 2)
main();
}
C. Varies
Answer: Option D
Answer: Option B
83. Assignment statements assigning value to local static variables are executed only
once?
A. true
B. false
Answer: Option B
A. %s
B. %d
C. %f
Answer: Option B
A. true
B. false
Answer: Option B
A. YES
B. NO
C. Depends on the compiler
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.
Answer: (A)
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;
}
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;
}
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 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 ");
}
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)
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.
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
Answer: (C)
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
{
If x > 100 then fun = x – 10;
else
fun = fun(fun(x + 11));
}
Answer: (C)
Answer: (A)
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: