My Report

C++ Programming Practice Test 4


Correct Answer: 2 points | Wrong: -1 point
Grades: A* (100% score) | A (80%-99%) | B (60%-80%) | C (40%-60%) | D (0%-40%)
advertisement

1. What is syntax of defining a destructor of class A?

2. How many types of constructors are there in C++?

3. What will be the output of the following C++ code?

  
    #include <iostream>
    #include <string>
    using namespace std;
    int main ()
    {
        string str ("Microsoft");
        for (size_t i = 0; i < str.length();)
        {
            cout << str.at(i-1);
        }
        return 0;
    }

4. Given the following C++ code. How would you define the < operator for Box class so that when boxes b1 and b2 are compared in if block the program gives correct result?

#include <iostream>
#include <string>
using namespace std;
class Box
{
	int capacity;
     public:
	Box(){}
	Box(double capacity){
		this->capacity = capacity;
	}
};

int main(int argc, char const *argv[])
{
	Box b1(10);
	Box b2 = Box(14);
	if(b1 < b2){
		cout<<"Box 2 has large capacity.";
	}
	else{
		cout<<"Box 1 has large capacity.";
	}
	return 0;
}

5. What will be the output of the following C++ code?

 
    #include <iostream>
    using namespace std;
    class sample
    {
        public:
        sample(int i) : m_i(i) { }
        public:
        int operator()(int i = 0) const 
        { 
            return m_i + i; 
        }
        operator int () const   
        { 
            return m_i; 
        }
        private:
        int m_i;
        friend int g(const sample&);
    };
    int f(char c)
    {
        return c;
    }
    int main()
    {
        sample f(2);
        cout << f(2);
        return 0;
    }

6. Which concepts does the Pre Increment use?

7. In Linux, how do the heaps and stacks are managed?

8. In case of non-static member functions how many maximum object arguments a binary operator overloaded function can take?

9. What will be the output of the following C++ code?

 
    #include <iostream>
    #include <complex>
    using namespace std;
    int main ()
    {
        complex<double> mycomplex (20.0, 2.0);
        cout << imag(mycomplex) << endl;
        return 0;
    }

10. What will be the output of the following C++ code?

#include <iostream>
#include <string>
using namespace std;
int main ()
{
  std::string str ("Sanfoundry.");
  str.back() = '!';
  std::cout << str << endl;
  return 0;
}

 

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.