C++ Questions
C++ Questions
C++ Questions
SET-1
1. Which of the following type of class allows only one object of it to be created?
A. Virtual class
B. Abstract class
C. Singleton class
D. Friend class
Answer: Option C
Answer: Option B
Answer: Option B
Answer: Option B
5. Which of the following concepts means determining at runtime what method to invoke?
A. Data hiding
B. Dynamic Typing
C. Dynamic binding
D. Dynamic loading
Answer: Option C
6. Which of the following term is used for a function defined inside a class?
A. Member Variable
B. Member function
C. Class function
D. Classic function
Answer: Option B
Answer: Option C
Answer: Option D
Answer: Option C
10. Which of the following concepts of OOPS means exposing only necessary information to client?
A. Encapsulation
B. Abstraction
C. Data hiding
D. Data binding
Answer: Option C
Answer: Option D
12. cout is a/an __________ .
A. operator
B. function
C. object
D. macro
Answer: Option C
13. Which of the following concepts provides facility of using object of one class inside another class?
A. Encapsulation
Answer: Option C
Answer: Option B
Answer: Option D
16. Which of the following concepts means adding new components to a program as it runs?
A. Data hiding
B. Dynamic typing
C. Dynamic binding
D. Dynamic loading
Answer: Option D
Answer: Option A
Answer: Option C
Answer: Option B
Answer: Option D
21.Which of the following is correct about class and structure?
A. class can have member functions while structure cannot.
B. class data members are public by default while that of structure are private.
C. Pointer to structure or classes cannot be declared.
D. class data members are private by default while that of structure are public by default.
Answer: Option D
22. Which of the following concepts means wrapping up of data and functions together?
A. Abstraction
B. Encapsulation
C. Inheritance
D. Polymorphism
Answer: Option B
23.Which of the following concepts means waiting until runtime to determine which function to call?
A. Data hiding
B. Dynamic casting
C. Dynamic binding
D. Dynamic loading
Answer: Option C
Answer: Option B
Answer: Option B
26. Which of the following is the correct class of the object cout?
A. iostream
B. istream
Answer: Option C
Answer: Option C
Answer: Option D
Answer: Option D
30.Which one of the following options is correct about the statement given below? The compiler
checks the type of reference in the object and not the type of object.
A. Inheritance
B. Polymorphism
C. Abstraction
D. Encapsulation
Answer: Option B
31.Which of the following is the correct way of declaring a function as constant?
A. const int ShowData(void) { /* statements */ }
B. int const ShowData(void) { /* statements */ }
C. int ShowData(void) const { /* statements */ }
D. Both A and B
Answer: Option C
Answer: Option D
34.Which of the following factors supports the statement that reusability is a desirable feature of a
language?
A. It decreases the testing time.
B. It lowers the maintenance cost.
C. It reduces the compilation time.
D. Both A and B.
Answer: Option D
35.Which of the following ways are legal to access a class data member using this pointer?
A. this->x
B. this.x
C. *this.x
D. *this-x
Answer: Option A
Answer: Option D
A. Only I is true.
B. Both I and II are false.
C. Only II is true.
D. Both I and II are true.
Answer: Option B
38.What happens if the base and derived class contains definition of a function with same
prototype?
A. Compiler reports an error on compilation.
B. Only base class function will get called irrespective of object.
Answer: Option D
39.Which of the following are available only in the class hierarchy chain?
A. Public data members
B. Private data members
C. Protected data members
D. Member functions
Answer: Option C
Answer: Option C
41. Which of the following operators cannot be overloaded?
A. []
B. ->
C. ?:
D. *
Answer: Option C
42. In which of the following a virtual call is resolved at the time of compilation?
A. From inside the destructor.
B. From inside the constructor.
C. From inside the main().
D. Both A and B.
Answer: Option D
Answer: Option D
44. Which one of the following is the correct way to declare a pure virtual function?
A. virtual void Display(void){0};
B. virtual void Display = 0;
C. virtual void Display(void) = 0;
D. void Display(void) = 0;
45. Which of the following header file includes definition of cin and cout?
A. istream.h
B. ostream.h
C. iomanip.h
D. iostream.h
Answer: Option D
46. Which of the following keyword is used to overload an operator?
A. overload
B. operator
C. friend
D. override
Answer: Option B
Answer: Option D
Answer: Option B
49. Which one of the following is correct about the statements given below?
A. Only II is correct.
B. Both I and II are correct.
C. Only I is correct.
D. Both I and II are incorrect.
Answer: Option C
50. Which of the following is an invalid visibility label while inheriting a class?
A. public
B. private
Answer: Option D
Answer: Option D
Answer: Option B
Answer: Option D
54. Which of the following access specifier is used as a default in a class definition?
A. protected
B. public
C. private
D. friend
Answer: Option C
Answer: Option D
Answer: Option B
SET-2
1) Procedure oriented Programs are called as
A Structured programming
B Object oriented programming
C Functional programming
D None of the above
3)_______________ enables you to hide, inside the object, both the data fields and the methods
that act on that data.
A Encapsulation
B Polymorphism
C Inheritance
D Overloading
4)For a 16 bit word length Integer data type values lie between ________________
A 32768 to 32767.
B 0 to 32767
C -128 to 128
D 0 to 128
7)A __________ is an abstract idea that can be represented with data structures and functions.
A class
B object
C loop
D data type
8)Automatic Initialization of object is carried out using a special member function called
____________________
A friend
B casting
C reference parameter
D constructor.
9)A class can allow non-member functions and other classes to access its own private data, by
making them as _________________.
A private
B protected
C Friend
D public
11)A ___________ class can share selected properties of its base classes,
A abstarct
B derived
C subclass
D both b & c
12)A _______________ is the address of a memory location and provides an indirect way of
accessing data in memory.
A Constant
B variable
C Pointer
D object
13)To perform identical operations for each type of data compactly and conveniently we have to use
A Exception
B constant parameter
C function templates
D None of the above
16)The ________________ objects have values that can be tested for various error conditions.
A osstream
B ofstream
C stream
D ifstream
17)Which function return the current position of the get or put pointer in bytes.
A tellg ()
B tellp ()
C tell ()
D Both A and B
22)Find the Hierarchy Of Data Types 1. Long double 2. Double 3. Float 4. Long 5. Int 6. Char
A 1-2-3-4-5-6
B 1-3-2-4-5-6
C 1-2-3-5-4-6
D 1-2-4-3-5-6
23)State True or False 1. A static member function cannot be declared virtual, whereas a non-static
member functions can be declared as virtual 2. A static member function cannot have access to the
'this' pointer of the class.
A 1-T,2-F
B 1-F,2-T
C 1-T,2-T
D 1-F,2-F
24)State True or False 1. The this pointer is used as a pointer to the class object instance by the
member function. 2. By using polymorphism, you cannot create new objects that perform the same
functions as the base object
A 1-T,2-F
B 1-T,2-T
C 1-F,2-F
D 1-T,2-T
25)Which functions return the maximum pixels in each row (number of columns) and each column
(number of rows) of the screen
A getmar() and getmac()
26)what does this program do # include <fstream.h> void main() { const int max = 80; char buffer[80];
ifstream infile ("out.txt"); while (infile) { infile.getline(buffer,max); cout << buffer; } }
A check a ACII charcter in a File.
B reads an String File.
C reads an Ascii String File.
D check a String File.
27)Find the output of the following program #include <iostream.h> Void main () { int age=15; if (age
> 12 && age < 20) cout<<You are a teen-aged person; else cout<<You are not teen-aged; }
A 15
B You are a teen-aged person
C 20
D You are not teen-aged
28)The first index number in an array starts with _________ and the last index number of an array of
size n will be ______
A 0 & n-1
B 1 & n-1
C 0&n
D 1&n
29)State True or False 1. Public data members can be accessed directly in the main function without
an object. 2. Constructors can be overloaded.
A 1-F, 2-F
B 1-F, 2-T
C 1-T, 2-T
D 1-T, 2-F
To overload an operator _____________keyword must be used along with the operator to be
overloaded.
30)To overload an operator _____________keyword must be used along with the operator to be
overloaded.
A Over
B Overload
C void
D Operator
33)What is the output of the program # include <iostream.h> void main() { int n=1; cout
<<endl<<"The numbers are ;"<<endl; do { cout <<n<<"\t"; n++; } while (n<=100); cout<<endl; }
A print natural numbers 0 to 99
B print natural numbers 1 to 99
C print natural numbers 0 to 100
D print natural numbers 1 to 100
34)What is the output of the program # include <iostream.h> class counter { private : int count;
public : counter() { count = 0; cout<<"New Object Created\n"; } void inc_count () { count++; } int
getcount() { return count; } ~counter() { cout<<"One Object Deleted\n" ; } }; void main() { counter
c1,c2; }Output:
A New Object Created New Object Created One Object Deleted
B New Object Created New Object Created
C New Object Created One Object Deleted
D New Object Created New Object Created One Object Deleted One Object Deleted
35)What is the output of the program class Window // Base class for C++ virtual function example
{ public: virtual void Create() { cout <<"Base class Window"<<endl; } }; class CommandButton : public
Window { public: void Create() { cout<<"Derived class Command Button "<<endl; } }; void main()
{ Window *x, *y; x = new Window(); x->Create(); y = new CommandButton(); y->Create(); }
A Base class Window
B Base class Window Derived class Command Button
C Derived class Command Button
D ERROR
36)What is the output of the program #include <iostream.h> template <class T, int N> class array { T
memblock [N]; public: void setmember (int x, T value); T getmember (int x); }; template <class T, int
N> array<T,N>::setmember (int x, T value) { memblock[x]=value; } template <class T, int N> T
array<T,N>::getmember (int x) { return memblock[x]; } int main () { array <int,5> myints; array
<float,5> myfloats; myints.setmember (0,100); myfloats.setmember (3,3.1416); cout <<
myints.getmember(0) << '\n'; cout << myfloats.getmember(3) << '\n'; return 0; }
A 100 3.1416
B syntax error
C 100
D No output
37)What is the use of the this program # include <fstream.h> void main() { ofstream tstfile
("out.dat"); char text[ ] = "This is a text line"; int i=0; while (text[i]) tstfile. put(text[i++]); }
A to create a file
B to print a text on monitor
C to read a file
D None of the above
38)In the below Declaration how many copies of x does DC inherits ? Class BC { Private: int x; }; Class
BC1: virtual public BC { Private : Float y; }; Class BC2: virtual public BC { Private: float z; }; Class DC:
public BC1, Public Bc2 { };
A One
39) The objects are ....... if all their attribute values are identical ?
A Identical
B Distinct
C Same
D None
40) Object is....... ?
A A concept
B Abstraction
C Thing
D All of above
41) The mechanism by which the data and functions are bound together with an object definition ?
A Inheritance
B Abstraction
C Polymorphism
D Encapsulation
Answer Key-
1 A
2 C
3 A
4 A
5 C
6 C
7 A
8 D
9 C
10 A
11 D
12 C
13 C
14 A
15 D
16 D
17 B
18 A
19 C
20 C
21 B
22 A
8. Which of the following is the most preferred way of throwing and handling exceptions?
a. Throw by value and catch by reference.
b. Throw by reference and catch by reference.
c. Throw by value and catch by value
d. Throw the pointer value and provide catch for teh pointer type.
15. Which of the STL containers store the elements contiguously (in adjecent memory locations)?
a. std::vector
b. std::list
c. std::map
d. std::set
18. What is the difference between overloaded functions and overridden functions?
a. Overloading is a dynamic or run-time binding and Overriding is static or compile-time binding
b. Redefining a function in a friend class is called function overriding while Redefining a function in a
derived class is called a overloaded fucntion.
c. Overloading is a static or compile-time binding and Overriding is dynamic or run-time binding
d. Redefining a function in a friend class is called function overloading while Redefining a function in
a derived class is called as overridden fucnion.
19. Which one of the following is not a valid reserved keyword in C++
a. Explicit
23. If class A is friend of class B and if class B is friend of class C, which of the following is true?
a. Class C is friend of class A
b. Class A is friend of class C
c. Class A and Class C do not have any friend relationship
d. None of the above
36. The two types of file structure existing in VSAM file are
a. Key sequenced structure, entry sequenced structure
37. How many copies of a class static member are shared between objects of the class?
a. A copy of the static member is shared by all objects of a class
b. A copy is created only when at least one object is created from that class
c. A copy of the static member is created for each instntiation of the class
d. No memory is allocated for static members of a class
38. Which looping process checks the test condition at the end of the loop?
a. for
b. while
c. do-while
d. no looping process checks the test condition at the end
40. Which of the following correctly describes the meaning ofnamespace feature in C++?
a. Namespaces refer to the memory space allocated for names used in a program
b. Namespaces refer to space between the names in a program
c. Namespaces refer to packing structure of classes in a program.
d. Namespaces provide facilities for organizing the names in aprogram to avoid name clashes.
41. Which of the following correctly describes the meaning of namespace feature in C++?
a. Namespaces refer to the memory space allocated for names used in a program
b. Namespaces refer to space between teh names in a program
c. Namespaces refer to space between the names in a program
d. namespaces provide facilities for organizing the names in a program to avoid name clashes
49. Which of the following language feature is not an access specifier in C++?
a. public
b. private
c. C protected
d. internal
53. Which of the following library function below by default aborts the program?
a. Terminate()
b. end()
54. If a member needs to have unique value for all the objects of that same class, declare the
member as
a. Global variable outside class
b. Local variable inside constructor
c. Static variable inside class
d. Dynamic variable inside class
55. Value of ix+j, if i,j are integer type and ix long type would be
a. integer
b. float
c. long integer
d. double percision
56. Which of the following below can perform conversions between pointers to related classes?
a. A. cast_static
b. B. dynamic_cast
c. c. static_cast
d. D. cast_dynamic
58. Vtables
a. creates a static table per class
b. creates a static table per object
c. creates a dynamic table per class
d. creates a dynamic table per object
59. When class B is inherited from class A, what is the order in which the constructers of those
classes are called
a. Class A first Class B next
b. Class B first Class A next
c. Class B's only as it is the child class
d. Class A's only as it is the parent class
60. Which of the following is the most general exception handler that catches exception of any type?
a. catch(std::exception)
b. catch(std::any_exception)
c. catch()
d. catch()
61. Which of the following is the most general exception handler that catches exception of any
type?
a. catch(std::exception)
b. catch(std::any_exception)
62. In a group of nested loops, which loop is executed the most number of times?
a. the outermost loop
b. the innermost loop
c. all loops are executed the same number of times
d. cannot be determined without knowing the size of the loops
63. What is the Difference between struct and class in terms of Access Modifier?
a. By default all the struct members are private while by default class members are public.
b. By default all the struct members are protected while by default class members are private.
c. By default all the struct members are public while by default class members are private.
d. By default all the struct members are public while by default class members are protected.
68. What defines a general set of operations that will be applied to various types of data?
a. Template class
b. Function template
c. Class template
d. Both a and c above
74. If there is more than one statement in the block of a for loop, which of the following must be
placed at the beginning and the ending of the loop block?
a. parentheses ( )
b. braces { }
c. brackets [ ].
d. arrows < >
75. Which of the following members do get inherited but become private members in child class
a. Public
b. Private
c. Protected
d. All the above
76. Which looping process is best used when the number of iterations is known?
a. for
b. while
c. do-while
d. all looping processes require that the iterations be known
78. Which of the following is the most common way of implementing C++?
a. C++ programs are directly compiled into native code by a compiler
81. What is the implicit pointer that is passed as the first argument for nonstatic member functions?
a. self pointer
b. std::auto_ptr pointer
c. Myself pointer
d. this pointer
84. Which of the following operators below allow to define the member functions of a class outside
the class?
a. ::
b. ?
c. :?
d. %
85. Which of the following is not a valid conditional inclusions in preprocessor directives
a. #ifdef
b. #ifundef
c. #endif
d. #elif
87. If a member needs to have unique value for all the objects of that same class, declare the
member as
a. Global variable outside class
b. Local variable inside constructor
c. Static variable inside class
d. Dynamic variable inside class
89. Which of the following functions below can be used Allocate space for array in memory?
a. calloc()
b. malloc()
c. Realloc()
d. both a and b
90. Which of the following is not a valid conditional inclusions in preprocessor directives
a. #ifdef
b. #ifundef
c. #endif
d. #elif
92. Minimum number of temporary variable needed to swap the contents of 2 variables is:
a. 1
b. 2
c. 3
d. 0
94. There is nothing like a virtual constructor of a class.
a. False
b. True
95. C++ provides facility to specify that the compiler should match function calls with the correct
definition at the run time. This process is called as
a. Static binding
b. Dynamic Binding
Answer Key
1-d 2-b 3-d 4-a 5-b 6-d 7-a 8-b 9-a 10 - d
11 - c 12 - d 13 - b 14 - d 15 - c 16 - a 17 - a 18 - b 19 - c 20 - c
21 - b 22 - a 23 - b 24 - c 25 - c 26 - d 27 - b 28 - b 29 - a 30 - c
31 - d 32 - c 33 - b 34 - a 35 - d 36 - c 37 - c 38 - a 39 - c 40 - a
41 - d 42 - d 43 - b 44 - d 45 - a 46 - a 47 - a 48 - c 49 - d 50 - d
51 - a 52 - b 53 - d 54 - a 55 - b 56 - c 57 - C 58 - D 59 - a 60 - a
61 - c 62 - c 63 - b 64 - c 65 - b 66 - b 67 - a 68 - d 69 - b 70 - d
71 - a 72 - a 73 - c 74 - c 75 - b 76 - c 77 - a 78 - c 79 - a 80 - c
81 - d 82 - d 83 - b 84 - d 85 - a 86 - b 87 - a 88 - b 89 - d 90 - a
91 - b 92 - d 93 - d 94 - a 95 - b 96 - b 97 - b 98 - b 99 - c 100 - b