Sample questions
Question 1Medium
What is the output of this program?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = "a long string";
s.insert(s.size() / 2, " * ");
cout << s << endl;
return 0;
}
- a long* string
- a long st*ring
- Depends on compiler
- None of the mentioned
Answer: D. None of the mentioned
Explanation
size 13/2 = 6, so the result is 'a long * string', which matches no listed output
Step-by-step solution
- The string is 'a long string', which has 13 characters.
- s.size() / 2 is 13 / 2, which is 6 in integer division.
- insert puts ' * ' before index 6, so the text becomes 'a long * string' with two spaces before string.
- No listed option matches this output, so the correct answer is option D, None of the mentioned.
Why the other options are wrong
- A. The star is not attached to 'long' with no space; the output has a space before it.
- B. The star is placed after 'long', not inside 'string'.
- C. The result is fixed by the code and does not depend on the compiler.
Tip: For insert(pos, text), the text goes before the character at index pos, and size() / 2 uses integer division.
Question 2Medium
Which of the following can derived class inherit...?
- members
- functions
- both a & b
- None of the mentioned
Answer: C. both a & b
Explanation
Suppose that you declare an object x of class A in the class definition of B.
As a result, class B will have access to all the public data members and member functions of class A. However, in class B, you have to access the data members and member functions of class A through object x.
Step-by-step solution
- A derived class inherits members from base class.
- It also inherits functions from base class.
- So both a & b are correct.
- Final answer: both a & b.
Why the other options are wrong
- A. Derived class can inherit functions too.
- B. Derived class can inherit members too.
- D. Both members and functions are inherited.
Tip: Always remember that inheritance includes both data and methods.
Question 3Medium
Where is the derived class is derived from...?
- derived
- base
- both a & b
- None of the mentioned
Answer: B. base
Explanation
Because derived inherits functions and variables from base.
A class can be derived from more than one classes, which means it can inherit data and functions from multiple base classes.
To define a derived class, we use a class derivation list to specify the base class(es). A class derivation list names one or more base classes and has the form: class derived-class: access-specifier base-class
Step-by-step solution
- Derived class inherits from base class.
- Base class provides functions and variables to derived class.
- So derived class is derived from base.
- Correct answer is B (base).
Why the other options are wrong
- A. Derived class cannot be a base for itself.
- C. Derived class is not derived from both base and derived.
- D. Derived class is derived from base, so this option is wrong.
Question 4Medium
What is the output of this program...?
#include <iostream>
using namespace std;
class BaseClass
{
protected:
int i;
public:
BaseClass(int x)
{
i = x;
}
~BaseClass()
{
}
};
class DerivedClass: public BaseClass
{
int j;
public:
DerivedClass(int x, int y): BaseClass(y)
{
j = x;
}
~DerivedClass()
{
}
void show()
{
cout << i << " " << j << endl;
}
};
int main()
{
DerivedClass ob(3, 4);
ob.show();
return 0;
}
- 4
- 4 3
- 3 4
- 3
Answer: B. 4 3
Explanation
DerivedClass(3, 4) passes 4 to BaseClass, so i = 4, and it sets j = 3. show() prints i, then j.
4 3
Step-by-step solution
- DerivedClass ob(3, 4) calls the constructor with x = 3 and y = 4.
- The constructor passes y (4) to BaseClass(y), so i becomes 4.
- The body sets j = x, so j becomes 3.
- show() prints i then j, giving 4 3: option B.
Why the other options are wrong
- A. Prints only i; j is also printed.
- C. The values are swapped: i is 4 from y, and j is 3 from x.
- D. Prints only j; i is printed too, and it is 4.
Tip: Base-class initialiser arguments come from the derived constructor's parameters, not from the base.
Question 5Medium
What is the output of this program...?
#include <iostream>
using namespace std;
class polygon
{
protected:
int width, height;
public:
void set_values (int a, int b)
{
width = a; height = b;}
};
class output1
{
public:
void output (int i);
};
void output1::output (int i)
{
cout << i << endl;
}
class rectangle: public polygon, public output1
{
public:
int area ()
{
return (width * height);
}
};
class triangle: public polygon, public output1
{
public:
int area ()
{
return (width * height / 2);
}
};
int main ()
{
rectangle rect;
triangle trgl;
rect.set_values (4, 5);
trgl.set_values (4, 5);
rect.output (rect.area());
trgl.output (trgl.area());
return 0;
}
- 20
- 10
- 20 10
- None of the mentioned
Answer: C. 20 10
Explanation
We are using the multiple inheritance to find the area of rectangle and triangle.
Output:
20
10
Step-by-step solution
- rect.set_values(4, 5) sets width = 4 and height = 5 in the polygon part of rect.
- rect.area() returns width * height = 4 * 5 = 20.
- rect.output(20) prints 20 and then a new line.
- trgl.set_values(4, 5) sets width = 4 and height = 5 in the polygon part of trgl.
- trgl.area() returns width * height / 2 = 20 / 2 = 10.
- trgl.output(10) prints 10 and then a new line.
- So the program prints two lines, 20 and then 10, which is the option 20 10.
Why the other options are wrong
- A. Only the rectangle area is printed. The triangle line also prints 10.
- B. Only the triangle area is printed. The rectangle line prints 20 first.
- D. The program does print output, so 'None of the mentioned' is wrong.
Tip: Each class's output() call prints its own argument, and endl moves to a new line after each value.
Question 6Medium
What does the "cerr" represent?
- Standard error stream
- Standard logging stream
- Input stream
- Output stream
Answer: A. Standard error stream
Explanation
cerr is an object of class ostream that represents the standard error stream. It is associated with the cstdio stream stderr.
Step-by-step solution
- Cerr is an output stream object of class ostream.
- It represents the standard error stream and is linked to stderr.
- So cerr represents the standard error stream, which is option A.
Why the other options are wrong
- B. There is no standard logging stream named cerr.
- C. Input is cin, not cerr.
- D. Output to the screen is cout; cerr is the error output stream.
Tip: Cout is standard output, cerr is standard error and clog is buffered logging output.
Question 7Medium
Which design patterns benefit from the multiple inheritance...?
- Adapter and observer pattern
- Code pattern
- Glue pattern
- None of the mentioned
Answer: A. Adapter and observer pattern
Explanation
The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.
It is mainly used to implement distributed event handling systems.
The Observer pattern is also a key part in the familiar Model View Controller (MVC) architectural pattern.
Step-by-step solution
- The question asks which named design pattern benefits from multiple inheritance.
- Adapter and observer are the two patterns named in option A.
- So option A, Adapter and observer pattern, is the correct answer.
Why the other options are wrong
- B. Code pattern is not a standard named design pattern.
- C. Glue pattern is not a standard named design pattern.
- D. Option A names patterns from the list, so 'none of the mentioned' is not correct.
Tip: Know the standard named patterns (Adapter, Observer, Factory and so on) when a question asks for a design pattern.
Question 8Medium
Which classes are called as mixin...?
- Represent a secondary design
- Classes express functionality which represent responsibilities.
- Both a & b
- None of the mentioned
Answer: B. Classes express functionality which represent responsibilities.
Explanation
A class that expresses functionality rather than its primary design role is called a mixin.
Step-by-step solution
- A mixin is a class that adds extra functionality rather than serving as the main design role of an object.
- Option B says mixin classes express functionality that represents responsibilities, which matches this definition.
- So the correct answer is option B.
Why the other options are wrong
- A. A mixin is named for the functionality it adds, not for a secondary design; option B states this directly.
- C. Option C claims both 0 and 1, and 0 is not correct.
- D. Option B is correct, so 'none of the mentioned' is wrong.
Tip: A mixin adds one behaviour to other classes; think of it as a bundle of functionality, not a main role.
Question 9Medium
Which operator is used to create the user-defined streams in c++ ?
- >>
- <<
- &
- Both a & b
Answer: D. Both a & b
Explanation
We can make user-defined types with streams by overloading the insertion operator (<<) to put objects into streams and the extraction operator (>>) to read objects from streams.
Step-by-step solution
- User-defined stream types are made by overloading the insertion operator << for output.
- They are also made by overloading the extraction operator >> for input.
- So both a and b are needed, which is option D.
Why the other options are wrong
- A. The >> operator alone covers input, so it is only half of the answer.
- B. The << operator alone covers output, so it is only half of the answer.
- C. The & operator is not a stream operator.
Tip: Overload << for output and >> for input when you write your own stream support.
Question 10Medium
What is the use of clog...?
- Standard logging stream
- Error stream
- Input stream
- None of the mentioned
Answer: A. Standard logging stream
Explanation
clog is an object of class ostream that represents the standard logging stream.It is associated with the cstdio stream stderr, like cerr.
Step-by-step solution
- Clog is an object of class ostream.
- It represents the standard logging stream.
- Clog is linked to stderr for error messages.
- This makes it suitable for logging purposes.
- Thus, clog is used for standard logging stream.
Why the other options are wrong
- B. Error stream is handled by cerr, not clog.
- C. Input stream is handled by cin, not clog.
- D. Clog is a valid standard logging stream.
Tip: Remember clog is for logging, not errors or input.
Practise all 27 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →