Professional Documents
Culture Documents
Q1: What Will Be The Output?
Q1: What Will Be The Output?
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;
}
int main()
{
func<15>();
return 0;
}
Q4: What will be the output?
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?
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?
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
}
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
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;
}