github

Made bySaurav Hathi

Test C/C++: Miscellaneous 2

Q1:

What is the output of this C++ program?

#include <iostream>

using namespace std;

struct Time

{

int hours;

int minutes;

int seconds;

};

int toSeconds(Time now);

int main()

{

Time t;

t.hours = 5;

t.minutes = 30;

t.seconds=45;

cout <<  toSeconds(t) << endl;

return 0;

}

int toSeconds(Time now)

{

return 3600 * now.hours + 60 * now.minutes + now.seconds;

}

Tags:
Section:
Core Programming
Options:
Q2:

What is the output of this C++ program?

#include <iostream>

using namespace std;

namespace first

{

int var = 5;

}

namespace second

{

double var = 3.1416;

}

int main ()

{

int a;

a = first::var + second::var;

cout << a;

return 0;

}

Tags:
Section:
Core Programming
Options:
Q3:

What is the output of this C++ program?

#include <iostream>

using namespace std;

namespace first

{

int x = 5;

int y = 10;

}

namespace second

{

double x = 3.1416;

double y = 2.7183;

}

int main ()

{

using first::x;

using second::y;

bool a, b;

a = x > y;

b = first::y < second::x;

cout << a << b;

return 0;

}

Tags:
Section:
Core Programming
Options:
Q4:

What is the output of the following C++ program?

#include <iostream>

using namespace std;

class A

{

int id;

static int count;

public:

A() {

count++;

id = count;

cout << "constructor for id " << id << endl;

}

~A() {

cout << "destructor for id " << id << endl;

}

};

int A::count = 0;

int main() {

A a[3];

return 0;

}

Tags:
Section:
Core Programming
Options:
Q5:

What will be the output of below C++ program?

#include <iostream>

using namespace std;

class A

{

protected:

int x;

public:

A() {x = 0;}

friend void show();

};

class B: public A

{

public:

B() : y (0) {}

private:

int y;

};

void show()

{

A a;

B b;

cout << "The default value of A::x = " << a.x << " ";

cout << "The default value of B::y = " << b.y;

}

Tags:
Section:
Core Programming
Options:
Q6:

What is the output of this C++ program?

#include <iostream>

#include <typeinfo>

using namespace std;

int main ()

{

int * a;

int b;

a = 0; b = 0;

if (typeid(a) != typeid(b))

{

cout << typeid(a).name()<<" "<<typeid(b).name();

}

return 0;

}

Tags:
Section:
Core Programming
Options:
Q7:

What is the output of this C++ program?

#include <iostream>

#include <typeinfo>

#include <exception>

using namespace std;

class base

{

virtual void f(){}

};

class derived : public base {};

int main ()

{

try

{

base* a = new base;

base* b = new derived;

cout << typeid(*a).name() << '\t';

cout << typeid(*b).name();

}

catch (exception& e)

{

cout << "Exception: " << e.what() << endl;

}

return 0;

}

Tags:
Section:
Core Programming
Options:
Q8:

What is the output of this C++ program?

#include <typeinfo>

#include <iostream>

using namespace std;

class A

{

public:

virtual ~A();

};

int main()

{

A* a = NULL;

try

{

cout << typeid(*a).name() << endl;

}

catch (bad_typeid)

{

cout << "Object is NULL" << endl;

}

return 0;

}

Tags:
Section:
Core Programming
Options:
Q9:

What is the output of this C++ program?

#include <iostream>

using namespace std;

struct A

{

virtual void f() 

{

cout << "Class A" << endl;

}

};

struct B : A

{

virtual void f()

{

cout << "Class B" << endl;

}

};

struct C : A

{

virtual void f()

{

cout << "Class C" << endl;

}

};

void f(A* arg)

{

B* bp = dynamic_cast<B*>(arg);

C* cp = dynamic_cast<C*>(arg);

if (bp)

bp -> f();

else if (cp)

cp -> f();

else

arg -> f(); 

};

int main()

{

A aobj;

C cobj;

A* ap = &cobj;

A* ap2 = &aobj;

f(ap);

f(ap2);

return 0;

}

Tags:
Section:
Core Programming
Options:
Q10:

What is the output of this C++ program?

#include <iostream>

using namespace std;

template<typename type>

class TestVirt

{

public:

virtual type TestFunct(type Var1)

{

return Var1 * 2;

}

};

int main()

{

TestVirt<int> Var1;

cout << Var1.TestFunct(100) << endl;

return 0;

}

Tags:
Section:
Core Programming
Options:
Q11:

What is the output of this C++ program?

#include <iostream>
using namespace std;
template<typename T>
inline T square(T x)
{
T result;
result = x * x;
return result;
};
int main()
{
int i, ii;
double y, yy;
i = 2;
y = 2.2;
ii = square(i);
cout<<i<<" "<<ii<<endl;
yy = square(y);
cout << y << " " << yy << endl;
return 0;
}

Tags:
Section:
Core Programming
Options:
Q12:

What is the output of this C++ program?

#include <iostream>

using namespace std;

template<typename T>

void loopIt(T x)

{

int count = 3;

T val[count];

for (int ii=0; ii < count; ii++)

{

val[ii] = x++;

cout <<  val[ii] << endl;

}

};

int main()

{

float xx = 2.1;

loopIt(xx);

return 0;

}

Tags:
Section:
Core Programming
Options:
Q13:

What is the output of this C++ program?

#include <iostream>

using namespace std;

template <class T, int N>

class mysequence

{

T memblock [N];

public:

void setmember (int x, T value);

T getmember (int x);

};

template <class T, int N>

void mysequence<T,N> :: setmember (int x, T value)

{

memblock[x] = value;

}

template <class T, int N>

T mysequence<T,N> :: getmember (int x)

{

return memblock[x];

}

int main ()

{ 

mysequence <int, 5> myints;

mysequence <double, 5> myfloats;

myints.setmember (0, 100);

myfloats.setmember (3, 3.1416);

cout << myints.getmember(0) << '\n';

cout << myfloats.getmember(3) << '\n';

return 0;

}

Tags:
Section:
Core Programming
Options:
Q14:

What is the output of this C++ program?

#include <iostream>

using namespace std;

template <class T>

T max (T& a, T& b)

{

return (a>b?a:b);

}

int main ()

{

int i = 5, j = 6, k;

long l = 10, m = 5, n;

k = max(i, j);

n = max(l, m);

cout << k << endl;

cout << n << endl;

return 0;

}

Tags:
Section:
Core Programming
Options:
Q15:

What is the output of this C++ program?

#include <iostream>

using namespace std;

template <class type>

class Test

{

public:

Test()

{

};

∼Test()

{ 

};

type Funct1(type Var1)

{

return Var1;

}

type Funct2(type Var2)

{

return Var2;

}

 };

int main()

{

Test<int> Var1;

Test<double> Var2;

cout << Var1.Funct1(200);

cout << Var2.Funct2(3.123);

return 0;

}

Tags:
Section:
Core Programming
Options:
Q16:

What is the output of this C++ program?

#include <iostream>

using namespace std;

template <typename T, int count>

void loopIt(T x)

{

T val[count];

for(int ii = 0; ii < count; ii++)

{

val[ii] = x++;

cout <<  val[ii] << endl;

}

};

int main()

{

float xx = 2.1;

loopIt<float, 3>(xx);

}

Tags:
Section:
Core Programming
Options:
Q17:

Where does a cin stops it extraction of data?

Tags:
Section:
Core Programming
Options:
Q18:

Which is used to get the input during runtime in C++?

Tags:
Section:
Core Programming
Options:
Q19:

What is the output of this C++ program?

#include <iostream>

using namespace std;

int main ()

{

int i;

cout << "Please enter an integer value: ";

cin >> i + 4;

return 0;

}

Tags:
Section:
Core Programming
Options:
Q20:

What is the output of this C++ program?

#include <iostream>

#include <string>

#include <sstream>

using namespace std;

int main ()

{

string mystr;

float price = 0;

int quantity = 0;

cout << "Enter price: ";

getline (cin, mystr);

stringstream(mystr) >> price;

cout << "Enter quantity: ";

getline (cin, mystr);

stringstream(mystr) >> quantity;

cout << "Total price: " << price * quantity << endl;

return 0;

}

Tags:
Section:
Core Programming
Options: