C++ Online Quiz


Advertisements


Following quiz provides Multiple Choice Questions (MCQs) related to C++ Framework. You will have to read all the given answers and click over the correct answer. If you are not sure about the answer then you can check the answer using Show Answer button. You can use Next Quiz button to check new set of questions in the quiz.

Questions and Answers

Q 1 - The default access specifer for the class members is

A - public

B - private

C - protected

D - None of the above.

Answer : B

Explaination

If a member/s appear in the class with following no access specifier, the default is private.

Answer : C

Explaination

If no access specifiers are specified for structure variables/functions, then the default is considered as public.

Q 3 - We can have varying number of arguments for the overloaded form of () operator.

A - True

B - False

Answer : A

Explaination

Q 4 - We can use this pointer in static member function of the class.

A - True

B - False

Answer : B

Explaination

False, as the static members do exist before the object of the respective class are created. 'this' is not applicable in the said context.

Q 6 - An array can be passed to the function with call by value mechanism.

A - True

B - False

Answer : B

Explaination

An array never is passed with call by value mechanism

Q 7 - What is the output of the following program?

#include<iostream>

using namespace std;
main() {
   char s[] = "hello", t[] = "hello";
   if(s==t)
      cout<<"eqaul strings";
}

A - Equal strings

B - Unequal strings

C - No output

D - Compilation error

Answer : C

Explaination

No output, as we are comparing both base addresses and are not same.

#include<iostream>

using namespace std;
main() {
   char s[] = "hello", t[] = "hello";
   if(s==t)
      cout<<"eqaul strings";
}

Q 8 - What will be the output of the following program?

#include<iostream>
#include<string.h>

using namespace std;
main() {
   cout<<strcmp("strcmp()","strcmp()");
}

A - 0

B - 1

C - -1

D - Invalid use of strcmp() function

Answer : A

Explaination

0, strcmp return 0 if both the strings are equal

#include<iostream>
#include<string.h>

using namespace std;
main() {
   cout<<strcmp("strcmp()","strcmp()");
}

Q 9 - What is the output of the following program?

#include<iostream>

using namespace std;
int x = 5;

int &f() {
	return x;
}
main() {
	f() = 10;
   cout<<x;
}

A - 5

B - Address of ‘x’

C - Compile error

D - 10

Answer : D

Explaination

A function can return reference, hence it can appear on the left hand side of the assignment operator.

#include<iostream>

using namespace std;
int x = 5;

int &f() {
	return x;
}
main() {
	f() = 10;
   cout<<x;
}

Q 10 - Does both the loops in the following programs prints the correct string length?

#include<iostream>

using namespace std;
main() {
   int i;
   
   char s[] = "hello";

   for(i=0; s[i]; ++i);
      cout<<i<<endl;

   i=0; 
   
   while(s[i++]);
      cout<<i;
}

A - Yes, both the loops prints the correct length

B - Only for loop prints the correct length

C - Only while loop prints the correct length

D - Compile error in the program.

Answer : B

Explaination

In while loop 'i' gets incremented after checking for '\0', hence giving 1 more than the length.

#include<iostream>

using namespace std;
main() {
   int i;
   
   char s[] = "hello";

   for(i=0; s[i]; ++i);
      cout<<i<<endl;

   i=0;
   
   while(s[i++]);
      cout<<i;
}

cpp_questions_answers.htm

Advertisements
E-Books Store