Please attempt Basic level C++ test. Assume that there is no compiler error and send answer to
abhilashatech@gmail.com. I will evaluate your answer sheet and send score card with correct answer sheet. I will be uploading medium and advance level test soon.
Time Duration: 30 Min
1)
template<unsigned N>
class B
{
public:
B() { std::cout << N; }
private:
B<N/2> m_a;
};
template<>
class B<0>
{
public:
B() { std::cout << 'B'; }
};
int main()
{
{ B<9>(); }
std::cout << std::endl;
return 0;
}
2)
class F
{
public:
F() { f1(); }
virtual ~F() { f(); }
virtual void f() const { std::cout << 1; }
virtual void f1() const { std::cout << 2; }
};
class G : public F
{
public:
G() { f1(); }
~G() { f(); }
private:
virtual void f() const { std::cout << 3; }
virtual void f1() const { std::cout << 4; }
};
int main(void)
{
F *a = new G;
delete a;
std::cout << std::endl;
return 0;
}
3)
class Base
{
public:
virtual void f(int n) { std::cout << n << 1 << ","; }
virtual ~Base() { }
void f(int n) const { std::cout << n << ","; }
};
class Derived : public Base
{
public:
void f(int n) { std::cout << (n << 2) << ","; }
void f(int n) const { std::cout << n + 1 << ","; }
};
int main(void)
{
const Base a;
Derived b;
Base &c = b;
const Base *d = &b;
a.f(3);
b.f(3);
c.f(2);
d->f(2);
std::cout << std::endl;
return 0;
}
4)
class Base
{
public:
Base(int n = 1) : m_i(n) { }
~Base() { std::cout << m_i; }
protected:
int m_i;
private:
int m_j;
};
class Derived : public Base
{
Base m_x;
Base m_a;
public:
Derived(int n) : m_x(m_i + 1) , m_a(n) { }
~Derived() {std::cout << m_i; --m_i; }
};
int main(void)
{
{
Derived b(4);
cout << sizeof(b) << ", ";
}
std::cout << std::endl;
retunrn 0;
}
5)
class Base
{
public:
Base(int i) : m_i(i) { }
int operator()(int i = 0) const { return m_i + i; }
operator int () const { return m_i + 2; }
private:
int m_i;
friend int g(const Base&);
};
static int ni = 0;
int f(char c){return ni+=2;}
int g(const Base& a){return ni+=3;}
int main(void)
{
Base f(3), g(4);
std::cout << f(1) << g(f) << std::endl;
return 0;
}
6)
class Base
{
public:
Base f1() { cout<<"1"; return *this; }
Base f2() { cout<<"2"; return *this; }
Base f3() { cout<<"3"; return *this;}
};
void foo()
{
Base obj;
obj.f1().f2().f3();
}
int main()
{
foo();
cout << endl;
return 0;
}
7)
class Base
{
public:
Base(int i = 0):n(i) { cout << "1" ;}
Base(const Base&) { cout << "2";}
Base& operator= (const Base&) { cout << "3"; return *this;}
~Base() { cout << "4";}
Base fn(Base a){ cout << "5"; return a;}
private:
int n;
};
int main()
{
Base a = 4;
Base b = a.fn(a);
return 0;
}
8)
struct Base {
Base(int x = 0) : m_x(x) {}
int operator()() { cout << m_x++; return m_x++; }
int m_x;
};
int main()
{
Base obj;
cout << obj() << obj();
cout << endl;
return 0;
}
9)
class X
{
public:
X(int x) { cout << "X"; }
X() { cout << "X1"; }
};
class Base
{
public:
int m_n;
void Overloading(int& nValue) { cout << "1";}
void Overloading(const int& nValue) { cout << "2";}
void Overloading(X nValue) { cout << "3";}
void Overloading(double nValue) { cout << "4";}
};
int main()
{
Base b;
int i = 10;
X objX;
b.Overloading('a');
b.Overloading(0);
b.Overloading(objX);
b.Overloading(3.14159f);
return 0;
}
10)
template<typename T> void Function(T x)
{
static int i = 0;
cout << i++ << " " << x <<" ";
}
int main()
{
int x = 3;
Function(x);
Function<int>(x);
Function<int const&>(x);
Function(4.132);
return 0;
}
abhilashatech@gmail.com. I will evaluate your answer sheet and send score card with correct answer sheet. I will be uploading medium and advance level test soon.
Time Duration: 30 Min
1)
template<unsigned N>
class B
{
public:
B() { std::cout << N; }
private:
B<N/2> m_a;
};
template<>
class B<0>
{
public:
B() { std::cout << 'B'; }
};
int main()
{
{ B<9>(); }
std::cout << std::endl;
return 0;
}
2)
class F
{
public:
F() { f1(); }
virtual ~F() { f(); }
virtual void f() const { std::cout << 1; }
virtual void f1() const { std::cout << 2; }
};
class G : public F
{
public:
G() { f1(); }
~G() { f(); }
private:
virtual void f() const { std::cout << 3; }
virtual void f1() const { std::cout << 4; }
};
int main(void)
{
F *a = new G;
delete a;
std::cout << std::endl;
return 0;
}
3)
class Base
{
public:
virtual void f(int n) { std::cout << n << 1 << ","; }
virtual ~Base() { }
void f(int n) const { std::cout << n << ","; }
};
class Derived : public Base
{
public:
void f(int n) { std::cout << (n << 2) << ","; }
void f(int n) const { std::cout << n + 1 << ","; }
};
int main(void)
{
const Base a;
Derived b;
Base &c = b;
const Base *d = &b;
a.f(3);
b.f(3);
c.f(2);
d->f(2);
std::cout << std::endl;
return 0;
}
4)
class Base
{
public:
Base(int n = 1) : m_i(n) { }
~Base() { std::cout << m_i; }
protected:
int m_i;
private:
int m_j;
};
class Derived : public Base
{
Base m_x;
Base m_a;
public:
Derived(int n) : m_x(m_i + 1) , m_a(n) { }
~Derived() {std::cout << m_i; --m_i; }
};
int main(void)
{
{
Derived b(4);
cout << sizeof(b) << ", ";
}
std::cout << std::endl;
retunrn 0;
}
5)
class Base
{
public:
Base(int i) : m_i(i) { }
int operator()(int i = 0) const { return m_i + i; }
operator int () const { return m_i + 2; }
private:
int m_i;
friend int g(const Base&);
};
static int ni = 0;
int f(char c){return ni+=2;}
int g(const Base& a){return ni+=3;}
int main(void)
{
Base f(3), g(4);
std::cout << f(1) << g(f) << std::endl;
return 0;
}
6)
class Base
{
public:
Base f1() { cout<<"1"; return *this; }
Base f2() { cout<<"2"; return *this; }
Base f3() { cout<<"3"; return *this;}
};
void foo()
{
Base obj;
obj.f1().f2().f3();
}
int main()
{
foo();
cout << endl;
return 0;
}
7)
class Base
{
public:
Base(int i = 0):n(i) { cout << "1" ;}
Base(const Base&) { cout << "2";}
Base& operator= (const Base&) { cout << "3"; return *this;}
~Base() { cout << "4";}
Base fn(Base a){ cout << "5"; return a;}
private:
int n;
};
int main()
{
Base a = 4;
Base b = a.fn(a);
return 0;
}
8)
struct Base {
Base(int x = 0) : m_x(x) {}
int operator()() { cout << m_x++; return m_x++; }
int m_x;
};
int main()
{
Base obj;
cout << obj() << obj();
cout << endl;
return 0;
}
9)
class X
{
public:
X(int x) { cout << "X"; }
X() { cout << "X1"; }
};
class Base
{
public:
int m_n;
void Overloading(int& nValue) { cout << "1";}
void Overloading(const int& nValue) { cout << "2";}
void Overloading(X nValue) { cout << "3";}
void Overloading(double nValue) { cout << "4";}
};
int main()
{
Base b;
int i = 10;
X objX;
b.Overloading('a');
b.Overloading(0);
b.Overloading(objX);
b.Overloading(3.14159f);
return 0;
}
10)
template<typename T> void Function(T x)
{
static int i = 0;
cout << i++ << " " << x <<" ";
}
int main()
{
int x = 3;
Function(x);
Function<int>(x);
Function<int const&>(x);
Function(4.132);
return 0;
}
No comments:
Post a Comment