0% found this document useful (0 votes)
27 views18 pages

C++ chioce

Uploaded by

Eyachew Tewabe
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
27 views18 pages

C++ chioce

Uploaded by

Eyachew Tewabe
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 18

C++ Programming MCQ (Multiple Choice

Questions)
Here are 1000 MCQs on C++ (Chapterwise).
1. Who invented C++?
a) Dennis Ritchie
b) Ken Thompson
c) Brian Kernighan
d) Bjarne Stroustrup
View Answer
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
View Answer
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 the correct syntax of including a user defined header files in C+
+?
a) #include [userdefined]
b) #include “userdefined”
c) #include <userdefined.h>
d) #include <userdefined>
View Answer
Answer: b
Explanation: C++ uses double quotes to include a user-defined header file. The correct
syntax of including user-defined is #include “userdefinedname”.

4. Which of the following is used for comments in C++?


a) /* comment */
b) // comment */
c) // comment
d) both // comment or /* comment */
View Answer
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.

5. Which of the following user-defined header file extension used in c++?


a) hg
b) cpp
c) h
d) hf
View Answer
Answer: c
Explanation: .h extensions are used for user defined header files. To include a user defined
header file one should use #include”name.h” i.e. enclosed within double quotes.
advertisement
6. Which of the following is a correct identifier in C++?
a) VAR_1234
b) $var_name
c) 7VARNAME
d) 7var_name
View Answer
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.

7. Which of the following is not a type of Constructor in C++?


a) Default constructor
b) Parameterized constructor
c) Copy constructor
d) Friend constructor
View Answer
Answer: d
Explanation: Friend function is not a constructor whereas others are a type of constructor
used for object initialization.

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


a) Left-right
b) Right-left
c) Bottom-up
d) Top-down
View Answer
Answer: c
Explanation: C++ is an object-oriented language and OOL uses a bottom-up approach to
solve/view a problem.

9. What is virtual inheritance in C++?


a) C++ technique to enhance multiple inheritance
b) C++ technique to ensure that a private member of the base class can be accessed
somehow
c) C++ technique to avoid multiple inheritances of classes
d) C++ technique to avoid multiple copies of the base class into children/derived class
View Answer
Answer: d
Explanation: Virtual inheritance is a C++ technique with which it ensures that a derived
class contains only one copy of the base class’s variables. Refer Wikipedia for more info.
10. What happens if the following C++ statement is compiled and executed?

int *ptr = NULL;


delete ptr;

a) The program is not semantically correct


b) The program is compiled and executed successfully
c) The program gives a compile-time error
d) The program compiled successfully but throws an error during run-time
View Answer
Answer: b
Explanation: The above statement is syntactically and semantically correct as C++ allows
the programmer to delete a NULL pointer, therefore, the program is compiled and executed
successfully.

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

#include <iostream>
#include <string>
using namespace std;
int main(int argc, char const *argv[])
{
char s1[6] = "Hello";
char s2[6] = "World";
char s3[12] = s1 + " " + s2;
cout<<s3;
return 0;
}

a) Hello
b) World
c) Error
d) Hello World
View Answer
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 understood what to do about this expression.

12. What is the difference between delete and delete[] in C++?


a) delete is syntactically correct but delete[] is wrong and hence will give an error if used in
any case
b) delete is used to delete normal objects whereas delete[] is used to pointer objects
c) delete is a keyword whereas delete[] is an identifier
d) delete is used to delete single object whereas delete[] is used to multiple(array/pointer of)
objects
View Answer
Answer: d
Explanation: delete is used to delete a single object initiated using new keyword whereas
delete[] is used to delete a group of objects initiated with the new operator.

13. What happens if the following program is executed in C and C++?


#include <stdio.h>
int main(void)
{
int new = 5;
printf("%d", new);
}

a) Error in C and successful execution in C++


b) Error in both C and C++
c) Error in C++ and successful execution in C
d) A successful run in both C and C++
View Answer
Answer: c
Explanation: new is a keyword in C++, therefore, we cannot declare a variable with name
new but as there is no such keyword new in C, therefore, the program is compiled and
executed successfully in C.

14. What happens if the following program is executed in C and C++?

#include <stdio.h>
void func(void)
{
printf("Hello");
}
void main()
{
func();
func(2);
}

a) Outputs Hello twice in both C and C++


b) Error in C and successful execution in C++
c) Error in C++ and successful execution in C
d) Error in both C and C++
View Answer
Answer: d
Explanation: As the func(void) needs no argument during its call, hence when we are calling
func(2) with 2 as passed as a parameter then this statement gives the error in both C++ and
C compiler.

15. Which of the following is correct about this pointer in C++?


a) this pointer is passed as a hidden argument in all static variables of a class
b) this pointer is passed as a hidden argument in all the functions of a class
c) this pointer is passed as a hidden argument in all non-static functions of a class
d) this pointer is passed as a hidden argument in all static functions of a class
View Answer
Answer: c
Explanation: As static functions are a type of global function for a class so all the object
shares the common instance of that static function whereas all the objects have there own
instance for non-static functions and hence they are passed as a hidden argument in all the
non-static members but not in static members.
16. What will be the output of the following C++ code?

1. #include <iostream>
2. #include <string>
3. #include <algorithm>
4. using namespace std;
5. int main()
6. {
7. string s = "spaces in text";
8. s.erase(remove(s.begin(), s.end(), ' ' ), s.end() ) ;
9. cout << s << endl;
10. }

a) spacesintext
b) spaces in text
c) spaces
d) spaces in
View Answer
Answer: a
Explanation: In this program, We formed a algorithm to remove spaces in the string.
Output:
$ g++ dan.cpp
$ a.out
spacesintext

17. Which of the following C++ code will give error on compilation?

================code 1=================
#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
cout<<"Hello World";
return 0;
}
========================================
================code 2=================
#include <iostream>
int main(int argc, char const *argv[])
{
std::cout<<"Hello World";
return 0;
}
========================================

a) Code 1 only
b) Neither code 1 nor code 2
c) Both code 1 and code 2
d) Code 2 only
View Answer
Answer: b
Explanation: Neither code 1 nor code 2 will give an error as both are syntactically correct as
in first code we have included namespace std and in second one we have used scope
resolution operator to resolve the conflict.

18. Which of the following type is provided by C++ but not C?


a) double
b) float
c) int
d) bool
View Answer
19. What is the value of p in the following C++ code snippet?

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int p;
6. bool a = true;
7. bool b = false;
8. int x = 10;
9. int y = 5;
10. p = ((x | y) + (a + b));
11. cout << p;
12. return 0;
13. }

a) 12
b) 0
c) 2
d) 16
View Answer
Answer: d
Explanation: | means bitwise OR operation so x | y (0101 | 1010) will be evaluated to 1111
which is integer 15 and as a is true and b is false so a+b(1 + 0) = 1. So final value of
expression in line #10 will be 15 + 1 = 16.

20. By default, all the files in C++ are opened in _________ mode.
a) Binary
b) VTC
c) Text
d) ISCII
View Answer
Answer: c
Explanation: By default, all the files in C++ are opened in text mode. They read the file as
normal text.

21. What will be the output of the following C++ function?

1. int main()
2. {
3. register int i = 1;
4. int *ptr = &i;
5. cout << *ptr;
6. return 0;
7. }

a) Runtime error may be possible


b) Compiler error may be possible
c) 1
d) 0
View Answer
Answer: b
Explanation: Using & on a register variable may be invalid, since the compiler may store the
variable in a register, and finding the address of it is illegal.

22. Which of the following correctly declares an array in C++?


a) array{10};
b) array array[10];
c) int array;
d) int array[10];
View Answer
Answer: d
Explanation: Because array variable and values need to be declared after the datatype only.

23. What is the size of wchar_t in C++?


a) Based on the number of bits in the system
b) 2 or 4
c) 4
d) 2
View Answer
Answer: a
Explanation: Compiler wants to make CPU as more efficient in accessing the next value.

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

#include<iostream>
using namespace std;
int main ()
{
int cin;
cin >> cin;
cout << "cin: " << cin;
return 0;
}

a) Segmentation fault
b) Nothing is printed
c) Error
d) cin: garbage value
View Answer
Answer: d
Explanation: cin is a variable hence overrides the cin object. cin >> cin has no meaning so
no error.
25. 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
View Answer
26. Which is more effective while calling the C++ functions?
a) call by object
b) call by pointer
c) call by value
d) call by reference
View Answer
Answer: d
Explanation: In the call by reference, it will just passes the reference of the memory
addresses of passed values rather than copying the value to new memories which reduces
the overall time and memory use.

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

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main(int argc, char const *argv[])
{
const char *a = "Hello\0World";
cout<<a;
return 0;
}

a) Hello
b) World
c) Error
d) Hello World
View Answer
Answer: a
Explanation: char* are terminated by a ‘\0’ character so the string “Hello\0World” will be cut
down to “Hello”.

28. Which of the following is used to terminate the function declaration in C++?
a) ;
b) ]
c) )
d) :
View Answer
Answer: a
Explanation: ; semicolon is used to terminate a function declaration statement in C++.

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


1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. char c = 74;
6. cout << c;
7. return 0;
8. }

a) I
b) J
c) A
d) N
View Answer
Answer: b
Explanation: The literal value for 74 is J. So it will be printing J.

30. What will be the output of the following C++ program?

1. #include <iomanip>
2. #include <iostream>
3. using namespace std;
4. int main()
5. {
6. cout << setprecision(17);
7. double d = 0.1;
8. cout << d << endl;
9. return 0;
10. }

a) compile time error


b) 0.100001
c) 0.11
d) 0.10000000000000001
View Answer
Answer: d
Explanation: The double had to truncate the approximation due to its limited memory, which
resulted in a number that is not exactly 0.1.
Output:
$ g++ float2.out
$ a.out
0.10000000000000001

31. Which keyword is used to define the macros in c++?


a) #macro
b) #define
c) macro
d) define
View Answer
Answer: b
Explanation: #define is the keyword that is used to define the macros in c++.
32. What is the correct syntax of accessing a static member of a class in C++?

---------------------------
Example class:
class A
{
public:
static int value;
}
---------------------------

a) A->value
b) A^value
c) A.value
d) A::value
View Answer
Answer: d
Explanation: Scope resolution operator(::) is used to access a static member of a class.

33. The C++ code which causes abnormal termination/behaviour of a program should be
written under _________ block.
a) catch
b) throw
c) try
d) finally
View Answer
Answer: c
Explanation: Code that leads to the abnormal termination of the program should be written
under the try block.

34. 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
View Answer
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.

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

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a = 5;
6. float b;
7. cout << sizeof(++a + b);
8. cout << a;
9. return 0;
10. }

a) 2 5
b) 4 5
c) 4 6
d) 2 6
View Answer
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.
Output:
$ g++ size3.cpp
$ a.out
4 5

36. Which of the following symbol is used to declare the preprocessor directives in C++?
a) $
b) ^
c) #
d) *
View Answer
Answer: c
Explanation: # symbol is used to declare the preprocessor directives.

37. What will be the output of the following C++ program?

#include<iostream>
using namespace std;
int main()
{
int a = 5;
auto check = [=]()
{
a = 10;
};
check();
cout<<"Value of a: "<<a<<endl;
return 0;
}

a) Segmentation fault
b) Value of a: 5
c) Value of a: 10
d) Error
View Answer
Answer: d
Explanation: As this lambda expression is capturing the extrenal variable by value therefore
the value of a cannot be changes inside the lambda expression hence the program gives
error.

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


#include <iostream>
using namespace std;
void square (int *x, int *y)
{
*x = (*x) * --(*y);
}
int main ( )
{
int number = 30;
square(&number, &number);
cout << number;
return 0;
}

a) 30
b) Error
c) Segmentation fault
d) 870
View Answer
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.

39. 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
View Answer
Answer: c
Explanation: Polymorphism is literally meant class having many forms.

40. What will be the output of the following C++ program?

#include <iostream>
#include <string>
using namespace std;
int main ()
{
std::string str ("Sanfoundry.");
str.back() = '!';
std::cout << str << endl;
return 0;
}

a) Sanfoundry!
b) Sanfoundry!.
c) Sanfoundry.
d) Sanfoundry.!
View Answer
Answer: a
Explanation: back() function modifies the last character of the string with the character
provided.

41. Pick the incorrect statement about inline functions in C++?


a) Saves overhead of a return call from a function
b) They are generally very large and complicated function
c) These functions are inserted/substituted at the point of call
d) They reduce function call overheads
View Answer
Answer: b
Explanation: Inline are functions that are expanded when it is called. The whole code of the
inline function gets inserted/substituted at the point of call. In this, they help in reducing the
function call overheads. Also they save overhead of a return call from a function. Inline
functions are generally kept small.

42. What will be the output of the following C++ program?

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int n = 5;
6. void *p = &n;
7. int *pi = static_cast<int*>(p);
8. cout << *pi << endl;
9. return 0;
10. }

a) 5
b) 6
c) compile time error
d) runtime error
View Answer
Answer: a
Explanation: We just casted this from void to int, so it prints 5
Output:
$ g++ poi1.cpp
$ a.out
5

43. 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
View Answer
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.
44. 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
View Answer
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.

45. What will be the output of the following C++ program?

#include <iostream>
using namespace std;
int main()
{
try
{
try
{
throw 20;
}
catch (int n)
{
cout << "Inner Catch\n";
throw;
}
}
catch (int x)
{
cout << "Outer Catch\n";
}
return 0;
}

a) Outer Catch
b)

Inner Catch

Outer Catch

c) Error
d) Inner Catch
View Answer
Answer: b
Explanation: The exception thrown by the inner try catch block is caught by the inner block
hence “Inner Catch” is printed but as inner catch block again throws an exception further
therefore the exception is thrown further which is caught by the outer catch block hence
“Outer Catch” is also printed.
46. Which concept allows you to reuse the written code in C++?
a) Inheritance
b) Polymorphism
c) Abstraction
d) Encapsulation
View Answer
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.

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

1. #include <iostream>
2. using namespace std;
3. int operate (int a, int b)
4. {
5. return (a * b);
6. }
7. float operate (float a, float b)
8. {
9. return (a / b);
10. }
11. int main()
12. {
13. int x = 5, y = 2;
14. float n = 5.0, m = 2.0;
15. cout << operate(x, y) <<"\t";
16. cout << operate (n, m);
17. return 0;
18. }

a) 10.0 5
b) 10 2.5
c) 10.0 5.0
d) 5.0 2.5
View Answer
Answer: b
Explanation: In this program, we are divide and multiply the values.
Output:
$ g++ over3.cpp
$ a.out
10 2.5

48. 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
View Answer
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.

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

1. #include <iostream>
2. using namespace std;
3. int main ()
4. {
5. int a, b, c;
6. a = 2;
7. b = 7;
8. c = (a > b) ? a : b;
9. cout << c;
10. return 0;
11. }

a) 12
b) 14
c) 6
d) 7
View Answer
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
Output:
$ g++ ess1.cpp
$ a.out
7

50. 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
View Answer
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;

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

1. #include <stdio.h>
2. #include<iostream>
3. using namespace std;
4. int main ()
5. {
6. int array[] = {0, 2, 4, 6, 7, 5, 3};
7. int n, result = 0;
8. for (n = 0; n < 8; n++)
9. {
10. result += array[n];
11. }
12. cout << result;
13. return 0;
14. }

a) 21
b) 27
c) 26
d) 25
View Answer
Answer: b
Explanation: We are adding all the elements in the array and printing it. Total elements in
the array is 7, but our for loop will go beyond 7 and add a garbage value.

52. What will be the output of the following C++ program?

1. #include <iostream>
2. #include <string>
3. using namespace std;
4. int main ()
5. {
6. string str ("Sanfoundry");
7. for (size_t i = 0; i < str.length();)
8. {
9. cout << str.at(i-1);
10. }
11. return 0;
12. }

a) runtime error
b) Sanfo
c) S
d) Sanfoundry
View Answer
Answer: a
Explanation: This program will terminate because the cout element is out of range.

53. What will be the output of the following C++ program?

#include <iostream>
using namespace std;
class A{
public:
A(){
cout<<"Constructor called\n";
}
~A(){
cout<<"Destructor called\n";
}
};
int main(int argc, char const *argv[])
{
A *a = new A[5];
delete[] a;
return 0;
}

a) Segmentation fault
b) “Constructor called” five times and then “Destructor called” five times
c) “Constructor called” five times and then “Destructor called” once
d) Error
View Answer
Answer: b
Explanation: In the above program we have first initiated five-pointer variables using new
keyword hence fives time constructor will be called after that as we using delete[] (used for
deleting multiple objects) to delete variables hence all the five objects created will be
destroyed and hence five times destructor will be called.

You might also like