1-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;
}
a) Hello
b) World
c) Error
d) Hello World
View Answer
Answer: c
2-What is the value of p in the following C++ code snippet?
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
3-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.
4-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
View Answer
Answer: a
5- 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
View Answer
Answer: d
6-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
View Answer
Answer: b
7-What is the role of destructors in Classes?
a) To modify the data whenever required
b) To destroy an object when the lifetime of an object ends
c) To initialize the data members of an object when it is created
d) To call private functions from the outer world
View Answer
Answer: b
8-What is syntax of defining a destructor of class A?
a) A(){}
b) ~A(){}
c) A::A(){}
d) ~A(){};
View Answer
Answer: b
What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A{
mutable int a;
public:
A(){
cout<<"A's Constructor called\n";
}
~A(){
cout<<"A's Destructor called\n";
}
};
class B{
A a;
public:
B(){
cout<<"B's Constructor called\n";
}
~B(){
cout<<"B's Destructor called\n";
}
};
int main(int argc, char const *argv[])
{
B b1;
}
a)
A's Constructor called
B's Constructor called
b)
advertisement
A's Destructor called
B's Destructor called
c)
A's Constructor called
B's Constructor called
B's Destructor called
A's Destructor called
d)
A's Constructor called
B's Constructor called
A's Destructor called
B's Destructor called
View Answer
Answer: c
9-What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class A{
public:
int a;
A(){
cout<<"Constructor called\n";
}
} a;
int main(int argc, char const *argv[])
{
cout<<"Main function\n";
return 0;
}
a)
Constructor called
Main function
b)
Main function
Constructor called
c) Error
d) Segmentation fault
View Answer
Answer: a
10-What does this template function indicates?
==================
template<class T>
void func(T a)
{
cout<<a;
}
==================
a) A function taking a single generic parameter and returning a generic type
b) A function taking a single generic parameter and returning nothing
c) A function taking single int parameter and returning a generic type
d) A function taking a single generic parameter and returning a specific non-void type
View Answer
Answer: b
11-What does this template function indicates?
==================
template<class T, class U>
U func(T a, U b)
{
cout<<a<<"\t"<<b;
}
==================
a) A function taking a single generic parameter and returning a generic type which
may be different from argument type
b) A function taking a single generic parameter and returning a generic type which
must be different from argument type
c) A function taking a single generic parameter and returning a generic type which
must have the same type as argument type
d) A function taking a single generic parameter and returning a specific non-void type
View Answer
Answer: a
12-What is the syntax of an explicit call for a template? Assume the given template
function.
template<class T, class U>
void func(T a, U b)
{
cout<<a<<"\t"<<b<<endl;
}
a) func<int,char>(3,’a’);
b) func(3,’a’)<int,char>;
c) <int,char>func(3,’a’);
d) func(<int>3,<char>’a’);
View Answer
Answer: a
13- What will be the output of the following C++ code?
1. #include <iostream>
2. using namespace std;
3. class poly
4. {
5. protected:
6. int width, height;
7. public:
8. void set_values(int a, int b)
9. {
10. width = a; height = b;
11. }
12. };
13. class Coutput
14. {
15. public:
16. void output(int i);
17. };
18. void Coutput::output(int i)
19. {
20. cout << i;
21. }
22. class rect:public poly, public Coutput
23. {
24. public:
25. int area()
26. {
27. return(width * height);
28. }
29. };
30. class tri:public poly, public Coutput
31. {
32. public:
33. int area()
34. {
35. return(width * height / 2);
36. }
37. };
38. int main()
39. {
40. rect rect;
41. tri trgl;
42. rect.set_values(3, 4);
43. trgl.set_values(4, 5);
44. rect.output(rect.area());
45. trgl.output(trgl.area());
46. return 0;
47. }
a) 1212
b) 1210
c) 1010
d) 1250
View Answer
Answer: b
14- What will be the output of the following C++ code?
1. #include <iostream>
2. using namespace std;
3. class A
4. {
5. public:
6. A(int n )
7. {
8. cout << n;
9. }
10. };
11. class B: public A
12. {
13. public:
14. B(int n, double d)
15. : A(n)
16. {
17. cout << d;
18. }
19. };
20. class C: public B
21. {
22. public:
23. C(int n, double d, char ch)
24. : B(n, d)
25. {
26. cout <<ch;
27. }
28. };
29. int main()
30. {
31. C c(5, 4.3, 'R');
32. return 0; 33. }
a) 54.3R
b) R4.35
c) 4.3R5
d) R2.6
View Answer
Answer: a
15- What will be the output of the following C++ code ?
1. #include <iostream>
2. using namespace std;
3. class sample
4. {
5. public:
6. virtual void example() = 0;
7. };
8. class Ex1:public sample
9. {
10. public:
11. void example()
12. {
13. cout << "ubuntu";
14. }
15. };
16. class Ex2:public sample
17. {
18. public:
19. void example()
20. {
21. cout << " is awesome";
22. }
23. };
24. int main()
25. {
26. sample* arra[2];
27. Ex1 e1;
28. Ex2 e2;
29. arra[0]=&e1;
30. arra[1]=&e2;
31. arra[0]->example();
32. arra[1]->example();
33. }
a) ubuntu
b) is awesome
c) ubuntu is awesome
d) ubunt esome
Answer: c
16-What will be the output of the following C++ code?
1. #include <iostream>
2. using namespace std;
3. template<typename type>
4. type Max(type Var1, type Var2)
5. {
6. return Var1 > Var2 ? Var1:Var2;
7. }
8. int main()
9. {
10. int p;
11. p = Max(100, 200);
12. cout << p << endl;
13. return 0; 14. }
a) 100
b) 200
c) 300
d) 100200
Vi Answ
Answer:
e er b
17-What will be the output of the following C++ code?
1. #include
<iostream>
2. using
namespace
std;
3. template<ty
pename
type>
4. class Test
5. {
6. public:
7. Test()
8. {
9. };
10. ~Test()
11. {
12. };
13. type Funct1(type Var1)
14. {
15. return Var1;
16. }
17. type Funct2(type Var2)
18. {
19. return Var2;
20. }
21. };
22. int main()
23. {
24. Test<int> Var1;
25. Test<float> Var2;
26. cout << Var1.Funct1(200) << endl;
27. cout << Var2.Funct2(3.123) << endl;
28. return 0; 29. }
a) 2003.123
Vi Answ
Answer:
e er a
18- Which is the correct syntax of defining a pure virtual function?
a) pure virtual return_type func();
b) virtual return_type func() pure;
c) virtual return_type func() = 0;
Answer: c
19- What will be the output of the following C++ code?
#includ <iostrea >
#i
e lu <strin
m >
nc denamespace
usi g s ;
ng ma (i
i argc,
t ch con *ar [
{
n in n ar st gv ]
ch s [6] = "Hello ;
ch
ar 1 [6] = "World
s " ;
ch
ar 2 [1 ] ="s + " " + s ;
s
ar <3s ;
co 2 1 2
ut < 30;
retur
} n
a) Hello World
b) Hello
c) World
d) Error
View Answer
Answer: d
20-