25 câu hỏi
What is the output of the following code? #include using namespace std; int main() { int a = 20, b = 100; int &n = a; n = a++; n = &b; cout << a << "," << n << endl; system("pause"); }
21,21
20,21
21,22
compile error
What is the output of the following code? #include using namespace std; int main() { int main; main = 100; cout << main++ << endl; system("pause"); }
100
101
none
compile error
The design of classes in a way that hides the details of implementation from the user is known as:
Encapsulation
Information Hiding
Data abstraction
All of the above
Which of the following keywords do you think can be used when declaring static members in a class? (i) Public (ii) Private (iii) Protected
all of above
(i)
(i), (iii)
I want a nonmember function to have access to the private members of a class. The class must declare that function:
friend
inline
static
virtual
The ability to reuse objects already defined, perhaps for a different purpose, with modification appropriate to the new purpose, is referred to as
Information hiding
Inheritance
Redefinition
Overloading
What do you think is the outcome of calling a redefined non-virtual function using a base-class pointer?
The appropriate redefined version of the function will be used
The base-class version of the function will always be used
The outcome is unpredictable
A run-time error will occur
A class member that is to be shared among all objects of a class is called
A const member
A reference parameter
A static member
A function member
What is a base class?
An abstract class that is at the top of the inheritance hierarchy.
A class with a pure virtual function in it.
A class that inherits from another class
A class that is inherited by another class, and thus is included in that class.
A variable that is declared protected:
Is visible only in the subclasses (and not in the class it is declared in)
Is visible only in the class it is declared in
Is visible to all classes, but modifiable only in the class where it is declared
Is visible in the class it is declared in, and all of its sub-classes
What is a destructor?
A function called when an instance of a class is initialized
A function that is called when an instance of a class is deleted
A special function to change the value of dynamically allocated memory
A function that is called in order to change the value of a variable
In protected inheritance:
The public members of the base class become public
The public members of the base class become protected
The protected members of the base class become private
The public members of the base class become inaccessible
If a class declares a variable static, this means:
Each instance of a class will have its own copy of the variable
Changing the variable in one instance will have no effect on other instances of the class
Changing the variable in one instance will have no effect on other instances of the class
Every instance of the class must consider the value of the static variable before initializing
In case of a copy constructor, which of the following is true?
Used to instantiate an object from another existing object
To copy one object to another existing object
Can be a substitute for a ‘=’ operator
All of the above
A class declaring another class as a friend will:
Have wine and cheese with that other friend
Allow that class to declare an instance of it in its list of private variables
Allow the other class (the one declared as friend) to access to the declaring class’s private variables
Allow the class declaring the other as a friend to access the declared class’s private variables
Which of the following can be virtual?
constructors
destructors
static functions
None of the above
Where is an exception generated?
In the catch block
In the throw clause
In the constructor of a class
Only when memory allocation fails
Static member functions ___
can be used without an instantiation of an object
can only access static data
Both 1 and 2 are correct
Neither 1 nor 2 are correct
mmmmmmmmmm
The class must not have method
The class must have a constructor that takes no arguments
The class must have a function definition equal to zero
The class may only exist during the planning phase
In the following program, how many times Base’s constructor will be called? #include using namespace std; class Base { int static i; public: Base() { cout << "Base's constructor"; }; }; class Sub1 : public virtual Base {}; class Sub2 : public Base {}; class Multi : public Sub1, public Sub2 {}; void main() { Multi m; system("pause"); }
1
2
3
error
In the following code what would be the values of i1 and i2
#include <iostream>
using namespace std; namespace N1 { int f(int n) { return n * 2; } } namespace N2 { int f(double n) { return n * 3; } } void main() { using N1::f; int i1 = f(1.0); cout << "i1 = " << i1; using N2::f; int i2 = f(1.0); cout << "i1 = " << i2; system("pause"); }
i1=2 i2=2
i1=2 i2=3
i1=3 i2=2
Error
In the following code, which of the following variables can be accessed in “Friend”? class Base { public: int a; protected: int b; private: int c; }; class Derived : Base { int d; friend Friend; }; class Friend { Derived derived; };
only a and b
a,b and c
only a
error
What is the output of the following code? #include int count = 0; class obj { public: obj() { count++; } ~obj() { count--; } }; int main() { obj A, B, C, D, E; obj F; { obj G; } std::cout << count; return 0; }
0
5
6
7
What is wrong in the following code? #include class Base { public: Base() {}; virtual ~Base() {}; }; class Derived : protected Base { public: virtual ~Derived() {}; }; int main() { Base *pb = new Derived(); return 0; }
There is nothing wrong
One cannot have a ‘Base’ pointer to ‘Derived’ since it is not derived publicly
One need a derived class pointer to point to a derived class
One required to code a constructor for Derived
What is the output of the following code? #include using namespace std; class professor { public: professor() { cout << "professor "; }; }; class researcher { public: researcher() { cout << "researcher "; }; }; class teacher : public professor { public: teacher() { cout << "teacher "; }; }; class myprofessor : public teacher, public virtual researcher { public: myprofessor() { cout << "myprofessor "; }; }; int main() { myprofessor obj; system("pause"); return 0; }
professor researcher teacher myprofessor
researcher professor teacher myprofessor
myprofessor teacher researcher professor
myprofessor researcher professor teacher
