0% found this document useful (0 votes)
502 views69 pages

Functions, Namespaces & Exceptions in C++

The document provides information about C++ language interview questions and answers related to various topics like operators, statements, comments and indentation. It includes 10 questions for each topic along with their answers, explanations and code examples. The questions aim to help individuals practicing C++ programming improve their skills for interviews, placements and competitive exams. The detailed explanations of answers help in better understanding of C++ concepts.

Uploaded by

kibrom atsbha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
502 views69 pages

Functions, Namespaces & Exceptions in C++

The document provides information about C++ language interview questions and answers related to various topics like operators, statements, comments and indentation. It includes 10 questions for each topic along with their answers, explanations and code examples. The questions aim to help individuals practicing C++ programming improve their skills for interviews, placements and competitive exams. The detailed explanations of answers help in better understanding of C++ concepts.

Uploaded by

kibrom atsbha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 69

2.

Functions, Namespaces & Exceptions in C++

C++ Programming Questions and Answers – Operators

This section on C++ language interview questions and answers focuses on “Operators”. One shall
practice these interview questions to improve their C++ programming skills needed for various
interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams
and other competitive exams. These questions can be attempted by anyone focusing on learning
C++ programming language. They can be a beginner, fresher, engineering graduate or an
experienced IT professional. Our C++ language interview questions come with detailed explanation
of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ language interview questions on “Operators” along with answers,
explanations and/or solutions:

1. Which operator is having right to left associativity in the following?


a) Array subscripting
b) Function call
c) Addition and subtraction
d) Type cast
View Answer

Answer: d
Explanation: None.

2. Which operator is having the highest precedence?


a) postfix
b) unary
c) shift
d) equality
View Answer

Answer: a
Explanation: The operator which is having highest precedence is postfix and lowest is equality.

3. What is this operator called ?: ?


a) conditional
b) relational
c) casting operator
d) none of the mentioned
View Answer

Answer: a
Explanation: In this operator, if the condition is true means, it will return the first operator, otherwise
second operator.

4. What is the output of this program?

#include <iostream>
using namespace std;

int main()

int a;

a = 5 + 3 * 5;

cout << a;

return 0;

a) 35
b) 20
c) 25
d) 30
View Answer

Answer: b
Explanation: Because the * operator is having highest precedence, So it is executed first and then
the + operator will be executed.
Output:
$ g++ op1.cpp
$ a.out
20

5. What is the use of dynamic_cast operator?


a) it converts virtual base class to derived class
b) it converts virtual base object to derived objeccts
c) it will convert the operator based on precedence
d) none of the mentioned
View Answer

Answer: a
Explanation: Because the dynamic_cast operator is used to convert from base class to derived
class.

6. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int a = 5, b = 6, c, d;
c = a, b;

d = (a, b);

cout << c << ' ' << d;

return 0;

a) 5 6
b) 6 5
c) 6 7
d) none of the mentioned
View Answer

Answer: a
Explanation: It is a separtor here.In c,the value a is stored in c and in d the value b is stored in d
because of the bracket.
Output:
$ g++ op3.cpp
$ a.out
56

7. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int i, j;

j = 10;

i = (j++, j + 100, 999 + j);

cout << i;

return 0;

a) 1000
b) 11
c) 1010
d) 1001
View Answer

Answer: c
Explanation: j starts with the value 10. j is then incremented to 11. Next, j is added to 100. Finally, j
(still containing 11) is added to 999 which yields the result 1010.
Output:
$ g++ op2.cpp
$ a.out
1010

8. What is the output of this program?

#include <iostream>

using namespace std;

int main ()

int x, y;

x = 5;

y = ++x * ++x;

cout << x << y;

x = 5;

y = x++ * ++x;

cout << x << y;

return 0;

a) 749736
b) 736749
c) 367497
d) none of the mentioned
View Answer

Answer: a
Explanation: Because of the precedence the pre-increment and post increment operator, we got the
output as 749736.
Output:
$ g++ op.cpp
$ a.out
749736

9. What is the output of this program?

#include <iostream>

using namespace std;


int main()

int a = 5, b = 6, c;

c = (a > b) ? a : b;

cout << c;

return 0;

a) 6
b) 5
c) 4
d) 7
View Answer

Answer: a
Explanation: Here the condition is false on conditional operator, so the b value is assigned to c.
Output:
$ g++ op1.cpp
$ a.out
6

10. What is the output of this program?

#include <iostream>

using namespace std;

main()

double a = 21.09399;

float b = 10.20;

int c ,d;

c = (int) a;

d = (int) b;

cout << c <<' '<< d;

return 0;

a) 20 10
b) 10 21
c) 21 10
d) none of the mentioned
View Answer

Answer: c
Explanation: In this program, we are casting the operator to integer, So it is printing as 21 and 10.
Output:
$ g++ op5.cpp
$ a.out
21 10

C++ Programming Questions and Answers – Statements

This section on online C++ test focuses on “Statements”. One shall practice these test questions to
improve their C++ programming skills needed for various interviews (campus interviews, walkin
interviews, company interviews), placements, entrance exams and other competitive exams. These
questions can be attempted by anyone focusing on learning C++ programming language. They can
be a beginner, fresher, engineering graduate or an experienced IT professional. Our online C++ test
questions come with detailed explanation of the answers which helps in better understanding of C++
concepts.

Here is a listing of online C++ test questions on “Statements” along with answers, explanations
and/or solutions:

1. How many sequence of statements are present in c++?


a) 4
b) 3
c) 5
d) 6
View Answer

Answer: c
Explanation: There are five sequence of statements. They are Preprocessor directives, Comments,
Declarations, Function Declarations, Executable statements.

2. The if..else statement can be replaced by which operator?


a) Bitwise operator
b) Conditional operator
c) Multiplicative operator
d) None of the mentioned
View Answer

Answer: b
Explanation: In the conditional operator,it will predicate the output using the given condition.

3. The switch statement is also called as?


a) choosing structure
b) selective structure
c) certain structure
d) none of the mentioned
View Answer

Answer: b
Explanation: The switch statement is used to choose the certain code to execute, So it is also called
as selective structure.

4. The destination statement for the goto label is identified by what label?
a) $
b) @
c) *
d) :
View Answer

Answer: d
Explanation: None.

5. What is the output of this program?

#include <iostream>

using namespace std;

int main ()

int n;

for (n = 5; n > 0; n--)

cout << n;

if (n == 3)

break;

return 0;

a) 543
b) 54
c) 5432
d) 53
View Answer

Answer: a
Explanation: Inthis program, We are printing the numbers in reverse order but by using break
statement we stopped printing on 3.
Output:
$ g++ stat.cpp
$ a.out
543

6. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int a = 10;

if (a < 15)

time:

cout << a;

goto time;

break;

return 0;

a) 1010
b) 10
c) infinitely print 10
d) compile time error
View Answer

Answer: d
Explanation: Because the break statement need to be presented inside a loop or a switch statement.

7. What is the output of this program?

#include <iostream>

using namespace std;

int main()

{
int n = 15;

for ( ; ;)

cout << n;

return 0;

a) error
b) 15
c) infinite times of printing n
d) none of the mentioned
View Answer

Answer: c
Explanation: There is not a condition in the for loop, So it will loop continuously.

8. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int i;

for (i = 0; i < 10; i++);

cout << i;

return 0;

a) 0123456789
b) 10
c) 012345678910
d) compile time error
View Answer

Answer: b
Explanation: for loop with a semicolon is called as body less for loop. It is used only for incrementing
the variable values. So in this program the value is incremented and printed as 10.
Output:
$ g++ stat2.cpp
$ a.out
10

9. How many types of loops are there?


a) 4
b) 2
c) 3
d) 1
View Answer

Answer: a
Explanation: There are four types of loop. They are while, do while, nested, for loop.

10. Which looping process is best used when the number of iterations is known?
a) for
b) while
c) do-while
d) all looping processes require that the iterations be known
View Answer

Answer: a
Explanation: None.

C++ Programming Questions and Answers – Comments and Indentation

This section on C++ interview questions and answers focuses on “Comments and Indentation”. One
shall practice these interview questions to improve their C++ programming skills needed for various
interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams
and other competitive exams. These questions can be attempted by anyone focusing on learning
C++ programming language. They can be a beginner, fresher, engineering graduate or an
experienced IT professional. Our C++ interview questions come with detailed explanation of the
answers which helps in better understanding of C++ concepts.

Here is a listing of C++ interview questions on “Comments and Indentation” along with answers,
explanations and/or solutions:

1. How many types of comments are there in c++?


a) 1
b) 2
c) 3
d) 4
View Answer

Answer: b
Explanation: There are two types of comments. They are double slash and slash stared.

2. What is a comment in c++?


a) comments are parts of the source code disregarded by the compiler
b) comments are executed by compiler to find the meaning of the comment
c) comments are executable
d) none of the mentioned
View Answer

Answer: a
Explanation: Comments are used to add meaning to the program.

3. What type of comments does c++ support?


a) single line
b) multi line
c) single line and multi line
d) none of the mentioned
View Answer

Answer: c
Explanation: None.

4. What is the output of this program?

#include <iostream>

using namespace std;

int main()

/* this is comment*

cout << "hello world";

return 0;

a) hello world
b) hello
c) compile time error
d) none of the mentioned
View Answer

Answer: c
Explanation: Because the slash should need to be forward not backward.

5. What is used to write multi line comment in c++?


a) /* …. */
b) /$ …. $/
c) //
d) none of the mentioned
View Answer
Answer: a
Explanation: The /* is used to write the multi line comment.

6. What is the use of the indentation in c++?


a) distinguishes between comments and code
b) r distinguishes between comments and outer data
c) both a and b
d) none of the mentioned
View Answer

Answer: a
Explanation: None.

7. What is the output of this program?

#include <iostream>

using namespace std;

long factorial (long a)

if (a > 1)

return (a * factorial (a + 1));

else

return (1);

int main ()

long num = 3;

cout << num << "! = " << factorial ( num );

return 0;

a) 6
b) 24
c) segmentation fault
d) compile time error
View Answer

Answer: c
Explanation: As we have given in the function as a+1, it will exceed the size and so it arises the
segmentation fault.
Output:
$ g++ arg3.cpp
$ a.out
segmentation fault

8. What is the output of this program?

#include <iostream>

using namespace std;

void square (int *x)

*x = (*x + 1) * (*x);

int main ( )

int num = 10;

square(&num);

cout << num;

return 0;

a) 100
b) compile time error
c) 144
d) 110
View Answer

Answer: d
Explanation: We have increased the x value in operand as x + 1, so it will return as 110.
Output:
$ g++ arg2.cpp
$ a.out
110

9. What is the output of this program?

#include <iostream>

using namespace std;

int add(int a, int b);


int main()

int i = 5, j = 6;

cout << add(i, j) << endl;

return 0;

int add(int a, int b )

int sum = a + b;

a = 7;

return a + b;

a) 11
b) 12
c) 13
d) compile time error
View Answer

Answer: c
Explanation: The value of a has been changed to 7, So it returns as 13.
Output:
$ g++ arg1.cpp
$ a.out
13

10. What will happen when we use void in argument passing?


a) It will not return value to its caller
b) It will return value to its caller
c) Maybe or maynot be return value to its caller
d) None of the mentioned
View Answer

Answer: a
Explanation: As void is not having any return value, it will not return the value to the caller.

11. What is the output of this program?

#include <iostream>

using namespace std;

void Sum(int a, int b, int & c)


{

a = b + c;

b = a + c;

c = a + b;

int main()

int x = 2, y =3;

Sum(x, y, y);

cout << x << " " << y;

return 0;

a) 2 3
b) 6 9
c) 2 15
d) compile time error
View Answer

Answer: c
Explanation: We have passed three values and it will manipulate according to the given condition
and yield the result as 2 15.
Output:
$ g++ arg.cpp
$ a.out
2 15

C++ Programming Questions and Answers – Function Declarations


This section on tough C++ programming questions focuses on “Function Declarations”. One shall
practice these questions to improve their C++ programming skills needed for various interviews (campus
interviews, walkin interviews, company interviews), placements, entrance exams and other competitive
exams. These questions can be attempted by anyone focusing on learning C++ programming language.
They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++
programming questions come with detailed explanation of the answers which helps in better
understanding of C++ concepts.

Here is a listing of tough C++ programming questions on “Function Declarations” along with answers,
explanations and/or solutions:
1. Where does the execution of the program starts?
a) user-defined function
b) main function
c) void function
d) none of the mentioned
View Answer

Answer: b
Explanation: Normally the execution of the program in c++ starts from main only.

2. What are mandatory parts in function declaration?


a) return type, function name
b) return type, function name, parameters
c) parameters, function name
d) none of the mentioned
View Answer

Answer: a
Explanation: In a function, return type and function name are mandatory all else are just used as a
choice.

3. which of the following is used to terminate the function declaration?


a) :
b) )
c) ;
d) none of the mentioned
View Answer

Answer: c
Explanation: None.

4. How many max number of arguments can present in function in c99 compiler?
a) 99
b) 90
c) 102
d) 127
View Answer

Answer: d
Explanation: None.

5. Which is more effective while calling the functions?


a) call by value
b) call by reference
c) call by pointer
d) none of the mentioned
View Answer

Answer: b
Explanation: In the call by reference, it will just copy the address of the variable to access it, so it will
reduce the memory in accessing it.

6. 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
View Answer

Answer: c
Explanation: We have to use the semicolon to declare the function in line 3. If we did means, the
program will execute.

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
View Answer

Answer: a
Explanation: In this program, we called by value so the value will not be changed, So the output is 10
Output:
$ g++ fun.cpp
$ a.out
10

8. What is the scope of the variable declared in the user definied function?
a) whole program
b) only inside the {} block
c) main function
d) none of the mentioned
View Answer

Answer: b
Explanation: The variable is valid only in the function block as in other.

9. How many minimum number of functions are need to be presented in c++?


a) 0
b) 1
c) 2
d) 3
View Answer
Answer: b
Explanation: The main function is the mandatory part, it is needed for the execution of the program to
start.

C++ Programming Questions and Answers – Argument Passing


This section on tough C++ programming questions focuses on “Argument Passing”. One shall practice
these questions to improve their C++ programming skills needed for various interviews (campus
interviews, walkin interviews, company interviews), placements, entrance exams and other competitive
exams. These questions can be attempted by anyone focusing on learning C++ programming language.
They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++
programming questions come with detailed explanation of the answers which helps in better
understanding of C++ concepts.

Here is a listing of tough C++ programming questions on “Argument Passing” along with answers,
explanations and/or solutions:

1. How many ways of passing a parameter are there in c++?


a) 1
b) 2
c) 3
d) 4
View Answer

Answer: c
Explanation: There are three ways of passing a parameter. They are pass by value,pass by reference and
pass by pointer.

2. Which is used to keep the call by reference value as intact?


a) static
b) const
c) absolute
d) none of the mentioned
View Answer

Answer: b
Explanation: Because const will not change the value of the variables during the execution.

3. By default how the value are passed in c++?


a) call by value
b) call by reference
c) call by pointer
d) none of the mentioned
View Answer
Answer: a
Explanation: None.

4. What is the output of this program?

#include <iostream>

using namespace std;

void copy (int& a, int& b, int& c)

a *= 2;

b *= 2;

c *= 2;

int main ()

int x = 1, y = 3, z = 7;

copy (x, y, z);

cout << "x =" << x << ", y =" << y << ", z =" << z;

return 0;

a) 2 5 10
b) 2 4 5
c) 2 6 14
d) none of the mentioned
View Answer

Answer: c
Explanation: Because we multiplied the values by 2 in the copy function.
Output:
$ g++ arg6.cpp
$ a.out
x = 2,y = 6,z = 14

5. What is the new value of x?

#include <iostream>

using namespace std;


void fun(int &x)

x = 20;

int main()

int x = 10;

fun(x);

cout << "New value of x is " << x;

return 0;

a) 10
b) 20
c) 15
d) none of the mentioned
View Answer

Answer: b
Explanation: As we passed by reference, the value is changed and it is returned as 20.
Output:
$ g++ arg5.cpp
$ a.out
20

6. What is the output of this program?

#include <iostream>

using namespace std;

long factorial (long a)

if (a > 1)

return (a * factorial (a + 1));

else

return (1);

}
int main ()

long num = 3;

cout << num << "! = " << factorial ( num );

return 0;

a) 6
b) 24
c) segmentation fault
d) compile time error
View Answer

Answer: c
Explanation: As we have given in the function as a+1, it will exceed the size and so it arises the
segmentation fault.
Output:
$ g++ arg3.cpp
$ a.out
segmentation fault

7. What is the output of this program?

#include <iostream>

using namespace std;

void square (int *x)

*x = (*x + 1) * (*x);

int main ( )

int num = 10;

square(&num);

cout << num;

return 0;

}
a) 100
b) compile time error
c) 144
d) 110
View Answer

Answer: d
Explanation: We have increased the x value in operand as x+1, so it will return as 110.
Output:
$ g++ arg2.cpp
$ a.out
110

8. What is the output of this program?

#include <iostream>

using namespace std;

int add(int a, int b);

int main()

int i = 5, j = 6;

cout << add(i, j) << endl;

return 0;

int add(int a, int b )

int sum = a + b;

a = 7;

return a + b;

a) 11
b) 12
c) 13
d) compile time error
View Answer
Answer: c
Explanation: The value of a has been changed to 7, So it returns as 13.
Output:
$ g++ arg1.cpp
$ a.out
13

9. What will happen when we use void in argument passing?


a) It will not return value to its caller
b) It will return value to its caller
c) Maybe or maynot be return any value to its caller
d) None of the mentioned
View Answer

Answer: a
Explanation: As void is not having any return value, it will not return the value to the caller.

10. What is the output of this program?

#include <iostream>

using namespace std;

void Sum(int a, int b, int & c)

a = b + c;

b = a + c;

c = a + b;

int main()

int x = 2, y =3;

Sum(x, y, y);

cout << x << " " << y;

return 0;

a) 2 3
b) 6 9
c) 2 15
d) compile time error
View Answer

Answer: c
Explanation: We have passed three values and it will manipulate according to the given condition and
yield the result as 2 15
Output:
$ g++ arg.cpp
$ a.out
2 15

C++ Programming Questions and Answers – Value Return

This section on C++ interview questions and answers focuses on “Value Return”. One shall practice
these interview questions to improve their C++ programming skills needed for various interviews
(campus interviews, walkin interviews, company interviews), placements, entrance exams and other
competitive exams. These questions can be attempted by anyone focusing on learning C++
programming language. They can be a beginner, fresher, engineering graduate or an experienced IT
professional. Our C++ interview questions come with detailed explanation of the answers which
helps in better understanding of C++ concepts.

Here is a listing of C++ interview questions on “Value Return” along with answers, explanations
and/or solutions:

1. How many types of returning values are present in c++?


a) 1
b) 2
c) 3
d) 4
View Answer

Answer: c
Explanation: The three types of returning values are return by value, return by reference and return
by address.

2. What will you use if you are not intended to get a return value?
a) static
b) const
c) volatile
d) void
View Answer

Answer: d
Explanation: Void is used to not to return anything.

3. Where does the return statement returns the execution of the program?
a) main function
b) caller function
c) same function
d) none of the mentioned
View Answer

Answer: b
Explanation: None.

4. What is the output of this program?

#include <iostream>

using namespace std;

int max(int a, int b )

return ( a > b ? a : b );

int main()

int i = 5;

int j = 7;

cout << max(i, j );

return 0;

a) 5
b) 7
c) either 5 or 7
d) none of the mentioned
View Answer

Answer: b
Explanation: In this program, we are returning the maximum value by using conditional operator.
Output:
$ g++ ret.cpp
$ a.out
7

5. What is the output of this program?

#include <iostream>

using namespace std;


double & WeeklyHours()

double h = 46.50;

double &hours = h;

return hours;

int main()

double hours = WeeklyHours();

cout << "Weekly Hours: " << hours;

return 0;

a) 46.5
b) 6.50
c) compile time error
d) none of the mentioned
View Answer

Answer: a
Explanation: We are returning the value what we get as input.
Output:
$ g++ ret1.cpp
$ a.out
46.5

6. What is the output of this program?

#include <iostream>

using namespace std;

int mult (int x, int y)

int result;

result = 0;

while (y != 0)

{
result = result + x;

y = y - 1;

return(result);

int main ()

int x = 5, y = 5;

cout << mult(x, y) ;

return(0);

a) 20
b) 25
c) 30
d) 35
View Answer

Answer: b
Explanation: We are multiplying these values by adding every values.
Output:
$ g++ ret.cpp
$ a.out
25

7. When will we use the function overloading?


a) same function name but different number of arguments
b) different function name but same number of arguments
c) same function name but same number of arguments
d) different function name but different number of arguments
View Answer

Answer: a
Explanation: In function overloading, we can use any number of arguments but same function name.

8. What is the output of this program?

#include <iostream>

using namespace std;

int gcd (int a, int b)

{
int temp;

while (b != 0)

temp = a % b;

a = b;

b = temp;

return(a);

int main ()

int x = 15, y = 25;

cout << gcd(x, y);

return(0);

a) 15
b) 25
c) 375
d) 5
View Answer

Answer: d
Explanation: In this program, we are finding the gcd of the number.
Output:
$ g++ ret5.cpp
$ a.out
5

C++ Language Interview Questions - Overloaded Function Names -


Sanfoundry
by Manish

This section on C++ language interview questions and answers focuses on “Overloaded Function
Names”. One shall practice these interview questions to improve their C++ programming skills
needed for various interviews (campus interviews, walkin interviews, company interviews),
placements, entrance exams and other competitive exams. These questions can be attempted by
anyone focusing on learning C++ programming language. They can be a beginner, fresher,
engineering graduate or an experienced IT professional. Our C++ language interview questions
come with detailed explanation of the answers which helps in better understanding of C++
concepts.

Here is a listing of C++ language interview questions on “Overloaded Function Names” along
with answers, explanations and/or solutions:

1. Which of the following permits function overloading on c++?


a) type
b) number of arguments
c) type & number of arguments
d) none of the mentioned
View Answer

Answer: c
Explanation: None.

2. In which of the following we cannot overload the function?


a) return function
b) caller
c) called function
d) none of the mentioned
View Answer

Answer: a
Explanation: While overloading the return function, it will rise a error, So we can’t overload the
return function.

3. Function overloading is also similar to which of the following?


a) operator overloading
b) constructor overloading
c) destructor overloading
d) none of the mentioned
View Answer

Answer: b
Explanation: In constructor overloading, we will be using the same options availed in function
overloading.

4. What is the output of this program?

1. #include <iostream>
2. using namespace std;
3. void print(int i)
4. {
5. cout << i;
6. }
7. void print(double f)
8. {
9. cout << f;
10. }
11. int main(void)
12. {
13. print(5);
14. print(500.263);
15. return 0;
16. }

a) 5500.263
b) 500.2635
c) 500.263
d) none of the mentioned
View Answer

Answer: a
Explanation: In this program, we are printing the values and the values will be print(5) will be
printed first because of the order of the execution.
Output:
$ g++ over.cpp
$ a.out
5500.263

5. What is the output of this program?

1. #include <iostream>
2. using namespace std;
3. int Add(int X, int Y, int Z)
4. {
5. return X + Y;
6. }
7. double Add(double X, double Y, double Z)
8. {
9. return X + Y;
10. }
11. int main()
12. {
13. cout << Add(5, 6);
14. cout << Add(5.5, 6.6);
15. return 0;
16. }

a) 11 12.1
b) 12.1 11
c) 11 12
d) compile time error
View Answer
Answer: d
Explanation: None.

6. What is the output of the following program?

1. #include <iostream>
2. using namespace std;
3. int operate (int a, int b)
4. {
5. return (a * b);
6. }
7. float operate (float a, float b)
8. {
9. return (a / b);
10. }
11. int main()
12. {
13. int x = 5, y = 2;
14. float n = 5.0, m = 2.0;
15. cout << operate(x, y) <<"\t";
16. cout << operate (n, m);
17. return 0;
18. }

a) 10.0 5.0
b) 5.0 2.5
c) 10.0 5
d) 10 2.5
View Answer

Answer: d
Explanation: In this program, we are divide and multiply the values.
Output:
$ g++ over3.cpp
$ a.out
10 2.5

7. Overloaded functions are


a) Very long functions that can hardly run
b) One function containing another one or more functions inside it
c) Two or more functions with the same name but different number of parameters or type
d) None of the mentioned
View Answer

Answer: c
Explanation: None.

8. What will happen while using pass by reference


a) The values of those variables are passed to the function so that it can manipulate them
b) The location of variable in memory is passed to the function so that it can use the same
memory area for its processing
c) The function declaration should contain ampersand (& in its type declaration)
d) All of the mentioned
View Answer

Answer: b
Explanation: In pass by reference, we can use the function to access the variable and it can
modify it. Therefore we are using pass by reference.

9. When our function doesn’t need to return anything means what will we use/send as parameter
in function?
a) void
b) blank space
c) both void & blank space
d) none of the mentioned
View Answer

Answer: b
Explanation: None.

10. What are the advantages of passing arguments by reference?


a) Changes to parameter values within the function also affect the original arguments.
b) There is need to copy parameter values (i.e. less memory used)
c) There is no need to call constructors for parameters (i.e. faster)
d) All of the mentioned
View Answer

Answer: d
Explanation: None.

C++ Puzzles - Default Arguments - Sanfoundry


by Manish

This section on C++ questions and puzzles focuses on “Default Arguments”. One shall practice
these questions and puzzles to improve their C++ programming skills needed for various
interviews (campus interviews, walkin interviews, company interviews), placements, entrance
exams and other competitive exams. These programming puzzles can be attempted by anyone
focusing on learning C++ programming language. They can be a beginner, fresher, engineering
graduate or an experienced IT professional. Our C++ questions come with detailed explanation
of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ questions and puzzles on “Default Arguments” along with answers,
explanations and/or solutions:
1. If the user didn’t supply the value, what value will it take?
a) default value
b) rise an error
c) both default value & rise an error
d) none of the mentioned
View Answer

Answer: a
Explanation: If the user didn’t supply the value means, the compiler will take the given value in
the argument list.

2. Where can the default parameter be placed by the user?


a) leftmost
b) rightmost
c) both leftmost & rightmost
d) none of the mentioned
View Answer

Answer: b
Explanation: None.

3. Which value will it take when both user and default values are given?
a) user value
b) default value
c) custom value
d) none of the mentioned
View Answer

Answer: a
Explanation: The default value will be used when the user value is not given, So in this case, the
user value will be taken.

4. What is the output of this program?

1. #include <iostream>
2. using namespace std;
3. void func(int a, bool flag = true)
4. {
5. if (flag == true )
6. {
7. cout << "Flag is true. a = " << a;
8. }
9. else
10. {
11. cout << "Flag is false. a = " << a;
12. }
13. }
14. int main()
15. {
16. func(200, false);
17. return 0;
18. }

a) Flag is true. a = 200


b) Flag is false. a = 100
c) Flag is false. a = 200
d) Flag is true. a = 100
View Answer

Answer: c
Explanation: In this program, we are passing the value, as it evaluates to false, it produces the
output as following.
Output:
$ g++ def.cpp
$ a.out
Flag is false. a = 200

5. What is the output of this program?

1. #include <iostream>
2. #include <string>
3. using namespace std;
4. string askNumber(string prompt = "Please enter a number: ");
5. int main()
6. {
7. string number = askNumber();
8. cout << "Here is your number: " << number;
9. return 0;
10. }
11. string askNumber(string prompt)
12. {
13. string number;
14. cout << prompt;
15. cin >> number;
16. return number;
17. }

a) 5
b) 6
c) the number you entered
d) compile time error
View Answer

Answer: c
Explanation: In this program, we are getting a number and printing it.
Output:
$ g++ def1.cpp
$ a.out
Please enter a number:
5
Here is your number:5

6. What is the output of this program?

1. #include <iostream>
2. using namespace std;
3. void Values(int n1, int n2 = 10)
4. {
5. using namespace std;
6. cout << "1st value: " << n1;
7. cout << "2nd value: " << n2;
8. }
9. int main()
10. {
11. Values(1);
12. Values(3, 4);
13. return 0;
14. }

a) 1st value: 1
2nd value: 10
1st value: 3
2nd value: 4
b) 1st value: 1
2nd value: 10
1st value: 3
2nd value: 10
c) compile time error
d) none of the mentioned
View Answer

Answer: a
Explanation: In this program, We are passing the values as by default values rules it is working.
Output:
$ g++ def2.cpp
$ a.out
1st value: 1
2nd value: 10
1st value: 3
2nd value: 4

7. What we can’t place followed by the non-default arguments?


a) trailing arguments
b) default arguments
c) both trailing & default arguments
d) none of the mentioned
View Answer
Answer: b
Explanation: None.

8. If we start our function call with default arguments means, what will be proceeding
arguments?
a) user argument
b) empty arguments
c) default arguments
d) none of the mentioned
View Answer

Answer: c
Explanation: As a rule, the default argument must be followed by default arguments only.

9. What is the default return type of a function ?


a) int
b) void
c) float
d) char
View Answer

Answer: b
Explanation: None.

10. What is the output of this program?

1. #include <iostream>
2. using namespace std;
3. int func(int m = 10, int n)
4. {
5. int c;
6. c = m + n;
7. return c;
8. }
9. int main()
10. {
11. cout << func(5);
12. return 0;
13. }

a) 15
b) 10
c) compile time error
d) none of the mentioned
View Answer

Answer: c
Explanation: We can’t use the user argument infront of the default argument.
C++ Programming Questions and Answers – Unspecified Number of
Arguments
This section on C++ Programming quiz focuses on “Unspecified Number of Arguments”. One shall
practice these quizzes to improve their C++ programming skills needed for various interviews (campus
interviews, walkin interviews, company interviews), placements, entrance exams and other competitive
exams. These questions can be attempted by anyone focusing on learning C++ programming language.
They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ quiz
comes with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ Programming quiz on “Unspecified Number of Arguments” along with answers,
explanations and/or solutions:

1. Which header file is used to pass unknown number of arguments to function?


a) stdlib.h
b) string.h
c) stdarg.h
d) none of the mentioned
View Answer

Answer: c
Explanation: Because the cstdarg defines this header file to process the unknown number of arguments.

2. How can you access the arguments that are manipulated in the function?
a) va_list
b) arg_list
c) both va_list & arg_list
d) none of the mentioned
View Answer

Answer: a
Explanation: None.

3. What is the output of this program?

#include <iostream>

#include <stdarg.h>

using namespace std;

float avg( int Count, ... )

va_list Numbers;
va_start(Numbers, Count);

int Sum = 0;

for (int i = 0; i < Count; ++i )

Sum += va_arg(Numbers, int);

va_end(Numbers);

return (Sum/Count);

int main()

float Average = avg(10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

cout << "Average of first 10 whole numbers : " << Average;

return 0;

a) 4
b) 5
c) 6
d) 7
View Answer

Answer: a
Explanation: We are just calculating the average of these numbers using cstdarg.
Output:
$ g++ uka.cpp
$ a.out
Average of first 10 whole numbers 4

4. What is the maximum number of arguments or parameters that can be


present in one function call?
a) 64
b) 256
c) 255
d) 16
View Answer

Answer: b
Explanation: None.

5. What is the output of this program?


#include <iostream>

#include <stdarg.h>

using namespace std;

int add (int num, ...)

int sum = 0;

va_list args;

va_start (args,num);

for (int i = 0; i < num; i++)

int num = va_arg (args,int);

sum += num;

va_end (args);

return sum;

int main (void)

int total = add(8, 1, 2, -1, 4, 12, -2, 9, 7);

cout << "The result is " << total;

return 0;

a) 32
b) 23
c) 48
d) compile time error
View Answer

Answer: a
Explanation: We are adding these numbers by using for statement and stdarg.
Output:
$ g++ uka.cpp
$ a.out
The result is 32

6. What is the output of this program?

#include <iostream>

#include <stdarg.h>

using namespace std;

void dumplist(int, ...);

int main()

dumplist(2, 4, 8);

dumplist(3, 6, 9, 7);

return 0;

void dumplist(int n, ...)

va_list p;

int i;

va_start(p, n);

while (n-->0)

i = va_arg(p, int);

cout << i;

va_end(p);

a) 2436
b) 48697
c) 1111111
d) compile time error
View Answer
Answer: b
Explanation: In this program, we are eradicating the first value
by comparing using while operator.
Output:
$ g++ rka3.cpp
$ a.out
48697

7. What is the output of this program?

#include <iostream>

#include <stdarg.h>

using namespace std;

int flue(char c,...);

int main()

int x, y;

x = flue('A', 1, 2, 3);

y = flue('1', 1.0,1, '1', 1.0f, 1l);

cout << x << y;

return 0;

int flue(char c,...)

return c;

a) 6549
b) 4965
c) 6646
d) compile time error
View Answer

Answer: a
Explanation: In this program, we are returning the ascii value of the character and printing it.
Output:
$ g++ rka4.cpp
$ a.out
6549

8. Which header file should you include if you are to develop a function that can accept variable number
of arguments?
a) varag.h
b) stdlib.h
c) stdio.h
d) stdarg.h
View Answer

Answer: d
Explanation: None.

9. What is the output of this program?

#include <iostream>

#include <stdarg.h>

using namespace std;

void fun(std::string msg, ...);

int main()

fun("IndiaBIX", 1, 4, 7, 11, 0);

return 0;

void fun(std::string msg, ...)

va_list ptr;

int num;

va_start(ptr, msg);

num = va_arg(ptr, int);

num = va_arg(ptr, int);

cout << num;

}
a) 6
b) 5
c) 8
d) 4
View Answer

Answer: d
Explanation: In this program, we are moving the pointer to the second value and printing it.
Output:
$ g++ uka6.cpp
$ a.out
4

10. What will initialize the list of arguments in stdarg.h header file?
a) va_list
b) va_start
c) va_arg
d) none of the mentioned
View Answer

Answer: b
Explanation: None.

C++ Programming Questions and Answers – Pointer to Function


This section on C++ questions and puzzles focuses on “Pointer to Function”. One shall practice these
questions and puzzles to improve their C++ programming skills needed for various interviews (campus
interviews, walkin interviews, company interviews), placements, entrance exams and other competitive
exams. These programming puzzles can be attempted by anyone focusing on learning C++ programming
language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our
C++ questions come with detailed explanation of the answers which helps in better understanding of
C++ concepts.

Here is a listing of C++ questions and puzzles on “Pointer to Function” along with answers, explanations
and/or solutions:

1. To which does the function pointer point to?


a) variable
b) constants
c) function
d) absolute variables
View Answer
Answer: c
Explanation: None.

2. What we will not do with function pointers?


a) allocation of memory
b) de-allocation of memory
c) both allocation & de-allocation of memory
d) none of the mentioned
View Answer

Answer: c
Explanation: As it is used to execute a block of code, So we will not allocate or deallocate memory.

3. What is the default calling convention for a compiler in c++?


a) __cdecl
b) __stdcall
c) __pascal
d) __fastcall
View Answer

Answer: a
Explanation: None.

4. What is te output of this program?

#include <iostream>

using namespace std;

int add(int first, int second)

return first + second + 15;

int operation(int first, int second, int (*functocall)(int, int))

return (*functocall)(first, second);

int main()

int a;
int (*plus)(int, int) = add;

a = operation(15, 10, plus);

cout << a;

return 0;

a) 25
b) 35
c) 40
d) 45
View Answer

Answer: c
Explanation: In this program, we are adding two numbers with 15, So we got the output as 40.
Output:
$ g++ pfu2.cpp
$ a.out
40

5. What is the output of this program?

#include <iostream>

using namespace std;

void func(int x)

cout << x ;

int main()

void (*n)(int);

n = &func;

(*n)( 2 );

n( 2 );

return 0;

}
a) 2
b) 20
c) 21
d) 22
View Answer

Answer: d
Explanation: As we are calling the function two times with the same value, So it is printing as 22.
Output:
$ g++ pfu.cpp
$ a.out
22

6. What is the output of this program?

#include <iostream>

using namespace std;

int n(char, int);

int (*p) (char, int) = n;

int main()

(*p)('d', 9);

p(10, 9);

return 0;

int n(char c, int i)

cout << c << i;

return 0;

a) d9
9
b) d9d9
c) d9
d) compile time error
View Answer
Answer: a
Explanation: None.
Output:
$ g++ pfu1.cpp
$ a.out
d9
9

7. What is the output of this program?

#include <iostream>

using namespace std;

int func (int a, int b)

cout << a;

cout << b;

return 0;

int main(void)

int(*ptr)(char, int);

ptr = func;

func(2, 3);

ptr(2, 3);

return 0;

a) 2323
b) 232
c) 23
d) compile time error
View Answer

Answer: d
Explanation: In this program, we can’t do the casting from char to int, So it is raising an error.
8. What are the mandatory part to present in function pointers?
a) &
b) retrun values
c) data types
d) none of the mentioned
View Answer

Answer: c
Explanation: The data types are mandatory for declaring the variables in the function pointers.

9. which of the following can be passed in function pointers?


a) variables
b) data types
c) functions
d) none of the mentioned
View Answer

Answer: c
Explanation: None.

10. What is meaning of following declaration?


int(*ptr[5])();
a) ptr is pointer to function
b) ptr is array of pointer to function
c) ptr is pointer to such function which return type is array
d) ptr is pointer to array of function
View Answer

Answer: b
Explanation: In this expression, ptr is array not pointer.

C++ Programming Questions and Answers – Macros


This section on C++ interview questions and answers focuses on “Macros”. One shall practice these
interview questions to improve their C++ programming skills needed for various interviews (campus
interviews, walkin interviews, company interviews), placements, entrance exams and other competitive
exams. These questions can be attempted by anyone focusing on learning C++ programming language.
They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++
interview questions come with detailed explanation of the answers which helps in better understanding
of C++ concepts.

Here is a listing of C++ interview questions on “Macros” along with answers, explanations and/or
solutions:
1. which keyword is used to define the macros in c++?
a) macro
b) define
c) #define
d) none of the mentioned
View Answer

Answer: c
Explanation: None.

2. Which symbol is used to declare the preprocessor directives?


a) #
b) $
c) *
d) ^
View Answer

Answer: a
Explanation: None.

3. How many types of macros are there in c++?


a) 1
b) 2
c) 3
4) 4
View Answer

Answer: b
Explanation: There are two types of macros. They are object-like and function-like.

4. What is the mandatory preprosessor directive for c++?


a) #define <iostream>
b) #include <iostream>
c) #undef <iostream>
d) none of the mentioned
View Answer

Answer: b
Explanation: For a c++ program to execute, we need #include<iostream>.

5. What is the output of this program?

#include <iostream>

using namespace std;


#define MIN(a,b) (((a)<(b)) ? a : b)

int main ()

float i, j;

i = 100.1;

j = 100.01;

cout <<"The minimum is " << MIN(i, j) << endl;

return 0;

a) 100.01
b) 100.1
c) compile time error
d) none of the mentioned
View Answer

Answer: a
Explanation: In this program, we are getting the minimum number using conditional operator.
Output:
$ g++ mac3.cpp
$ a.out
The minimum value is 100.01

6. What is the output of this program?

#include <iostream>

using namespace std;

int main ()

cout << "Value of __LINE__ : " << __LINE__ << endl;

cout << "Value of __FILE__ : " << __FILE__ << endl;

cout << "Value of __DATE__ : " << __DATE__ << endl;

cout << "Value of __TIME__ : " << __TIME__ << endl;

return 0;

}
a) 5
b) details about your file
c) compile time error
d) none of the mentioned
View Answer

Answer: b
Explanation: In this program, we are using the macros to print the information about the file.
Output:
$ g++ mac2.cpp
$ a.out
Value of __LINE__ : 5
Value of __FILE__ : mac1.cpp
Value of __DATE__ : Oct 10 2012
Value of __TIME__ : 22:24:37

7. What is the output of this program?

#include <iostream>

using namespace std;

#define SquareOf(x) x * x

int main()

int x;

cout << SquareOf(x + 4);

return 0;

a) 16
b) 64
c) compile time error
d) none of the mentioned
View Answer

Answer: d
Explanation: In this program, as we haven’t initiailzed the variable x, we will get a output of ending digit
of 4.
Output:
$ g++ mac1.cpp
$ a.out
75386824

8. What is the output of this program?

#include <iostream>

using namespace std;

#define PR(id) cout << "The value of " #id " is "<<id

int main()

int i = 10;

PR(i);

return 0;

a) 10
b) 15
c) 20
d) none of the mentioned
View Answer

Answer: a
Explanation: In this program, we are just printing the declared values.
Output:
$ g++ mac.cpp
$ a.out
10

9. What is the output of this program?

#include <iostream>

using namespace std;

#define MAX 10

int main()

int num;

num = ++MAX;

cout << num;


return 0;

a) 11
b) 10
c) compile time error
d) none of the mentioned
View Answer

Answer: c
Explanation: Macro Preprocessor only replaces occurance of macro symbol with macro symbol value, So
we can’t increment the value.

10. What is the other name of the macro?


a) scripted directive
b) executed directive
c) link directive
d) none of the mentioned
View Answer

Answer: a
Explanation: When the compiler encounters a previously defined macro, it will take the result from that
execution itself.

C++ Programming Questions and Answers – Modularization and


Interfaces
This section on C++ quiz focuses on “Modularization and Interfaces”. One shall practice these online
quizzes to improve their C++ programming skills needed for various interviews (campus interviews,
walkin interviews, company interviews), placements, entrance exams and other competitive exams.
These questions can be attempted by anyone focusing on learning C++ programming language. They can
be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ quiz comes with
detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ quiz on “Modularization and Interfaces” along with answers, explanations and/or
solutions:

1. which of the following is used to implement the c++ interfaces?


a) absolute variables
b) abstract classes
c) constant variables
d) none of the mentioned
View Answer
Answer: b
Explanation: None.

2. What is the ability to group some lines of code that can be included
in the program?
a) specific task
b) program control
c) modularization
d) macros
View Answer

Answer: c
Explanation: Modularization is also similar to macros but it is used to build large projects.

3. How many types does functions fall depends on modularization?


a) 1
b) 2
c) 3
d) 4
View Answer

Answer: b
Explanation: There are two types of functions.They are program control and specific task.

4. How many types of modularization are there in c++?


a) 4
b) 3
C) 1
d) none of the mentioned
View Answer

Answer: d
Explanation: There are two types of modular programming.They are interface and implementation.

5. What does the client module import?


a) macro
b) records
c) interface
d) none of the mentioned
View Answer

Answer: c
Explanation: Because they access the functions in the module using interface.
6. Identify the correct statement.
a) c++ does not have built-in interfaces
b) c++ does have built-in interfaces
c) c++ have no cocept of interfaces
d) none of the mentioned
View Answer

Answer: a
Explanation: None.

7. What is similar to interface in c++


a) methods
b) instance of class
c) pure abstract class
d) none of the mentioned
View Answer

Answer: c
Explanation: None.

8. Which of the following implements the module in the program?


a) macro
b) header files
c) macro& header files
d) none of the mentioned
View Answer

Answer: b
Explanation: We can include the group of code by using the #include header file.

C++ Puzzles - Namespaces - Sanfoundry


by Manish

This section on C++ questions and puzzles focuses on “Namespaces”. One shall practice these
questions and puzzles to improve their C++ programming skills needed for various interviews
(campus interviews, walkin interviews, company interviews), placements, entrance exams and
other competitive exams. These programming puzzles can be attempted by anyone focusing on
learning C++ programming language. They can be a beginner, fresher, engineering graduate or
an experienced IT professional. Our C++ questions come with detailed explanation of the
answers which helps in better understanding of C++ concepts.

Here is a listing of C++ questions and puzzles on “Namespaces” along with answers,
explanations and/or solutions:
1. Which operator is used to signify the namespace?
a) conditional operator
b) ternary operator
c) scope operator
d) none of the mentioned
View Answer

Answer: c
Explanation: None.

2. Identify the correct statement.


a) Namespace is used to group class, objects and functions
b) Namespace is used to mark the beginning of the program
c) Namespace is used to seperate the class, objects
d) None of the mentioned
View Answer

Answer: a
Explanation: Namespace allow you to group class, objects and functions. It is used to divide the
global scope into the sub-scopes.

3. What is the use of Namespace?


a) To encapsulate the data
b) To structure a program into logical units
c) Encapsulate the data & structure a program into logical units
d) None of the mentioned
View Answer

Answer: b
Explanation: The main aim of the namespace is to understand the logical units of the program
and to make the program so robust.

4. What is the general syntax for accessing the namespace variable?


a) namespaceid::operator
b) namespace,operator
c) namespace#operator
d) none of the mentioned
View Answer

Answer: a
Explanation: None.

5. What is the output of this program?

1. #include <iostream>
2. using namespace std;
3. namespace first
4. {
5. int var = 5;
6. }
7. namespace second
8. {
9. double var = 3.1416;
10. }
11. int main ()
12. {
13. int a;
14. a = first::var + second::var;
15. cout << a;
16. return 0;
17. }

a) 8.31416
b) 8
c) 9
d) compile time error
View Answer

Answer: b
Explanation: As we are getting two variables from namespace variable and we are adding that.
Output:
$ g++ name.cpp
$ a.out
8

6. What is the output of these program?

1. #include <iostream>
2. using namespace std;
3. namespace first
4. {
5. int x = 5;
6. int y = 10;
7. }
8. namespace second
9. {
10. double x = 3.1416;
11. double y = 2.7183;
12. }
13. int main ()
14. {
15. using first::x;
16. using second::y;
17. bool a, b;
18. a = x > y;
19. b = first::y < second::x;
20. cout << a << b;
21. return 0;
22. }
a) 11
b) 01
c) 00
d) 10
View Answer

Answer: d
Explanation: We are inter mixing the variable and comparing it which is bigger and smaller and
according to that we are printing the output.
Output:
$ g++ name1.cpp
$ a.out
10

7. What is the output of this program?

1. #include <iostream>
2. using namespace std;
3. namespace Box1
4. {
5. int a = 4;
6. }
7. namespace Box2
8. {
9. int a = 13;
10. }
11. int main ()
12. {
13. int a = 16;
14. Box1::a;
15. Box2::a;
16. cout << a;
17. return 0;
18. }

a) 4
b) 13
c) 16
d) compile time error
View Answer

Answer: c
Explanation: In this program, as there is lot of variable a and it is printing the value inside the
block because it got the highest priority.
Output:
$ g++ name2.cpp
$ a.out
16

8. What is the output of this program?


1. #include <iostream>
2. using namespace std
3. namespace space
4. {
5. int x = 10;
6. }
7. namespace space
8. {
9. int y = 15;
10. }
11. int main(int argc, char * argv[])
12. {
13. space::x = space::y =5;
14. cout << space::x << space::y;
15. }

a) 1015
b) 1510
c) 55
d) compile time error
View Answer

Answer: c
Explanation: We are overriding the value at the main function and so we are getting the output as
55.
Output:
$ g++ name4.cpp
$ a.out
55

9. What is the output of this program?

1. #include <iostream>
2. using namespace std;
3. namespace extra
4. {
5. int i;
6. }
7. void i()
8. {
9. using namespace extra;
10. int i;
11. i = 9;
12. cout << i;
13. }
14. int main()
15. {
16. enum letter { i, j};
17. class i { letter j; };
18. ::i();
19. return 0;
20. }
a) 9
b) 10
c) compile time error
d) none of the mentioned
View Answer

Answer: a
Explanation: A scope resolution operator without a scope qualifier refers to the global
namespace.

10. Which keyword is used to access the variable in namespace?


a) using
b) dynamic
c) const
d) static
View Answer

Answer: a
Explanation: None.

C++ Programming Questions and Answers – Exceptions


This section on C++ Programming quiz focuses on “Exceptions”. One shall practice these quizzes to
improve their C++ programming skills needed for various interviews (campus interviews, walkin
interviews, company interviews), placements, entrance exams and other competitive exams. These
questions can be attempted by anyone focusing on learning C++ programming language. They can be a
beginner, fresher, engineering graduate or an experienced IT professional. Our C++ quiz comes with
detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ Programming quiz on “Exceptions” along with answers, explanations and/or
solutions:

1. To where does the program control transfers when exception is arised?


a) catch
b) handlers
c) throw
d) none of the mentioned
View Answer

Answer: b
Explanation: When a exception is arised mean, the exception is caught by handlers and then it decides
the type of exception.

2. Which key word is used to check exception in the block of code?


a) catch
b) throw
c) try
d) none of the mentioned
View Answer

Answer: c
Explanation: The try() statement is used for exceptios in c++.

3. What will happen when the exception is not caught in the program?
a) error
b) program will execute
c) block of that code will not execute
d) none of the mentioned
View Answer

Answer: a
Explanation: None.

4. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int age = 0;

try

if (age < 0)

throw "Positive Number Required";

cout << age;

catch(const char *Message)

cout << "Error: " << Message;

}
return 0;

a) 0
b) error:Positive Number Required
c) compile time error
d) none of the mentioned
View Answer

Answer: a
Explanation: As the zero marks the beginning of the positive number, it is printed as output
Output:
$ g++ excep.cpp
$ a.out
0

5. What is the output of this program?

#include <iostream>

using namespace std;

void PrintSequence(int StopNum)

int Num;

Num = 1;

while (true)

if (Num >= StopNum)

throw Num;

cout << Num;

Num++;

int main(void)

try

{
PrintSequence(20);

catch(int ExNum)

cout << "Caught an exception with value: " << ExNum;

return 0;

a) compile time error


b) prints first 19 numbers
c) prints first 19 numbers and throws exception at 20
d) none of the mentioned
View Answer

Answer: c
Explanation: In this program, we are printing upto 19 numbers and when executing the 20, we are
raising a exception.
Output:
$ g++ excep1.cpp
$ a.out
12345678910111213141516171819Caught an exception with value: 20

6. What is the output of this program?

#include <iostream>

using namespace std;

double division(int a, int b)

if (b == 0)

throw "Division by zero condition!";

return (a / b);

int main ()
{

int x = 50;

int y = 2;

double z = 0;

try

z = division(x, y);

cout << z;

catch(const char *msg)

cerr << msg;

return 0;

a) 25
b) 20
c) Division by zero condition!
d) None of the mentioned
View Answer

Answer: a
Explanation: In this program, we resembling the division by using the exception handling.
Output:
$ g++ excep2.cpp
$ a.out
25

7. What is the output of this program?

#include <iostream>

using namespace std;

int main()

char* buff;
try

buff = new char[1024];

if (buff == 0)

throw "Memory allocation failure!";

else

cout << sizeof(buff) << "Byte successfully allocated!"<<endl;

catch(char *strg)

cout<<"Exception raised: "<<strg<<endl;

return 0;

a) 4 Bytes allocated successfully


b) 8 Bytes allocated successfully
c) Memory allocation failure
d) Depends on the size of data type
View Answer

Answer: d
Explanation: As we are allocating the memory to the variables and if there is not suffcient size means, it
will throw an exception.
Output:
$ g++ excep3.cpp
$ a.out
4 Bytes allocated successfully

8. What is the output of this program?

#include <iostream>

using namespace std;

void Funct();

int main()

{
try

Funct();

catch(double)

cerr << "caught a double type..." << endl;

return 0;

void Funct()

throw 3;

a) caught a double type


b) compile time error
c) abnormal program termination
d) none of the mentioned
View Answer

Answer: c
Explanation: As we are throwing integer to double it will raise as abnormal program after termination
throw statement.
Output:
$ g++ excep4.cpp
$ a.out
terminate called after throwing an instance of ‘int’
Aborted

9. What is the output of this program?

#include <iostream>

#include <exception>

using namespace std;

int main()
{

try

int * array1 = new int[100000000];

int * array2 = new int[100000000];

int * array3 = new int[100000000];

int * array4 = new int[100000000];

cout << "Allocated successfully";

catch(bad_alloc&)

cout << "Error allocating the requested memory." << endl;

return 0;

a) Allocated successfully
b) Error allocating the requested memory
c) Depends on the memory of the computer
d) None of the mentioned
View Answer

Answer: c
Explanation: In this program, we allocating the memory to the arrays by using excetion handling and we
handled the exception by standard exception.
Output:
$ g++ excep5.cpp
$ a.out
Allocated successfully

10. What will happen when the handler is not found for exception?
a) calls the standard library function terminate()
b) raise an error
c) executes the remaining block
d) none of the mentioned
View Answer
Answer: a
Explanation: None.

You might also like