Functions, Namespaces & Exceptions in C++
Functions, Namespaces & Exceptions in C++
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:
Answer: d
Explanation: None.
Answer: a
Explanation: The operator which is having highest precedence is postfix and lowest is equality.
Answer: a
Explanation: In this operator, if the condition is true means, it will return the first operator, otherwise
second operator.
#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
Answer: a
Explanation: Because the dynamic_cast operator is used to convert from base class to derived
class.
#include <iostream>
int main()
int a = 5, b = 6, c, d;
c = a, b;
d = (a, b);
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
#include <iostream>
int main()
int i, j;
j = 10;
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
#include <iostream>
int main ()
int x, y;
x = 5;
y = ++x * ++x;
x = 5;
y = x++ * ++x;
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
#include <iostream>
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
#include <iostream>
main()
double a = 21.09399;
float b = 10.20;
int c ,d;
c = (int) a;
d = (int) b;
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
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:
Answer: c
Explanation: There are five sequence of statements. They are Preprocessor directives, Comments,
Declarations, Function Declarations, Executable statements.
Answer: b
Explanation: In the conditional operator,it will predicate the output using the given condition.
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.
#include <iostream>
int main ()
int 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
#include <iostream>
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.
#include <iostream>
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.
#include <iostream>
int main()
int 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
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.
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:
Answer: b
Explanation: There are two types of comments. They are double slash and slash stared.
Answer: a
Explanation: Comments are used to add meaning to the program.
Answer: c
Explanation: None.
#include <iostream>
int main()
/* this is comment*
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.
Answer: a
Explanation: None.
#include <iostream>
if (a > 1)
else
return (1);
int main ()
long num = 3;
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
#include <iostream>
*x = (*x + 1) * (*x);
int main ( )
square(&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
#include <iostream>
int i = 5, j = 6;
return 0;
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
Answer: a
Explanation: As void is not having any return value, it will not return the value to the caller.
#include <iostream>
a = b + c;
b = a + c;
c = a + b;
int main()
int x = 2, y =3;
Sum(x, y, 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
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.
Answer: a
Explanation: In a function, return type and function name are mandatory all else are just used as a
choice.
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.
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.
#include <iostream>
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.
#include <iostream>
{
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.
Here is a listing of tough C++ programming questions on “Argument Passing” along with answers,
explanations and/or solutions:
Answer: c
Explanation: There are three ways of passing a parameter. They are pass by value,pass by reference and
pass by pointer.
Answer: b
Explanation: Because const will not change the value of the variables during the execution.
#include <iostream>
a *= 2;
b *= 2;
c *= 2;
int main ()
int x = 1, y = 3, z = 7;
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
#include <iostream>
x = 20;
int main()
int x = 10;
fun(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
#include <iostream>
if (a > 1)
else
return (1);
}
int main ()
long num = 3;
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
#include <iostream>
*x = (*x + 1) * (*x);
int main ( )
square(&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
#include <iostream>
int main()
int i = 5, j = 6;
return 0;
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
Answer: a
Explanation: As void is not having any return value, it will not return the value to the caller.
#include <iostream>
a = b + c;
b = a + c;
c = a + b;
int main()
int x = 2, y =3;
Sum(x, y, 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
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:
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.
#include <iostream>
return ( a > b ? a : b );
int main()
int i = 5;
int j = 7;
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
#include <iostream>
double h = 46.50;
double &hours = h;
return hours;
int main()
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
#include <iostream>
int result;
result = 0;
while (y != 0)
{
result = result + x;
y = y - 1;
return(result);
int main ()
int x = 5, y = 5;
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
Answer: a
Explanation: In function overloading, we can use any number of arguments but same function name.
#include <iostream>
{
int temp;
while (b != 0)
temp = a % b;
a = b;
b = temp;
return(a);
int main ()
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
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:
Answer: c
Explanation: None.
Answer: a
Explanation: While overloading the return function, it will rise a error, So we can’t overload the
return function.
Answer: b
Explanation: In constructor overloading, we will be using the same options availed in function
overloading.
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
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.
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
Answer: c
Explanation: None.
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.
Answer: d
Explanation: None.
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.
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.
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. }
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
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
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
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.
Answer: b
Explanation: None.
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:
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.
#include <iostream>
#include <stdarg.h>
va_list Numbers;
va_start(Numbers, Count);
int Sum = 0;
va_end(Numbers);
return (Sum/Count);
int main()
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
Answer: b
Explanation: None.
#include <stdarg.h>
int sum = 0;
va_list args;
va_start (args,num);
sum += num;
va_end (args);
return sum;
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
#include <iostream>
#include <stdarg.h>
int main()
dumplist(2, 4, 8);
dumplist(3, 6, 9, 7);
return 0;
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
#include <iostream>
#include <stdarg.h>
int main()
int x, y;
x = flue('A', 1, 2, 3);
return 0;
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.
#include <iostream>
#include <stdarg.h>
int main()
return 0;
va_list ptr;
int num;
va_start(ptr, msg);
}
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.
Here is a listing of C++ questions and puzzles on “Pointer to Function” along with answers, explanations
and/or solutions:
Answer: c
Explanation: As it is used to execute a block of code, So we will not allocate or deallocate memory.
Answer: a
Explanation: None.
#include <iostream>
int main()
int a;
int (*plus)(int, int) = add;
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
#include <iostream>
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
#include <iostream>
int main()
(*p)('d', 9);
p(10, 9);
return 0;
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
#include <iostream>
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.
Answer: c
Explanation: None.
Answer: b
Explanation: In this expression, ptr is array not pointer.
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.
Answer: a
Explanation: None.
Answer: b
Explanation: There are two types of macros. They are object-like and function-like.
Answer: b
Explanation: For a c++ program to execute, we need #include<iostream>.
#include <iostream>
int main ()
float i, j;
i = 100.1;
j = 100.01;
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
#include <iostream>
int main ()
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
#include <iostream>
#define SquareOf(x) x * x
int main()
int x;
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
#include <iostream>
#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
#include <iostream>
#define MAX 10
int main()
int num;
num = ++MAX;
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.
Answer: a
Explanation: When the compiler encounters a previously defined macro, it will take the result from that
execution itself.
Here is a listing of C++ quiz on “Modularization and Interfaces” along with answers, explanations and/or
solutions:
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.
Answer: b
Explanation: There are two types of functions.They are program control and specific task.
Answer: d
Explanation: There are two types of modular programming.They are interface and implementation.
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.
Answer: c
Explanation: None.
Answer: b
Explanation: We can include the group of code by using the #include header file.
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.
Answer: a
Explanation: Namespace allow you to group class, objects and functions. It is used to divide the
global scope into the sub-scopes.
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.
Answer: a
Explanation: None.
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
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
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
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
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.
Answer: a
Explanation: None.
Here is a listing of C++ Programming quiz on “Exceptions” along with answers, explanations and/or
solutions:
Answer: b
Explanation: When a exception is arised mean, the exception is caught by handlers and then it decides
the type of exception.
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.
#include <iostream>
int main()
int age = 0;
try
if (age < 0)
}
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
#include <iostream>
int Num;
Num = 1;
while (true)
throw Num;
Num++;
int main(void)
try
{
PrintSequence(20);
catch(int ExNum)
return 0;
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
#include <iostream>
if (b == 0)
return (a / b);
int main ()
{
int x = 50;
int y = 2;
double z = 0;
try
z = division(x, y);
cout << z;
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
#include <iostream>
int main()
char* buff;
try
if (buff == 0)
else
catch(char *strg)
return 0;
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
#include <iostream>
void Funct();
int main()
{
try
Funct();
catch(double)
return 0;
void Funct()
throw 3;
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
#include <iostream>
#include <exception>
int main()
{
try
catch(bad_alloc&)
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.