C++ Programming Questions and Answers – C++ Concepts – 2

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “C++ Concepts – 2”.

1. Which of the following is the scope resolution operator?
a) .
b) *
c) ::
d) ~
View Answer

Answer: c
Explanation: :: operator is called scope resolution operator used for accessing a global variable from a function which is having the same name as the variable declared in the function.

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

#include<iostream>
using namespace std;
int x = 1;
int main()
{
    int x = 2;
    {
        int x = 3;
        cout << ::x << endl;
    }
    return 0;
}

a) 1
b) 2
c) 3
d) 123
View Answer

Answer: a
Explanation: While printing x we are using :: operator hence the refernce is given to global variable hence the global variable x = 1 is printed.
advertisement
advertisement

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

Note: Join free Sanfoundry classes at Telegram or Youtube
#include<iostream>
using namespace std;
class A
{
  ~A(){
    cout<<"Destructor called\n";
  }
};
int main()
{
    A a;
    return 0;
}

a) Destructor called
b) Nothing will be printed
c) Error
d) Segmentation fault
View Answer

Answer: c
Explanation: Whenever a destructor is private then one should not define any normal object as it will be destroyed at the end of the program which will call destructor and as destructor is private the program gives error during compile while in case of pointer object the compiler at compile does not know about the object, therefore, does not gives compile error. Hence when the destructor is private then the programmer can declare pointer object but cannot declare a normal object.
advertisement

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

#include<iostream>
using namespace std;
class A
{
  ~A(){
     cout<<"Destructor called\n";
   }
};
int main()
{
    A *a1 = new A();
    A *a2 = new A();
    return 0;
}

a) Destructor called
b)

Destructor called
Destructor called
advertisement

c) Error
d) Nothing is printed
View Answer

Answer: d
Explanation: The pointer object is created is not deleted hence the destructor for these objects is not called hence nothing is printed on the screen.

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

#include<iostream>
using namespace std;
int x[100];
int main()
{
    cout << x[99] << endl;
}

a) Garbage value
b) 0
c) 99
d) Error
View Answer

Answer: b
Explanation: In C++ all the uninitialized variables are set to 0 therefore the value of all elements of the array is set to 0.

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

#include<iostream>
using namespace std;
int main ()
{
   int cin;
   cin >> cin;
   cout << "cin: " << cin;
   return 0;
}

a) cin: garbage value
b) Error
c) Segmentation fault
d) Nothing is printed
View Answer

Answer: a
Explanation: cin is a variable hence overrides the cin object. cin >> cin has no meaning so no error.

7. Which of the following operator has left to right associativity?
a) Unary operator
b) Logical not
c) Array element access
d) addressof
View Answer

Answer: c
Explanation: Array element has left to right associativity i.e. expressions are evaluated from left to right in case of array element access.

8. Which of the following is accessed by a member function of a class?
a) The object of that class
b) All members of a class
c) The public part of a class
d) The private part of a class
View Answer

Answer: b
Explanation: A member function of a class can access all the members of its class whether they are private, protected or public.

9. What is the size of a character literal in C and C++?
a) 4 and 1
b) 1 and 4
c) 1 and 1
d) 4 and 4
View Answer

Answer: a
Explanation: The size of a character literal is 4 in case of C but it is one in case of C++. You can do printf(“%d”, (int)sizeof(‘a’)); in both C and C++ to check this.

10. What is the size of a character type in C and C++?
a) 4 and 1
b) 1 and 4
c) 1 and 1
d) 4 and 4
View Answer

Answer: c
Explanation: The size of a character type in both C and C++ is 1. You can do printf(“%d”, (int)sizeof(char)); in both C and C++ to check this.

11. Which of the following is correct?
a) struct tag is required in both C and C++ while declaring an object of the structure
b) struct is not required in C but required in C++ while declaring an object of the structure
c) struct is not required in C++ but required in C while declaring an object of the structure
d) struct tag is not required in both C and C++ while declaring an object of the structure
View Answer

Answer: c
Explanation: C++ does not require struct keyword while declaring an object of the structure whereas in C we require struct tag for declaring an object.

12. Which of the following is correct?
a) struct cannot have member function in C but it can in C++
b) struct cannot have member function in C++ but it can in C
c) struct cannot have member function in both C and C++
d) struct can have member function in both C and C++
View Answer

Answer: a
Explanation: struct can have member function in C++ whereas member functions are not allowed in case of C.

13. What happens if we run the following code in both C and C++?

#include<stdio.h>
struct STRUCT
{
  int a;
  int func()
  {
      printf("HELLO THIS IS STRUCTURE\n");
  }
};
int main()
{
  struct STRUCT s;
  s.func();
  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++
View Answer

Answer: b
Explanation: As C does not allows the structure to have member functions, therefore, it gives an error in case of C but as C++ does allow structures to have member functions, therefore, the C++ does not give an error.

14. What happens if we run the following code in both C and C++?

#include<stdio.h>
struct STRUCT
{
  int a = 5;
  int func()
  {
      printf("%d\n", a);
  }
};
int main()
{
  struct STRUCT s;
  s.func();
  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++
View Answer

Answer: b
Explanation: As C does not allows to initialize any member inside the structure, therefore, the program gives error whereas in case of C++ this is allowed therefore the program does not give any error.

15. What happens if the following program is compiled in both C and C++?

#include<stdio.h>
struct STRUCT
{
  int static a;
};
int main()
{
  struct STRUCT s;
  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++
View Answer

Answer: b
Explanation: C does not allow the programmer to declare any static members inside a class whether in C++ it is allowed to declare static variables.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.