250+ TOP MCQs on Pointers to Members and Answers

This section on C++ language interview questions and answers on “Pointers to Members”. 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 “Pointers to Members” along with answers, explanations and/or solutions:

1. Which is referred by pointers to member?
a) Static members of class objects
b) Non-static members of class objects
c) Referring to whole class
d) Dynamic members of class objects
Answer: b
Clarification: We cannot use a pointer to member to point to a static class member because the address of a static member is not associated with any particular object.

2. What should be used to point to a static class member?
a) Smart pointer
b) Dynamic pointer
c) Normal pointer
d) Static pointer
Answer: c
Clarification: Normal pointer is sed to point to a static class member.

3. Which operator is used in pointer to member function?
a) .*
b) ->*
c) Both .* & ->*
d) $*
Answer: c
Clarification: The pointer to member operators .* and ->* are used to bind a pointer to a member of a specific class object.

4. What will be the output of the following C++ code?

  1.     #include 
  2.     using namespace std;
  3.     class X 
  4.     {
  5.         public:
  6.         int a;
  7. 		void f(int b) 
  8. 		{
  9. 			cout<< b << endl;
  10. 		}
  11.     };
  12.     int main() 
  13.     {
  14.         int X :: *ptiptr = &X :: a;
  15.         void (X :: * ptfptr) (int) = &X :: f;
  16.         X xobject;
  17.         xobject.*ptiptr = 10;
  18.         cout << xobject.*ptiptr << endl;
  19.         (xobject.*ptfptr) (20);
  20.     }

a)

   10
   20

b)

   20
   10

c) 20
d) 10
Answer: a
Clarification: In this program, We are assigning 10 and printing it in the
main function and then for value 20, We are passing the value to class and
printing it.
Output:

$ g++ ptm.cpp
$ a.out
10
20

5. What will be the output of the following C++ code?

  1.     #include 
  2.     using namespace std;
  3.     class Testpm 
  4.     {
  5.         public:
  6.         void m_func1() 
  7.         { 
  8.             cout << "func1n";
  9.         }
  10.         int m_num;
  11.     };
  12.     void (Testpm :: *pmfn)() = &Testpm :: m_func1;
  13.     int Testpm :: *pmd = &Testpm :: m_num;
  14.     int main() 
  15.     {
  16.         Testpm ATestpm;
  17.         Testpm *pTestpm = new Testpm;
  18.         (ATestpm.*pmfn)();
  19.         (pTestpm ->* pmfn)();
  20.         ATestpm.*pmd = 1;
  21.         pTestpm ->* pmd = 2;
  22.         cout << ATestpm.*pmd << endl
  23.         << pTestpm ->* pmd << endl;
  24.     }

a) func1
b)

   func1
   func1

c)

   1
   2

d)

   func1
   func1
   1
   2

View Answer

Answer: d
Clarification: In this program, As we are passing the value twice to the method
in the class, It is printing the func1 twice and then it is printing the given
value.
Output:

$ g++ ptm1.cpp
$ a.out
func1
func1
1
2

 
 

6. What will be the output of the following C++ code?

  1.     #include 
  2.     using namespace std;
  3.     class Car
  4.     {
  5.         public:
  6.         int speed;
  7.     };
  8.     int main()
  9.     {
  10.         int Car :: *pSpeed = &Car :: speed;
  11.         Car c1;
  12.         c1.speed = 1;           
  13.         cout << c1.speed << endl;
  14.         c1.*pSpeed = 2;     
  15.         cout  << c1.speed << endl;
  16.         return 0;
  17.     }

a) 1
b) 2
c) Both 1 & 2
d) 4
Answer: c
Clarification: In this program, We are printing the value by direct access and another one by using pointer to member.
Output:

$ g++ ptm2.cpp
$ a.out
1
2

7. What will be the output of the following C++ code?

  1.     #include 
  2.     using namespace std;
  3.     class bowl 
  4.     {
  5.         public:
  6.         int apples;
  7.         int oranges;
  8.     };
  9.     int count_fruit(bowl * begin, bowl * end, int bowl :: *fruit)
  10.     {
  11.         int count = 0;
  12.         for (bowl * iterator = begin; iterator != end; ++ iterator)
  13.             count += iterator ->* fruit;
  14.         return count;
  15.     }
  16.     int main()
  17.     {
  18.         bowl bowls[2] = {{ 1, 2 },{ 3, 5 }};
  19.         cout << "I have " << count_fruit(bowls, bowls + 2, & bowl :: apples) << " applesn";
  20.         cout << "I have " << count_fruit(bowls, bowls + 2, & bowl :: oranges) << " orangesn";
  21.         return 0;
  22.     }

a)

   I have 4 apples
   I have 7 oranges

b)

   I have 3 apples
   I have 5 oranges

c)

   I have 1 apples
   I have 5 oranges

d)

   I have 1 apples
   I have 7 oranges

View Answer

Answer: a
Clarification: In this program, We are passing the value to the class and adding the values and printing it in the main.
Output:

$ g++ ptm3.cpp
$ a.out
I have 4 apples
I have 7 oranges

 
 

8. What will be the output of the following C++ code?

  1.     #include 
  2.     using namespace std;
  3.     class Foo
  4.     {
  5.         public:
  6.         Foo(int i = 0){ _i = i;}
  7.         void f()
  8.         {
  9.             cout << "Executed"<<endl;
  10.         }
  11.         private:
  12.         int _i;
  13.     };
  14.     int main()
  15.     {
  16.         Foo *p = 0;
  17.         p -> f();
  18.     }

a) Executed
b) Error
c) Runtime error
d) 10
Answer: a
Clarification: In this program, We passes the value to the class and printing it.
Output:

$ g++ ptm4.cpp
$ a.out
Executed

9. Which is the best design choice for using pointer to member function?
a) Interface
b) Class
c) Structure
d) Block
Answer: a
Clarification: Interface is the best design choice for using pointer to member function.

10. What is the operation for .*?
a) It combines the first operand and the second operand
b) It separates the first operand and the second operand
c) It reduces the data size
d) It combines the first operand and the second operand and terminates third operand
Answer: a
Clarification: The binary operator .* combines its first operand, which must be an object of class type, with its second operand, which must be a pointer-to-member type.

contest

250+ TOP MCQs on STL Container Any – 1 and Answers

C++ Programming Multiple Choice Questions & Answers (MCQs) on “STL Container Any – 1”.

1. What is any in C++?
a) STL container used to store a single value of any type
b) Exception class in C++
c) Fundamental type provided by C++
d) Template data type
Answer: a
Clarification: Any is an STL container provided by C++ to store value or objects of any type.

2. In how many different ways any-container can be constructed?
a) 1
b) 2
c) 3
d) 4
Answer: c
Clarification: There are three basic ways of constructing any variable. They are done using copy initialization, using the constructor or using an assignment operator.

3. What is the correct syntax of constructing any using copy initialization?
a) any variable_name = value;
b) any variable_name(value);
c)

any variable_name;
variable_name = value;

d) any variable_name = value;
Answer: a
Clarification: To initialize an any variable using copy initialization we use the following syntax:

any variable_name = value;

4. What is the correct syntax of constructing any using parameterized constructor?
a) any variable_name = value;
b) any variable_name(value);
c)

any variable_name;
variable_name = value;

d) any variable_name = value;
Answer: b
Clarification: To initialize an any variable using parameterized constructor we use the following syntax:

any variable_name(value);

5. What is the correct syntax of constructing any using assignment operator?
a) any variable_name = value;
b) any variable_name(value);
c)

any variable_name;
variable_name = value;

d) any variable_name = value;
Answer: b
Clarification: To initialize an any variable using assignment operator we use the following syntax:

any variable_name;
variable_name = value;

6. Which of the following syntax is used to convert any variable to its original type?
a) any_cast();
b) any_cast(variable_name);
c) (variable_name);
d) any_cast(variable_name);
Answer: d
Clarification: The syntax used to convert the any variable to its original type is as follows:

any_cast(variable_name);

7. Which header file is required to use any container?
a)
b)
c)
d)
Answer: a
Clarification: header file is required to use any container and its realted functions.

8. What will be the output of the following C++ code?

#include
#include
using namespace std;
int main()
{
	int a = 5;
	any var = a;
	cout<<var<<endl;
	return 0;
}

a) 5
b) Compile-time error
c) Run-time error
d) Nothing is printed
Answer: b
Clarification: C++ does not allow programmer to directly print the value of any container variable. One need type cast the any variable before printing.

9. What will be the output of the following C++ code?

#include
#include
#include
using namespace std;
int main()
{
	string s = "Hello World";
	any var(s);
	cout<<any_cast<string>(var)<<endl;
	return 0;
}

a) Run-time error
b) Compile-time error
c) Hello World
d) Nothing is printed
Answer: d
Clarification: In the above program as we have converted the value to its original type before printing therefore the program runs perfectly and outputs “Hello World”.

10. What will be the output of the following C++ code?

#include
#include
#include
using namespace std;
int main()
{
	string s = "Hello World";
	any var(s);
	cout<<any_cast<char*>(var)<<endl;
	return 0;
}

a) Hello World
b) Compile-time error
c) Run-time error
d) Nothing is printed
Answer: c
Clarification: In this program as we are trying to convert an string into char* which is not same therefore the program gives run-time error saying bad_any_cast.

250+ TOP MCQs on Standard Library Algorithms and Answers

This section on online C++ test on “Standard Library Algorithms”. 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 “Standard Library Algorithms” along with answers, explanations and/or solutions:

1. What is the header file used for declaring the standard library algorithms?
a) container
b) algorithm
c) library
d) iterator
Answer: b
Clarification: C++ Standard Library, algorithms are components that perform algorithmic operations on containers and other sequences. For this operation, We have to use header file.

2. Pick out the correct method in the c++ standard library algorithm.
a) mismatch
b) maximum
c) minimum
d) maxmatch
Answer: a
Clarification: It is a method in the search operation in standard library algorithms.

3. What is the use of make_heap in the heap operation?
a) Rearrange a heap
b) Deform a heap
c) Form a heap
d) Delete a heap
Answer: c
Clarification: It is used to rearranges a range so that it becomes a heap.

4. What will be the output of the following C++ code?

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     int main ()
  6.     {
  7.         int first[] = {5, 10, 15, 20, 25};
  8.         int second[] = {50, 40, 30, 20, 10};
  9.         vector<int> v(10);      
  10.         vector<int> :: iterator it;
  11.         sort (first, first + 5);   
  12.         sort (second, second + 5); 
  13.         it = set_union (first, first + 5, second, second + 5, v.begin());
  14.         cout << int(it - v.begin());
  15.         return 0;
  16.     }

a) 6
b) 7
c) 8
d) 9
Answer: c
Clarification: In this program, We used the union function to find the number of elements.
Output:

5. What will be the output of the following C++ code?

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     int main () 
  6.     {
  7.         vector<int> myvector (4);
  8.         fill (myvector.begin(), myvector.begin() + 2, 3);
  9.         fill (myvector.begin() + 1, myvector.end() - 1, 4);
  10.         for (vector<int> :: iterator it = myvector.begin(); it != myvector.end(); ++it)
  11.             cout << ' ' << *it;
  12.         return 0;
  13.     }

a) 3 4
b) 3 4 4
c) 3 4 & 3 4 4
d) 3 4 4 0
Answer: d
Clarification: In this program, We filled out the vector values by using criteria in the for loop.
Output:

$ g++ sla1.cpp
$ a.out
3 4 4 0

6. What will be the output of the following C++ code?

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     int main () 
  6.     {
  7.         vector<int> myvector;
  8.         for (int i = 1; i < 6; ++i) 
  9.             myvector.push_back(i);
  10.         reverse(myvector.begin(), myvector.end());
  11.         for (vector<int> :: iterator it = myvector.begin(); it != myvector.end(); ++it)
  12.             cout << ' ' << *it;
  13.         return 0;
  14.     }

a) 1 2 3 4 5
b) 5 4 3 2 1
c) 0 1 2 3 4
d) 5 4 1 2 3
Answer: b
Clarification: In this program, We reversed the vector values by using the reverse function.
Output:

$ g++ sla2.cpp
$ a.out
5 4 3 2 1

7. What will be the output of the following C++ code?

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     int main () 
  6.     {
  7.         int myints[] = {10, 20, 30, 30, 20, 10, 10, 20};
  8.         int mycount = count (myints, myints + 8, 10);
  9.         cout << "10 appears " << mycount << " times.n";
  10.         vector<int> myvector (myints, myints+8);
  11.         mycount = count (myvector.begin(), myvector.end(), 20);
  12.         cout << "20 appears " << mycount  << " times.n";
  13.         return 0;
  14.     }

a) 3 3
b) 3 1
c) 8
d) 9
Answer: a
Clarification: In this program, We are counting the number of 10’s and 20’s in the myints.
Output:

$ g++ sla3.cpp
$ a.out
10 appears 3 times
20 appears 3 times

8. What will be the output of the following C++ code?

  1.     #include  
  2.     #include 
  3.     using namespace std;
  4.     int main () 
  5.     {
  6.         int myints[] = {10, 20, 30, 30, 20, 10, 10, 20};  
  7.         int* pbegin = myints;                      
  8.         int* pend = myints + sizeof(myints) / sizeof(int);
  9.         pend = remove (pbegin, pend, 20);      
  10.         for (int* p = pbegin; p != pend; ++p)
  11.             cout << ' ' << *p;
  12.         return 0;
  13.     }

a) 10, 20, 30, 30, 20, 10, 10, 20
b) 10, 30, 30, 10, 10
c) 10, 20, 20, 10, 10, 10, 20
d) 10, 20, 20, 10, 30, 10, 15
Answer: b
Clarification: In this program, We are removing all the 20’s and then we are
printing the remaining.
Output:

$ g++ sla4.cpp
$ a.out
10, 30, 30, 10, 10

9. What is the type of the first item in the heap?
a) Bigger than others
b) Lower than others
c) Mean value of the heap
d) Equal to others
Answer: a
Clarification: In C++, when we say heap we mean max heap and first element of max is bigger than others.

10. Pick out the correct library in the following choices.
a) Search
b) Generate
c) Numeric
d) All of the mentioned
Answer: d
Clarification: These are the available libraries in C++.

250+ TOP MCQs on String Characters and Answers

This section on C++ questions and puzzles on “String Characters”. 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 “String Characters” along with answers, explanations and/or solutions:

1. Which is an instantiation of the basic_string class template?
a) Character
b) String class
c) Memory
d) Iterator
Answer: b
Clarification: The string class is an instantiation of the basic_string class template.

2. Which character is used to terminate the string?
a) $
b) Null
c) Empty
d) @
Answer: b
Clarification: A string of characters is stored in successive elements of a character array are terminated by the NULL character.

3. How does the strings are stored in the memory?
a) Contiguous
b) Non-contiguous
c) Null
d) sequence
Answer: a
Clarification: The characters of a string are stored contiguously in the memory.

4. What will be the output of the following C++ code?

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         string str;
  7.         string str2="Steve jobs";
  8.         string str3="He founded apple";
  9.         str.append(str2);
  10.         str.append(str3, 6, 3);
  11.         str.append(str3.begin() + 6, str3.end());
  12.         str.append(5,0x2e);
  13.         cout << str << 'n';
  14.         return 0;
  15.     }

a) Steve jobs
b) He founded apple
c) Steve
d) Steve jobsndended apple…..
Answer: d
Clarification: In this program, We are adding characters to the string, So the output will as follows after addition of characters.
Output:

$ g++ str.cpp
$ a.out
Steve jobsndended apple.....

5. What will be the output of the following C++ code?

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         string str ("Test string");
  7.         for ( string :: iterator it = str.begin(); it != 5; ++it)
  8.             cout << *it;
  9.         return 0;
  10.     }

a) Test
b) string
c) Test string
d) Error
Answer: d
Clarification: In the for loop, We are not allowed to give a numeric value in string, So it is arising an error.

6. What will be the output of the following C++ code?

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         string name ("Jobs");
  7.         string family ("Steve");
  8.         name += " Apple ";
  9.         name += family;
  10.         name += 'n';
  11.         cout << name;
  12.         return 0;
  13.     }

a) Steve Jobs
b) Apple
c) Jobs Apple Steve
d) Apple Steve
Answer: c
Clarification: In this program, We are adding the characters at the end of the current value.
Output:

$ g++ str1.cpp
$ a.out
Jobs Apple Steve

7. What will be the output of the following C++ code?

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         string str="Steve Jobs founded the apple";
  7.         string str2 = str.substr (6, 4);
  8.         unsigned pos = str.find("the");
  9.         string str3 = str.substr (pos);
  10.         cout << str2 << ' ' << str3 << 'n';
  11.         return 0;
  12.     }

a) Jobs the apple
b) the apple
c) Steve
d) Jobs
Answer: a
Clarification: In this program, We are finding the substring of the string by using the substr function.
Output:

$ g++ str2.cpp
$ a.out
Jobs the apple

8. What will be the output of the following C++ code?

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         string str ("Steve jobs");
  7.         cout << str.length();
  8.         return 0;
  9.     }

a) 8
b) 10
c) 12
d) 9
Answer: b
Clarification: In this program, We are finding the length of the string.
Output:

$ g++ str3.cpp
$ a.out
10

9. Where are the strings stored?
a) Stack
b) Heap
c) Both Stack & Heap
d) Queue
Answer: c
Clarification: Dynamic strings(dynamic length) are stored in heap and static string(with fixed size) are stored in stack.

10. What will happen if a string is empty?
a) It can’t be created
b) Raises an error
c) It can be used
d) It cannot be used
Answer: c
Clarification: An empty string is a character array with the NULL character in the zeroth index position.

250+ TOP MCQs on Lambda Expressions and Answers

C++ Programming Multiple Choice Questions & Answers (MCQs) on “Lambda Expressions”.

1. What is lambda expression in C++?
a) A technique of C++ that allows us to write inline functions without a name
b) A technique of C++ that allows us to write overloaded functions
c) A technique of C++ that allows us to write functions that are called more than once
d) A technique of C++ that allows us to write functions without parameters
Answer: a
Clarification: Lambda expression is a technique available in C++ that helps the programmer to write inline functions that will be used once in a program and so there is no need of providing names top them. Hence they are a type of inline functions without names.

2. What is the syntax of defining lambda expression?
a) [capture clause](parameters) -> return_type { body of the function }
b) [parameters](capture clause) -> return_type { body of the function }
c) [parameters:capture clause]() -> return_type { body of the function }
d) [capture clause:parameters]() -> return_type { body of the function }
Answer: a
Clarification: The correct syntax of defining a lambda expression is given below:

[capture clause](parameters) -> return_type 
{ 
	the body of the function 
}

3. What is the correct statement about lambda expression?
a) The return type of lambda expression can be neglected in some cases
b) The return type of lambda expression must be specified in all cases
c) Lambda expression should be very large functions
d) Lambda expression is also available in C
Answer: a
Clarification: Return type in lambda expression can be ignored in some cases as the compiler will itself figure that out but not in all cases. Lambda expression is used to define small functions, not large functions. Lambda expression is introduced in C++.

4. In how many ways we can capture the external variables in the lambda expression?
a) 1
b) 2
c) 3
d) 4
Answer: c
Clarification: There are three ways in which we can capture the external variables inside the lambda expression namely capture by reference, capture by value and capture by both that is mixed capture.

5. Which of the following operator is used to capture all the external variable by reference?
a) &
b) =
c) *
d) &&
Answer: a
Clarification: The lambda expression uses & operator to capture the external variable by reference.

6. Which of the following operator is used to capture all the external variable by value?
a) &
b) =
c) *
d) &&
Answer: b
Clarification: The lambda expression uses = operator to capture the external variable by value.

7. Which is the correct syntax of capturing a variable ‘X’ by reference and other variable ‘Y’ by value in lambda expression?
a) [&X, Y]
b) [X, &y]
c) [X, Y]
d) [&x, &Y]
Answer: a
Clarification: In order to capture a variable by reference we use & operator whereas when we capture a single variable by value then we just write the name of that variable without any operator preceding it, So the correct way of capturing the variables X and Y, in this case, is [&X, Y].

8. What will be the output of the following C++ code?

#include
using namespace std;
int main()
{
	int x = 5;
	auto check = []() -> bool 
        {
		if(x == 0)
			return false;
		else
			return true;
	};
	cout<<check()<<endl;
	return 0;
}

a) 1
b) 0
c) Error
d) Segmentation fault
Answer: c
Clarification: The above code gives an error because x is neither passed as a parameter in lambda expression nor it is declared as a local variable inside the expression. So the only x that will be referred is the outside x but as the lambda expression does not capture any variable, therefore, it is also not allowed to access the external variable x hence as variable x is not defined therefore the program gives the error.

9. What will be the output of the following C++ code?

#include
using namespace std;
int main()
{
	int a = 5;
	auto check = [](int x) 
        {
		if(x == 0)
			return false;
		else
			return true;
	};
	cout<<check(a)<<endl;
	return 0;
}

a) 0
b) 1
c) Error
d) Segmentation fault
Answer: b
Clarification: The program is correct. In this program you can observe that we have specified the return type of the expression though also the program runs fine because compiler is able to find out the return type of the expression.

10. What will be the output of the following C++ code?

#include
using namespace std;
int main()
{
	int a = 5;
	auto check = [=]() 
        {
		a = 10;
	};
	check();
	cout<<"Value of a: "<<a<<endl;
	return 0;
}

a) Value of a: 5
b) Value of a: 10
c) Error
d) Segmentation fault
Answer: c
Clarification: As this lambda expression is capturing the extrenal variable by value therefore the value of a cannot be changes inside the lambda expression hence the program gives error.

11. What will be the output of the following C++ code?

#include
using namespace std;
int main()
{
	int a = 5;
	auto check = [&]() 
        {
		a = 10;
	};
	check();
	cout<<"Value of a: "<<a<<endl;
	return 0;
}

a) Value of a: 5
b) Value of a: 10
c) Error
d) Segmentation fault
Answer: b
Clarification: As this lambda expression is capturing the extrenal variable by reference therefore the change in value of a will be reflected back outside the expression therefore the value of a will now be 10 and 10 is printed.

12. What will be the output of the following C++ code?

#include
using namespace std;
int main()
{
	int a = 5;
	int b = 5;
	auto check = [&a]() 
        {
		a = 10;
		b = 10;
	}
	check();
	cout<<"Value of a: "<<a<<endl;
	cout<<"Value of b: "<<b<<endl;
	return 0;
}

a) Value of a: 5
b) Value of a: 10
c) Error
d) Segmentation fault
Answer: c
Clarification: As this lambda expression is not capturing variable b but trying to access the external variable b hence the program gives an error.

250+ TOP MCQs on C++ Concepts – 3 and Answers

C++ Programming Question Paper on “C++ Concepts – 3”.

1. Which of the following statement is correct?
a) Structure in C allows Constructor definition
b) Structure in C++ allows Constructor definition
c) Both allow Constructor definition
d) C allows constructor definition while C++ does not
Answer: b
Clarification: As C does not allow the programmer to define a function inside a structure and constructor itself is a function, therefore, the constructor definition is not allowed in C whereas such definitions are allowed in C++.

2. What happens if the following code is compiled on both C and C++?

#include
struct STRUCT
{
private:
	int a;
};
int main()
{
	printf("%dn", (int)sizeof(struct STRUCT));
	return 0;
}

a) The program runs fine and both prints output “HELLO THIS IS STRUCTURE”
b) The program gives an error in case of C but runs perfectly in case of C++
c) The program gives an error in case of C++ but runs perfectly in case of C
d) The program gives an error in case of both C and C++
Answer: b
Clarification: Access specifiers like private, protected and the public are used because the OOPs concept and as C is not an Object Oriented language, therefore, access specifiers are not defined in C and hence C gives error whereas C++ does not.

3. Which of the following is correct about this pointer in C++?
a) this pointer is passed as a hidden argument in all the functions of a class
b) this pointer is passed as a hidden argument in all non-static functions of a class
c) this pointer is passed as a hidden argument in all static functions of a class
d) this pointer is passed as a hidden argument in all static variables of a class
Answer: b
Clarification: As static functions are a type of global function for a class so all the object shares the common instance of that static function whereas all the objects have there own instance for non-static functions and hence they are passed as a hidden argument in all the non-static members but not in static members.

4. Which of the following operator is used with this pointer to access members of a class?
a) .
b) !
c) ->
d) ~
Answer: c
Clarification: this pointer is a type of pointer and as we know pointer object uses the arrow(->) operator to access the members of the class, therefore, this pointer uses -> operator.

5. Why this pointer is used?
a) To access the members of a class which have the same name as local variables in that scope
b) To access all the data stored under that class
c) To access objects of other class
d) To access objects of other variables
Answer: a
Clarification: this pointer is used to access the members of a class which have the same name as local variables in that part of the code.

6. How many types of polymorphism are there?
a) 1
b) 2
c) 3
d) 4
Answer: b
Clarification: There are two types of polymorphism in C++ namely compile-time polymorphism and run-time polymorphism.

7. What is the other name of compile-time polymorphism?
a) Static polymorphism
b) Dynamic polymorphism
c) Executing polymorphism
d) Non-executing polymorphism
Answer: a
Clarification: Compile-time polymorphism is also known as static polymorphism as it is implemented during the compile-time.

8. What is the other name of run-time polymorphism?
a) Static polymorphism
b) Dynamic polymorphism
c) Executing polymorphism
d) Non-executing polymorphism
Answer: b
Clarification: Run-time polymorphism is also known as dynamic polymorphism as it is implemented during the run-time of the program.

9. Which of the following is correct about static polymorphism?
a) In static polymorphism, the conflict between the function call is resolved during the compile time
b) In static polymorphism, the conflict between the function call is resolved during the run time
c) In static polymorphism, the conflict between the function call is never resolved during the execution of a program
d) In static polymorphism, the conflict between the function call is resolved only if it required
Answer: a
Clarification: The conflict between which function to call is resolved during the compile time in static polymorphism i.e. before the execution of the program starts.

10. Which of the following is correct about dynamic polymorphism?
a) In dynamic polymorphism, the conflict between the function call is resolved during the compile time
b) In dynamic polymorphism, the conflict between the function call is resolved during the run time
c) In dynamic polymorphism, the conflict between the function call is never resolved during the execution of the program
d) In dynamic polymorphism, the conflict between the function call is resolved at the beginning of the program
Answer: b
Clarification: The conflict between which function to call is resolved during the run time in dynamic polymorphism i.e. the conflict is resolved when the execution reaches the function call statement.

11. Which of the following operator(s) can be used with pointers?
i) – only
ii) +, *
iii) +, –
iv) +, -, *
v) /
vi) + only
a) i only
b) vi only
c) ii and v
d) iv
Answer: a
Clarification: The only arithmetic operator that can be used with a pointer is – subtraction operator. No arithmetic operator can be used with pointers.

12. What is std in C++?
a) std is a standard class in C++
b) std is a standard namespace in C++
c) std is a standard header file in C++
d) std is a standard file reading header in C++
Answer: b
Clarification: std is a standard namespace present in C++ which contains different stream classes and objects like cin, cout, etc. and other standard functions.

13. What will be the output of the following C++ code?

#include 
 
int main(int argc, char const *argv[])
{
	cout<<"Hello World";
	return 0;
}

a) Hello World
b) Compile-time error
c) Run-time error
d) Segmentation fault
Answer: b
Clarification: cout is defined under the namespace std and without including std namespace we cannot cout, therefore, the program gives an error.

14. Which of the following syntax can be used to use a member of a namespace without including that namespace?
a) namespace::member
b) namespace->member
c) namespace.member
d) namespace~member
Answer: a
Clarification: To use a member of a namespace without including the namespace is done by this syntax namespace::member.

15. Which of the following C++ code will give error on compilation?

================code 1=================
#include 
using namespace std;
int main(int argc, char const *argv[])
{
	cout<<"Hello World";
	return 0;
}
========================================
================code 2=================
#include 
int main(int argc, char const *argv[])
{
	std::cout<<"Hello World";
	return 0;
}
========================================

a) Both code 1 and code 2
b) Code 1 only
c) Code 2 only
d) Neither code 1 nor code 2
Answer: d
Clarification: Neither code 1 nor code2 will give error as both are syntactically correct as in first code we have included namespace std and in second one we have used scope resolution operator to resolve the conflict.