C++ MCQs

C++ MCQs

Try to answer these 600+ C++ MCQs and check your understanding of the C++ subject.
Scroll down and let's begin!

1: What does OOD stand for?

A.   Operating on Objects in Design

B.   Object-Oriented Design

C.   Overly Objective Design

D.   Object-oriented database

2: What is output of following program? #include <iostream> int main() { double x = 1.0; for(int i = 0; i < 3; ++i) x *= 0.1; std::cout << x * 1e3 - 1; }

A.   Always 0.

B.   It's depend of double implementation, usually not exactly zero.

C.   Always not 0.

3: struct A { int n; }; A a; What is the visibility of a.n?

A.   private

B.   protected

C.   public

D.   Visibility is defined for classes only.

4: std::make_heap() converts a range into a heap and std::sort_heap() turns a heap into a sorted sequence.

A.   true

B.   false

5: If you have two different C++ functions which have the same name but different parameter types, it is called...

A.   inline functions.

B.   recursive functions.

C.   function overloading.

6: Which of the following is a valid variable declaration statement?

A.   int a; b; c;

B.   int a, b, c;

C.   int a:

7: C++ statements are separated by this symbol:

A.   Hash symbol (#)

B.   Colon (:)

C.   Semi-colon (;)

D.   Addition sign (+)

8: Which of the following is a valid C++ function declaration which does not return a value?

A.   int myFunction( int a, int b)

B.   myFunction( int a, int b)

C.   void myFunction( int a, int b)

9: How do you declare an integer variable x in C++?

A.   declare x as integer;

B.   int x;

C.   x int;

D.   int<x>;

E.   x is integer;

10: Which of the following is not a loop structure?

A.   stop when loop

B.   do while loop

C.   for loop

11: Which of the following is not a fundamental data type in C++?

A.   char

B.   wide

C.   bool

12: Which of the following operators below allow you to define the member functions of a class outside the class?

A.   ?

B.   ,

C.   :%

D.   ::

13: Which of the following is not a C++ primitive type?

A.   int

B.   real

C.   float

D.   double

14: Which of the following statements tests to see if the sum is equal to 10 and the total is less than 20, and if so, prints the text string "incorrect."?

A.   None of these options

B.   if( (sum == 10) || (total < 20) )printf(

C.   if( (sum == 10) && (total < 20) )printf(

D.   ctrl+alt+del

15: Choose the function declaration which you would use if you did not need to return any value.

A.   myfunction(void)

B.   void myfunction()

C.   myfunction()

16: Which of the following is a reserved word in C++?

A.   CHAR

B.   char

C.   character

D.   Char

17: Which statement assigns to variable a the address of variable b?

A.   a = b;

B.   a = &b;

C.   a = *b;

18: Which is a valid comment statement in C++?

A.   Both of these

B.   /* this is a comment */

C.   // this is a comment

19: A void pointer is a special type of pointer which indicates the absence of a type for the pointer.

A.   True

B.   False

20: What does the following statement mean? const int a = 50;

A.   The value of a cannot change from 50.

B.   The initial value of a is 50 but you can change it.

C.   none of these

21: In C++, a single line comment needs to be begun with

A.   a leading //.

B.   all of these

C.   a leading /**.

22: What is the value of i after the following statement(s)? int i (4.36);

A.   4

B.   4.36

C.   4.4

D.   5

23: In the following line of C++ code, int foo[50]; what does the number 50 represent?

A.   The maximum integer value that can be placed in the array.

B.   The initial value of the first array element.

C.   The number of integer elements the array shall hold.

24: The printmsg function does not require any arguments. Choose the statement which calls the function.

A.   printmsg();

B.   void printmsg();

C.   printmsg;

25: Can constructors be overloaded?

A.   Yes

B.   No

C.   Depends on the situation.

26: What is an advantage to using C++ Templates?

A.   reduce code duplication

B.   templates are typesafe

C.   all of these

D.   increase code flexibility

27: Which is(are) an example(s) of valid C++ function prototype(s)?

A.   int myFunction( int a, int b);

B.   all of these

C.   int myFunction(int, int);

28: Classes can contain static member variables which are global to the class and...

A.   none of these

B.   can be accessed by all objects of the same class.

C.   their values will change for each object of the same class.

29: What does the sizeof(arg) operator do?

A.   returns the maximum value of arg

B.   returns the size in bytes of arg

C.   returns the length in characters of arg

30: What is the difference between a class and a struct

A.   You can declare functions in a class, you cannot declare functions in a struct.

B.   They are the same.

C.   You cannot overload an operator in a struct.

D.   The members of a class are private by default, and the members of a struct are public by default.

31: True or False: In C++, a comment can only be specified with a leading //.

A.   True

B.   False

32: std::vector<int> v(4); std::fill(v.begin(), v.end(), 4); What is the content of vector v?

A.   4,4,4,4

B.   0,1,2,3

C.   4,5,6,7

D.   1,2,3,4

33: What will 'int a = 'a';' do?

A.   It will declare a new variable a and set it to 97 (assuming a machine that uses ASCII).

B.   It will declare a new variable a and set it to its previous value.

C.   It will cause an infinite loop.

D.   Nothing, it is an error and won't compile.

34: Which of the following can cause a memory corruption error?

A.   Freeing memory which has already been freed.

B.   All of these

C.   Using an address before memory is allocated and set.

35: Consider this code fragment: a = 25; b = &a; What does b equal?

A.   value contained in the address of a

B.   address of a

C.   25

36: Which of the following is not a specific type casting operator in the C++ language?

A.   dynamic_cast

B.   reinterpret_cast

C.   unknown_cast

D.   const_cast

37: A structure item exists in your code with an integer member units. You have the following variable declaration: item * myItem;. How do you access the value of units?

A.   *(myItem.units)

B.   myItem->units

C.   myItem.units

38: Defined data types (typedef) allow you to create...

A.   different types in C++.

B.   alternate names for existing types in C++.

39: Which of the following is a valid variable identifier in C++?

A.   m_test

B.   1_str

C.   class

40: What does the line: #include <iostream> mean in a C++ program?

A.   It tells the program to include the standard library header files.

B.   It tells the preprocessor to include the iostream standard file.

C.   It tells the preprocessor to include the iostream standard file only if it it required by the program.

41: Given this code, what is the output? #include struct shape { virtual void move() { std::cout << "shape::move\n"; } }; struct circle : public shape { void move() { std::cout << "circle::move\n"; } }; struct rectangle : public shape { void move() { std::cout << "rectangle::move\n"; } }; int main() { shape *s; s = new shape(); s->move(); s = new circle(); s->move(); s = new rectangle(); s->move(); return 0; }

A.   rectangle::move circle::move shape::move

B.   Compile error

C.   Runtime error

D.   shape::move shape::move shape::move

E.   shape::move circle::move rectangle::move

42: Within a class declaration, the statement "virtual int foo() = 0;" does what?

A.   Declares a volatile virtual function.

B.   Declares a pure virtual function.

C.   Causes a compiler error.

D.   Declares a default virtual function.

43: std::vector<int> foo(5);

A.   Initializes a vector with 5 elements of value 0.

B.   Initializes a vector with an element with the value 5.

44: Where does the compiler first look for file.h in the following directive: #include "file.h" ?

A.   In all directories specified by the PATH environment variable on the machine.

B.   In the default directories where it is configured to look for the standard header files

C.   The same directory that includes the file containing the directive.

45: What is the value of 2--2?

A.   Nothing, that is not a valid C++ expression.

B.   0

C.   -2

D.   2

E.   4

46: True or False: A class that has a pure virtual method can be instantiated.

A.   True

B.   False

47: Define a way other than using the keyword inline to make a function inline

A.   The function must be defined as the friend function.

B.   The function must be defined inside the class.

C.   The function must be defined outside the class.

48: Which of these is a difference between struct and class types?

A.   No difference.

B.   There are no inheritances with structs. Classes may be derived.

C.   Structs only allow variable definitions. Classes also allow function definition.

D.   Structs have public privacy by default, classes use private.

49: What is the size of the character array which would hold the value "Helloo"?

A.   8

B.   6

C.   7

50: In C++, what is the difference between these two declarations: void foo(); void foo(void);

A.   The second one is illegal.

B.   The first one is illegal.

C.   None, they are equivalent.

D.   One of them takes no value, the other takes any value.

51: Which of the following rules apply to operator overloading in C++?

A.   Both of the other answers are correct.

B.   Cannot redefine the meaning of built in types

C.   Cannot have default arguments

52: Which class(es) can be used to perform both input and output on files in C++?

A.   ofstream

B.   fstream

C.   ifstream

D.   All of the answers are correct.

53: What is the data type for the following: L"Hello World"?

A.   an integer string

B.   a string

C.   a wide character string

54: Thinking about data members and addressable memory, how are a struct, class and union different?

A.   No difference, all three are allocated memory sequentially

B.   There is no difference, all three are allocated memory randomly

C.   Union and class data members are allocated in memory sequentially whereas struct data members are allocated memory randomly

D.   Struct and class data members are allocated in memory sequentially whereas a union is allocated memory randomly

E.   Struct and class data members are allocated in memory sequentially whereas a union is allocated enough memory for the largest data member only

55: Which C++ keyword allows the compiler to determine the type of a variable by the value used to initialized it?

A.   auto

B.   var

C.   abstract

D.   virtual

56: int *array = new int[10]; delete array;

A.   This code will correctly free memory

B.   This code has undefined behavior

57: What does the "explicit" keyword do?

A.   It prevents a single-argument constructor from being used in an implicit conversion

B.   It requires a variable to reside in main memory instead of a processor's cache

C.   It makes the declaration of a default constructor mandatory

58: What is a virtual function in C++?

A.   A class member function that you expect to be redefined in derived classes.

B.   A class member function that must be redefined in derived classes.

C.   A class member function which does not need to be defined in the base class.

59: What is the time complexity of delete the first variable in a deque object (e.g., deque<int> a;)?

A.   O(n/2)

B.   O(logn)

C.   O(n)

D.   O(1)

60: Suppose int * a = new int[3]; How would you deallocate the memory block pointed by a?

A.   delete a[3];

B.   delete[] a;

C.   delete a;

D.   delete a[];

E.   delete[3] a;

61: Which operator cannot be overloaded by a class member function?

A.   ==

B.   ?

C.   *

D.   []

E.   ++

62: Which of the following statements uses a Lambda expression?

A.   std::regex e ("\\b(sub)([^ ]*)");

B.   int (*minus)(int, int) = subtract;

C.   bool is_odd = [](int n) {return n%2==1;};

63: A void pointer is a special type of pointer which indicates the...

A.   absence of a type for the pointer.

B.   none of these

C.   pointer has a NULL value.

64: Which of the following calls method foo() from the parent class Parent of the current class?

A.   this->parent->foo();

B.   Parent.foo();

C.   Parent::foo();

D.   Parent instance; instance.foo;

65: An anonymous namespace is used to...

A.   nest namespaces

B.   support closures

C.   prevent external access to declarations local to a compilation unit

D.   disambiguate declarations from other namespaces

A.   No.

B.   Yes.

C.   Yes, but only if you #include first.

D.   No, you need to add "const" before "char".

67: What is the data range for an unsigned integer value in C++ on a system where ints are 32 bits?

A.   0 to 2,147,483,647

B.    0 to 65,535

C.   0 to 4,294,967,295

D.    0 to 255

68: How would you access "blue" in the "color" enum class? enum class color { red, blue, green };

A.   blue

B.   color.blue

C.   color[1]

D.   color::blue

69: String literals can extend to more than a single line of code by putting which character at the end of each unfinished line?

A.   a newline (\n)

B.   a tab (\t)

C.   a backslash (\)

70: Which of the following is a potential side-effect of inlining functions?

A.   C++ standard guarantees that inlining does not result in any adverse side-effects

B.   The size of program's stack segment increases

C.   The size of the compiled binary increases

D.   The size of program's heap segment increases

71: What type of exceptions can the following function throw: int myfunction (int a);?

A.   All

B.   Standard

C.   None

72: Which one is theorically faster ?

A.   ++i

B.   i++

C.   Both are equally fast.

73: What is the value of x after the following code: int x = 0; if (x = 1) { x = 2; } else { x = 1; }

A.   The code will not compile

B.   1

C.   2

D.   0

74: What is the value of 10.10 % 3?

A.   1

B.   1.0

C.   3.03

D.   1.01

E.   None, that is an invalid mix of types.

75: class A { int x; protected: int y; public: int z; }; class B: private A { }; What is the privacy level of B::z?

A.   protected

B.   public

C.   B does not inherit access to z from A.

D.   private

76: If you do not supply any constructors for your class, which constructor(s) will be created by the compiler?

A.   Copy Constructor

B.   Default constructor

C.   Both of these

77: What is a key difference between a struct and union in terms of memory size?

A.   A union is the size of whichever data member is initialized whereas the size of a struct is the sum of the size of the struct data members

B.   A union is the size of its largest data member whereas the size of a struct is the sum of the size of the struct data members

C.   No difference; they are the sum of the sizes of their respective data members

D.   A union is the size of its largest data member whereas the size of a struct is at least the sum of the size of the struct data members

78: Given: union a { int x; short y; }; a var[20]; How many bytes of memory does var occupy?

A.   This is invalid C++ code

B.   80

C.   Depends

D.   120

E.   4

79: What is the output of the following code? int a=8; for(int i=1; i<=i*3; i++) n++;

A.   Depend upon the Execution.

B.   Infinite loop.

C.   Finite loop.

D.   All are wrong

80: Will the code below compile without error? struct c0 { int i; c0(int x) { i = x; } }; int main() { c0 x1(1); c0 x2(x1); return 0; }

A.   No. The constructor is not public.

B.   No. c0 x2 ( x1 ) will return error.

C.   No. struct types do not have constructors.

D.   Yes.

81: Which is NOT a valid hash table provided by the STL?

A.   hash_multiset

B.   hash_set

C.   hash_table

D.   hash_map

E.   hash_multimap

82: Where T is a type: std::vector<T>::at vs std::vector<T>::operator[]:

A.   at is always bounds checked. operator[] is not.

B.   at is not always bounds checked. operator[] is.

C.   at is equivalent to operator[]

83: enum { a, b, c = b + 2 }; What is the value of c?

A.   4

B.   Compile error.

C.   3

D.   2

84: What is the type being defined here: typedef A (B::*C)(D, E) const;

A.   A is defined to be a constant function in namespace B taking arguments of types D and E, returning a pointer to type C.

B.   C is defined to be a constant member function pointer of class B taking arguments of types D and E, returning type A.

C.   B is defined to be a class containing a constant member function called A, taking arguments of types D and E, returning a pointer to type C.

85: std::tuple person{"John Doe", 42}; std::cout << std::get<1>(person); What is the output?

A.   Compile error

B.   John Doe

C.   42

86: What is the below code? struct code { unsigned int x: 4; unsigned int y: 4; };

A.   A struct declaration with 2 arrays of int.

B.   A bit field structure declaration.

C.   A struct with in place initialization of its members.

D.   Invalid C++ code.

E.   A bit selector declaration.

87: What is the guaranteed complexity of std::push_heap?

A.   O(log(n))

B.   O(n)

C.   O(n^2)

D.   O(1)

88: According to the C++ standard, what is sizeof(void)?

A.   It depends on the host computer's word size.

B.   Nothing, void doesn't have a size.

C.   0

D.   1

E.   4

89: What is the output of the following program? #include #include int main () { std::vector int_values {3}; for (auto const& vv: int_values) { std::cout << vv; } }

A.   None of these

B.   3

C.   333

D.   Program fails during compilation

E.   000

90: int a[] {1, 2, 3}; a[[] { return 2; }()] += 2; What is the value of a[2]?

A.   3

B.   Undefined behavior

C.   5

D.   Compile error: malformed attribute.

E.   4

91: Is it possible to create class instance placed at a particular location in memory?

A.   Only by dirty hack with reinterpret_cast.

B.   No. Only allocation on stack or in dynamic memory is allowed.

C.   Yes, placement new does this.

92: class foo { foo(){}; }; class boo : public foo { boo() : foo() {}; }; which standard allows compilation of this code.

A.   none, the code wont compile

B.   c++11

C.   c++03

D.   c++98

E.   c++0x

93: What is value of x, if sizeof(int) == 4? unsigned int a = 0x98765432; unsigned int x = a >> 33;

A.   0x98765432

B.   1

C.   This is undefined behavior

D.   0

E.   0x4C3B2A19

94: What is the output of the following program? int a, b = 3; const int& ar[]= {a, b}; ar[0] = 2; std::cout << ar[0];

A.   3

B.   no output; program is ill-formed

C.   0

D.   2

95: std::vector<int> v(10); std::iota(v.begin(), v.end(), 10); What is the content of vector v?

A.   1,2,3,4,5,6,7,8,9,10

B.   10,11,12,13,14,15,16,17,18,19,20

C.   1,2,3,4,5,6,7,8,9

D.   10,11,12,13,14,15,16,17,18,19

96: Which function always returns an rvalue reference from "x", which can be used to indicate the object is going to be destroyed soon?

A.   std::xvalue(x)

B.   std::move(x)

C.   std::shift(x)

D.   std::destroy(x)

97: bool is_even(int i) { return i % 2 == 0; } int v[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; std::partition(v, v + 10, is_even); What is the content of array v?

A.   0,1,2,3,4,5,6,7,8,9

B.   9,8,7,6,5,4,3,2,1

C.   std::partition() doesn't work with plain arrays.

D.   9,1,7,3,5,4,6,2,8,0

E.   0,8,2,6,4,5,3,7,1,9

98: class A { int x; protected: int y; public: int z; }; class B: public virtual A { }; What is the privacy level of B::x?

A.   private

B.   B does not inherit access to x from A.

C.   public

D.   protected

99: The value of "(sizeof(short) == sizeof(int) && sizeof(int) == sizeof(long))" is

A.   implementation defined

B.   compiler error

C.   false

D.   true

100: What is the output of the following program? template void foo(U&, T&) { std::cout << "first"; } template void foo(int&, const T&) { std::cout << "second"; } int main() { int a; double g = 2.; foo(a, g); return 0; }

A.   compile error: ambiguous call

B.   first

C.   second