All Exams  >   EmSAT Achieve  >   Mock Tests for EmSAT Achieve 2025  >   All Questions

All questions of C++ for EmSAT Achieve Exam

Predict the output of following C++ program.
#include<iostream>
using namespace std;
int &fun()
{
    static int x = 10;
    return x;
}
int main()
{
    fun() = 30;
    cout << fun();
    return 0;
}
  • a)
    Compiler Error: Function cannot be used as lvalue
  • b)
    10
  • c)
    20
  • d)
    30
Correct answer is option 'D'. Can you explain this answer?

Understanding the Program Structure
The provided C++ program demonstrates the use of references and static variables. Here's a breakdown of how it operates:
Function Explanation
- The function `fun()` declares a static integer `x` initialized to 10.
- Since `x` is declared static, it retains its value between function calls.
- The function returns a reference to `x`, allowing the caller to modify its value directly.
Main Function Execution
- In `main()`, `fun() = 30;` calls the function, which returns a reference to `x`.
- The value of `x` is then set to 30 through the reference.
Output Generation
- The next line `cout < fun();`="" calls="" `fun()`="" again,="" which="" still="" refers="" to="" the="" same="" static="" variable="" `x`.="" -="" since="" `x`="" was="" modified="" to="" 30="" in="" the="" previous="" step,="" this="" line="" outputs="" `30`.="" />Conclusion
- The final output of the program is `30` since the static variable `x` retains its modified value across function calls.
- Thus, the correct answer is option 'D', as the program effectively demonstrates the use of static local variables and references in C++.

Which of the following is true?
  • a)
    Static methods cannot be overloaded.
  • b)
    Static data members can only be accessed by static methods.
  • c)
    Non-static data members can be accessed by static methods.
  • d)
    Static methods can only access static members (data and methods)
Correct answer is option 'D'. Can you explain this answer?

Understanding Static Methods and Members
Static methods and members play a crucial role in object-oriented programming. Let’s break down the points regarding static methods and why option 'D' is the correct answer.
Static Methods and Their Limitations
- Static Context: Static methods belong to the class rather than any instance of the class. This means they are invoked without creating an object of the class.
- Accessing Members: Static methods can only directly access static data members and other static methods. This is because they do not have a reference to any specific object, which is necessary to access non-static members.
Analysis of Other Options
- Option A - False: Static methods can be overloaded. Overloading occurs when multiple methods have the same name but differ in parameters.
- Option B - False: Static data members can be accessed by both static and non-static methods. Non-static methods can access static members freely.
- Option C - False: Non-static data members cannot be accessed directly by static methods. Static methods lack context to any particular instance.
Conclusion
In summary, static methods can only access static members (both data and methods) because they do not belong to any specific instance of the class. This makes option 'D' the only accurate statement among the choices provided. Understanding this principle is essential for effective programming practices.

Output of following program? 
#include <iostream>
#include<string>
using namespace std;
class Base
{
public:
    virtual string print() const
    {
        return "This is Base class";
    }
};
class Derived : public Base
{
public:
    virtual string print() const
    {
        return "This is Derived class";
    }
};
void describe(Base p)
{
    cout << p.print() << endl;
}
int main()
{
    Base b;
    Derived d;
    describe(b);
    describe(d);
    return 0;
}
  • a)
    This is Derived class
    This is Base class
  • b)
    This is Base class
    This is Derived class
  • c)
    This is Base class
    This is Base class
  • d)
    Compiler Error
Correct answer is option 'C'. Can you explain this answer?

Understanding the Program's Output
The program defines a simple class hierarchy with a base class and a derived class. Let's break down the key components to understand the output.
Class Definitions
- Base Class:
- Contains a virtual function `print()` that returns a string "This is Base class".
- Derived Class:
- Inherits from `Base` and overrides the `print()` function to return "This is Derived class".
Function Behavior
- Function `describe(Base p)`:
- Takes an object of type `Base` as a parameter and calls its `print()` method.
Key Points on Object Slicing
- When you call `describe(b)`, the `print()` method of the `Base` class is invoked because `b` is an object of type `Base`.
- When you call `describe(d)`, the `print()` method of the `Derived` class is not called as expected. This is because `d` is passed by value, resulting in object slicing. The `Derived` portion of `d` is discarded, and only the `Base` part is passed to the `describe` function.
Resulting Output
- Therefore, both calls to `describe()` ultimately invoke the `print()` method of the `Base` class.
- The output of the program is: "This is Base class" printed twice.
Conclusion
The correct output is option C: "This is Base class" followed by "This is Base class". Understanding object slicing is crucial in C++ when dealing with inheritance and polymorphism.

Predict the output of following C++ program
#include <iostream>
using namespace std;
 
int i;
 
class A
{
public:
    ~A()
    {
        i=10;
    }
};
 
int foo()
{
    i=3;
    A ob;
    return i;
}
 
int main()
{
    cout << foo() << endl;
    return 0;
}
  • a)
    0
  • b)
    3
  • c)
    10
  • d)
    None of the above
Correct answer is option 'B'. Can you explain this answer?

While returning from a function, destructor is the last method to be executed. The destructor for the object “ob” is called after the value of i is copied to the return value of the function. So, before destructor could change the value of i to 10, the current value of i gets copied & hence the output is i = 3. 

How can we make a C++ class such that objects of it can only be created using new operator? If user tries to create an object directly, the program produces compiler error
  • a)
    Not possible
  • b)
    By making destructor private
  • c)
    By making constructor private
  • d)
    By making both constructor and destructor private
Correct answer is option 'B'. Can you explain this answer?

See the following example.
// Objects of test can only be created using new
class Test
{
private:
    ~Test() {}
friend void destructTest(Test* );
};
 
// Only this function can destruct objects of Test
void destructTest(Test* ptr)
{
    delete ptr;
}
 
int main()
{
    // create an object
    Test *ptr = new Test;
 
    // destruct the object
    destructTest (ptr);
 
    return 0;
}

#include <iostream>
using namespace std;
 
class A
{
public:
    virtual void fun() { cout << "A::fun() "; }
};
 
class B: public A
{
public:
   void fun() { cout << "B::fun() "; }
};
 
class C: public B
{
public:
   void fun() { cout << "C::fun() "; }
};
 
int main()
{
    B *bp = new C;
    bp->fun();
    return 0;
}
  • a)
    A::fun()
  • b)
    B::fun()
  • c)
    C::fun()
  • d)
    None of the above
Correct answer is option 'C'. Can you explain this answer?

The important thing to note here is B::fun() is virtual even if we have not uses virtual keyword with it. When a class has a virtual function, functions with same signature in all descendant classes automatically become virtual. We don't need to use virtual keyword in declaration of fun() in B and C. They are anyways virtual.

Which one of the following is correct, when a class grants friend status to another class?
  • a)
    The member functions of the class generating friendship can access the members of the friend class.
  • b)
    All member functions of the class granted friendship have unrestricted access to the members of the class granting the friendship.
  • c)
    Class friendship is reciprocal to each other.
  • d)
    There is no such concept.
Correct answer is option 'B'. Can you explain this answer?

When a class grants friend status to another class then all member functions of the class granted friendship have unrestricted access to the members of the class granting the friendship.
The member functions of the class generating friendship can access the members of the friend class.Incorrect statement.
Class friendship is not reciprocal to each other.
So, option (B) is correct.

Output?
#include<iostream>
using namespace std;
int fun(int x = 0, int y = 0, int z)
{  return (x + y + z); }
int main()
{
   cout << fun(10);
   return 0;
}
  • a)
    10
  • b)
    0
  • c)
    20
  • d)
    Compiler Error
Correct answer is option 'D'. Can you explain this answer?

All default arguments must be the rightmost arguments. The following program works fine and produces 10 as output.
#include <iostream>
using namespace std;
int fun(int x, int y = 0, int z = 0)
{  return (x + y + z); }
int main()
{
   cout << fun(10);
   return 0;
}

Which of the following is not correct for virtual function in C++ ?
  • a)
    Must be declared in public section of class.
  • b)
    Virtual function can be static.
  • c)
    Virtual function should be accessed using pointers.
  • d)
    Virtual function is defined in base class.
Correct answer is option 'B'. Can you explain this answer?

(B) Virtual functions cannot be declared as static. Static member functions in C++ do not participate in dynamic binding (polymorphism), which is a fundamental feature of virtual functions. Static functions are resolved at compile time based on the type of the pointer or reference, whereas virtual functions are resolved at runtime based on the actual type of the object they're invoked on.
So, the correct choice is (B) Virtual function can be static.

Predict the output of following C++ program?
include<iostream>
using namespace std;
 
class Test
{
protected:
    int x;
public:
    Test (int i):x(i) { }
    void fun() const  { cout << "fun() const " << endl; }
    void fun()        {  cout << "fun() " << endl;     }
};
 
int main()
{
    Test t1 (10);
    const Test t2 (20);
    t1.fun();
    t2.fun();
    return 0;
}
  • a)
    Compiler Error
  • b)
    fun()
    fun() const
  • c)
    fun() const
    fun() const
  • d)
    fun()
    fun()
Correct answer is option 'B'. Can you explain this answer?

The two methods ‘void fun() const’ and ‘void fun()’ have same signature except that one is const and other is not. Also, if we take a closer look at the output, we observe that, ‘const void fun()’ is called on const object and ‘void fun()’ is called on non-const object. C++ allows member methods to be overloaded on the basis of const type. Overloading on the basis of const type can be useful when a function return reference or pointer. We can make one function const, that returns a const reference or const pointer, other non-const function, that returns non-const reference or pointer. 

Predict the output of following program
#include <iostream>
using namespace std;
int main()
{
    const char* p = "12345";
    const char **q = &p;
    *q = "abcde";
    const char *s = ++p;
    p = "XYZWVU";
    cout << *++s;
    return 0;
}
  • a)
    Compiler Error
  • b)
    c
  • c)
    b
  • d)
    Garbage Value
Correct answer is option 'B'. Can you explain this answer?

Output is ‘c’ const char* p = “12345″ declares a pointer to a constant. So we can’t assign something else to *p, but we can assign new value to p. const char **q = &p; declares a pointer to a pointer. We can’t assign something else to **q, but we can assign new values to q and *q. *q = “abcde”; changes p to point to “abcde” const char *s = ++p; assigns address of literal ”bcde” to s. Again *s can’t be assigned a new value, but s can be changed. The statement printf(“%cn”, *++s) changes s to “cde” and first character at s is printed.

Consider the below C++ program.
#include<iostream>
using namespace std;
class A
{
public:
     A(){ cout <<"1";}
     A(const A &obj){ cout <<"2";}
};
class B: virtual A
{
public:
    B(){cout <<"3";}
    B(const B & obj){cout<<"4";}
};
class C: virtual A
{
public:
   C(){cout<<"5";}
   C(const C & obj){cout <<"6";}
};
class D:B,C
{
public:
    D(){cout<<"7";}
    D(const D & obj){cout <<"8";}
};
int main()
{
   D d1;
   D d(d1);
}
Which of the below is not printed? This question is contributed by Sudheendra Baliga
  • a)
    2
  • b)
    4
  • c)
    6
  • d)
    All of the above
Correct answer is option 'D'. Can you explain this answer?

Output will be 13571358 as 1357 (for D d1) and as 1358 (for D d(d1))......reason is that ......during inheritance we need to explicitly call copy constructor of base class otherwise only default constructor of base class is called. One more thing, as we are using virtual before base class, there will be only one copy of base class in multiple inheritance. And without virtual output will be......13157....&...13158 as (1315713158) respectively for each derived class object.

#include <iostream>
using namespace std;
class Test {
  static int count;
  int id;
public:
  Test() {
    count++;
    id = count;
    cout << "Constructing object number " << id << endl;
    if(id == 4)
       throw 4;
  }
  ~Test() { cout << "Destructing object number " << id << endl; }
};
int Test::count = 0;
int main() {
  try {
    Test array[5];
  } catch(int i) {
    cout << "Caught " << i << endl;
  }
}
  • a)
    Constructing object number 1
    Constructing object number 2
    Constructing object number 3
    Constructing object number 4
    Destructing object number 1
    Destructing object number 2
    Destructing object number 3
    Destructing object number 4
    Caught 4
  • b)
    Constructing object number 1
    Constructing object number 2
    Constructing object number 3
    Constructing object number 4
    Destructing object number 3
    Destructing object number 2
    Destructing object number 1
    Caught 4
  • c)
    Constructing object number 1
    Constructing object number 2
    Constructing object number 3
    Constructing object number 4
    Destructing object number 4
    Destructing object number 3
    Destructing object number 2
    Destructing object number 1
    Caught 4
  • d)
    Constructing object number 1
    Constructing object number 2
    Constructing object number 3
    Constructing object number 4
    Destructing object number 1
    Destructing object number 2
    Destructing object number 3
    Caught 4
Correct answer is option 'B'. Can you explain this answer?

The destructors are called in reverse order of constructors. Also, after the try block, the destructors are called only for completely constructed objects.

It is possible to define a class within a class termed as nested class. There are _____ types of nested classes.
  • a)
    2
  • b)
    3
  • c)
    4
  • d)
    5
Correct answer is option 'A'. Can you explain this answer?

It is possible to define a class within a class termed as nested class. There are two types of nested classes. 
1 - Outer class will use argument of inner class. 
2 - Inner and outer class are independent to each other.(different argument) 
So, option (A) is correct.

What is the advantage of exception handling ?
1. Remove error-handling code from the software's main line of code.
2. A method writer can choose to handle certain exceptions and delegate others to the caller.
3. An exception that occurs in a function can be handled anywhere in the function call stack.
  • a)
    Only 1
  • b)
    1, 2 and 3
  • c)
    1 and 3
  • d)
    1 and 2
Correct answer is option 'B'. Can you explain this answer?

Advantage of exception handling are :-
1. Remove error-handling code from the software’s main line of code.
2. A method writer can choose to handle certain exceptions and delegate others to the caller.
3. An exception that occurs in a function can be handled anywhere in the function call stack.
4. Separating Error-Handling Code from “Regular” Code.
5. Propagating Errors Up the Call Stack.

What should be put in a try block?
1. Statements that might cause exceptions
2. Statements that should be skipped in case of an exception
  • a)
    Only 1
  • b)
    Only 2
  • c)
    Both 1 and 2
  • d)
    None of the above
Correct answer is option 'C'. Can you explain this answer?

The statements which may cause problems are put in try block. Also, the statements which should not be executed after a problem occurred, are put in try block. Note that once an exception is caught, the control goes to the next line after the catch block.

When a method in a subclass has the same name and type signatures as a method in the superclass, then the method in the subclass _____ the method in the superclass.
  • a)
    Overloads
  • b)
    Friendships
  • c)
    Inherits
  • d)
    Overrides
Correct answer is option 'D'. Can you explain this answer?

When a method in a subclass has the same name and type signatures as a method in the superclass, then the method in the subclass overrides the method in the superclass.
Overloading allows different methods to have same name, but different signatures where signature can differ by number of input parameters or type of input parameters or both. Overloading is related to compile time (or static) polymorphism..
Friend Class A friend class can access private and protected members of other class in which it is declared as friend..
The capability of a class to derive properties and characteristics from another class is called Inheritance.
So, option (D) is correct.

What is the return value of f(p, p) if the value of p is initialized to 5 before the call? Note that the first parameter is passed by reference, whereas the second parameter is passed by value.
int f(int &x, int c) {
   c  = c - 1;
   if (c == 0) return 1;
   x = x + 1;
   return f(x, c) * x;
  • a)
    3024
  • b)
    6561
  • c)
    55440
  • d)
    161051
Correct answer is option 'B'. Can you explain this answer?

Since c is passed by value and x is passed by reference, all functions will have same copy of x, but different copies of c. f(5, 5) = f(x, 4)*x = f(x, 3)*x*x = f(x, 2)*x*x*x = f(x, 1)*x*x*x*x = 1*x*x*x*x = x^4 Since x is incremented in every function call, it becomes 9 after f(x, 2) call. So the value of expression x^4 becomes 9^4 which is 6561.
#include <stdio.h>
int f(int &x, int c)
{
    c  = c - 1;
    if (c == 0) return 1;
    x = x + 1;
    return f(x, c) * x;
}
int main()
{
    int p = 5;
    printf("%d", f(p, p));
}

Which of the following operators are overloaded by default by the compiler in every user defined classes even if user has not written?
1) Comparison Operator ( == )
2) Assignment Operator ( = ) 
  • a)
    Both 1 and 2
  • b)
    Only 1
  • c)
    Only 2
  • d)
    None of the two
Correct answer is option 'C'. Can you explain this answer?

Assign operator is by default available in all user defined classes even if user has not implemented. The default assignement does shallow copy. But comparison operator "==" is not overloaded.
#include<iostream>
using namespace std;
class Complex {
private:
    int real, imag;
public:
    Complex(int r = 0, int i =0)  {real = r;   imag = i;}
};
int main()
{
    Complex c1(10, 5), c2(2, 4);
    // For example, below code works fine
    c1 = c2;
    // But this code throws compiler error
    if (c1 == c2)
       cout << "Same";
    return 0;
}

Which of the following cannot be passed to a function in C++ ?
  • a)
    Constant
  • b)
    Structure
  • c)
    Array
  • d)
    Header file
Correct answer is option 'D'. Can you explain this answer?

Header file can not be passed to a function in C++. While array, constant and structure can be passed into a function. So, option (D) is correct.

Assume that an integer takes 4 bytes and there is no alignment in following classes, predict the output.
#include<iostream>
using namespace std;
class base {
    int arr[10];
};
class b1: public base { };
class b2: public base { };
class derived: public b1, public b2 {};
int main(void)
{
  cout << sizeof(derived);
  return 0;
}
  • a)
    40
  • b)
    80
  • c)
    0
  • d)
    4
Correct answer is option 'B'. Can you explain this answer?

Since b1 and b2 both inherit from class base, two copies of class base are there in class derived. This kind of inheritance without virtual causes wastage of space and ambiguities. virtual base classes are used to save space and avoid ambiguities in such cases. For example, following program prints 48. 8 extra bytes are for bookkeeping information stored by the compiler (See this for details)
#include<iostream>
using namespace std;
 
class base {
  int arr[10];     
};
 
class b1: virtual public base { };
 
class b2: virtual public base { };
 
class derived: public b1, public b2 {};
 
int main(void)

  cout << sizeof(derived);
  return 0;
}

In C++, const qualifier can be applied to
1) Member functions of a class
2) Function arguments
3) To a class data member which is declared as static4) R
Reference variables
  • a)
    Only 1, 2 and 3
  • b)
    Only 1, 2 and 4
  • c)
    All
  • d)
    Only 1, 3 and 4
Correct answer is option 'C'. Can you explain this answer?

When a function is declared as const, it cannot modify data members of its class. When we don't want to modify an argument and pass it as reference or pointer, we use const qualifier so that the argument is not accidentally modified in function. Class data members can be declared as both const and static for class wide constants. Reference variables can be const when they refer a const location.

Which of the following, in C++, is inherited in a derived class from base class ?
  • a)
    constructor
  • b)
    destructor
  • c)
    data members
  • d)
    virtual methods
Correct answer is option 'C'. Can you explain this answer?

Data members in C++ is inherited in a derived class from base class
Constructor is a member function of a class which initializes objects of a class. In C++,Constructor is automatically called when object(instance of class) create.It is special member function of the class.
Destructor is a member function which destructs or deletes an object.
Virtual methods is a method which is redefined(Over-riden) in derived class.
So, option (C) is correct.

Implicit return type of a class constructor is:
  • a)
    not of class type itself
  • b)
    class type itself
  • c)
    a destructor of class type
  • d)
    a destructor not of class type
Correct answer is option 'B'. Can you explain this answer?

Truly, constructor doesn't have any return type not even void. However, as the definition of Constructor goes, it is used to initialize an object of the class. So implicitly, they return the current instance of the class whose constructor it is. Therefore, Implicit return type of a class constructor is class type itself.
Option (B) is correct.

Predict the output?
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
using namespace std;
class Test {
    int x;
public:
    void* operator new(size_t size);
    void operator delete(void*);
    Test(int i) {
        x = i;
        cout << "Constructor called \\n";
    }
    ~Test() { cout << "Destructor called \\n"; }
};

void* Test::operator new(size_t size)
{
    void *storage = malloc(size);
    cout << "new called \\n";
    return storage;
}
void Test::operator delete(void *p )
{
    cout<<"delete called \\n";
    free(p);
}
int main()
{
    Test *m = new Test(5);
    delete m;
    return 0;
}
  • a)
    new called
    Constructor called
    delete called
    Destructor called
  • b)
    new called
    Constructor called
    Destructor called
    delete called
  • c)
    Constructor called
    new called
    Destructor called
    delete called
  • d)
    Constructor called
    new called
    delete called
    Destructor called
Correct answer is option 'B'. Can you explain this answer?

Consider the following statement
    Test *ptr = new Test;  
There are actually two things that happen in the above statement--memory allocation and object construction; the new keyword is responsible for both. One step in the process is to call operator new in order to allocate memory; the other step is to actually invoke the constructor. Operator new only allows us to change the memory allocation method, but does not do anything with the constructor calling method. Keyword new is responsible for calling the constructor, not operator new.

How does C++ compiler differs between overloaded postfix and prefix operators?
  • a)
    C++ doesn\'t allow both operators to be overloaded in a class
  • b)
    A postfix ++ has a dummy parameter
  • c)
    A prefix ++ has a dummy parameter
  • d)
    By making prefix ++ as a global function and postfix as a member function.
Correct answer is option 'B'. Can you explain this answer?

See the following example:
class Complex
{
private:
    int real;
    int imag;
public:
    Complex(int r, int i)  {  real = r;   imag = i; }
    Complex operator ++(int);
    Complex & operator ++();
};
Complex &Complex::operator ++()
{
    real++; imag++;
    return *this;
}
Complex Complex::operator ++(int i)
{
    Complex c1(real, imag);
    real++; imag++;
    return c1;
}
int main()
{
    Complex c1(10, 15);
    c1++; 
    ++c1;
    return 0;
}

#include <iostream>
using namespace std;
template <typename T>
T max(T x, T y)
{
    return (x > y)? x : y;
}
int main()
{
    cout << max(3, 7) << std::endl;
    cout << max(3.0, 7.0) << std::endl;
    cout << max(3, 7.0) << std::endl;
    return 0;
}
  • a)
    7
    7.0
    7.0
  • b)
    Compiler Error in all cout statements as data type is not specified.
  • c)
    Compiler Error in last cout statement as call to max is ambiguous.
  • d)
    None of the above
Correct answer is option 'C'. Can you explain this answer?

The first and second call to max function is a valid call as both the arguments passed are of same data type (i.e int and float respectively). But the third call to max function has arguments of different data type and hence it will generate Compiler Error in last cout statement as call to max is ambiguous. Hence option C is correct

Chapter doubts & questions for C++ - Mock Tests for EmSAT Achieve 2025 2025 is part of EmSAT Achieve exam preparation. The chapters have been prepared according to the EmSAT Achieve exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for EmSAT Achieve 2025 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of C++ - Mock Tests for EmSAT Achieve 2025 in English & Hindi are available as part of EmSAT Achieve exam. Download more important topics, notes, lectures and mock test series for EmSAT Achieve Exam by signing up for free.

Top Courses EmSAT Achieve