1) What happens if the following C++ statement is compiled and executed?
int *ptr = NULL;
delete ptr;
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.
2) 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;
}
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.
3) 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
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.
4) 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++
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.
5) 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++
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.
6) 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
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.
7 ) What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string s = "spaces in text";
s.erase(remove(s.begin(), s.end(), ' ' ), s.end() ) ;
cout << s << endl;
}
a) spacesintext
b) spaces in text
c) spaces
d) spaces in
Answer: a
Explanation: In this program, We formed a algorithm to remove spaces in the string.
Output:
8) 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
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.
9) Which of the following type is provided by C++ but not C?
a) double
b) float
c) int
d) bool
Answer: d
Explanation: C++ provides the boolean type to handle true and false values whereas no such type is provided in C.
10) What is the value of p in the following C++ code snippet?
#include <iostream>
using namespace std;
int main()
{
int p;
bool a = true;
bool b = false;
int x = 10;
int y = 5;
p = ((x | y) + (a + b));
cout << p;
return 0;
}
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.
11 ) 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
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.
12) 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”.
13) What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int a = 5;
float b;
cout << sizeof(++a + b);
cout << a;
return 0;
}
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.
14) 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.
15) 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.
16) 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
nswer: 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;
17) 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.
18) Output
#include<iostream>
using namespace std;
int main()
{
int x = 5;
int y = 6;
x = x^y;
y = x^y;
x = x^y;
cout << x <<" " << y;
}
ans: 6 5 ( we can use ^ operator to swap two numbers without using extra space)
19) Output ?
#include<iostream>
using namespace std;
int main()
{
if(~0==1)
{
cout << "yes" << endl;
}
else
{
cout << "no" << endl;
}
}
output: no
cout << ~0 << endl; does not equal to 1 but , -1
20) Difference between typedef and #define?
The #define
and typedef
directives in C/C++ serve different purposes and operate in distinct ways:
1. #define
Preprocessor Directive:
#define
is a preprocessor directive used to create macro definitions.Text Replacement: It performs a simple text replacement before the compilation begins. It does not respect the type system of the language.
Syntax:
cppCopy code#define NAME value_or_code
Examples:
Constant Definition:
cppCopy code#define PI 3.14
This replaces every instance of
PI
with3.14
during preprocessing.Macro with Arguments:
cppCopy code#define SQUARE(x) ((x) * (x))
This replaces
SQUARE(5)
with((5) * (5))
.
No Type Checking: Because it’s purely text replacement,
#define
does not offer type checking, and misuse can lead to errors that are difficult to debug.
2. typedef
Keyword:
typedef
is a keyword used to create an alias (alternative name) for a data type.Type Alias: It allows you to define a new name for an existing type, which can make the code more readable or easier to modify.
Syntax:
cppCopy codetypedef existing_type new_name;
Examples:
Simple Alias:
cppCopy codetypedef unsigned long ulong;
Now,
ulong
can be used in place ofunsigned long
.Complex Type:
cppCopy codetypedef int* IntPointer;
Now,
IntPointer
can be used in place ofint*
.
Type-Safe: Unlike
#define
,typedef
respects the language's type system, making it safer and more reliable for defining type aliases, especially in complex code.
Key Differences:
Purpose:
#define
: Used for creating constants or macros.typedef
: Used for creating type aliases.
Operation:
#define
: Works by textual substitution before compilation.typedef
: Creates a new type name during compilation, without affecting the original type.
Type Checking:
#define
: No type checking.typedef
: Type-safe, with full type checking.
In general, use typedef
when you need to define a new name for an existing type, and use #define
for simple constants or macros.
21) Question: If class A
has an add
function with protected access and several other members in the public access specifier, and class B
inherits class A
privately, which of the following members will the user not be able to call from an object of class B
?
Options: A) The add
function from class A
B) The public members of class A
C) Any member inherited from class A
D) Both A and B
Correct Answer: C) Any member inherited from class A
Explanation: When a class B
inherits from another class A
privately, all the members of A
—whether they are public or protected—become private members of class B
. Private members are not accessible outside the class.
Protected members (like the
add
function in classA
) are meant to be accessible within the class and its derived classes. However, when inherited privately, these members become private in the derived class, meaning they are only accessible within the derived class itself and not through objects of the derived class.Public members of
A
will also become private inB
when inherited privately. This means they also cannot be accessed directly through objects ofB
.
Since the inheritance is private, the user will not be able to call any member of class A
from an object of class B
, making option C the correct answer.
22) What is the primary purpose of declaring a destructor as virtual in a base class in C++?
Options: A) To allow overloading of the destructor
B) To enable the creation of objects dynamically
C) To ensure the derived class's destructor is called when an object is deleted through a base class pointer
D) To allow the destructor to be called multiple times
Correct Answer: C) To ensure the derived class's destructor is called when an object is deleted through a base class pointer
Explanation:
- Declaring a destructor as virtual ensures that when a derived class object is deleted through a base class pointer, the derived class's destructor is invoked first, followed by the base class's destructor. This prevents resource leaks and undefined behavior.
23) Output ?
#include<iostream>
using namespace std;
class Test1
{
int x;
public:
void show() { }
};
class Test2
{
int x;
public:
virtual void show() { }
};
int main(void)
{
cout<<sizeof(Test1)<<endl;
cout<<sizeof(Test2)<<endl;
return 0;
}
Output:
4
8
There is only one difference between Test1 and Test2. show() is non-virtual in Test1, but virtual in Test2. When we make a function virtual, compiler adds an extra pointer vptr to objects of the class. Compiler does this to achieve run time polymorphism
24)
#include<iostream>
using namespace std;
class Test
{
private:
static int count;
public:
Test& fun(); // fun() is non-static now
};
int Test::count = 0;
Test& Test::fun()
{
Test::count++;
cout<<Test::count<<" ";
return *this;
}
int main()
{
Test t;
t.fun().fun().fun().fun();
return 0;
}
Output:
Output:
1 2 3 4
25)
#include <iostream>
using namespace std;
int fun(int a, int b = 1, int c =2)
{
return (a + b + c);
}
int main()
{
cout << fun(12, ,9);
return 0;
}
main.cpp: In function ‘int main()’:
main.cpp:11:25: error: expected primary-expression before ‘,’ token
11 | cout << fun(12, ,9);
we cant skip values
correct : fun(12) or fun(12,4,5);