Programming in C++ Multiple Choice Questions 

Programming in C++ Multiple Choice Questions 

1. What is C++?

a)            C++ is an object oriented programming language

b)            C++ is a procedural programming language

c)            C++ supports both procedural and object oriented programming language

d)            C++ is a functional programming language

Answer:- c

Explanation: C++ supports both procedural(step by step instruction) and object oriented programming (using the concept of classes and objects).

2. Who invented C++?

a)            Dennis Ritchie

b)            Ken Thompson

c)            Brian Kernighan

d)            Bjarne Stroustrup

Answer:– d

Explanation: Bjarne Stroustrup is the original creator of C++ in 1979 at AT&T Bell Labs.

3.Which of the following approach is used by C++?

a)            Left-right

b)            Right-left

c)            Bottom-up

d)            Top-down

Answer:– c

Explanation: C++ is an object-oriented language and OOL uses a bottom-up approach to solve/view a problem.

4.By default, all the files in C++ are opened in _________ mode.

a)            Binary

b)            VTC

c)            Text

d)            ISCII

Answer:- c

Explanation: By default, all the files in C++ are opened in text mode. They read the file as normal text.

5.Which of the following is the correct syntax to add the header file in the C++ program? 

1.            #include “userdefined.h” 

2.            #include <userdefined.h>

3.            <include> “userdefined.h”

4.            Both A & B 

Answer:- Both A & B

6.Which of the following statements is correct about the class?

1.            A class is an instance of its object 

2.            An object is the instance of the data type of that class 

3.            An object is an instance of its class 

4.            All of above 

Answer:- An object is an instance of its class

7.Which of the following is the correct syntax to print the message in C++ language?

1.            Out <<“Hello world!;

2.            cout <<“Hello world!”; 

3.            Cout << Hello world! ;

4.            None of the above 

Answer:- cout <<“Hello world!”;

8.Which of the following can be considered as the correct syntax for declaring an array of pointers of integers that has a size of 10 in C++? 

1.            int *arr = new int*[10] 

2.            int *arr = new int[10];

3.            int arr = new int[10]; 

4.            int **arr = new int*[10];

 Answer:- int **arr = new int*[10];

9.Which of the following is the correct syntax for printing the address of the first element? 

1. array[0];  2. array[2]; 

3.            array[1]; 

4.            None of the above 

Answer:- array[0]; 

10. Which of the following is the correct identifier? 

1.            varname@ 

2.            $var_name 

3.            VAR_123 

4.            None of the above 

Answer:- VAR_123 

11.Which of the following is the correct syntax for declaring the array?

1.            int array [5]; 

2.            init array []

3.            Array[5]; 

4.            None of the above 

Answer:- int array [5];

12. The programming language that has the ability to create new data types is called_______________

1.            Overloaded 

2.            Reprehensible 

3.            Extensible 

4.            Encapsulated

Answer:- Extensible

13. Which of the following refers to characteristics of an array? 

1.            An array is a set of distinct data items 

2.            An array is a set of similar data items 

3.            An array can hold different types of datatypes

4.            None of the above

 Answer:- An array is a set of similar data items

14.Which of the following features must be supported by any programming language to become a pure object-oriented programming language? 

1.            Inheritance

2.            Encapsulation 

3.            Polymorphism

4.            All of the above 

Answer:- All of the above

 15. Which one of the following represents the tab? 

1.            \t 

2.            \n 

3.            \r 

4.            None of the above

Answer:- \t 

16. Which of the following is the address operator?

1.            & 

2.            #

3.            % 

4.            @ 

Answer:- & 

17. Which of the following is the correct syntax to read the single character to console in the C++ language? 

1.            get(ch)

2.            Scanf(ch)

3.            Read ch() 

4.            Getline vh() 

Answer:- get(ch)

18.For inserting a new line in C++ program, which one of the following statements can be used?

1.            \r 

2.            \n

3.            endl

4.            both 2 and 3

 Answer:- both 2 and 3

19. What is the use of the indentation in c++?

a)            r distinguishes between comments and inner data

b)            distinguishes between comments and outer data

c)            distinguishes between comments and code

d)            r distinguishes between comments and outer data

Answer:- c

Explanation: To distinguish between different parts of the program like comments, codes, etc.

20. Which of the following is used to terminate the function declaration in C++? a) ;

b)            ]

c)            )

d)            :

Answer:- a

Explanation: ; semicolon is used to terminate a function declaration statement in C++.

21.What is Inheritance in C++?

a)            Deriving new classes from existing classes

b)            Overloading of classes

c)            Classes with same names

d)            Wrapping of data into a single class

Answer:- a

Explanation: Inheritance is the concept of OOPs in which new classes are derived from existing classes in order to reuse the properties of classes 

22.What is meant by a polymorphism in C++?

a)            class having only single form

b)            class having four forms

c)            class having many forms

d)            class having two forms

Answer:- c

23.What is abstract class in C++?

a)            Any Class in C++ is an abstract class

b)            Class from which any class is derived

c)            Class specifically used as a base class with atleast one virtual functions

d)            Class specifically used as a base class with atleast one pure virtual functions

Answer:- d

Explanation: An abstract class is defined as a class which is specifically used as a base class. An abstract class should have atleast one pure virtual function.

24.Which concept allows you to reuse the written code in C++?

a)            Inheritance

b)            Polymorphism

c)            Abstraction

d)            Encapsulation

Answer:- a

Explanation: Inheritance allows you to reuse your already written code by inheriting the properties of written code into other parts of the code, hence allowing you to reuse the already written code.

25. How structures and classes in C++ differ?

a)            Structures by default hide every member whereas classes do not

b)            In Structures, members are public by default whereas, in Classes, they are private by default

c)            Structures cannot have private members whereas classes can have

d)            In Structures, members are private by default whereas, in Classes, they are public by default

Answer:- b

Explanation: Structure members are public by default whereas, class members are private by default. Both of them can have private and public members

26. What will be the output of the following C++ code?

#include <iostream>     
using namespace std;

    enum colour 

    {

        green, red, blue, white, yellow, pink

    };   

 int main()

    {

        cout << green<< red<< blue<< white<< yellow<< pink;         return 0;

    }

a)            012345

b)            123456

c)            compile time error

d)            runtime error

Answer:– a

Explanation: The enumerator values start from zero if it is unassigned.

27.What will be the output of the following C++ code?

#include <iostream>    

using namespace std;

    enum test 

    {

        A = 32, B, C

    };    

int main()

    {

                                    Cout<<A<<B<<C;

Return 0;

    }

a)            323334

b)            323232

c)            323130

d)            323134

Answer:– a

Explanation: If we not assigned any value to enum variable means, then the next number to initialized number will be allocated to the variable.

28. What is operator overloading in C++?

a)            Overriding the operator meaning by the user defined meaning for user defined data type

b)            Redefining the way operator works for user defined types

c)            Ability to provide the operators with some special meaning for user defined data type d) All of the mentioned

Answer:- d

Explanation: Operator overloading helps programmer to give his/her own meaning to an operator for user defined data types(eg, classes).

29. Pick out the compound assignment statement./

a)            a = a – 5

b)            a = a / b

c)            a -= 5

d)            a = a + 5

Answer:- C

Explanation: When we want to modify the value of a variable by performing an operation on the value currently stored, We will use compound assignment statement. In this option, a -=5 is equal to a = a-5.

30.Which operator is having the highest precedence in c++?

a)            array subscript

b)            Scope resolution operator

c)            static_cast

d)            dynamic_cast’

Answer:- B

Explanation: Scope resolution operator is having the highest precedence in c++.

31.What is this operator called “?:” ?

a)            conditional

b)            relational

c)            casting operator

d)            unrelational

Answer:- A

Explanation: In this operator, if the condition is true means, it will return the first operator, otherwise second operator.

32. The constants are also called as

a)            Const

b)            preprocessor

c)            literals

d)            none of these

Answer:- C

Explanation: No explanation is given for this question.

33.Pick the odd one out.

a)            array type

b)            character type

c)            boolean type

d)            integer type

Answer:- C

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

a)            integer

b)            boolean

c)            character

d)            float

Answer:- B

Explanation: Logical values can be either true or false, so the boolean type is suited for it.

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

a)            enumeration

b)            classes

c)            both enumeration and classes

d)            int

Answer:- C

Explanation: They must be defined by the users before use, unlike the other types which are readily available.

36.____ is the smallest individual unit in a program.

a)            Variable

b)            Control

c)            Character

d)            Token

Answer:- D

37.What are the escape sequences?

a)            Set of characters that convey special meaning in a program

b)            Set of characters that whose use are avoided in C++ programs

c)            Set of characters that are used in the name of the main function of the program

d)            Set of characters that are avoided in cout statements

Answer:– A

Explanation: Escape sequence is a set of characters that convey a special meaning to the program. They are used to convey a meaning which cannot be conveyed directly.

38.Which of the following is not a fundamental type is not present in C but present in C++? a) int

b)            float

c)            bool

d)            void

Answer:- C

39.What is the size of a boolean variable in C++?

a)            1 bit

b)            1 byte

c)            4 bytes

d)            2 bytes

Answer:- A

Explanation: Boolean uses only 1 bit as it stores only truth values which can be true(1) or false(0).

40. Which of the following is the scope resolution operator?

a) .

b)            *

c)            ::

d)            ~

Answer:- C

Explanation: :: operator is called scope resolution operator used for accessing a global variable from a function which is having the same name as the variable declared in the function.

42.         Define Encapsulation with an example

43.         Define Abstraction with an example

44.         Define Inheritance with an example

45.         Define Polymorphism with an example

46.         Write C++ Program to find the sum of 2 numbers

47.         Abstract class is the class with no objects created directly

a. True b. False

Answer:-  a. true

48.         The class where objects behave like a data type, which is known as___

a.message datatype 

b. method datatype 

c. User defined datatype 

d. Abstract datatype

Answer:-  abstract datatype

49.         Operators such as _____cannot be overloaded

a.            +

b. ++

c. ::

d. ==

Answer:- c. ::

50. The function that can access and manipulate the private entities of class

a. Abstract class

b. Method class

c. User defined class

d. Friend function

Answer:- d. Friend function

51. A constructor  that accepts no parameters is called the .

a. default constructor

b. parameterized

c. implicit constructor

d. null constructor

Answer:- a. default constructor

52. Which of the following is/are the types of C++ expressions.

i) constant  expressions ii) integral iii) float         iv) null

a. i, ii and iii only

b. ii, iii and iv only

c. i, iii and iv only

d. All i, ii, iii and iv

53. The following is ____________ expressions x=y=z=0

a. Embedded assignment

b. Logical assignment

c. Chained assignment

d. Compound assignment

Answer:– c. Chained assignment

54.         Destructor is the member function whose name as the class name but is preceded by a ____

a. hash

b. dot.

c. dollor

d. tilde

55.         When a function is defined inside a class, this function is called .

a.            Inside function

b. Class function

c. Inline function

d. Interior function

Answer:– c. Inline function

56. The major goal of inheritance in C++ is

A)           To facilitate the conversion of data types

B)           To help modular programming

C)           To facilitate the re usability of code

D)           To extend the capabilities of a class

Answer:- C) To facilitate the reusability of code

57. When a function is defined inside a class, this function is called ………….

A)           Inside function

B)           Class function

C)           Inline function

D)           Interior function

Answer:-  C) Inline function

58. Which of the following operators could be overloaded?

A)           Size of

B)           +

C)           +=

D)           ::

Answer:- B) +

59. Operators such as …………………. cannot be overloaded.

A)           +

B)           ++

C)           : :

D)           = =

Answer:- C) : :

60. The C++ header file ……………….. contains function prototypes for the standard input and standard output functions.

A)           &lt;iomanip&gt;

B)           &lt;fstream&gt;

C)           &lt;iostream&gt;

D) &lt;cstdio&gt;

Answer:- C) &lt;iostream&gt;

61. The binding of data and functions together into a single class-type variable is referred to as …………..

A) encapsulation

B)Polymorphism

C)inheriatnce

D)overloading     

Answer:-  A) encapsulation

62. Which of the following statements about member functions are True or False.

i)             A member function can call another member function directly with using the dot operator.

ii)            A member function can access the private data of the class.

A)           i-True, ii-True

B)           i-False, ii-True

C)           i-True, ii-False

D)           i-True, ii-True

Answer:- B) i-False, ii-True

63. …………………. refer to the names of variables, functions, arrays, classes etc. created by the programmer.

A) Keywords

B)           Identifiers

C)           Constants

D)           Strings

Answer:-  B) Identifiers

64………………….. are explicitly reserved identifiers and cannot be used as names for the program variables or other user-defined program elements.

A)           Keywords

B)           Identifiers

C)           Constants

D)           Strings

Answer:-  A) Keywords

65. State whether the following statements are True or False for C++ identifiers.

i)             Only alphabetic characters, digits and underscores are permitted.

ii)            The name can start with a digit. iii) Uppercase and lowercase letters are distinct.

A)           i-True, ii-True, iii-False

B)           i-True, ii-False, iii-True

C)           i-True, ii-False, iii-False

D)           i-True, ii-True, iii-True

Answer:-  . B) i-True, ii-False, iii-True

66. In C++, ………………….. refer to fixed values that do not change during the execution of a program.

A)           Identifiers

B)           Constants

C)           Strings

D)           Operators

Answer:-  B) Constants

67. Which of the following is NOT the  user-defined data type in C++ .

A)           Structure

B)           Pointer

C)           Union

d) None of the above

Answer:- D

68. Which of the following is NOT the  user-defined data type in C++ .

A)           Structure

B)           Pointer

C)           Union

D)           Class

Answer:– B) Pointer

69. Which of the following is/are the derived data types in C++.

i) array    ii) function    iii) pointer    iv) class

A)           i, ii and iii only

B)           ii, iii and iv only

C)           i, iii and iv only

D)           All i, ii, iii and iv

Answer:–  A) i, ii and iii only

70. We can create …………………….. in C++ using the qualifier constant and defining a set of integer constant using enum keywords.

A)           basic constant

B)           number constant

C)           symbolic constant D) named constant

Answer:– C) symbolic constant

71. C++ permits initialization of the variables at run time which is referred to as ………………. initialization.

A) static

B)           dynamic

C)           variable

D)           runtime

Answer:-  B) dynamic

72. Which of the following is the scope resolution operator in C++.

A) : :

B)           : : *

C)           _&gt;*

D)           . *

Answer:–  A) : :

73. …………….. operator can be used to uncover a hidden variable.

A)           pointer-to-member

B)           memory release

C)           scope resolution

D)           line feed

Answer:-  C) scope resolution

74. The …………….. manipulator is used in an output statement which causes a linefeed to be inserted.

A) setw

B)           delete

C)           endl

D)           symbol

Answer:-  C) endl

75. Which of the following is/are the types of C++ expressions.

i) constant expressions    ii) integral expressions

iii) float expressions         iv) null expressions

A)           i, ii and iii only

B)           ii, iii and iv only

C)           i, iii and iv only

D)           All i, ii, iii and iv

Answer:-  A) i, ii and iii only

76.…………………. produce results of type bool which takes a value true or false.

A)           Relational expressions

B)           Float expressions

C)           Logical expressions

D)           Bitwise expressions

Answer:-  A) Relational expressions

77.…………… combine to or more relational expressions and produces bool type results.

A)           Relational expressions

B)           Float expressions

C)           Logical expressions

D)           Bitwise expressions

Answer:-  C) Logical expressions

78.The operator ………………… is known as a compound assignment or short-hand assignment operator.

A) =

B)           = =

C)           += =

D)           +=

Answer:- D) +=

79. What will be the output of the following C++ code?

a. In main 10 5                b. In main 5 10         c. error            d. No output

Explanation: As the function is called by reference i.e. all the changes are done directly into the memories of a and b. Therefore changes made to a and b in swap function is reflected back to main function. Hence the values of a and b in swap as well as in main function is changed.

80. What will be the output of the following C++ code? #include <iostream>     using namespace std;

    int main()

    {       int a = 9;                               int & aref = a;

            a++;                                       cout << “The value of a is ” << aref;           return 0;               

     }

a. 9       b. 10        c. Error         d. 11

Answer:– b

Explanation: The value is declared and it isincrementedrement, so it’s value is 10.

81. A constructor has the same …………….. as that of class.

A)           variable

B)           object

C)           function.

D) name

Answer:-  D) name

82.A constructor  that accepts no parameters is called the …………….

A)           default constructor

B)           parameterized constructor

C)           implicit constructor

D)           null constructor

Answer:–  A) default constructor

83. Constructors cannot be inherited, through a derived class can call the ……………….

constructor. A) base class

B)           derived class

C)           void class

D)           default class

Answer:- A) base class

84. State whether the following statements about  the constructor are True or False.

i)             constructors should be declared in the private section.

ii)            constructors are invoked automatically when the objects are created.

A)           True, True

B)           True, False

C)           False, True

D)           False, False

Answer:- C) False, True

85. Destructor is a member function whose name is same as the class name but is preceded by a

………..

A) tilde

B) hash

C)           dot

D)           dollor

Answer:- A) tilde

86. A destructor is used to destroy the objects that have been created by a ………………..

A) objects at the run time

B)           destructor class

C)           function

D)           constructor

Answer:- D) constructor

87. An ………………….. with a constructor or destructor cannot be used as a member or a union.

A) class

B)           object

C)           function D) variable

Answer:-  B) object

88. The ……………. inherits some or all of the properties of the ……….. class.

A)           base, derived

B)           derived, base

C)           derived, initial

D)           base, final

ANSWER:-  B) derived, base

89.A derived class with only one base class is called …………… inheritance.

A)           single

B)           multiple

C)           multilevel

D)           hierarchical

ANSWER:-  A) single

90. A class can inherit properties from more than one class which is known as ……….inheritance.

A)           single

B)           multiple

C)           multilevel

D)           hierarchical

ANSWER:-  B) multiple

91. A class can be derived from another derived class which is known as ………. inheritance. A) single

B)           multiple

C)           multilevel

D)           hierarchical

ANSWER:-  C) multilevel

92. When the properties of one class are inherited by more than one class, which is called ……… inheritance.

A) single

B)           multiple

C)           multilevel

D)           hierarchical

ANSWER:–  D) hierarchical

93. When a base class is privately inherited by a derived class public members of the base class become ………. of the derived class.

A)           private members

B)           protected members

C)           Public members

D)           Not inherited

ANSWER:-  A) private members

94. When a base class is privately inherited by derived class public members of the base class canonly be accessed by the ……… of the derived class.

A)           non-member functions

B)           friend functions

C)           member functions

D)           class members

ANSWER:-  C) member functions

95. When a protected member is inherited in public mode, it becomes ……….. in the derived class too and therefore is accessible by member functions of the derived class.

A)           protected

B)           private

C)           public

D)           friend

ANSWER:-  A) protected

96.State whether the following statements about inheritance are True or False.

i)             A public member of a class can be accessed by its own objects using the dot operator.

ii)            While inheriting, the private members of the base class will never become members of its derived class. A) True, False

B)           False, True

C)           True, True

D)           False, False

ANSWER:–  C) True, True

97. A member declared as ………….. is accessible by the member functions within its class and any class immediately derived from it.

A)           protected

B)           private

C)           public

D)           friend

ANSWER:–  A) protected

98.When the base class is publicly inherited, public members of the base class become …………. of the derived class.

A) private members

B)           protected members

C)           Public members

D)           Not inherited

ANSWER:-  C) Public members

99. In the protected derivation, both the public and protected members of the base class become …………. members of the derived class.

A)           protected

B)           private

C)           public

D)           friend

ANSWER:–  A) protected

100. What will be the order of execution of base class constructors in the following method of inheritance?

class A: public B, public C {….};

A)           B(); C(); A();

B)           C(); B(); A();

C)           A(); B(); C();

D)           B(); A(); C();

ANSWER:-  A) B(); C(); A();

101.What will be the order of execution of base class constructors in the following method of inheritance?

class A: public B, virtual public C {….};

A)           B(); C(); A();

B)           C(); B(); A();

C)           A(); B(); C();

D)           B(); A(); C();

ANSWER:–  B) C(); B(); A();

102. While the friend functions and the member functions of a friend class can have direct access toboth the private and protected data, the member functions of a derived class can directly access only the ………… data.

A)           protected

B)           private

C)           public

D)           friend

ANSWER:-  A) protected

103. In ……………………. inheritance, the constructors are executed in the order of inheritance. A) multipath

B)           multiple

C)           multilevel

D)           hierarchical

ANSWER:–  C) multilevel

104. In ……………….. inheritance, the base classes are constructed in the order in which they appear in the deceleration of the derived class.

A)           multipath

B)           multiple

C)           multilevel

D)           hierarchical

ANSWER:–  B) multiple

105. ……………….. inheritance may lead to duplication of inherited members from a ‘grandparent’ base class.

A)           multipath

B)           multiple

C)           multilevel

D)           hierarchical

ANSWER:-  A) multipath

106. The member functions of a derived class can directly access only the ……………….. data. A) private and protected

B)           private and public

C)           protected and public

D)           private, protected and public

ANSWER:–  C) protected and public

107.The friend functions and the member functions of a friend class can directly access the ………………. data.

A)           private and protected

B)           private and public

C)           protected and public

D)           private, protected and public

ANSWER:-  A) private and protected

108.Object based language differs from object oriented language as it does not support features _____ .

1.Encapsulation 2.Inheritance 3.Dynamic Binding 4.Abstraction 5. Polymorphism

a.            only 3 ,4

b.            only 1,3,5

c.            2,4,5

d.            Only 2,3

ANSWER:- Only 2,3

109. Which operator is used to create the user-defined streams in c++?

a) >>

b) <<

c) &

d) Both >>&<<

Answer:- d

Explanation: We can make user-defined types with streams by overloading the insertion operator (<<) to put objects into streams and the extraction operator (>>) to read objects from streams.

110.What does the cerr represent?

a)            Standard error stream

b)            Standard logging stream

c)            Input stream

d)            Output stream

Answer:- a

Explanation: cerr is an object of class ostream that represents the standard error stream. It is associated with the cstdio stream stderr.

111. What is the use of clog? a) Standard logging stream

b)            Error stream

c)            Input stream

d)            output stream

Answer:- a

Explanation: clog is an object of class ostream that represents the standard logging stream. It is associated with the cstdio stream stderr, like cerr.

112.Which classes are called as mixin? a) Represent a secondary design

b)            Classes express functionality which represents responsibilities

c)            Standard logging stream

d)            Represent a priary design

Answer:- b

Explanation: A class that expresses functionality rather than its primary design role is called a mixin.

113. What will happen when introduce the interface of classes in a run-time polymorphic hierarchy?

a)            Separation of interface from implementation

b)            Merging of interface from implementation

c)            Separation of interface from debugging

d)            Merging of interface from debugging

Answer:- a

Explanation: Separation of interface from implementation introduce the interface of classes in a run-time polymorphic hierarchy.

114.Which operator is used to insert the data into file? a) >>b) <<c) <d) >

Answer:- b

Explanation: You can write information to a file from your program using the stream insertion operator <<;.

115.Which function is used to position back from the end of file object? a) seekg

b)            seekp

c)            both seekg &amp; seekp

d)            seekf

Answer: a

Explanation: The member function seekg is used to position back from the end of file object.

116. Which member function is used to determine whether the stream object is currently associated with a file? a) is_open

b)            buf

c)            string

d)            is_out

Answer:- a

Explanation: The member function is_open can be used to determine whether the stream object is currently associated with a file.

117. Which header file is used for reading and writing to a file? a) #include<iostream>

b)            #include<fstream>

c)            #include<file>

d)            #include<fe>

Answer :- b

 118.It is not possible to combine two or more file opening mode in open () method.

a. True

b. False

ANSWER :- False

119. __________ is return type of is_open() function. a. int

b.            bool

c.            float

d.            char *

ANSWER :- bool

120. To create an output stream, we must declare the stream to be of class ___________ . a. ofstream

b.            ifstream

c.            iostream

d.            None of these

ANSWER :- ofstream

121. What is an exception in C++ program?

a)            A problem that arises during the execution of a program

b)            A problem that arises during compilation

c)            Also known as the syntax error

d)            Also known as semantic error

Answer :- a

122. What is the correct syntax of the try-catch block?

a) try

{

                              // programable codes…..

}

catch(Exceptions)

{

                              // Code for handling exceptions….

}

b)            try()

{

                              // programable codes…..

}

catch(Exceptions)

{

                              // Code for handling exceptions….

}

c)            try

{

                              // programable codes…..

} catch {

                              // Code for handling exceptions….

}

d)            try()

{

                              // programable codes…..

} catch

{

                              // Code for handling exceptions….

}

Answer :- a

Explanation: Try-catch block has the following syntax:

try{

                              // codes that needs to check for exceptions

} catch(Exception E1){

// codes for handling exception…. 

// Exception E denotes the type of exception this block is handling.

} catch(Exception E2){

                              // other exception that needs to be handled…

}

122. Which part of the try-catch block is always fully executed?

a) try part

b)            catch part

c)            finally part

d)            throw part

Answer :- c

Explanation: finally part of the try-catch block is always executed whether exceptions are caught or not.

123.What is the difference between error and exception? a) Both are the same

b)            Errors can be handled at the run-time but the exceptions cannot

c)            Exceptions can be handled at the run-time but the errors cannot

d)            Both can be handled during run-time

Answer :- c

Explanation: Exceptions can be handled during run-time whereas errors cannot be because exceptions occur due to some unexpected conditions during run-time whereas about errors compiler is sure and tells about them during compile-time.

124. What is Re-throwing an exception means in C++?

a)            An exception that is thrown again as it is not handled by that catching block

b)            An exception that is caught twice

c)            An exception that is not handled in one caught hence thrown again

d)            All of the mentioned

Answer:- d

Explanation: Exception that is caught by a catch block but not handled by that catch block can be re-thrown by that catch block to further try-catch block.

125. Which of the following is an exception in C++? a) Divide by zero

b)            Semicolon not written

c)            Variable not declared

d)            An expression is wrongly written

Answer :- a

Explanation: Exceptions are those which are encountered during run-time of the program. semicolon, variable not declared and the wrong expression are compile-time errors, therefore, they are not exceptions. Divide by zero is the problem that is encountered during run-time, therefore, it is an exception.

126.By default, what a program does when it detects an exception? a) Continue running

b)            Results in the termination of the program

c)            Calls other functions of the program

d)            Removes the exception and tells the programmer about an exception

Answer:- b

Explanation: By default, whenever a program detects an exception the program crashes as it does not know how to handle it hence results in the termination of the program.

127.Why do we need to handle exceptions?

a)            To avoid unexpected behaviour of a program during run-time

b)            To let compiler remove all exceptions by itself

c)            To successfully compile the program

d)            To get correct output

Answer :- a

Explanation: We need to handle exceptions in a program to avoid any unexpected behaviour during run-time because that behaviour may affect other parts of the program. Also, an exception is detected during run-time, therefore, a program may compile successfully even with some exceptions cases in your program.

128. Where should we place catch block of the derived class in a try-catch block? a) Before the catch block of Base class

b)            After the catch block of Base class

c)            Anywhere in the sequence of catch blocks

d)            After all the catch blocks

Answer :- a

Explanation: C++ asks the programmer to place the catch block of derived class before a catch block of the base class, otherwise derived catch block will never be executed.

129.An uncaught handler returns to _______________ a) main function

b)            its caller

c)            Function it is called by

d)            waits there for some time

Answer :- c

Explanation: Uncaught handler returns to its callee(i.e. the function it is called by).

130. Exception handlers are declared with ____________ keyword. a) try

b)            catch

c)            throw

d)            finally

Answer:- b

Explanation: C++ uses catch block to handle any exceptions that occur during run-time of the program.

 131. In nested try-catch block, if the inner catch block gets executed, then______________ a) Program stops immediately

b)            Outer catch block also executes

c)            Compiler jumps to the outer catch block and executes remaining statements of the main() function

d)            Compiler executes remaining statements of outer try-catch block and then the main() function

Answer:- d

Explanation: The inner catch block will be executed then remaining part of the outer try block will be executed and then the main bock will be executed.

132. If inner catch block is unable to handle the exception thrown then__________ a) The compiler looks for the outer try-catch block

b)            Program stops abnormally

c)            The compiler will check for appropriate catch handler of the outer try block

d)            The compiler will not check for appropriate catch handler of the outer try block

Answer :- c

Explanation: In such cases, the compiler will try to find an appropriate outer catch block to handle the exception otherwise if nothing is there then occurs the abnormal behaviour of the program.

133. How many parameters are there in getline function?

A.           1

B.           2

C.           3

D.           4

Answer :- Option C

Explanation: There are three parameters in getline function. They are a pointer to an array of characters and maximum number of characters and a delim.

 134.What can be used to input a string with blankspace?

A.           inline

B.           getline

C.           putline

D.           None of the mentioned

Answer :- Option B Explanation:

If a user wants to input a sentence with blankspaces, then he may use the function getline.

135. Pick out the correct objects about the instantiation of output stream.

A.           cout

B.           cerr

C.           clog

D.           All of the mentioned

Answer :- Option D

Explanation: cout, cerr and clog are the standard objects for the instantiation of output stream class.

136. What is meant by ofstream in c++?

A.           Writes to a file

B.           Reads from a file

C.           Both a & b

D.           None of the mentioned

Answer:- Option A

Explanation: ofstream is a stream class to write on files.

137. How many groups of output of operation are there in c++? a) 1

b)            2

c)            3

d)            4

Answer :- b

Explanation: There are two groups of output operation in c++. They are formatted output and unformatted output.

138. Where does a cin stops it extraction of data? a) By seeing a blank space

b)            By seeing (

c)            By seeing a blank space & (

d)            By seeing <

Answer :- a

Explanation: cin will stop its extraction when it encounters a blank space

139. Which is used to get the input during runtime? a) cout

b)            cin

c)            coi

d)            cinout

Answer :- b

Explanation: cin is mainly used to get the input during the runtime.

140. When will the cin can start processing of input? a) After pressing return key

b)            BY pressing blank space

c)            After pressing return key & BY pressing blank space

d)            BY pressing delete space

Answer :- a

Explanation: When you give some input to console the processing of the input starts when the user presses enter/return key.

141. What must be specified when we construct an object of class ostream? a) stream

b)            streambuf

c)            memory

d)            steamostream

Answer:- b

Explanation: If you construct an object of class ostream, you must specify a streambuf object to the constructor.

142. How many groups of output of operation are there in c++? a) 1

b)            2

c)            3

d)            4

Answer:- b

Explanation: There are two groups of output operation in c++. They are formatted output and unformatted output.

143. What is the use of the ‘finally’ keyword?

a)            It used to execute at the starting of the program

b)            It will be executed at the end of the program even if the exception arised

c)            It will be executed at the starting of the program even if the exception arised

d)            It will be executed at the middle of the program even if the exception arised

Answer:- b

Explanation: finally keyword will be executed at the end of all the exception.

144. How do define the user-defined exceptions?

a)            inheriting and overriding exception class functionality

b)            overriding class functionality

c)            inheriting class functionality

d)            delting and adding class member

Answer:- a

Explanation: User defined exceptions can be done by inheriting and overriding the exception class functionality.

145.       How many types of exception handling are there in c++? a) 1

b)            2

c)            3

d)            4

Answer:- b

Explanation: There are two types of exception handling in c++. They are synchronous exception handling and asynchronous exception handling.

146.       Which block should be placed after try block? a) catch

b)            throw

c)            either catch or throw

d)            handler

Answer:- a

Explanation: Syntax of try catch block

147.  When exceptions are used?

a) To preserve the program

b)            Exceptions are used when postconditions of a function cannot be satisfied

c)            Exceptions are used when postconditions of a function can be satisfied

d)            Exceptions are used when preconditions of a function cannot be satisfied

Answer :- c

Explanation: Exceptions are used when postconditions of a function can be satisfied.

Leave a Reply

Your email address will not be published. Required fields are marked *