Object Oriented Programming With C++

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Prasad Mutkule
P
Prasad Mutkule
Community Contributor
Quizzes Created: 1 | Total Attempts: 280
| Attempts: 280 | Questions: 20
Please wait...
Question 1 / 20
0 %
0/100
Score 0/100
1. Which of the following provides a reuse mechanism?

Explanation

Inheritance provides a reuse mechanism in object-oriented programming. It allows a class to inherit the properties and methods of another class, known as the parent or base class. This means that the derived class, also known as the child or subclass, can reuse the code and functionality of the parent class without having to rewrite it. This promotes code reusability, as common attributes and behaviors can be defined in a base class and inherited by multiple subclasses. Inheritance also enables the creation of hierarchical relationships between classes, allowing for more organized and modular code.

Submit
Please wait...
About This Quiz
Object Oriented Programming With C++ - Quiz

Explore key concepts of Object Oriented Programming in C++ through this interactive assessment. Cover topics from basic syntax to advanced principles like inheritance and singleton classes, enhancing your... see moreunderstanding and skills in modern software development. see less

2. The process of building new classes from existing one is called _________.

Explanation

Inheritance is the process of building new classes from existing ones. It allows a class to inherit the properties and methods of another class, known as the parent or base class. This enables code reuse and promotes the concept of hierarchy in object-oriented programming. By inheriting from a parent class, the child class automatically gains access to its attributes and behaviors, and can also add its own unique characteristics. Inheritance is a fundamental concept in object-oriented programming and plays a crucial role in creating modular and extensible code.

Submit
3. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
 
int fun(int=0, int = 0);
 
int main()
{
  cout << fun(5);
  return 0;
}
int fun(int x, int y) { return (x+y); }

Explanation

The code calls the function "fun" with one argument, which is the value 5. The function "fun" takes two arguments, but both have default values of 0. Therefore, the function will add 5 and 0 together and return the result, which is 5. This value is then printed to the console using the "cout" statement in the "main" function. So, the output of the code will be 5.

Submit
4. Which of the following accesses a variable in structure *b?

Explanation

The arrow operator "->" is used to access a member variable in a structure pointer. In this case, "b->var" is the correct syntax to access the variable "var" in the structure pointed to by "b".

Submit
5. What will be the output of the following C++ code?
#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;
}

Explanation

The output of the code will be "fun() fun() const". This is because the first function call t1.fun() will call the non-const version of the fun() function since t1 is not a const object. The second function call t2.fun() will call the const version of the fun() function since t2 is a const object.

Submit
6.  The operator used for dereferencing or indirection is ____

Explanation

The operator used for dereferencing or indirection is the asterisk (*). This operator is used to access the value stored at the address pointed to by a pointer variable. It is used when we want to manipulate or retrieve the value that a pointer is pointing to.

Submit
7. Overloading the function operator

Explanation

This answer is correct because overloading the function operator requires a class with an overloaded operator. In C++, the function operator, also known as the parentheses operator (), can be overloaded to allow objects to be used like functions. This means that when an object of this class is called with parentheses, it will invoke the overloaded operator function. Therefore, in order to overload the function operator, a class must have an overloaded operator. The other options mentioned in the question are not directly related to overloading the function operator.

Submit
8. Which of the following type of class allows only one object of it to be created?

Explanation

A singleton class allows only one object of it to be created. This is achieved by making the constructor of the class private, so that it cannot be accessed from outside the class. The class provides a static method that returns the instance of the class, and this method ensures that only one instance is created and returned. Singleton classes are often used in scenarios where there should be a single point of access to a resource or when there is a need for global access to an object.

Submit
9. Which of the following statements is correct?

Explanation

A derived class pointer cannot point to a base class because a derived class is an extension of a base class and contains additional members and functions that are not present in the base class. Therefore, if a derived class pointer were to point to a base class, it would not be able to access or utilize these additional members and functions, resulting in a loss of functionality.

Submit
10. How the template class is different from the normal class?

Explanation

A template class is different from a normal class because it can generate objects of classes based on the template type. This means that the template class can be used to create multiple instances of different classes using the same template. Additionally, a template class can help in making generic classes, which allows for more flexibility and reusability in code. Lastly, using a template class can also save system memory as it avoids the need to create separate classes for similar functionality. Therefore, all of the mentioned options are correct.

Submit
11. If a class C is derived from class B, which is derived from class A, all through public inheritance, then a class C member function can access

Explanation

In this scenario, class C is derived from class B, which is derived from class A through public inheritance. This means that class C inherits all the protected members of both class A and class B. Therefore, a class C member function can access the protected data in both class A and class B. However, it cannot access the private data in either class A or class B, and it can only access the public data in class C itself.

Submit
12. In C++, dynamic memory allocation is accomplished with the operator ____

Explanation

In C++, dynamic memory allocation is accomplished with the operator "new". The "new" operator is used to allocate memory for objects at runtime and returns a pointer to the newly allocated memory. This allows programmers to allocate memory dynamically based on their program's needs, rather than relying solely on static memory allocation. By using "new", programmers can create objects of various sizes and types during program execution, enhancing the flexibility and efficiency of their code.

Submit
13. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class Box
{
	int capacity;
    public:
	Box(int cap){
		capacity = cap;
	}
 
	friend void show();
};
 
void show()
{	
	Box b(10);
	cout<<"Value of capacity is: "<<b.capacity<<endl;
}
 
int main(int argc, char const *argv[])
{
	show();
	return 0;
}

Explanation

The code defines a class called "Box" with a private member variable "capacity" and a constructor that initializes the capacity. The function "show()" is declared as a friend function of the Box class, allowing it to access the private member variable.

In the main function, the "show()" function is called, which creates an object of the Box class with a capacity of 10 and prints the value of the capacity.

Therefore, the output of the code will be "Value of capacity is: 10".

Submit
14. In an assignment statement a=b Which of the following statement is true?

Explanation

The correct answer is that the value of b is assigned to variable a, but any later changes made to variable b will not affect the value of variable a. This means that once the assignment is made, the value of a remains independent of any changes made to b.

Submit
15. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A
{
	static int a;
    public:
	void show()
        {
		a++;
		cout<<"a: "<<a<<endl;
	}
};
 
int A::a = 5;
 
int main(int argc, char const *argv[])
{
	A a;
	return 0;
}

Explanation

The code defines a class A with a static member variable a and a member function show(). In the main function, an object of class A is created. However, since there is no code that calls the show() function, there will be no output.

Submit
16. What is the final value of x when the code int x; for(x=0; x<10; x++) {} is run?

Explanation

The code initializes the variable x to 0 and then enters a for loop. In the loop, x is incremented by 1 each time until it reaches a value of 10. Since the loop condition is x

Submit
17. RunTime Polymorphism is achieved by ______

Explanation

RunTime Polymorphism is achieved by using virtual functions. Virtual functions are functions that are declared in a base class and can be overridden in derived classes. The use of the virtual keyword allows the compiler to determine at runtime which version of the function to call based on the actual type of the object. This enables polymorphic behavior, where a pointer or reference to a base class can be used to invoke different implementations of a function depending on the actual object type.

Submit
18. What is the output of the following code char symbol[3]={'a','b','c'}; for (int index=0; index<3; index++) cout << symbol [index];

Explanation

The output of the code is "abc". The code declares an array of characters called "symbol" with a size of 3. It initializes the array with the characters 'a', 'b', and 'c'. Then, it uses a for loop to iterate over the elements of the array and prints each character using the cout statement. Therefore, the output will be the characters 'a', 'b', and 'c' printed sequentially, resulting in "abc".

Submit
19. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A{
	int a;
	A(){
		a = 5;
	}
 
public:
	void assign(int i){
		a = i;
	}
	int return_value(){
		return a;
	}
};
int main(int argc, char const *argv[])
{
	A obj;
	obj.assign(10);
	cout<<obj.return_value();
}

Explanation

The code will result in an error because the constructor of class A is declared as private, which means it cannot be accessed outside the class. Therefore, when trying to create an object of class A in the main function, it will result in an error.

Submit
20. To perform stream I/O with disk files in C++, you should

Explanation

In C++, to perform stream I/O with disk files, it is recommended to use classes derived from ios. This is because C++ provides a set of classes that are specifically designed for handling file input and output operations. These classes, such as ifstream and ofstream, provide convenient and efficient methods for opening, reading, writing, and closing files. By using these classes, you can take advantage of the object-oriented features of C++ and ensure proper resource management through automatic file closing when the objects are destroyed.

Submit
View My Results

Quiz Review Timeline (Updated): Mar 17, 2023 +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 17, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • May 01, 2020
    Quiz Created by
    Prasad Mutkule
Cancel
  • All
    All (20)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Which of the following provides a reuse mechanism?
The process of building new classes from existing one is called...
What will be the output of the following C++ code? ...
Which of the following accesses a variable in structure *b?
What will be the output of the following C++ code? ...
 The operator used for dereferencing or indirection is ____
Overloading the function operator
Which of the following type of class allows only one object of it to...
Which of the following statements is correct?
How the template class is different from the normal class?
If a class C is derived from class B, which is derived from class A,...
In C++, dynamic memory allocation is accomplished with the operator...
What will be the output of the following C++ code? ...
In an assignment statement a=b Which of the following statement is...
What will be the output of the following C++ code? ...
What is the final value of x when the code int x; for(x=0; x<10;...
RunTime Polymorphism is achieved by ______
What is the output of the following code ...
What will be the output of the following C++ code? ...
To perform stream I/O with disk files in C++, you should
Alert!

Advertisement