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.