C++ Related Important MCQ S (PART-2)
C++ Related Important MCQ S (PART-2)
me/hpjoa
Instagram-https://instagram.com/study_of_h.p?utm_medium=copy_link
#include <iostream>
#include <string>
#include <cstring>
cout<<a;
return 0;
a) Hello
b) World
c) Error
d) Hello World
Answer: a
a) ;
b) ]
c) )
d) :
Answer: a
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
Answer: b
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. }
b) 0.100001
c) 0.11
d) 0.10000000000000001
Answer: d
Output:
$ g++ float2.out
$ a.out
0.10000000000000001
a) #macro
b) #define
c) macro
d) define
Answer: b
---------------------------
Example class:
class A
public:
---------------------------
a) A->value
b) A^value
c) A.value
d) A::value
Answer: d
a) catch
b) throw
c) try
d) finally
Answer: c
b) Overloading of classes
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.
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
Answer: b
Output:
$ g++ size3.cpp
$ a.out
4 5
Which of the following symbol is used to declare the preprocessor
directives in C++?
a) $
b) ^
c) #
d) *
Answer: c
#include<iostream>
int main()
int a = 5;
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
Answer: d
#include <iostream>
*x = (*x) * --(*y);
int main ( )
return 0;
a) 30
b) Error
c) Segmentation fault
d) 870
Answer: d
Answer: c
#include <string>
int main ()
str.back() = '!';
return 0;
a) Sanfoundry!
b) Sanfoundry!.
c) Sanfoundry.
d) Sanfoundry.!
Answer: a
Explanation: back() function modifies the last character of the string with
the character provided.
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.
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
d) runtime error
Answer: a
$ a.out
Output:
$ g++ poi1.cpp
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
Answer: d
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
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.
#include <iostream>
int main()
try
try
throw 20;
catch (int n)
throw;
}
catch (int x)
return 0;
a) Outer Catch
b)
Inner Catch
Outer Catch
c) Error
d) Inner Catch
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.
a) Inheritance
b) Polymorphism
c) Abstraction
d) Encapsulation
Answer: a
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
Answer: b
Explanation: In this program, we are divide and multiply the values.
Output:
$ g++ over3.cpp
$ a.out
10 2.5
Answer: b
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
Answer: d
Output:
$ g++ ess1.cpp
$ a.out
What is the benefit of c++ input and output over c input and output?
b) Sequence container
c) Exception
d) Type safety
printf(“%d”, a);
cout<<a;
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:
printf(“%d”, a);
cout<<a;
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
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
Answer: a
Explanation: This program will terminate because the cout element is out
of range.
#include <iostream>
public:
A(){
cout<<"Constructor called\n";
~A(){
cout<<"Destructor called\n";
};
A *a = new A[5];
delete[] a;
return 0;
a) Segmentation fault
b) “Constructor called” five times and then “Destructor called” five times
d) Error
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.