Google News
logo
CPP - Quiz(MCQ)
1 .
What will be the output of the following C++ code?
 
    #include <iostream>
    using namespace std;
    int main()
    {
        char name[30];
        cout << "Enter name: ";
        gets(name);
        cout << "Name: ";
        puts(name);
        return 0;
    }
A)
compile time error
B)
jobs
C)
jobsjobs
D)
program will not run

Correct Answer : Option (A) :   compile time error


Explaination : This program will run on older version of C++ with the inclusion of #include header file, but for on new compiler C++14 and above the gets is removed from the header file so it will not run on them even after inclusion of cstdio header file.

A)
<sstring>
B)
<sstream>
C)
<string>
D)
<iostream>

Correct Answer : Option (C) :   <string>


Explanation : stringstream is available under the header file <string> in C++.

A)
arrays
B)
data
C)
functions
D)
both data & functions

Correct Answer : Option (D) :   both data & functions


Explanation : The classes in C++ encapsulates(i.e. put together) all the data and functions related to them for manipulation.

A)
object A { int x; };
B)
class A { int x; };
C)
public class A { }
D)
class B { }

Correct Answer : Option (B) :   class A { int x; };


Explanation : A class declaration terminates with semicolon and starts with class keyword.

A)
private
B)
public
C)
protected
D)
public & protected

Correct Answer : Option (A) :   private


Explanation : By default all the data members and member functions of class are private.

A)
access is public by default
B)
access is denied
C)
access is private by default
D)
access is protected by default

Correct Answer : Option (A) :   access is public by default


Explanation : For structures, by default all the data members and member functions are public.

A)
Atomic data type
B)
Derived data type
C)
User defined derived data type
D)
Fundamental data type

Correct Answer : Option (C) :   User defined derived data type


Explanation : Fundamental/Atomic data type includes int, char, float, double and void. Derived data type includes arrays, pointers, references, function and constants. User defined derived data type includes class, structure, union and enumeration.

A)
By passing self as a parameter in the member function
B)
Using * with the name of that object
C)
Using a special keyword object
D)
Using this pointer

Correct Answer : Option (D) :   Using this pointer


Explanation : In Classes objects are self-referenced using this pointer inside the member functions. for example this->value to access the data member value of that object.

A)
A member that is global throughout the class
B)
A member that can be updated even if it a member of constant object
C)
A member that can never be changed
D)
A member that can be updated only if it not a member of constant object

Correct Answer : Option (B) :   A member that can be updated even if it a member of constant object


Explanation : Mutable members are those which can be updated even if it a member of a constant object. You can change their value even from a constant member function of that class.

10 .
What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A
{
	int a = 5;
    public:
	void change(int i){
		a = i;
	}
	static void value_of_a(){
		cout<<a;
	}
};
 
int main(int argc, char const *argv[])
{
	A a1 = A();
	a1.change(10);
	a1.value_of_a();
	return 0;
}
A)
Segmentation Fault
B)
Error
C)
5
D)
10

Correct Answer : Option (B) :   Error


Explaination : As value_of_a() is a static function and static member can access only static members therefore the program will give error.