Join Telegram - https://t.
me/hpjoa
Instagram-https://instagram.com/study_of_h.p?utm_medium=copy_link
C++ Relate Importan MCQ
{PART-2}
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
Answer: a
Explanation: char* are terminated by a ‘\0’ character so the string
“Hello\0World” will be cut down to “Hello”.
Which of the following is used to terminate the function declaration in
C++?
a) ;
b) ]
c) )
d) :
Answer: a
Explanation: ; semicolon is used to terminate a function declaration
statement in C++.
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
Answer: b
Explanation: The literal value for 74 is J. So it will be printing J.
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
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
Which keyword is used to define the macros in c++?
a) #macro
b) #define
c) macro
d) define
Answer: b
Explanation: #define is the keyword that is used to define the macros in
c++.
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
Answer: d
Explanation: Scope resolution operator(::) is used to access a static
member of a class.
The C++ code which causes abnormal termination/behaviour of a program
should be written under _________ block.
a) catch
b) throw
c) try
d) finally
Answer: c
Explanation: Code that leads to the abnormal termination of the program
should be written under the try block.
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
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.
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
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
Which of the following symbol is used to declare the preprocessor
directives in C++?
a) $
b) ^
c) #
d) *
Answer: c
Explanation: # symbol is used to declare the preprocessor directives.
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
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.
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
Answer: d
Explanation: As we are passing value by reference therefore the
changein the value is reflected back to the passed variable number
hence value of number is changed to 870.
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
Answer: c
Explanation: Polymorphism is literally meant class having many forms.
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.!
Answer: a
Explanation: back() function modifies the last character of the string with
the character provided.
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
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.
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
Answer: a
$ a.out
Explanation: We just casted this from void to int, so it prints 5
Output:
$ g++ poi1.cpp
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
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.
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
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.
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
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.
Which concept allows you to reuse the written code in C++?
a) Inheritance
b) Polymorphism
c) Abstraction
d) Encapsulation
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.
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
Answer: b
Explanation: In this program, we are divide and multiply the values.
Output:
$ g++ over3.cpp
$ a.out
10 2.5
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
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.
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
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
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
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:
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;
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
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.
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
Answer: a
Explanation: This program will terminate because the cout element is out
of range.
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
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.