Which of The Following C++ Code Will Give Error On Compilation?
Which of The Following C++ Code Will Give Error On Compilation?
Which of The Following C++ Code Will Give Error On Compilation?
#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
#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
================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
14. Assume that an integer and a pointer each takes 4 bytes. Also, assume that there is no alignment
in objects. Predict the output following program.
#include<iostream>
using namespace std;
class Test
{
static int x;
int *ptr;
int y;
};
int main()
{
Test t;
cout << sizeof(t) << " ";
cout << sizeof(Test *);
}
a) 12 4
b) 12 12
c) 8 4
d) 8 8
15. What is the output of this C++ program?
#include
using namespace std;
void square (int *x)
{
*x = (*x)++ * (*x);
}
void square (int *x, int *y)
{
*x = (*x) * --(*y);
}
int main ( )
{
int number = 30;
square(&number, &number);
cout << number;
return 0;
}
a) 910
b) 920
c) 870
d) 900
16. Which one of the following is correct, when a class grants friend status to another class?
a) The member functions of the class generating friendship can access the members of the friend
class.
b) All member functions of the class granted friendship have unrestricted access to the members of
the class granting the friendship.
c) Class friendship is reciprocal to each other.
d) There is no such concept.
17. Predict output of the following program.
#include<iostream>
using namespace std;
class Base
{
public:
virtual void show() { cout<<" In Base n"; }
};
class Derived: public Base
{
public:
void show() { cout<<"In Derived n"; }
};
int main(void)
{
Base *bp = new Derived;
bp->show();
Base &br = *bp;
br.show();
return 0;
}
a) In Base In Base
b) In Base In Derived
c) In Derived In Derived
d) In Derived In Base
18. Assume that an integer takes 4 bytes and there is no alignment in following classes, predict the
output.
#include<iostream>
using namespace std;
class base {
int arr[10];
};
class b1: public base { };
class b2: public base { };
class derived: public b1, public b2 {};
int main(void)
{
cout << sizeof(derived);
return 0;
}
a) 40
b) 80
c) 0
d) 4
19. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
namespace Box1
{
int a = 4;
}
namespace Box2
{
int a = 13;
}
int main ()
{
int a = 16;
Box1::a;
Box2::a;
cout << a;
return 0;
}
a) 4
b) 13
c) 16
d) Compile time error
20. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void duplicate (int& a, int& b, int& c)
{
a *= 2;
b *= 2;
c *= 2;
}
int main ()
{
int x = 1, y = 3, z = 7;
duplicate (x, y, z);
cout << x << y << z;
return 0;
}
a) 1468
b) 2812
c) 2614
d) 2713
1. C 2. A 3. B 4. D 5. B
6. D 7. D 8. C 9. D 10. C
11. A 12. C 13. A 14. C 15. C
16. B 17. C 18. B 19. C 20. C