Hướng đối tượng C++

1. A ____ is a special member function used to initialize the data members of a class.
A. constructor
B. destructor
C. static method
Đáp án

A
Hàm tạo được gọi khi tạo 1 đối tượng (object), hàm tạo khởi tạo member data của class

2. The default access for members of a class is ___ .
A. private
B. public
C. protected
D. protect
Đáp án

A

3. Member functions of a class are normally made ___ and data members of a class are normally
made ___ .

A. private, public
B. protected, public
C. public, private
D. public, protected
Đáp án

C
Khi design class, dữ liệu được đóng gói, và truy xuất dữ liệu thông qua các phương thức.
Do vậy, dữ liệu kiểu private, phương thức kiểu public.

4. Inheritance enables ___ which saves time in development , and encourages using previously
proven and high quality software.

A. reusability
B. encapsulation
C. development
Đáp án

A

5. The three member access specifiers are ___, ___ and ___ .
A. public, private, protected
B. public, private, protect
C. public, private, static
Đáp án

A

6. A “has a” relationship between classes represents ___ and an “is a” relationship between
classes represent ___ .

A. containment, inheritance
B. hiding, inheritance
C. encapsulation, inheritance
Đáp án

A

7. A pure virtual function is specified by placing ___ at the end of its prototype in the class
definition.

A. =0
B. =1
C. =-1
Đáp án

A
Hàm pure virtual được define như sau: virtual int fnc() =0;

8. A operator ___ is called as de-referencing operator.
A. &
B. *
C. &&
Đáp án

B

9. The size of a class with no data members and member functions is ___ bytes
A. 0
B. 1
C. 4
Đáp án

B

10. A class is called as abstract base class if it has a ___ function
A. pure virtual
B. static
C. private
Đáp án

A

11. Run-time or dynamic allocation of memory may be done using the C++ operator ___ .
A. new
B. alloc
C. malloc
Đáp án

A

12. Operator overloading permits to extend the applicability of existing C++ operators so that they work with new ___ or ___.
A. data types, objects
B. data types, constant
C. data types, pointer
Đáp án

A

13. A ___ is a set of instance or values.
A. class
B. object
C. function
Đáp án

B

14. C++ programming language was designed and developed by ___ at ___.
A. Steven Job, AT&T Bell Labs
B. Bjarne Stroustrup, AT&T Bell Labs
C. Guido van Rossum, M&D Lab
Đáp án

B

15. C++ is a ___ programming language with ___ extensions.
A. procedural, object oriented
B. object, procedural
C. procedural, STL
Đáp án

A

16. Pointers are ___ that contain the addresses of other variables and ____ .
A. value, object
B. object, class
C. variables, functions
Đáp án

C

17. A program can use the address of any variable in an expression, except variables declared with the ___ storage class.
A. static
B. register
C. auto
Đáp án

B
register storage class là biến được lưu ở thanh ghi, và không thể lấy địa chỉ thanh ghi bằng toán tử &

18. new operator allocates memory blocks from the ___.
A. Stack
B. Heap
C. Register
Đáp án

B

19. The new operator throws a ___ when heap is exhausted.
A. runtime exception
B. syntax error
C. logic error
Đáp án

A

20. The (assert.h) header is used for ___ .
A. debugging
B. checking memory leak
C. library for time
Đáp án

A
Thư viện (assert.h) define các function assert() sử dụng cho việc debug

21. The constructor and destructor of a class in C++ are called ___
A. automatically
B. manually
C. none of above
Đáp án

A
Hàm tạo và hàm hủy trong C/C++ được gọi tự động

22. Two or more functions may have the same name, as long as their ___ are different.
A. return type
B. parameter lists
C. none of above
Đáp án

B
Trong C++, có thể define nhiều hàm số có cùng tên được gọi là chồng hàm (overloading). Tuy nhiên, cần tuân thủ rule: các hàm số cùng tên phải có tập tham số truyền vào là khác nhau (về số lượng hoặc kiểu). Nếu 2 hàm trùng tên và cả bộ tham số truyền vào thì khi biên dịch sẽ báo lỗi kể cả 2 hàm đó khác kiểu dữ liệu trả về.

23. A constructor with default arguments for all its parameters is called a ___ constructor.
A. static
B. run-time
C. default
Đáp án

C

24. Static member functions can access only the ___ data members of a class.
A. dynamic
B. static
C. public
Đáp án

B

25. The two types of polymorphism is : ____ & ____ .
A. Run time and compile time
B. Preprocessor, compile time
C. Preprocessor, Linker
Đáp án

A

26. A file stream is an extension of a ___ stream.
A. console
B. windows
C. none of above
Đáp án

A

27. The Standard Template Library(STL) is a library of ___ templates.
A. container class
B. time class
C. none of above
Đáp án

A

28. Run time polymorphism is ___ than the compile time polymorphism.
A. less flexible
B. more flexible
C. none of above
Đáp án

B
Đa hình động (run-time) thì mềm dẻo, linh hoạt hơn đa hình tĩnh.

29. With private inheritance, public and protected members of the base class become ___ members of
the derived class.

A. private
B. public
C. protected
Đáp án

A

30. The STL Container adapters contains the ___ , ___ and ___ STL containers.
A. Stack, Queue, Priority_queue
B. Set, Multiset, Map
C. vectors, lists, deques
Đáp án

C

31. The ___ block contains code that directly or indirectly might cause an exception to be thrown.
A. catch
B. try
C. none of above
Đáp án

B

32. When writing function or class template, one use a ___ to specify a generic data type.
A. template parameters
B. keyword
C. none of above
Đáp án

A

33. Data items in a class may be public.
A. TRUE
B. FALSE
Đáp án

A

34. Class members are public by default.
A. TRUE
B. FALSE
Đáp án

B
Nếu member của class không khai báo private/public/protected, thì compiler hiểu là private.

35. Friend functions have access only to public members of the class.
A. TRUE
B. FALSE
Đáp án

B
Friend function có thể truy xuất đến thành phần private của class

36. A function designed as public can be accessible by non-members of a class.
A. TRUE
B. FALSE
Đáp án

A

37. Friendship is commutative.
A. TRUE
B. FALSE
Đáp án

B
Tính chất hàm bạn, class bạn không có tính chất giao hoán.

38. new and delete are operators.
A. TRUE
B. FALSE
Đáp án

A

39. ‘new’ operator be overloaded ?
A. TRUE
B. FALSE
Đáp án

A

40. We can implement generic classes using templates
A. TRUE
B. FALSE
Đáp án

A

41. Using operator overloading we can invent new operators.
A. TRUE
B. FALSE
Đáp án

B

42. Inheritance helps in making a general class into a more specific class.
A. TRUE
B. FALSE
Đáp án

A

43. A base class is never used to create objects.
A. TRUE
B. FALSE
Đáp án

B

44. Classes can be inherited privately.
A. TRUE
B. FALSE
Đáp án

A

45. It Is legal to return local variables from a function which returns by reference.
A. TRUE
B. FALSE
Đáp án

B

46. Constructors can be virtual like virtual destructors
A. TRUE
B. FALSE
Đáp án

B

47. C++ is a pure object oriented programming language.
A. TRUE
B. FALSE
Đáp án

B

48. In C++ one can define a function within another function.
A. TRUE
B. FALSE
Đáp án

B

49. A class encapsulates the implementation and interface of a user-defined data type and constitutes an abstract data type.
A. TRUE
B. FALSE
Đáp án

A

50. In c++ an identifier can begin with a $ sign.
A. TRUE
B. FALSE
Đáp án

A

51. “My Variable” is a valid identifier in C++.
A. TRUE
B. FALSE
Đáp án

A

52. In C++ one can get the memory addresses of variables and functions.
A. TRUE
B. FALSE
Đáp án

A

53. %= is not a operator in C++
A. TRUE
B. FALSE
Đáp án

B

54. std::cout is a standard input stream.
A. TRUE
B. FALSE
Đáp án

B

55. Preprocessor #define macro and inline functions use the same mechanism.
A. TRUE
B. FALSE
Đáp án

B

56. All C++ functions are recursive.
A. TRUE
B. FALSE
Đáp án

A

57. The ‘break’ keyword is only used in the switch..case statement.
A. TRUE
B. FALSE
Đáp án

B

58. The new operator returns the address and size of the memory block that it allocates.
A. TRUE
B. FALSE
Đáp án

B

59. The heap storage is used for local objects.
A. TRUE
B. FALSE
Đáp án

B

60. It is not necessary to initialize a reference to real object when it is declared.
A. TRUE
B. FALSE
Đáp án

B

61. There can be a null reference.
A. TRUE
B. FALSE
Đáp án

B

62. One can reassign reference after it is initialized.
A. TRUE
B. FALSE
Đáp án

B
Tham chiếu phải được khởi tạo ngay khi khai báo biến. Và không reassign biến tham chiếu cho vào địa chỉ mới
Ví dụ:
int x, y;
int &r = x;
&r = y; // error

63. It is nothing wrong that a function returning a reference to an automatic variable.
A. TRUE
B. FALSE
Đáp án

B

64. One can apply pointer arithmetic with reference variables.
A. TRUE
B. FALSE
Đáp án

B
Không apply phép toán con trỏ vào biến tham chiếu

65. The preprocessor processes source code before the compiler does.
A. TRUE
B. FALSE
Đáp án

A

66. A class is a basic unit of object-oriented programming.
A. TRUE
B. FALSE
Đáp án

B
Đơn vị nhỏ nhất của lập trình hướng đối tượng là object.

67. A function template defines a parameterized nonmember function, which enables a program to call the same function with different types of arguments.
A. TRUE
B. FALSE
Đáp án

A

68. Destructors can be overloaded.
A. TRUE
B. FALSE
Đáp án

B

69. Static data members cannot be private.
A. TRUE
B. FALSE
Đáp án

B

70. Static member functions can use this pointer.
A. TRUE
B. FALSE
Đáp án

B

71. One cannot use enumerations in a class.
A. TRUE
B. FALSE
Đáp án

A

72. One cannot create an object of a virtual class.
A. TRUE
B. FALSE
Đáp án

B

73. A class that builds a linked list should destroy the list in the class destructor.
A. TRUE
B. FALSE
Đáp án

A

74. Once an exception has been thrown, it is not possible for the program to jump back to the throw point.
A. TRUE
B. FALSE
Đáp án

A

75. In C++, only one catch block can handle all the exceptions.
A. TRUE
B. FALSE
Đáp án

A

76. There can be only one catch block in a program.
A. TRUE
B. FALSE
Đáp án

B

77. When an exception if throw, but not caught, the program ignores the error.
A. TRUE
B. FALSE
Đáp án

B

78. A class object passed to a function template must overload any operators used on the class object by the template.
A. TRUE
B. FALSE
Đáp án

A

79. In the function template definition it is not necessary to use each type parameter declared in the template prefix.
A. TRUE
B. FALSE
Đáp án

B

80. It is possible to overload a function template and an ordinary (non-template) function.
A. TRUE
B. FALSE
Đáp án

A

81. A class template may not be used as a base class.
A. TRUE
B. FALSE
Đáp án

B

82. When declaring an iterator from the STL, the compiler automatically creates the right kind, depending upon the container it is used with.
A. TRUE
B. FALSE
Đáp án

A

83. ‘ios’ stream is derived from iostream.
A. TRUE
B. FALSE
Đáp án

B

84. What is the output of the following code?

#include <iostream>

using namespace std;

int main()
{
    int a = 20;
    int &n = a;
    n = a++;
    a = n++;
    cout << a << "," << n << endl;
    system("pause");
}

A. 20,21
B. 20,20
C. 21, 22
D. none of above
Đáp án

D
Kết quả: 22,22

85. What is the output of the following code?

#include <iostream>

using namespace std;

int main()
{
    int a = 20, b = 100;
    int &n = a;
    n = a++;
    n = &b;
    cout << a << "," << n << endl;
    system("pause");
}

A. 21,21
B. 20,21
C. 21,22
D. compile error
Đáp án

D
n = &b; ==> lỗi ‘=’: cannot convert from ‘int *’ to ‘int’

86. What is the output of the following code?

#include <iostream>

using namespace std;

int main()
{
    int main;
    main = 100;
    cout << main++ << endl;
    system("pause");
}

A. 100
B. 101
C. none
D. compile error
Đáp án

A

87. The design of classes in a way that hides the details of implementation from the user is known as:
A. Encapsulation
B. Information Hiding
C. Data abstraction
D. All of the above
Đáp án

D

88. Which of the following keywords do you think can be used when declaring static members in a class?
(i) Public
(ii) Private
(iii) Protected
A. all of above
B. (i)
C. (i), (iii)
Đáp án

A

89. I want a nonmember function to have access to the private members of a class. The class must declare that function:
A. friend
B. inline
C. static
D. virtual
Đáp án

A

90. The ability to reuse objects already defined, perhaps for a different purpose, with modification appropriate to the new purpose, is referred to as
A. Information hiding
B. Inheritance
C. Redefinition
D. Overloading
Đáp án

B

91. What do you think is the outcome of calling a redefined non-virtual function using a base-class pointer?
A. The appropriate redefined version of the function will be used
B. The base-class version of the function will always be used
C. The outcome is unpredictable
D. A run-time error will occur
Đáp án

B

92. A class member that is to be shared among all objects of a class is called
A. A const member
B. A reference parameter
C. A static member
D. A function member
Đáp án

C

93. What is a base class?
A. An abstract class that is at the top of the inheritance hierarchy.
B. A class with a pure virtual function in it.
C. A class that inherits from another class
D. A class that is inherited by another class, and thus is included in that class.
Đáp án

D

94. A variable that is declared protected:
A. Is visible only in the subclasses (and not in the class it is declared in)
B. Is visible only in the class it is declared in
C. Is visible to all classes, but modifiable only in the class where it is declared
D. Is visible in the class it is declared in, and all of its sub-classes
Đáp án

D

95. What is a destructor?
A. A function called when an instance of a class is initialized
B. A function that is called when an instance of a class is deleted
C. A special function to change the value of dynamically allocated memory
D. A function that is called in order to change the value of a variable
Đáp án

B

96. In protected inheritance:
A. The public members of the base class become public
B. The public members of the base class become protected
C. The protected members of the base class become private
D. The public members of the base class become inaccessible
Đáp án

B

97. If a class declares a variable static, this means:
A. Each instance of a class will have its own copy of the variable
B. Changing the variable in one instance will have no effect on other instances of the class
C. There will be only one instance of the variable initialized for all classes
D. Every instance of the class must consider the value of the static variable before initializing
Đáp án

C

98. In case of a copy constructor, which of the following is true?
A. Used to instantiate an object from another existing object
B. To copy one object to another existing object
C. Can be a substitute for a ‘=’ operator
D. All of the above
Đáp án

D

99. A class declaring another class as a friend will:
A. Have wine and cheese with that other friend
B. Allow that class to declare an instance of it in its list of private variables
C. Allow the other class (the one declared as friend) to access to the declaring class’s private variables
D. Allow the class declaring the other as a friend to access the declared class’s private variables
Đáp án

C

100. Which of the following can be virtual?
A. constructors
B. destructors
C. static functions
D. None of the above
Đáp án

B

101. Where is an exception generated?
A. In the catch block
B. In the throw clause
C. In the constructor of a class
D. Only when memory allocation fails
Đáp án

B

102. Static member functions ___
A. can be used without an instantiation of an object
B. can only access static data
C. Both 1 and 2 are correct
D. Neither 1 nor 2 are correct
Đáp án

C

103. What makes a class abstract?
A. The class must not have method
B. The class must have a constructor that takes no arguments
C. The class must have a function definition equal to zero
D. The class may only exist during the planning phase
Đáp án

C

104. In the following program, how many times Base’s constructor will be called?

#include <iostream>

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");
}

A. 1
B. 2
C. 3
D. error
Đáp án

B
Giải thích:

105. 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");
}

A. i1=2 i2=2
B. i1=2 i2=3
C. i1=3 i2=2
D. Error
Đáp án

B

106. 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;
};

A. only a and b
B. a,b and c
C. only a
D. error
Đáp án

D
Việc khai báo class Derived : Base có nghĩa là class Derived kế thừa kiểu private từ class Base.
Do vậy, các member của class Derived (kể cả class Friend) không thể access member của class Base.

107. What is the output of the following code?

#include <iostream>

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;
}

A. 0
B. 5
C. 6
D. 7
Đáp án

C
Giải thích: có 7 đối tượng A,B,C,D,E,F,G được tạo -> hàm tạo được gọi 7 lần -> count = 7. Tuy nhiên, đối tượng G bị hủy khi ra khỏi dấu }, hàm hủy được gọi -> count = 6

108. What is wrong in the following code?

#include <iostream>

class Base
{
public:
    Base() {};
    virtual ~Base() {};
};

class Derived : protected Base
{
public:
    virtual ~Derived() {};
};

int main()
{
    Base *pb = new Derived();
    return 0;
}

A. There is nothing wrong
B. One cannot have a ‘Base’ pointer to ‘Derived’ since it is not derived publicly
C. One need a derived class pointer to point to a derived class
D. One required to code a constructor for Derived
Đáp án

B
Giải thích: class Derived kế thừa class Base kiểu protected, hàm tạo Base() là thành phần protected của class Derived.

109. What is the output of the following code?

#include <iostream>

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;
}

A. professor researcher teacher myprofessor
B. researcher professor teacher myprofessor
C. myprofessor teacher researcher professor
D. myprofessor researcher professor teacher
Đáp án

B

110. What is the output of the following code?

#include <iostream>

using namespace std;

class Parent
{
public:
    Parent() { Status(); }
    virtual ~Parent() { Status(); }
    virtual void Status() { cout << "Parent "; }
};
class Child : public Parent
{
public:
    Child() { Status(); }
    virtual ~Child() { Status(); }
    virtual void Status() { cout << "Child "; }
};
void main()
{
    Child c;
}

A. Parent Parent
B. Parent Child Child Parent
C. Child Parent Parent Child
D. Error
Đáp án

B
Giải thích: Class Child kế thừa từ class Parent.
– Khi đối tượng Child được tạo, hàm tạo của class cơ sở sẽ được gọi trước, sau đó mới gọi đến hàm tạo class kế thừa.
– Khi kết thúc chương trình, đối tượng Child bị hủy, hàm hủy của lớp kế thừa được gọi trước, sau đó mới đến hàm hủy lớp cơ sở.

111. What is wrong in the following code?

#include <iostream>

using namespace std;

class Base
{
public:
    virtual void Method() = 0 { n = 1; }
private:
    int n;
};

class D1 :Base {};

class D2 :public D1
{
    int i;
    void Method() { i = 2; }
};
int main()
{
    D2 test;
    return 0;
}

A. There is no error
B. There is a syntax error in the declaration of “Method”
C. Class D2 does not have access to “Method”
D. Class D1 must define “Method”
Đáp án

A

Be the first to comment

Leave a Reply

Your email address will not be published.

*