πŸ“Š CPP Programming
Q. Output of following C++ program?
#include<iostream>
using namespace std;
int main()
{
int x = 10;
int& ref = x;
ref = 20;
cout << "x = " << x << endl ;
x = 30;
cout << "ref = " << ref << endl;
return 0;
}
  • (A) x = 20; ref = 30
  • (B) x = 20; ref = 20
  • (C) x = 10; ref = 30
  • (D) x = 30; ref = 30
πŸ’¬ Discuss
βœ… Correct Answer: (A) x = 20; ref = 30
πŸ“Š CPP Programming
Q. What is the difference between struct and class in C++?
  • (A) All members of a structure are public and structures don’t have constructors and destructors
  • (B) Members of a class are private by default and members of struct are public by default. When deriving a struct from a class/struct, default access-specifier for a base class/struct is public and when deriving a class, default access specifier is private.
  • (C) All members of a structure are public and structures don’t have virtual functions
  • (D) All of the above
πŸ’¬ Discuss
βœ… Correct Answer: (B) Members of a class are private by default and members of struct are public by default. When deriving a struct from a class/struct, default access-specifier for a base class/struct is public and when deriving a class, default access specifier is private.
πŸ“Š CPP Programming
Q. Predict the output of following C++ program.
#include<iostream>
using namespace std;
class Empty {};
int main() {
cout << sizeof(Empty);
return 0;
}
  • (A) A non-zero value
  • (B) 0
  • (C) Compiler Error
  • (D) Runtime Error
πŸ’¬ Discuss
βœ… Correct Answer: (A) A non-zero value
πŸ“Š CPP Programming
Q. class Test {
int x;
};
int main() {
Test t;
cout << t.x;
return 0;
}
  • (A) 0
  • (B) Garbage Value
  • (C) Compiler Error
  • (D) None
πŸ’¬ Discuss
βœ… Correct Answer: (C) Compiler Error
πŸ“Š CPP Programming
Q. Which of the following is true?
  • (A) All objects of a class share all data members of class
  • (B) Objects of a class do not share non-static members. Every object has its own copy.
  • (C) Objects of a class do not share codes of non-static methods, they have their own copy
  • (D) None of the above
πŸ’¬ Discuss
βœ… Correct Answer: (B) Objects of a class do not share non-static members. Every object has its own copy.
πŸ“Š CPP Programming
Q. A member function can always access the data in __________, (in C++).
  • (A) the class of which it is member
  • (B) the object of which it is a member
  • (C) the public part of its class
  • (D) the private part of its class
πŸ’¬ Discuss
βœ… Correct Answer: (A) the class of which it is member
πŸ“Š CPP Programming
Q. Which of the following is not correct for virtual function in C++?
  • (A) Must be declared in public section of class.
  • (B) Virtual function can be static.
  • (C) Virtual function should be accessed using pointers.
  • (D) Virtual function is defined in base class.
πŸ’¬ Discuss
βœ… Correct Answer: (B) Virtual function can be static.
πŸ“Š CPP Programming
Q. Which of the following is not correct (in C++)?
1. Class templates and function templates are instantiated in the same way
2. Class templates differ from function templates in the way they are initiated
3. Class template is initiated by defining an object using the template argument
4. Class templates are generally used for storage classes
  • (A) (1)
  • (B) (2), (4)
  • (C) (2), (3), (4)
  • (D) (4)
πŸ’¬ Discuss
βœ… Correct Answer: (C) (2), (3), (4)
πŸ“Š CPP Programming
Q. Which of the following cannot be passed to a function in C++?
  • (A) Constant
  • (B) Structure
  • (C) Array
  • (D) Header file
πŸ’¬ Discuss
βœ… Correct Answer: (D) Header file
πŸ“Š CPP Programming
Q. Which of the following, in C++, is inherited in a derived class from base class?
  • (A) Constructor
  • (B) Destructor
  • (C) Data members
  • (D) Virtual methods
πŸ’¬ Discuss
βœ… Correct Answer: (C) Data members

Jump to