Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Q1: What will be the output?

template​ ​<typename​ ​V>


void​ ​func(const​ ​V&y)
{
static​ ​int​ ​num = 0;
std::cout << "y = "​ < ​ < y << " num = "​ <​ < num << std::endl;
++num;
return;
}

int​ ​main()
{
func<int> (2);
std::cout << std::endl;
func<int>(3);
cout << std::endl;
func<double>(4.1);
std::cout << std::endl;
return​ ​0;
}

Q2: What will be the output?


template​ ​<typename​ ​V>
V maxim(V x, V y)
{
return​ ​(x > y)? x : y;
}
int​ ​main()
{
std::cout << maxim(4, 9) << std::endl;
std::cout << maxim(4.0, 9.0) << std::endl;
std::cout << maxim(4, 9.0) << std::endl;
return​ ​0;
}

Q3: What will be the output?​


template​ ​<int​ ​i>
void​ ​func()
{
i = 20;
std::cout << i;
}

int​ ​main()
{
func<15>();
return​ ​0;
}
Q4: What will be the output?

using namespace std;


class Parent {
​virtual void fun() ​{​ cout​ << ​"from Parent"​ << ​endl​;}
public​:
​virtual​ ~Parent() {fun();}
​void parentFunction() ​{fun();}
};

class Child :​ ​public​ Parent {


​void fun() ​{​ cout​ << ​"from Child"​ << ​endl​;}
public​:
~Child() {fun();}
};

int main(void) ​{
Parent* parent = ​new​ Child;
parent->parentFunction();
​delete​ parent;
​return​ ​0​;
}

Q5: What is a ​const​ function? Why can we never have a ​static const​ function?

Q6: What will be the number of iterations of this loop?


unsigned short int limit = 33000;

for (unsigned short int i = 0; i < 2 * limit; ++i)


{
// do something;
}

Q7: What will be the output?


using namespace std;
struct Test
{
​int​ data[​2​];

Test(​int​ x, ​int​ y) : data{x, y} {}


​virtual void func() ​{}
};

int main(int argc, char **argv)


{
​Test test(22, 33)​;

​int​ *arr = (​int​ *) &a;


​cout​ << arr[​2​] << ​endl​;
​return​ ​0​;
}

Q8: What will be the output?


int main()
{
​cout​ << ​50u​ - ​100​;
​return​ ​0​;
}

Q9: How can we ensure that an instance of C++ class cannot be copied at all? Write
a C++ class as an example to implement it

Q10: Implement a function that takes pointer to array and size N as parameter and
returns the largest product of 3 numbers.
How would you improve the algorithm if the array was sorted?

Q11: What does the below function print?


thread::hardware_concurrency();

Q12: What’s the output of below code?


void​ ​threadFunc()
{
std::this_thread::sleep_for(
std::chrono::seconds(1));
}

int​ ​main()
{
std::thread​ ​t1; ​

if​ ​(t1.joinable())
std::cout << "YES\n";
else
std::cout << "NO\n";

t1 = std::thread(threadFunc);

if​ ​(t1.joinable())
std::cout << "YES\n";
else
std::cout << "NO\n";

t1.join(); ​
if​ ​(t1.joinable())
cout << "YES\n";
else
cout << "NO\n";

​ ;
return​ 0
}

Q13: What’s the error(s) in the below code?


using namespace std;
const int SIZE = 10;

mutex myMutex, myMutex1, myMutex2;

void shared_cout_thread_even(int i)
{
lock_guard<mutex> g1(myMutex1);
lock_guard<mutex> g2(myMutex2);
cout << " " << i << " ";
}

void shared_cout_thread_odd(int i)
{
lock_guard<mutex> g2(myMutex2);
lock_guard<mutex> g1(myMutex1);
cout << " " << i << " ";
}

void shared_cout_main(int i)
{
lock_guard<mutex> g(myMutex);
cout << " " << i << " ";
}

void f(int n)
{
for(int i = SIZE*(n-1); i < SIZE*n ; i++) {
if(n % 2 == 0)
shared_cout_thread_even(i);
else
shared_cout_thread_odd(i);
}
}
int main()
{
thread t1(f, 1); // 0-9
thread t2(f, 2); // 10-19
thread t3(f, 3); // 20-29
thread t4(f, 4); // 30-39
thread t5(f, 5); // 40-49

for(int i = 0; i > -SIZE; i--)


shared_cout_main(i); // (0, -49)

t1.join();
t2.join();
t3.join();
t4.join();
t5.join();

return 0;
}

Q14: What’s the error(s) in the below code? How can we resolve them?
#include<iostream.h>
#include<conio.h>
class​ ​ClassA
{
public:
int​ ​a;
};

class​ ​ClassB : public​ ​ClassA
{
public:
int​ ​b;
};
class​ ​ClassC : public​ ​ClassA
{
public:
int​ ​c;
};

class​ ​ClassD : public​ ​ClassB, public​ C
​ lassC
{
public:
int​ ​d;
};

void​ ​main()
{

ClassD obj;

obj.a = 10;
obj.b = 20;
obj.c = 30;
obj.d = 40;

cout<< "\n A from ClassB : "<< obj.ClassB::a;
cout<< "\n A from ClassC : "<< obj.ClassC::a;

cout<< "\n B : "<< obj.b;
cout<< "\n C : "<< obj.c;
cout<< "\n D : "<< obj.d;

}

You might also like