cpp mcq questions and answers

Join Our Official AiJobsAdda Telegram Channel

Multiple Choice Question & Answer:-

1) If the class name is X, what is the type of its “this” pointer (in a nonstatic, non-const member function)?

a. const X* const
b. X* const
c. X*
d. X&

Answer : d) X&

2) Which classes allow primitive types to be accessed as objects?

a. Storage
b. Virtual
c. Friend
d. Wrapper

Answer : b) Virtual

3) When is std::bad_alloc exception thrown?

a. When new operator cannot allocate memory
b. When alloc function fails
c. When type requested for new operation is considered bad, thisexception is thrown
d. When delete operator cannot delete the allocated (corrupted) object

Answer : d) When delete operator cannot delete the allocated (corrupted) object

4) Which one of the following is not a fundamental data type in C++

a. float
b. string
c. int
d. wchar_t

Answer : a) float

5) Which of the following is a valid destructor of the class name “Country”

a. int ~Country()
b. void Country()
c. int ~Country(Country obj)
d. void ~Country()

Answer : b) void Country()

6. Which of the following correctly describes C++ language?

a. Statically typed language
b. Dynamically typed language
c. Both Statically and dynamically typed language
d. Type-less language

Answer : d) Type-less language

7) Which of the following keyword supports dynamic method resolution?

a. abstract
b. Virtual
c. Dynamic
d. Typeid

Answer : a) abstract

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 the pointer type.

Answer : b) Throw by reference and catch by reference.

9) Which of the following is not true about preprocessor directives

a. They begin with a hash symbol
b. They are processed by a preprocessor
c. They form an integral part of the code
d. They have to end with a semi colon

Answer : a) They begin with a hash symbol

10) What’s wrong? while( (i < 10) && (i > 24))

a. the logical operator && cannot be used in a test condition
b. the while loop is an exit-condition loop
c. the test condition is always false
d. the test condition is always true

Answer : d) the test condition is always true

11) A continue statement causes execution to skip to

a. the return 0; statement
b. the first statement after the loop
c. the statement following the continue statement
d. the next iteration of the loop

Answer : c) the statement following the continue statement

12) What’s wrong? (x = 4 && y = 5) ? (a = 5) ; (b = 6);

a. the question mark should be an equal sign
b. the first semicolon should be a colon
c. there are too many variables in the statement
d. the conditional operator is only used with apstrings

Answer : d) the conditional operator is only used with apstrings

13) What’s wrong? for (int k = 2, k <=12, k++)

a. the increment should always be ++k
b. the variable must always be the letter i when using a for loop
c. there should be a semicolon at the end of the statement
d. the commas should be semicolons

Answer : b) the variable must always be the letter i when using a for loop

14) Which of the following is not recommended in a header file?

a. Type definitions (typedefs)
b. Class definitions
c. Function definitions
d. Template definitions

Answer : d) Template definitions

15) Which of the STL containers store the elements contiguously (in adjacent memory locations)?

a. std::vector
b. std::list
c. std::map
d. std::set

Answer : c) std::map

16) Which of the following is not a standard exception built in C++.

a. std::bad_creat
b. std::bad_alloc
c. std::bad_cast
d. std::bad_typeid

Answer : a) std::bad_creat

17) What does STL stand for?

a. Simple Template Library
b. Standard Template Library
c. Static Type Library
d. Single Type-based Library

Answer : b) Standard Template Library

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 an overloaded function.
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 function.

Answer : b) Redefining a function in a friend class is called function overriding while Redefining a function in a derived class is called an overloaded function.

19) Which one of the following is not a valid reserved keyword in C++

a. Explicit
b. Public
c. Implicit
d. Private

Answer : c) Implicit

20) Each pass through a loop is called a/an

a. enumeration
b. iteration
c. culmination
d. pass through

Answer : c) culmination

21) Which of the following is true about const member functions?

a. const members can be invoked on both const as well as nonconst objects
b. const members can be invoked only on const objects and not on nonconst objects
c. nonconst members can be invoked on const objects as well as nonconst objects
d. none of the above

Answer : b) const members can be invoked only on const objects and not on nonconst objects

22) Which of the following relationship is known as inheritancerelationship?

a. ‘has-a’ relationship
b. ‘is-a’ relationship
c. association relationship
d. none of the above

Answer : a) ‘has-a’ relationship

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

Answer : b) Class A is friend of class C

24) A direct access file is:

a. A file in which recoreds are arranged in a way they are inserted in a file
b. A file in which records are arranged in a particular order
c. Files which are stored on a direct access storage medium
d. None of the above

Answer : c) Files which are stored on a direct access storage medium

25) Which of the following is not a component of file system

a. Access method
b. Auxiliary storage management
c. Free integrity mechanism
d. None of the above

Answer : c) Free integrity mechanism

26) Seek time is

a. time taken to retrieve a dta
b. Time taken by read/write head mechanism to position itself over appropriate cylinder
c. Time taken by appropriate sector to come under read/write
d. None of the above

Answer : d) None of the above

27) ‘Prime area’ in context of file system is defined as

a. It is memory area created by operating system
b. It is an area into which data records are written
c. It is the main area of a web page
d. None of the above

Answer : b) It is an area into which data records are written

28) In mulit-list organization

a. Records that have equivalent value for a given secondary index item are linked together to form a list.
b. Records are loaded in ordered sequence defined by collating sequence by content of the key
c. Records are directly accessed by record key field
d. None of the above

Answer : b) Records are loaded in ordered sequence defined by collating sequence by content of the key

29) Which of the following is/are advantages of cellular partitioned structure:

a. Simultaneous read operations can be overlapped
b. Search time is reduced
c. Both a & b
d. None of the above

Answer : a) Simultaneous read operations can be overlapped

30) *ptr++ is equivalenet to:

a. ptr++
b. *ptr
c. ++*ptr
d. None of the above

Answer : c) ++*ptr

31) The conditional compilation

a. It is taken care of by the compiler
b. It is setting the compiler option conditionally
c. It is compiling a program based on a condition
d. none of above

Answer : d) none of above

32) Originally ‘C’ was developed as:

a. System programming language
b. General purpose language
c. Data processing language
d. None of above

Answer : c) Data processing language

33) An inverted file

a. Locates information about data in small files that are maintained apart from actual data record
b. A file which stores opposite records
c. A file which stores information about records of a system
d. None of above

Answer : b) A file which stores opposite records

34) Which of the following is not a file operation:

a. Repositioning
b. Truncating
c. Appending
d. None of above

Answer : a) Repositioning

35) Latency time is:

a. Time taken by read/write head mechanism to position itself over appropriate cylinder
b. Time taken to transfer a dta from memory
c. Time taken by appropriate sector to come under read/write head
d. None of above

Answer : d) None of above

36) The two types of file structure existing in VSAM file are

a. Key sequenced structure, entry sequenced structure
b. Key sequence structure, exit sequenced structure
c. Entry sequence structure, exit sequenced structure
d. None of above

Answer : c) Entry sequence structure, exit 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

Answer : c) A copy of the static member is created for each instntiation of the 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

Answer: a) for

39) The default access level assigned to members of a class is ___________

a. Private
b. Public
c. Protected
d. Needs to be assigned

Answer : c) Protected

40) 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 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.

Answer : a) Namespaces refer to the memory space allocated for names used in a program

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

Answer : d) namespaces provide facilities for organizing the names in a program to avoid name clashes

42) Which of the following language is not supported by C++?

a. Exception Handling
b. Reflection
c. Operator Overloading
d. Namespaces

Answer : d) Namespaces

43) class derived: public base1, public base2 { } is an example of

a. Polymorphic inheritance
b. Multilevel inheritance
c. Hierarchical inheritance
d. Multiple inheritance

Answer : b) Multilevel inheritance

44) Which of the following languages is a subset of C++ language?

a. C language
b. Java Language
c. C# language
d. language

Answer : d) language

45) How do we declare an ‘interface’ class?

a. By making all the methods pure virtual in a class
b. By making all the methods abstract using the keyword ‘abstract’ in a class
c. By declaring the class as interface with the keyword ‘interface’
d. It is not possible to create interface class in C++

Answer : a) By making all the methods pure virtual in a class

46) How do we declare an abstract class?

a. By providing at least one pure virtual method (function signature followed by ==0;) in a class
b. By declaring at least one method abstract using the keyword ‘abstract’ in a class
c. By declaring the class abstract with the keyword ‘abstract’
d. It is not possible to create abstract classes in C++

Answer : a) By providing at least one pure virtual method (function signature followed by ==0;) in a class

47) Which of the following is not an advantage of secondary memory

a. It is cost-effective
b. It has large storage capacity
c. It has highest speed
d. It is easily portable

Answer : a) It is cost-effective

48. What happens when a pointer is deleted twice?

a. It can abort the program
b. It can cause a failure
c. It can cause an error
d. It can cause a trap

Answer : c. It can cause an error

49) Which of the following language feature is not an access specifier in C++?

a. public
b. private
c. C protected
d. internal

Answer : d) internal

50) Expression C=i++ causes

a. Value of i assigned to C and then i incremented by 1
b. i to be incremented by 1 and then value of i assigned to C
c. Value of i assigned to C
d. i to be incremented by 1

Answer : d) i to be incremented by 1

51) The statement i++; is equivalent to

a. i = i + i;
b. i = i + 1;
c. i = i – 1;
d. i –;

Answer : a) i = i + i;

52) In C language, a hexadecimal number is represented by writing

a. x
b. xo
c. ox
d. h

Answer : b) xo

53) Which of the following library function below by default aborts the program?

a. Terminate()
b. end()
c. Abort()
d. exit()

Answer : d) exit()

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

Answer : a) Global variable outside 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

Answer : b) float

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

Answer : c) c. static_cast

57) How do we define a constructor?

a. a. x~() {}
b. B. X() {}~
c. C. X() ~{}
d. D. ~X() {}

Answer: c) C. X() ~{}

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

Answer : 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

Answer : a) Class A first Class B next

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()

Answer : a) catch(std::exception)

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)
c. catch(…)
d. catch()

Answer : c) catch(…)

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

Answer : c) all loops are executed the same number of times

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.

Answer : b) By default all the struct members are protected while by default class members are private.

64) Inline functions are invoked at the time of

a. Run time
b. Compile time
c. Depends on how it is invoked
d. Both b and c above

Answer : c) Depends on how it is invoked

65) What is shallow copy?

a. A shallow copy creates a copy of the dynamically allocated objects too.
b. A shallow copy just copies the values of the data as they are.
c. A shallow copy creates a copy of the statically allocated objects too
d. Both b and c above

Answer : b) A shallow copy just copies the values of the data as they are.

66) What is deep copy?

a. A deep copy creates a copy of the dynamically allocated objects too.
b. A deep copy just copies the values of the data as they are.
c. A deep copy creates a copy of the statically allocated objects too
d. Both b and c above

Answer : b) A deep copy just copies the values of the data as they are.

67) Which of the following below is /are a valid iterator type?

a. Input Iterator
b. Backward Iterator
c. Forward Iterator
d. Both a and c above

Answer : a) Input Iterator

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

Answer : d) Both a and c above

69) Under which of the following circumstances, synchronization takes place?

a. When the file is closed
b. When the buffer is empty
c. Explicitly, with manipulators
d. both a and c

Answer : b) When the buffer is empty

70) 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

Answer : d) both a and b

71) Statement scanf(“%d”,80);

a. Assign an integer to variable i
b. Give an error message
c. Print the value of i
d. Assign an float to variable i

Answer : a) Assign an integer to variable i

72) STL is based on which of the following programming paradigms?

a. Structured Programming
b. Object Oriented Programming (OOP)
c. Functional Programming
d. Aspect Oriented Programming (AOP)

Answer : a) Structured Programming

73) Which of the following is not a component of file system

a. Access method
b. Auxiliary storage management
c. Free integrity mechanism
d. None of the above

Answer : c) Free integrity mechanism

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

Answer : b) braces { }

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

Answer : b) Private

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

Answer : c) do-while

77) In a C language ‘3’ represents

a. A digit
b. An integer
c. A character
d. A word

Answer : a) A digit

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
b. C++ programs are first compiled to intermediate code by a compiler and then executed by a virtual machine
c. C++ programs are interpreted by an interpreter
d. A C++ editor directly compiles and executes the program

Answer : c) C++ programs are interpreted by an interpreter

79) Which header file should we include for using std::auto_ptr?

a.
b.
c.
d.

Answer : c)

80) Which of the following operators can be implemented as a nonmember operator?

a. – (addition operator)
b. () (function call operator)
c. [. (array access operator)
d. + (addition operator)

Answer : c) [. (array access operator)

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

Answer : d) ‘this’ pointer

82) Which of the following operators can be overloaded?

a. . (dot or member access operator)
b. & (address-of operator)
c. sizeof operator
d. ?: (conditional operator)

Answer : d) ?: (conditional operator)

83) Which of the following operator cannot be overloaded?

a. == (equality operator)
b. –> (row operator)
c. :: (cope resolution operator)

Answer : a) == (equality operator)

84) Which of the following operators below allow to define the member functions of a class outside the class?

a. ::
b. ?
c. 😕
d. %

Answer : d) %

85) Which of the following is not a valid conditional inclusions in preprocessor directives

a. #ifdef
b. #ifundef
c. #endif
d. #elif

Answer : a) #ifdef

86) Which of the following is not a standard exception built in C++.

a. std::bad_creat
b. std::bad_alloc
c. std::bad_cast
d. std::bad_typeid

Answer : b) std::bad_alloc

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

Answer : a) Global variable outside class

88) Under which of the following circumstances, synchronization takes place?

a. When the file is closed
b. When the buffer is empty
c. Explicitly, with manipulators
d. both a and c

Answer : b) When the buffer is empty

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

Answer : 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

Answer : a) #ifdef

91) Value of a in a = (b = 5, b + 5); is

a. Junk value
b. Syntax error
c. 5
d. 10

Answer : b) Syntax error

92) Minimum number of temporary variable needed to swap the contents of 2 variables is:

a. 1
b. 2
c. 3
d. 0

Answer : d) 0

93) How to declare operator function?

a. operator operator sign
b. operator
c. operator sign
d. None of the mentioned

Answer : a) operator operator sign

94) There is nothing like a virtual constructor of a class.

a. False
b. True

Answer: a) False

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 : b) Dynamic Binding

96) The output of this program is

intmain ()
{
cout << “Hello World!”
return 0;
}

a. Hello World
b. Syntax error
c. 0
d. Hello World!

Answer : b) Syntax error

97) The return value of the following code is

Class1& test(Class1 obj)
{
Class1 *ptr = new Class1();
………
return ptr;
}

a. object of Class1
b. reference to ptr
c. reference of Class1
d. object pointed by ptr

Answer : b) reference to ptr

98) The output of

{
int a = 5;
int b = 10;
cout << (a>b?a:b);
}

a. 5
b. 10
c. Syntax error
d. None of above

Answer : b) 10

99) The output of this program is

int a = 10;
void main()
{
int a = 20;
cout << a << ::a;
}

a. Syntax error
b. 10 20
c. 20 10
d. 20 20

Answer : c) 20 10

100) Observe following program and answer

class Example
{
public: int a,b,c;
Example(){a=b=c=1;} //Constructor 1
Example(int a){a = a; b = c = 1;} //Constructor 2
Example(int a,int b){a = a; b = b; c = 1;} //Constructor 3
Example(int a,int b,int c){ a = a; b = b; c = c;} //Constructor 4
}

In the above example of constructor overloading, the following statement will call which constructor
Example obj = new Example (1,2,3);

a. Constructor 2
b. Constructor 4
c. Constrcutor 1
d. Type mismatch error