Quiznetik

CPP Programming | Set 1

1. Which of the followings is/are automatically added to every class, if we do not write our own?

Correct : D. All of the above

2. When a copy constructor may be called?

Correct : D. All of the above

3. Constructors have _____ return type.

Correct : D. no

4. Implicit return type of a class constructor is:

Correct : B. class type itself

5. Which of the following is true about constructors? 1) They cannot be virtual. 2) They cannot be private. 3) They are automatically called by new operator.

Correct : B. Only 1 and 3

6. Output of following program? #include<iostream> using namespace std; class Point { Point() { cout << "Constructor called"; } }; int main() { Point t1; return 0; }

Correct : A. Compiler Error

7. #include<iostream> using namespace std; class Point { public: Point() { cout << "Constructor called"; } }; int main() { Point t1, *t2; return 0; }

Correct : C. Constructor called

8. Which operator is having the highest precedence?

Correct : D. equality

9. Which of the following is FALSE about references in C++?

Correct : D. References cannot refer to constant value

10. Which of the following functions must use reference?

Correct : B. Copy Constructor

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

Correct : A. x = 20; ref = 30

12. What is the difference between struct and class in C++?

Correct : 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.

13. Predict the output of following C++ program. #include<iostream> using namespace std; class Empty {}; int main() { cout << sizeof(Empty); return 0; }

Correct : A. A non-zero value

14. class Test { int x; }; int main() { Test t; cout << t.x; return 0; }

Correct : C. Compiler Error

15. Which of the following is true?

Correct : B. Objects of a class do not share non-static members. Every object has its own copy.

16. A member function can always access the data in __________, (in C++).

Correct : A. the class of which it is member

17. Which of the following is not correct for virtual function in C++?

Correct : B. Virtual function can be static.

18. 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

Correct : C. (2), (3), (4)

19. Which of the following cannot be passed to a function in C++?

Correct : D. Header file

20. Which of the following, in C++, is inherited in a derived class from base class?

Correct : C. Data members

21. Which of the following is a correct statement?

Correct : A. Composition is a strong type of association between two classes with full ownership.

22. Which of the following is not a correct statement?

Correct : B. Abstract class can directly be initiated with ‘new’ operator.

23. When a method in a subclass has the same name and type signatures as a method in the superclass, then the method in the subclass _____ the method in the superclass.

Correct : D. Overrides

24. It is possible to define a class within a class termed as nested class. There are _____ types of nested classes.

Correct : A. 2

25. When one object reference variable is assigned to another object reference variable then

Correct : B. a copy of the reference is created.

26. Which of the following is not a member of class?

Correct : B. Friend function

27. How can we restrict dynamic allocation of objects of a class using new?

Correct : C. By making an empty private new and new[] operators

28. Which of the following operators cannot be overloaded?

Correct : D. All of the above

29. Which of the following operators are overloaded by default by the compiler in every user defined classes even if user has not written? 1) Comparison Operator (==) 2) Assignment Operator (=)

Correct : C. Only 2

30. Which of the following operators should be preferred to overload as a global function rather than a member method?

Correct : C. Insertion Operator <<

31. How C++ compiler does differ between overloaded postfix and prefix operators?

Correct : B. A postfix ++ has a dummy parameter

32. Which of the following operator functions cannot be global?

Correct : C. Conversion Operator

33. Which of the following is true about this pointer?

Correct : B. It is passed as a hidden argument to all non-static function calls

34. What is the use of this pointer?

Correct : D. All of the above

35. Which of the following in Object Oriented Programming is supported by Function overloading and default arguments features of C++?

Correct : B. Polymorphism

36. Output of the program? #include<iostream> using namespace std; int fun(int x = 0, int y = 0, int z) { return (x + y + z); } int main() { cout << fun(10); return 0; }

Correct : D. Compiler Error

37. Output of following program? #include <iostream> using namespace std; int fun(int=0, int = 0); int main() { cout << fun(5); return 0; } int fun(int x, int y) { return (x+y); }

Correct : B. 5

38. Which of the following is true?

Correct : D. Static methods can only access static members (data and methods)

39. If a function is friend of a class, which one of the following is wrong?

Correct : C. Friend functions are members of a class.

40. Which one of the following is correct, when a class grants friend status to another class?

Correct : B. All member functions of the class granted friendship have unrestricted access to the members of the class granting the friendship.

41. In C++, const qualifier can be applied to 1) Member functions of a class 2) Function arguments 3) To a class data member which is declared as static 4) Reference variables

Correct : C. All

42. How to create a dynamic array of pointers (to integers) of size 10 using new in C++? Hint: We can create a non-dynamic array using int *arr[10]

Correct : B. int **arr = new int *[10];

43. Which of the following is true about new when compared with malloc: 1) new is an operator, malloc is a function 2) new calls constructor, malloc doesn’t 3) new returns appropriate pointer, malloc returns void * and pointer needs to typecast to appropriate type.

Correct : D. All 1, 2 and 3

44. Predict the output? #include <iostream> using namespace std; class Test { int x; Test() { x = 5; } }; int main() { Test *t = new Test; cout << t->x; }

Correct : A. Compiler Error

45. Is it fine to call delete twice for a pointer? #include<iostream> using namespace std; int main() { int *ptr = new int; delete ptr; delete ptr; return 0; }

Correct : B. No

46. When the inheritance is private, the private methods in base class are __________ in the derived class (in C++).

Correct : A. inaccessible

47. What happens when delete is used for a NULL pointer? int *ptr = NULL; delete ptr;

Correct : C. No Error

48. Which of the following is true about virtual functions in C++?

Correct : D. All of the above

49. Which of the following is true about pure virtual functions? 1) Their implementation is not provided in a class where they are declared. 2) If a class has a pure virtual function, then the class becomes abstract class and an instance of this class cannot be created.

Correct : C. Only 2

50. What is the size of wchar_t in C++?

Correct : D. Based on the number of bits in the system

51. Which of the following is true about templates? 1) Template is a feature of C++ that allows us to write one code for different data types. 2) We can write one function that can be used for all data types including user defined types. Like sort(), max(), min(), ..etc. 3) We can write one class or struct that can be used for all data types including user defined types. Like Linked List, Stack, Queue,..etc. 4) Template is an example of compile time polymorphism.

Correct : D. 1, 2, 3 and 4

52. Which of the following is incorrect in C++? (1)When we write overloaded function we must code the function for each usage. (2)When we write function template we code the function only once. (3)It is difficult to debug macros (4)Templates are more efficient than macros

Correct : D. All are correct.

53. Pick the odd one out

Correct : A. array type

54. Which data type is used to represent the absence of parameters?

Correct : C. void

55. What does an escape code represent?

Correct : A. alert

56. Which type is best suited to represent the logical values?

Correct : B. Boolean

57. Identify the user-defined types from the following?

Correct : C. both enumeration and classes

58. Which of the following statements are true? int f (float)

Correct : B. f is a function taking an argument of type float and returning an integer

59. The value 132.54 can be represented using which data type?

Correct : A. double

60. When a language has the capability to produce new data type mean, it can be called as …...

Correct : B. extensible

61. Choose the operator which cannot be overloaded.

Correct : C. ::

62. Which operator is required to be overloaded as member function only?

Correct : D. =

63. Class function which is called automatically as soon as the object is created is called as __

Correct : A. Constructor

64. Which type of data file is analogous to an audio cassette tape?

Correct : B. Sequential access file

65. What is the built in library function to compare two strings?

Correct : B. strcmp()

66. Which of the following are member dereferencing operators in CPP? 1. * 2. :: 3. ->* 4. ::* 5. ->

Correct : A. Only 1, 3, 4

67. Which of the followings is/are pointer-to-member declarator?

Correct : C. ::*

68. Assigning one or more function body to the same name is called ____________.

Correct : B. Function Overloading

69. Default values for a function are specified when ____.

Correct : B. Function is declared

70. Which of the following best defines the syntax for template function?

Correct : C. Both a and b

71. Return type of uncaught_exception () is ________________.

Correct : B. bool

72. If inner catch handler is not able to handle the exception then__________.

Correct : C. Compiler will check for appropriate catch handler of outer try block

73. Attempting to throw an exception that is not supported by a function call results in calling _____________ library function.

Correct : C. unexpected()

74. The code of statements which may cause abnormal termination of the program should be written under_________ block.

Correct : A. Try

75. When a virtual function is redefined by the derived class, it is called___________.

Correct : B. Overriding

76. While overloading binary operators using member function, it requires ___ argument/s.

Correct : B. One

77. Where the default value of parameter have to be specified?

Correct : C. Function prototype

78. For automatic objects, constructors and destructors are called each time the objects

Correct : A. enter and leave scope

79. Which operation is used as Logical 'AND'

Correct : C. Operator-&&

80. When an ADT is implemented as a C++ class, which of the following should normally be true?

Correct : B. Member functions are public, member variables are private

81. Variable that are listed in function's calls are called

Correct : B. Declared parameter

82. What kind of error can arise when there is a problem with memory?

Correct : A. Segmentation fault

83. Which operations don’t throw anything?

Correct : B. Operations which are irreversible

84. What operation can be performed by destructor?

Correct : B. Resource cleanup

85. Which interface in the container is required for storage management?

Correct : B. Allocator interface

86. How can the member functions in the container be accessed?

Correct : A. Iterator

87. In which type of storage location are the vector members stored?

Correct : A. Contiguous storage locations

88. What do container adapter provide to interface?

Correct : A. Restricted interface

89. What does the sequence adaptor provide?

Correct : C. Interface to sequence container

90. Which operators is part of RTTI?

Correct : C. Both dynamic_cast() & typeid

91. At which time does the static_cast can be applied?

Correct : A. Compile-time construct

92. Which function is used to position back from the end of file object?

Correct : A. seekg

93. String class have a concat() function that is used to _____________________

Correct : C. Append one string at end of another string

94. Which among the following is/are type(s) of this pointer?

Correct : C. const or volatile

95. Which is the pointer which denotes the object calling the member function?

Correct : B. This pointer

96. Which property is shown most when upcasting is used?

Correct : C. Complex code simple syntax

97. If multiple inheritance is implemented, which upcasting will be correct?

Correct : B. Upcast to any base class

98. When are the pointer types known for upcasting the objects?

Correct : A. Compile time

99. Which among the following is a mandatory condition for downcasting?

Correct : C. It must be done explicitly

100. Which container provides random access iterators?

Correct : D. both vector & deque