C++ chioce
C++ chioce
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”.
#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.
#include <stdio.h>
void func(void)
{
printf("Hello");
}
void main()
{
func();
func(2);
}
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.
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.
1. int main()
2. {
3. register int i = 1;
4. int *ptr = &i;
5. cout << *ptr;
6. return 0;
7. }
#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.
#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++.
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.
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. }
---------------------------
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.
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.
#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.
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.
#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.
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
#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
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.
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.
#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.