9.functions - 41
9.functions - 41
A 7 6 5
B 5 6 7
C 7 7 7
D Compiler Dependent
ANS :-D
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
ANS :- A
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
ANS :- B
Question 4
#include <stdio.h>
int main()
{
printf("%d", main);
return 0;
}
ANS :- A
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;
}
ANS :- C
Question 6
Output of following program?
#include<stdio.h>
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
ANS :- A
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
ANS :- B
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.
ANS :- B
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);
}
ANS :- C
Question 10
In C, what is the meaning of following function prototype with empty
parameter list
void fun()
{
/* .... */
}
ANS :- B
Question 11
#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("%dn", fun(4, 1, 2, 3));
return 0;
}
A 3
B 5
C 6
D 10
ANS :- C
Question 12
Consider the following C-program:
void foo(int n, int sum)
{
int k = 0, j = 0;
if (n == 0) return;
k = n % 10;
j = n / 10;
sum = sum + k;
foo (j, sum);
printf ("%d,", k);
}
int main ()
{
int a = 2048, sum = 0;
foo (a, sum);
printf ("%dn", sum);
getchar();
}
A 8, 4, 0, 2, 14
B 8, 4, 0, 2, 0
C 2, 0, 4, 8, 14
D 2, 0, 4, 8, 0
ANS :- D
Question 13
Consider the following C-program:
double foo (double); /* Line 1 */
int main()
{
double da, db;
// input da
db = foo(da);
double foo(double a)
{
return a;
}
The above code compiled without any error or warning. If Line 1 is deleted,
the above code will show:
ANS :- D
Question 14
Consider the following C function
void swap (int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}
ANS :- D
Question 15
The value of j at the end of the execution of the following C program.
int incr(int i)
{
static int count = 0;
count = count + i;
return (count);
}
main()
{
int i,j;
for (i = 0; i <=4; i++)
j = incr(i);
}
A 10
B 4
C 6
D 7
ANS :- A
Question 16
The output of the following C program is __________.
void f1 (int a, int b)
{
int c;
c=a; a=b; b=c;
}
void f2 (int *a, int *b)
{
int c;
c=*a; *a=*b;*b=c;
}
int main()
{
int a=4, b=5, c=6;
f1(a, b);
f2(&b, &c);
printf (�%d�, c-a-b);
return 0;
}
A -5
B -4
C 5
D 3
ANS :- A
Question 17
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.
ANS :- B
Question 18
What�s going to happen when we compile and run the following C program
snippet?
#include "stdio.h"
int main()
{
int a = 10;
printf("=%d %d=",(a+1));
return 0;
}
A =11 0=
B =11 X= where X would depend on Compiler implementation
C Undefined behaviour
D Compiler Error due to missing argument for second %d
ANS :- C
Question 19
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(�GeeksQuizn�);
main();
return 0;
}
ANS :- E
Question 20
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
ANS :- A
Question 21
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;
}
ANS :- C
Question 22
What is the output of the following program?
#include <stdio.h>
int funcf (int x);
int funcg (int y);
main()
{
int x = 5, y = 10, count;
for (count = 1; count <= 2; ++count)
{
y += funcf(x) + funcg(x);
printf ("%d ", y);
}
}
funcf(int x)
{
int y;
y = funcg(x);
return (y);
}
funcg(int x)
{
static int y = 10;
y += 1;
return (y+x);
}
A 43 80
B 42 74
C 33 37
D 32 32
ANS :- A
Question 23
What is the output printed by the following program?
#include<stdio.h>
int f(int n, int k)
{
if (n == 0)
return 0;
else if (n % 2)
return f(n/2, 2*k) + k;
else return f(n/2, 2*k) - k;
}
int main ()
{
printf("%d", f(20, 1));
return 0;
}
A 5
B 8
C 9
D 20
ANS :- C
Question 24
Consider the following C program.
void f(int, short);
void main()
{
int i = 100;
short s = 12;
short *p = &s;
__________ ; // call to f()
}
Which one of the following expressions, when placed in the blank above, will
NOT result in a type checking error?
A f(s, *s)
B i = f(i,s)
C f(i,*s)
D f(i,*p)
ANS :- D
Question 25
Consider the following program:
int f(int *p, int n)
{
if (n <= 1) return 0;
else return max(f(p+1,n-1),p[0]-p[1]);
}
int main()
{
int a[] = {3,5,2,6,4};
printf("%d", f(a,5));
}
Note: max(x,y) returns the maximum of x and y. The value printed by this
program is
A 2
B 3
C 4
D 5
ANS :- B
Question 26
Given a boolean function f (x1, x2, ..., xn), which of the following
equations is NOT true
A f (x1, x2, ..., xn) = x1'f(x1, x2, ..., xn) + x1f(x1, x2, ..., xn)
B f (x1, x2, ..., xn) = x2f(x1, x2, �, xn) + x2'f(x1, x2, �,xn)
C f (x1, x2, ..., xn) = xn'f(x1, x2, �, 0) + xnf(x1, x2, �,1)
D f (x1, x2, ..., xn) = f(0, x2, �, xn) + f(1, x2, .., xn)
ANS :- D
Question 27
Consider the following C-program :
int fun()
{
static int num = 16;
return num--;
}
int main()
{
for(fun(); fun(); fun())
printf("%d ", fun());
return 0;
}
A Infinite loop
B 13 10 7 4 1
C 15 12 8 5 2
D 14 11 8 5 2
ANS :- D
Question 28
Consider the following program written in pseudo-code. Assume that x and y
are integers.
Count (x, y) {
if (y !=1 ) {
if (x !=1) {
print("*");
Count (x/2, y);
}
else {
y=y-1;
Count (1024, y);
}
}
}
The number of times that the print statement is executed by the call
Count(1024, 1024) is _______ . Note -This was Numerical Type question.
A 10230
B 10
C 1023
D 23010
ANS :- A
Question 29
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
ANS :- C
Question 30
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
ANS :- A
Question 31
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:
ANS :- D
Question 32
Which one of the following is correct for overloaded functions in C++ ?
ANS :- A
Question 33
Which of the following is/are side effects of scan conversion ? a. Aliasing
b. Unequal intensity of diagonal lines c. Overstriking in photographic applications
d. Local or Global aliasing
A a and b
B a, b and c
C a, c and d
D a, b, c and d
ANS :- D
Question 34
Consider the following C code. #include
#include
void main()
{
double pi = 3.1415926535;
int a = 1;
int i;
for(i=0; i < 3; i++)
if(a = cos(pi * i/2) )
printf("%d ",1);
else printf("%d ", 0);
}
ANS :- C
Question 35
Consider the following C function:
int f(int n)
{
static int i = 1;
if(n >= 5) return n;
n = n+i;
i++;
return f(n);
}
The value returned by f(1) is
A 5
B 6
C 7
D 8
ANS :- C
Question 36
Consider the following C++ program'
int a (int m)
{return ++m;}
int b(int&m)
{return ++m;}
int c(char &m)
{return ++m;}
void main 0
{
int p = 0, q = 0, r = 0;
p += a(b(p));
q += b(a(q));
r += a(c(r));
cout<
Assuming the required header files are already included, the above program:
ANS :- A
Question 37
What is the output of the following program ?
main (){
int x = 2, y = 5;
if(x < y) return (x = x + y);
else printf ("z1");
printf("z2");
A z2
B z1z2
C Compilation error
D None of these
ANS :- D
Question 38
Consider the following program
main()
{
int x = 1;
printf ("%d", (*char(char *)&x)) ;
}
Assuming required header files are included and if the machine in which this
program is executed is little-endian, then the output will be
A 0
B 99999999
C 1
D unpredictable
ANS :- D
Question 39
Consider the following C program
#include
main()
{
float sum = 0.0, j =1.0, i = 2.0;
while(i/j > 0.001) {
j = j + 1;
sum = sum + i/j;
printf ( "%fn", sum );
}
}
How many lines of output does this program produce?
ANS :-D
Question 40
Given below are three implementations of the swap( ) function in C++:
(a)
void swap (int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}
int main( )
{
int p = 0, q = 1;
swap (p, q);
}
(b)
void swap (int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
int main( )
{
int p = 0, q = 1;
swap (p, q);
}
(c)
void swap (int * a, int * b)
{
int * temp;
temp = a;
a = b;
b = temp;
}
int main( )
{
int p = 0, q = 1;
swap (&p, &q);
}
Which of these would actually swap the contents of the two integer variables
p and q?
A (a) only
B (b) only
C (c) only
D (b) and (c) only
ANS :- B
Question 41
Consider a Boolean function of �n� variables. The order of an algorithm that
determines whether the Boolean function produces a output 1 is
A Logarithmic
B Linear
C Quadratic
D Exponential
ANS :- D