mcq c--
mcq c--
c++
21CS382
1. 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.
2. 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).
3. Which of the following is used for comments in
C++?
a) /* comment */
b) // comment */
c) // comment
d) both // comment or /* comment */
Answer: d
Explanation: Both the ways are used
for commenting in C++
programming. // is used for single
line comments and /* … */ is used
for multiple line comments.
4. Which of the following is a correct
identifier in C++?
a) VAR_1234
b) $var_name
c) 7VARNAME
d) 7var_name
Answer: a
Explanation: The rules for writing an identifier is
as follows:
i) may contain lowercase/uppercase letters, digits
or underscore(_) only
ii) should start with a non-digit character
iii) should not contain any special characters like
@, $, etc.
5. 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.
6. What will be the output of the
following C++ code?
a) Hello
b) World
c) Error
d) Hello World
• Answer: c
• Explanation: There is no operation defined
for the addition of character array in C++
hence the compiler throws an error as it
does not understand what to do about this
expression.
7. What happens if the following
program is executed in C and C++?
a) Hello
b) World
c) Error
d) Hello World
•Answer: a
•Explanation: char* are terminated by a
‘\0’ character so the string “Hello\
0World” will be cut down to “Hello”.
14 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++.
15. What will be the output of the
following C++ code?
a) I
b) J
c) A
d) N
•Answer: b
•Explanation: The literal value for 74 is J.
So it will be printing J.
• 16. The C++ code which causes abnormal
termination/behaviour of a program should
be written under _________ block.
• a) catch
• b) throw
• c) try
• d) finally
Answer: c
Explanation: Code that leads to the
abnormal termination of the program
should be written under the try block.
17. 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 defined
earlier.
18. What will be the output of the
following C++ code?
a) 2 5
b) 4 5
c) 4 6
d) 2 6
• Answer: b
• Explanation: The a as a integer will be converted
to float while calculating the size. The value of
any variable doesn’t modify inside sizeof
operator. Hence value of variable a will remain 5.
•19. Which of the following symbol is
used to declare the preprocessor
directives in C++?
•a) $
•b) ^
•c) #
•d) *
•Answer: c
•Explanation: # symbol is used to
declare the preprocessor directives.
20. What will be the output of the
following C++ code?
a) 30
b) Error
c) Segmentation fault
d) 870
• Answer: d
• Explanation: As we are passing value by
reference therefore the change in the value
is reflected back to the passed variable
number hence value of number is changed
to 870.
21. 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
•Explanation: Polymorphism is
literally meant class having
many forms.
22. Which of the following constructors are
provided by the C++ compiler if not defined
in a class?
a) Copy constructor
b) Default constructor
c) Assignment constructor
d) All of the mentioned
• Answer: d
• Explanation: If a programmer does not define
the above constructors in a class the C++
compiler by default provides these constructors
to avoid error on basic operations.
23. 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.
24. What will be the output of the
following C++ code snippet?
a) 10.0 5
b) 10 2.5
c) 10.0 5.0
d) 5.0 2.5
•Answer: b
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?
a) 12
b) 14
c) 6
d) 7
•Answer: d
27. What is the benefit of c++ input and
output over c input and output?
• a) Both Type safety & Exception
• b) Sequence container
• c) Exception
• d) Type safety
• Answer: d
• Explanation: C++ input and output are type safety that
means we don’t need to specify the type of variable
we are printing.
• eg:
• in C we need to specify %d showing that an integer
will be printed, whereas in C++ we just cout the
variable.
• printf(“%d”, a);
• cout<<a;
28 What will be the output of the
following C++ code snippet?
a) 21
b) 27
c) 26
d) 25
29 Wrapping data and its related
functionality into a single entity is known as
a) Abstraction
b) Encapsulation
c) Polymorphism
d) Modularity
• Answer: b
• Explanation: In OOPs, the property of
enclosing data and its related functions into
a single entity(in C++ we call them classes) is
called encapsulation.
30 How structures and classes in C++ differ?
• a) In Structures, members are public by default
whereas, in Classes, they are private by default
• b) In Structures, members are private by default
whereas, in Classes, they are public by default
• c) Structures by default hide every member whereas
classes do not
• d) Structures cannot have private members whereas
classes can have
• Answer: a
• Explanation: Structure members are public
by default whereas, class members are
private by default. Both of them can have
private and public members.
31. What does polymorphism in OOPs mean?
a) Concept of allowing overiding of functions
b) Concept of hiding data
c) Concept of keeping things in differnt modules/files
d) Concept of wrapping things into a single unit
• Answer: a
• Explanation: In OOPs, Polymorphism is the
concept of allowing a user to override
functions either by changing the types or
number of parameters passed.
32 Which of the following explains
Polymorphism?
• a)
• Answer: c
• Explanation: Polymorphism means overriding the
same function by changing types or number of
arguments. So we have only two options which has
the same function names, but as one can observe that
in one option types, name and number of parameters
all are same which will lead to an error. Hence that is
wrong so the option having same name and different
types or number of parameters is correct.
33 Which of the following shows
multiple inheritances?
•a) A->B->C
•b) A->B; A->C
•c) A,B->C
•d) B->A
• Answer: c
• Explanation: In multiple inheritance, a single
class is inherited from two classes. So in A,B->C,
Class C is inherited from A and B, whereas in A-
>B->C, C from B and B from A called simple
inheritance, in A->B; A->C, B and C are inherited
from A which is called hierarchical inheritance.
34 How access specifiers in Class helps in
Abstraction?
• a) They does not helps in any way
• b) They allows us to show only required things to
outer world
• c) They help in keeping things together
• d) Abstraction concept is not used in classes
• Answer: b
• Explanation: Abstraction is the concept of
hiding things from the outer world and
showing only the required things to the
world, which is where access specifiers
private, protected and public helps in
keeping our knowledge hidden from the
world.
35 What does modularity mean?
• a) Hiding part of program
• b) Subdividing program into small independent
parts
• c) Overriding parts of program
• d) Wrapping things into single unit
• Answer: b
• Explanation: Modularity means dividing a
program into independent sub programs so
that it can be invoked from other parts of
the same program or any other program.
36 Which of the following feature of OOPs is
not used in the following C++ code?
a) Abstraction
b) Encapsulation
c) Inheritance
d) Polymorphism
• Answer: d
• Explanation: As i and j members are private i.e. they are hidden from
outer world therefore we have used the concept of abstraction. Next
data members and there related functions are put together into single
class therefore encapsulation is used. Also as class B is derived from A
therefore Inheritance concept is used. But as no function is
overloaded in any of the classes therefore, the concept of
polymorphism is missing here.
37 Which of the following class allows to declare
only one object of it?
a) Abstract class
b) Virtual class
c) Singleton class
d) Friend class
• Answer: c
• Explanation: Singleton class allows the
programmer to declare only one object of it,
If one tries to declare more than one object
the program results into error.
38 Which of the following is not a type of
Constructor?
a) Friend constructor
b) Copy constructor
c) Default constructor
d) Parameterized constructor
• Answer: a
• Explanation: Friend function is not a
constructor whereas others are a type of
constructor used for object initialization.
39 Out of the following, which is not a member of
the class?
a) Static function
b) Friend function
c) Constant function
d) Virtual function
• Answer: b
• Explanation: Friend function is not a
member of the class. They are given the
same access rights as the class member
function have but they are not actual
members of the class.
40 What is the other name used for functions
inside a class?
a) Member variables
b) Member functions
c) Class functions
d) Class variables
•Answer: b
•Explanation: Functions of a class are also
known as member functions of a class.
41 Which of the following cannot be a
friend?
a) Function
b) Class
c) Object
d) Operator function
•Answer: c
•Explanation: Objects of any class cannot
be made a friend of any other or same
class whereas functions, classes and
operator functions can be made a
friend.
42 What is the correct syntax of accessing a
static member of a class in C++?
a) A->value
b) A^value
c) A.value
d) A::value
•Answer: d
•Explanation: Scope resolution
operator(::) is used to access a static
member of a class.
43 What will be the output of the
following C++ code?
a) 12
b) 14
c) 6
d) 7
• Answer: d
• Explanation: We are using the ternary operator to
evaluate this expression. It will return first option, if
first condition is true otherwise it will return second
44 Which operator is having the highest
precedence?
a) postfix
b) unary
c) shift
d) equality
• Answer: a
• Explanation: The operator which is having the
highest precedence is postfix and lowest is
equality.
45 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.
46 Identify the incorrect option.
a) enumerators are constants
b) enumerators are user-defined types
c) enumerators are same as macros
d) enumerator values start from 0 by default
•Answer: c
•Explanation: Enumerators are used in
order to create our own types whereas
macros are textual substitutions.
47 In which type do the enumerators are stored
by the compiler?
a) string
b) integer
c) float
d) string & float
• Answer: b
• Explanation: In C++, enumerations are
stored as integers by the compiler starting
with 0.
48 To which of these enumerators can be
assigned?
• a) integer
• b) negative
• c) enumerator
• d) all of the mentioned
• Answer: d
• Explanation: Since enumerators evaluate to
integers, and integers can be assigned to
enumerators, enumerators can be assigned to
other enumerators.
49 What will happen when defining the enumerated
type?
• a) it will not allocate memory
• b) it will allocate memory
• c) it will not allocate memory to its variables
• d) allocate memory to objects
•Answer: a
•Explanation: Enumerator will allocate
the memory when its variables are
defined.
50 Which variable does equals in size with enum variable?
a) int variable
b) float variable
c) string variable
d) float & string variable
• Answer: a
• Explanation: The enum variable is converted
to an integer and stored by the compiler. So
both are equal in size.
51 What will be the output of the
following C++ code?
a) If you were cat, you would be 5
b) If you were cat, you would be 2
c) If you were cat, you would be 7
d) If you were cat, you would be 9
• Answer: b
• Explanation: The age will be divided by using
compound assignment operator and so it will
return the age of the cat according to your age.
52 What will be the output of the
following C++ code?
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.
53 What will be the output of the
following C++ code?
a) 012345
b) 123456
c) compile time error
d) runtime error
• Answer: a
• Explanation: The enumerator values start from
zero if it is unassigned.
54 How many ways of passing a parameter
are there in c++?
a) 1
b) 2
c) 3
d) 4
• Answer: c
• Explanation: There are three ways of
passing a parameter. They are pass by
value,pass by reference and pass by pointer.
55 Which is used to keep the call by reference
value as intact?
a) static
b) const
c) absolute
d) virtual
• Answer: b
• Explanation: Because const will not change
the value of the variables during the
execution.
56 Which of the following features must be supported
by any programming language to become a pure
object-oriented programming language?
• Encapsulation
• Inheritance
• Polymorphism
• All of the above
• Answer: D