My Report

C++ Programming Practice Test 3


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 will be the output of the following C++ code?

#include <iostream>
#include <string>
using namespace std;

class A
{
	static int a;

   public:
	void change(int i){
		a = i;
	}
	void value_of_a(){
		cout<<a;
	}
};

int main(int argc, char const *argv[])
{
	A a1 = A();
	a1.change(5);
	a1.value_of_a();
	return 0;
}

2. What does a class in C++ holds?

3. How many specifiers are present in access specifiers in class?

4. Pick the incorrect statement for namespaces in C++.

5. How many types of macros are there in c++?

6. If we start our function call with default arguments means, what will be proceeding arguments?

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

    #include <iostream>
    #include <stdarg.h>
    using namespace std;
    void fun(std::string msg, ...);
    int main()
    {
        fun("IndiaBIX", 1, 4, 7, 11, 0);
        return 0;
    }
    void fun(std::string msg, ...)
    {
        va_list ptr;
        int num;
        va_start(ptr, msg);
        num = va_arg(ptr, int);
        num = va_arg(ptr, int);
        cout << num;
    } 

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

    #include <iostream>
    #include <stdarg.h>
    using namespace std;
    void dumplist(int, ...);
    int main()
    {
        dumplist(2, 4, 8);
        dumplist(3, 6, 9, 7);
        return 0;
    }
    void dumplist(int n, ...)
    {
        va_list p;
        int i;
        va_start(p, n);
        while (n-->0) 
        {
            i = va_arg(p, int);
            cout << i;
        }
        va_end(p);
    } 

9. What changes you can do in the header files to avoid the redefinition that compiler will give when both the header files are included in the same program keeping the declaration of both the functions same?

Content of h1.h
------------------------------------------------
h1.h
#include <iostream>
using namespace std;
int func(int a){
	cout<<"Multiplied by 2";
	return 2*a;
}
------------------------------------------------

Content of h2.h
------------------------------------------------
h2.h
#include <iostream>
using namespace std;
int func(int a){
	cout<<"divided by 2";
	return a/2;
}
------------------------------------------------

10. Pick out the other definition of objects.


 

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.