# Object Oriented Programming With C++

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
P
Community Contributor
Quizzes Created: 1 | Total Attempts: 219
Questions: 20 | Attempts: 221  Settings  .

• 1.

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

• A.

10

• B.

9

• C.

0

• D.

1

A. 10
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 < 10, the loop will execute 10 times. Therefore, the final value of x will be 10.

Rate this question:

• 2.

### In an assignment statement a=b Which of the following statement is true?

• A.

The variable a and the variable b are equal.

• B.

The value of b is assigned to variable a but the later changes on variable b will not effect the value of variable a

• C.

The value of b is assigned to variable a and the later changes on variable b will effect the value of variable a

• D.

The value of variable a is assigned to variable b and the value of variable b is assigned to variable a.

B. The value of b is assigned to variable a but the later changes on variable b will not effect the value of variable a
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.

Rate this question:

• 3.

### Which of the following type of class allows only one object of it to be created?

• A.

Virtual class

• B.

Abstract class

• C.

Singleton class

• D.

Friend class

C. Singleton class
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.

Rate this question:

• 4.

### Which of the following statements is correct?

• A.

Base class pointer cannot point to derived class.

• B.

Derived class pointer cannot point to base class.

• C.

Pointer to derived class cannot be created.

• D.

Pointer to base class cannot be created.

B. Derived class pointer cannot point to base class.
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.

Rate this question:

• 5.

### Which of the following provides a reuse mechanism?

• A.

Abstraction

• B.

Inheritance

• C.

Dynamic binding

• D.

Encapsulation

B. Inheritance
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.

Rate this question:

• 6.

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

• A.

A b c

• B.

“abc”

• C.

Abc

• D.

‘abc’

C. Abc
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".

Rate this question:

• 7.

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

Inheritance,inheritance
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.

Rate this question:

• 8.

### 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

• A.

Protected and public data only in C and B.

• B.

Protected and public data only in C.

• C.

Private data in A and B.

• D.

Protected data in A and B.

D. Protected data in A and B.
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.

Rate this question:

• 9.

### To perform stream I/O with disk files in C++, you should

• A.

Open and close files as in procedural languages.

• B.

Use classes derived from ios

• C.

Use C language library functions to read and write data.

• D.

B. Use classes derived from ios
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.

Rate this question:

• 10.

• A.

Requires a class with an overloaded operator.

• B.

Requires a class with an overloaded [ ] operator.

• C.

Allows you to create objects that act syntactically like functions.

• D.

Usually make use of a constructor that takes arguments.

A. Requires a class with an overloaded 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.

Rate this question:

• 11.

### RunTime Polymorphism is achieved by ______

virtual function,Virtual Function,Virtual function,virtualfunction,Virtualfunction
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.

Rate this question:

• 12.

### In C++, dynamic memory allocation is accomplished with the operator ____

new
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.

Rate this question:

• 13.

### 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; }

• A.

fun() const fun()

• B.

fun() fun() const

• C.

fun() fun()

• D.

fun() const fun() const

B. fun() fun() const
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.

Rate this question:

• 14.

### 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); }

• A.

-5

• B.

0

• C.

10

• D.

5

D. 5
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.

Rate this question:

• 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; }

• A.

Segmentation fault

• B.

Error as a private member a is referenced outside the class

• C.

No output

• D.

Program compiles successfully but gives run-time error

C.  No output
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.

Rate this question:

• 16.

### 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(); }

• A.

Error

• B.

5

• C.

10

• D.

Segmentation fault

A. Error
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.

Rate this question:

• 17.

### 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; }

• A.

Value of capacity is: 10

• B.

Value of capacity is: 100

• C.

Error

• D.

Segmentation fault

A. Value of capacity is: 10
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".

Rate this question:

• 18.

### How the template class is different from the normal class?

• A.

Template class generate objects of classes based on the template type

• B.

Template class saves system memory

• C.

Template class helps in making genetic classes

• D.

All of the mentioned

D.  All of the mentioned
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.

Rate this question:

• 19.

### Which of the following accesses a variable in structure *b?

• A.

B->var;

• B.

B.var;

• C.

B-var;

• D.

B>var;

A. B->var;
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".

Rate this question:

• 20.

### The operator used for dereferencing or indirection is ____

• A.

->

• B.

–>>

• C.

*

• D.

&

C. *
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.

Rate this question:

Related Topics Back to top