C++ online quiz

foxinsocks_donuts

Following quiz provides Multiple Choice Questions (MCQs) related to C++ Framework. You will have to read all the given answers and choose over the correct answer. If you are not sure about the answer then you can check the answer using Show Answer link.

1. A trigraph character begins with

A – #
B – ##
C – ?
D – ??

Show Answer
Answer: C
Explaination: Few characters have alternative representation and start with ??. Eg. Fro [ equivalent is ??(

2. ‘cin’ is an __

A – Class
B – Object
C – Package
D – Namespace

Show Answer
Answer: B
Explaination: It’s an object of istream class.

3. Operators sizeof and ?:

A – Both can be overloaded
B – Both cannot be overloaded
C – Only sizeof can be overloaded
D – Only ?: can be overloaded

Show Answer
Answer: B
Explaination: Both the mentioned operators cannot be overloaded.

4. What is the output of the following program?

#include

using namespace std;
class Base {
public:
   void f() {
      cout<<"Base\n";
      }
};
class Derived:public Base {
public:
   f() {
      cout<f();
}

A – Base
B – Derived
C – Compile error
D – None of the above

Show Answer
Answer: A
Explaination: The method f() is not overridden therefore as per the pointer type respective method is called.

#include

using namespace std;
class Base {
public:
   void f() {
      cout<<"Base\n";
      }
};
class Derived:public Base {
public:
   f() {
      cout<f();
}

5. Which operator is used to resolve the scope of the global variable?

A – −>
B – .
C – *
D – ::

Show Answer
Answer: D
Explaination: Scope resolution operator is used to resolve for the global scope of a variable if the local and global variables conflict by name.

6. Choose the option not applicable for the constructor.

A – Cannot be called explicitly.
B – Cannot be overloaded.
C – Cannot be overridden.
D – None of the above.

Show Answer
Answer: C
Explaination: A constructor can’t be overridden.

7. What is the output of the following program?

#include

using namespace std;
main()
{ 
   int x = 5;

   if(x==5)
   {	
      if(x==5) break;
      cout<<"Hello";
   } 

      cout<<”Hi”; 
}

A – Compile error
B – Hi
C – HelloHi
D – Hello

Show Answer
Answer: A
Explaination: Compile error, keyword break can appear only within loop/switch statement.

#include

using namespace std;
main()
{ 
   int x = 5;

   if(x==5)
   {	
      if(x==5) break;
      cout<<"Hello";
   } 

      cout<<”Hi”; 
}

8. What is the output of the following program?

#include

using namespace std;
void swap(int m, int n)
{
   int x = m;

	m = n;
	n = x;
}
main()
{
   int x = 5, y = 3;
   
	swap(x,y);
	cout<<x<<” “<<y;
}

A – 3 5
B – 5 3
C – 5 5
D – Compile error

Show Answer
Answer: B
Explaination: 5 3, call by value mechanism can’t alter actual arguments.

#include

using namespace std;
void swap(int m, int n)
{
   int x = m;

	m = n;
	n = x;
}
main()
{
   int x = 5, y = 3;
   
	swap(x,y);
	cout<<x<<” “<<y;
}

9. What is the output of the following program?

#include

using namespace std;
void f() {
	static int i = 3;
   
	cout<<i;
	if(--i) f();
}
main() {
	f();
}

A – 3 2 1 0
B – 3 2 1
C – 3 3 3
D – Compile error

Show Answer
Answer: B
Explaination: As the static variable retains its value from the function calls, the recursion happens thrice.

#include

using namespace std;
void f() {
	static int i = 3;
   
	cout<<i;
	if(--i) f();
}
main() {
	f();
}

10. What is the output of the following program?

#include

using namespace std;
void main()
{
   char s[] = "C++";
   
	cout<<s<<" ";
	s++;
	cout<<s<<" ";
}

A – C++ C++
B – C++ ++
C – ++ ++
D – Compile error

Show Answer
Answer: D
Explaination: ‘s’ refers to a constant address and cannot be incremented.

#include

using namespace std;
void main()
{
   char s[] = "C++";
   
	cout<<s<<" ";
	s++;
	cout<<s<<" ";
}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s