You are on page 1of 8

C++ Functions & Strings Quiz

1. What is the only function all C++ programs must contain?


A. start()
B. system()
C. main()
D. program()
Answer: c

2. In a C program, following variables are defined:


float x = 2.17;
double y = 2.17;
long double z = 2.17;
Which of the following is correct way for printing these variables via printf.
(A) printf(“%f %lf %Lf”,x,y,z);
(B) printf(“%f %f %f”,x,y,z);
(C) printf(“%f %ff %fff”,x,y,z);
(D) printf(“%f %lf %llf”,x,y,z);

Answer: (A)
Explanation: In C language, float, double and long double are called real data types. For “float”, “double”
and “long double”, the right format specifiers are %f, %lf and %Lf from the above options. It should be
noted that C standard has specified other format specifiers as well for real types which are %g, %e etc.

3. Which is not a proper function prototype?


A. int funct(char x, char y);
B. double funct(char x)
C. void funct();
D. char x();

Answer: (B)
4. What is the return type of the function with prototype: "int func(char x, float v, double t);"
A. char
B. int
C. float
D. double

Answer: (B)

5. Which of the following is a valid function call (assuming the function exists)?
A. funct;
B. funct x, y;
C. funct();
D. int funct();

Answer: (C)

6. What character ends all strings?


A. '.'
B. ' '
C. '\0'
D. '\n'

Answer: (C)

7. Which of the following reads in a string named x with one hundred characters?
A. cin.getline(x, 100, '\n');
B. cin.getline(100, x, '\n');
C. readline(x, 100, '\n');
D. read(x);

Answer: (A)

8. Which of the following functions compares two strings?


A. compare();
B. stringcompare();
C. cmp();
D. strcmp();
Answer: (D)
9. Which of the following adds one string to the end of another?
A. append();
B. stringadd();
C. strcat();
D. stradd();

Answer: (C)

10. 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)
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.

11. 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)
In C, function parameters are always passed by value. Pass-by-reference is simulated in C by explicitly
passing pointer values.

12. 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)
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.

13. Output of following program?


#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: 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,
};

14. What is the output of following program?


# include <stdio.h>
int main()
{
char str1[] = "GeeksQuiz";
char str2[] = {'G', 'e', 'e', 'k', 's', 'Q', 'u', 'i', 'z'};
int n1 = sizeof(str1)/sizeof(str1[0]);
int n2 = sizeof(str2)/sizeof(str2[0]);
printf("n1 = %d, n2 = %d", n1, n2);
return 0;
}

(A) n1 = 10, n2 = 9

(B) n1 = 10, n2 = 10

(C) n1 = 9, n2 = 9

(D) n1 = 9, n2 = 10

Answer: (A)
The size of str1 is 10 and size of str2 9.

When an array is initialized with string in double quotes, compiler adds a ‘\0’ at the end.

15. What does the following fragment of C-program print?


char c[] = "GATE2011";
char *p =c;
printf("%s", p + p[3] - p[1]) ;

(A) GATE2011
(B) E2011
(C) 2011
(D) 011

Answer: (C)
See comments for explanation.

char c[] = "GATE2011";

// p now has the base address string "GATE2011"


char *p = c;

// p[3] is 'E' and p[1] is 'A'.


// p[3] - p[1] = ASCII value of 'E' - ASCII value of 'A' = 4
// So the expression p + p[3] - p[1] becomes p + 4 which is
// base address of string "2011"
printf("%s", p + p[3] - p[1]); // prints 2011

16. 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)
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.

17. 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)
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.

18. In below program, what would you put in place of “?” to print “Quiz”?
#include <stdio.h>
int main()
{
char arr[] = "GeeksQuiz";
printf("%s", ?);
return 0;
}

(A) arr
(B) (arr+5)
(C) (arr+4)
(D) Not possible

Answer: (B)
Since %s is used, the printf statement will print everything starting from arr+5 until it finds ‘\0’

19. What is the output of this program?

#include <iostream>
using namespace std;
void mani()
void mani()
{
cout<<"hai";
}
int main()
{
mani();
return 0;
}

a) hai
b) haihai
c) compile time error
d) none of the mentioned

Answer: c
We have to use the semicolon to declare the function in line 3. This is called a function declaration and a
function declaration ends with a semicolon.
20. 7. What is the output of this program?

#include <iostream>
using namespace std;
void fun(int x, int y)
{
x = 20;
y = 10;
}
int main()
{
int x = 10;
fun(x, x);
cout << x;
return 0;
}

a) 10
b) 20
c) compile time error
d) none of the mentioned

Answer: a
In this program, we called by value so the value will not be changed, So the output is 10

You might also like