100% found this document useful (1 vote)
42 views15 pages

9.functions - 41

The document contains a series of multiple-choice questions related to C programming, covering topics such as function behavior, pointers, variable scope, and output predictions. Each question is followed by four answer options, with the correct answer indicated. The questions test knowledge of C syntax, semantics, and programming concepts.

Uploaded by

harshacsds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
42 views15 pages

9.functions - 41

The document contains a series of multiple-choice questions related to C programming, covering topics such as function behavior, pointers, variable scope, and output predictions. Each question is followed by four answer options, with the correct answer indicated. The questions test knowledge of C syntax, semantics, and programming concepts.

Uploaded by

harshacsds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 15

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

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;
}

A Address of main function


B Compiler Error
C Runtime Error
D Some random value

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;
}

A GeeksQuiz GeeksQuiz GeeksQuiz


B GeeksQuiz GeeksQuiz
C Compiler Error
D Runtime Error

ANS :- C

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

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);
}

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

ANS :- C

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.

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();
}

What does the above program print?

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:

A no compile warning or error


B some compiler-warnings not leading to unintended results
C some compiler-warnings due to type-mismatch eventually leading to
unintended results
D compiler errors

ANS :- D

Question 14
Consider the following C function
void swap (int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}

In order to exchange the values of two variables x and y.

A Call swap (x, y)


B Call swap (&x, &y)
C swap(x,y) cannot be used as it does not return any value
D swap(x,y) cannot be used as the parameters are passed by value

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;
}

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.

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;
}

A It�ll result in compile error because foo is used without


parentheses.
B No compile error and some garbage value would be passed to foo
function. This would make 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.

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;
}

What is output of above program?

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:

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

ANS :- D

Question 32
Which one of the following is correct for overloaded functions in C++ ?

A Compiler sets up a separate function for every definition of


function.
B Compiler does not set up a separate function for every definition of
function.
C Overloaded functions cannot handle different types of objects.
D Overloaded functions cannot have same number of arguments.

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);
}

What would the program print?


A 000
B 010
C 101
D 111

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:

A results in compilation error


B print 123
C print 111
D print 322

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?

A 0-9 lines of output


B 10-19 lines of output
C 20-29 lines of output
D More than 29 lines of output

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

You might also like