C++ Multiple Choice Questions
C++ Multiple Choice Questions
MCQ Based on Basics of C++
1) #include<userdefined.h>
Which of the following is the correct syntax to add the header file in the C++ program?
#include<userdefined>
#include "userdefined.h"
<include> "userdefined.h"
Both A and B
Answer: D
Explanation: To include the herder files in the C++ program user can use any of the following given syntax.
#include <Filename.h> #include <Filename.h>
Or
#include "filename.h" #include "filename.h"
2) Which of the following is the correct syntax to print the message in C++ language?
cout <<"Hello world!";
Cout << Hello world! ;
Out <<"Hello world!;
None of the above
Answer: A
Explanation: To print the message in the C++ language user can use the following syntax:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
cout << "I am learning C++";
return 0;
} #include <iostream> using namespace std; int main() { cout << "Hello World!"; cout << "I am learning C++"; return 0; }
3) Which of the following is the correct identifier?
$var_name
VAR_123
varname@
None of the above
Answer: B
Explanation: There are some certain rules that must be followed by the users while writing the identifiers.
It may contain uppercase/lowercase letters, digits, and underscore (_) only.
It always starts only with non-digit characters.
It should not contain any special characters like $, @etc
4) Which of the following is the address operator?
@
#
&
%
Answer: C
Explanation: To print the address of any variable, a user can use the "&" operator. To understand it more clearly, consider the following example:
#include <iostream>
#include<conio.h>
using namespace std;
int main() {
// Declare Variables
int a;
int *pt;
cout << "C++ Pointer Example Program : Print Pointer Address\n";
a = 10;
pt = &a;
cout << "\n[a ]:Value of A = " << a;
cout << "\n[*pt]:Value of A = " << *pt;
cout << "\n[&a ]:Address of A = " << &a;
cout << "\n[pt ]:Address of A = " << pt;
cout << "\n[&pt]:Address of pt = " << &pt;
cout << "\n[pt ]:Value of pt = " << pt;
getch();
return 0;
} #include <iostream> #include<conio.h> using namespace std; int main() { // Declare Variables int a; int *pt; cout << "C++ Pointer Example Program : Print Pointer Address\n"; a = 10; pt = &a; cout << "\n[a ]:Value of A = " << a; cout << "\n[*pt]:Value of A = " << *pt; cout << "\n[&a ]:Address of A = " << &a; cout << "\n[pt ]:Address of A = " << pt; cout << "\n[&pt]:Address of pt = " << &pt; cout << "\n[pt ]:Value of pt = " << pt; getch(); return 0; }
5) Which of the following features must be supported by any programming language to become a pure object-oriented programming language?
Encapsulation
Inheritance
Polymorphism
All of the above
Answer: D
Explanation: There is nothing that forces a user to use the OOP concept in C++. In contrast, it is necessary for a programming language that it must support all three features as encapsulation, inheritance, and polymorphism completely to become a pure Object-Oriented Language.
6) The programming language that has the ability to create new data types is called___.
Overloaded
Encapsulated
Reprehensible
Extensible
Answer: D
Explanation: A language that can generate the new data types is known as the extensible languages as they can handle the new data types.
7) Which of the following is the original creator of the C++ language?
Dennis Ritchie
Ken Thompson
Bjarne Stroustrup
Brian Kernighan
Answer: C
Explanation: Bjarne Stroustrup creates C++ language during 1997 at AT&T Bell Labs.
8) Which of the following is the correct syntax to read the single character to console in the C++ language?
Read ch()
Getline vh()
get(ch)
Scanf(ch)
Answer: C
Explanation: The "cin.get()" is one of the several functions provided in C++ language that is used to read the single or multiple characters to console.
9) Which of the following statements is correct about the formal parameters in C++?
Parameters with which functions are called
Parameters which are used in the definition of the function
Variables other than passed parameters in a function
Variables that are never used in the function
Answer: A
Explanation: Parameters that are used in the body of the function are known as formal parameters. These parameters represent the parameters passed to the function and are only used inside the function's body.
10) The C++ language is ______ object-oriented language.
Pure Object oriented
Not Object oriented
Semi Object-oriented or Partial Object-oriented
None of the above
Answer: C
Explanation:
The common thing about the Pure Object-oriented language it provides three basic features like Inheritance, Encapsulation, and Polymorphism. Each programming language that supports these entire three features is known as the Pure-Object oriented language. Whereas if a programming language support all these three features but not support completely are known as the Partial-Object oriented languages or the Semi Object-oriented languages
The main reasons why the C++ programming language is Known as Semi-Object oriented language are as follows:
Availability of the Friend function-
A friend class is allowed to access private and protected members of another class, within which it is declared a friend. It may be very useful for some time, but still, it violates the rule of the Object-Oriented features.
Concept of the Global variable-
As we all know that we can declare a global variable in C++ language that can be easily accessible from anywhere within the program. So again, C++ does not provide complete privacy because no one is restricted to access and manipulate that data/information. Hence it offers partial Encapsulation, unlike Java Language, in which a user only allows to declare a variable within the class and can provide access specifier to it.
The main function is Out-side the class-
C++ is an object-oriented language, but still, object-oriented is not fundamentally related (or implicit) to the language. So a user can easily write a valid, well-defined C++ code even without using any object once.
11) Which of the following features is required to be supported by the programming language to become a pure object-oriented programming language?
Encapsulation
Inheritance
Polymorphism
All of the above
Answer: D
Explanation: There is nothing that forces a user to use the OOP concept in C++. In contrast, it is necessary for a programming language that it must support all three features of Encapsulation, Inheritance, and Polymorphism completely to become a Pure Object-Oriented Language.
12) Which of the following comment syntax is correct to create a single-line comment in the C++ program?
//Comment
/Comment/
Comment//
None of the above
Answer: A
Explanation: To create a single line comment (or one-line comment) in C++ program one can use the "// write comment" syntax. We can understand it more easily with the help of following given Program:
Example
#include <stdio.h>
int main(void)
{
// This is a single line comment
// Welcome user comment
printf("Welcome to Javatpoint");
return 0;
} #include <stdio.h> int main(void) { // This is a single line comment // Welcome user comment printf("Welcome to Javatpoint"); return 0; }
Output
Welcome to Javatpoint
13) C++ is a ___ type of language.
High-level Language
Low-level language
Middle-level language
None of the above
Answer: C
Explanation: C++ contains the features of both types of high and Low-level programming languages, and it is also not wrong if we call it the combination of both high and low-level languages.
14) For inserting a new line in C++ program, which one of the following statements can be used?
\n
\r
\a
None of the above
Answer: A
Explanation:
To insert a new line or to jump on to the next line, one can use the "\n." In c++, there is also an alternative is available that is " endl," which is also used for breaking a line in the output. Let see the example of both the "\n" and "endl."
Example: Using "\n."
#include <iostream>
using namespace std;
int main() {
cout << "Hello World! \n\n";
cout << "I am learning C++";
return 0;
} #include <iostream> using namespace std; int main() { cout << "Hello World! \n\n"; cout << "I am learning C++"; return 0; }
Output
Hello World! I am learning C++
Example: With using "end"
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
cout << "I am learning C++";
return 0; #include <iostream> using namespace std; int main() { cout << "Hello World!" << endl; cout << "I am learning C++"; return 0;
Output
Hello World! I am learning C++
15) Which one of the following represents the tab?
\n
\t
\r
None of the above
Answer: B
Explanation: The "\t" is a type of space sequence representing the tab, which means a set of blank space adds to the line. To understand it more clearly, consider the following example:
Program
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter the first number\t";
cin>>a;
cout<<"Enter the second number\t";
cin>>b;
if(a>b)
cout<<"Greatest number is\t"<<a;
else
cout<<"Greatest number is\t"<<b;
return 0;
} #include<iostream> using namespace std; int main() { int a,b; cout<<"Enter the first number\t"; cin>>a; cout<<"Enter the second number\t"; cin>>b; if(a>b) cout<<"Greatest number is\t"<<a; else cout<<"Greatest number is\t"<<b; return 0; }
Output
Enter the first number 12 Enter the second number 13 Greatest number is 13
Set of MCQ Based on Arrays of the C++
1) Which of the following refers to characteristics of an array?
An array is a set of similar data items
An array is a set of distinct data items
An array can hold different types of datatypes
None of the above
Answer: A
Explanation: Basically, an array is a set of similar data items that are stored in the contiguous memory blocks. You can access any data item randomly using the index address of the element s.
2) If we stored five elements or data items in an array, what will be the index address or the index number of the array's last data item?
3
5
4
88
Answer: C
Explanation: The array uses the contiguous block of memory for storing the data elements. Hence the data items pushed into an array are stored at the continuous address space. The index number of the array begins with zero so, the first data item pushed into the array will be stored at zero and so on. Hence index number of the last (fifth) element will be 4.
3) Which of the following is the correct syntax for declaring the array?
init array []
int array [5];
Array[5];
None of the above
Answer: B
Explanation: To declare an array in C++, we first need to specify its data type according to requirements such as int or char, afterward that the array's name and the size of the array. So the correct answer will be B.
Example: Array declaration by specifying size and initializing elements int arr[8] = { 10, 20, 30, 40 };
The compiler will create an array of size 8, initializes the first four elements as specified by the user and rest elements as 0
The above is just same as "int arr[] = {11, 22, 32, 44, 0, 0,0,0}".
4) Which of the following is the correct syntax for printing the address of the first element?
array[0];
array[1];
array[2];
None of the above
Answer: A
Explanation: To print or access the first data item of the array, the correct syntax is "array[0];" because the array's index begins with zero instead of one. So the correct answer will be A.
5) Which of the following gives the 4th element of the array?
Array[0];
Array[0];
Array[3];
None of the above
Answer: C
Explanation: The index of the array begins with zero instead of 1. To print the 4th element of the array, the index number will be n-1 or (3);
6) What is the output of the given program?
#include < stdio.h >
using namespace std;
int main()
{
int array[] = {10, 20, 30};
cout << -2[array];
return 0;
} #include < stdio.h > using namespace std; int main() { int array[] = {10, 20, 30}; cout << -2[array]; return 0; }
-15
-30
Compiler error
Garbage value
Answer: B
Explanation: It will print the negative value of the specified values because the "-" sign we used while specifying the array elements. So the correct answer will be the "-30".
7) Which type of memory is used by an Array in C++ programming language?
Contiguous
None-contiguous
Both A and B
Not mentioned
Answer: A
Explanation: In the C and C++ programming language, the memory used by an array is usually contiguous, which means when an array is declared or initialized in the program a block of memory is selected form the memory space immediately. The major drawback of an array is that users have to guess the size of the array before actually using it, in which a significant amount of memory will be occupied even when it is not used. This later creates the problem of lack of memory.
8) Which of the following is the correct definition of sorting?
Sorting is a type of process in which the data or information is ordered into a specific order. Example increasing orders, decreasing.
Sorting information or data only in increasing order
Sorting is a type of process in which data elements are modified or manipulated
None of the above
Answer: A
Explanation: Sorting is a type of technique or process in which the locations of data elements hold by the array are reordered or manipulated. So the correct answer will be A.
For Example
Un-sorted Array
4 3 2 1 5 6 7
Sorted Array
1 2 3 4 5 6 7
9) How many types of the array are there in the C++ programming language?
In the C++ programming language, there are three types of arrays
In the C++ programming language, there are four types of arrays
In the C++ programming language, there are two types of arrays
Both A and B
Answer: C
Explanation: In the C++ programming language, there are mainly two types of arrays that are Single Dimension arrays and Multi-Dimension arrays. In Single-Dimension arrays, the same types of values are hold in the form of a List, while on the other hand, in Multi-Dimension arrays; values are stored in the form of a matrix.
10) In C++, for what purpose the "rank()" is used?
It returns the size of each dimension
It returns the maximum number of elements that can be stored in the array
It returns the dimension of the specified array
None of the above
Answer: C
Explanation: In C++, one can use the rank function where he wants to know about the dimensions( e.g., single-dimension, multi-dimension) of a specific array by just passing it to the "rank()" function.
11) Which one of the following is the correct definition of the "is_array();" function in C++?
It checks that the specified variable is of the array or not
It checks that the specified array of single dimension or not
It checks that the array specified of multi-dimension or not
Both B and C
Answer: A
Explanation: In C++, the "is_array();" is used for checking that the specified element or data item belongs to the array type or not.
12) Observer the given C++ program carefully and choose the correct output from the given options:
Program
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout<<is_array<int>::value; // case A
cout<<is_array<char[10]>::value; // case B
cout<<is_array<string>::value; // case c
return 0;
} #include <iostream> #include <string> using namespace std; int main() { cout<<is_array<int>::value; // case A cout<<is_array<char[10]>::value; // case B cout<<is_array<string>::value; // case c return 0; }
110
001
010
None of the above
Answer: C
Explanation: The correct output will be "010" Because in both cases, A and C variables passed to the "is_array()" function are different, so the function returns zero in both cases while in the case of B, they are the same. Hence it returns one as the output.
13) Read the given C++ program carefully and choose the correct output from the given options:
Program
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout<<rank<int[10]>::value; // Case A
cout<<rank<char[10][10]>::value; // Case B
cout<<rank<string[10][10][10]>::value; //Case C
return 0;
} #include <iostream> #include <string> using namespace std; int main() { cout<<rank<int[10]>::value; // Case A cout<<rank<char[10][10]>::value; // Case B cout<<rank<string[10][10][10]>::value; //Case C return 0; }
121
321
123
010
Answer: C
Explanation: As we can see in the above program, here, the "rank()" function is used. The "rank()" function is used to know about the dimensions of the passed array. In case A the array passed to the "rank()" function is single-dimensional, In case B, the passed array is 2-dimensional, and in case C, the array passed is three-dimensional. So the output return by the "rank()" function is 123.
14) What did we call an array of the one-dimensional array?
Single Dimensional array
Multi-Dimensional array
2D Array (or 2-Dimensional array)
Both A and B
Answer: C
Explanation: An array of one-dimensional array is known as the 2-dimensional array or 2D Array-like . We can understand it more clearly with the help of the following example:
Initialization of 2-Dimensional Array
int array[2][3]={ {3,2,5} , {9,5,4} } int array[2][3]={ {3,2,5} , {9,5,4} }
15) Which types of arrays are always considered as linear arrays?
Single-dimensional
Multi-dimensional
Both A and B
None of the above
Answer: A
Explanation: Single-dimensional arrays are always considered as linear arrays.
16) Which of the following can be considered as the object of an array?
Index of an array
Elements of the Array
Functions of the Array
All of the above
Answer: B
Explanation: The elements in the array are known as the objects of the array.
17) How many types of elements can an array store?
Same types of elements
Char and int type
Only char types
All of the above
Answer: A
Explanation: An array can hold several elements except that all the data elements are of the same type.
18) Elements of a one-dimensional array are numbered as 0,1,2,3,4,5, and so on; these numbers are known as ____
Subscript of Array
Members of Array
Index values
Both A and C
Answer: D
Explanation: The elements of one-dimensional array are indexed as 0,1,2,3,... and these numbers are also known as the index values or subscripts of the array.
MCQ Based on the Oops Concepts of the C++
1) Which of the following statement is true about the new and malloc?
I. The "new" is a type of operator while "malloc" is a kind of function
II. "new" invokes a constructor, whereas "malloc" does not invoke the constructor
III. "malloc" returns void pointer and also needed to typecast whereas "new" returns required the pointer
Only I
Both I and II
I, II, III
None of the above
Answer: C
Explanation: All statements mentioned in the above question are completely true about the "malloc" and the "new." The "malloc" is a kind of function available in the C++ language, while the "new" is a type of operator that invokes the Constructor.
2) Which of the following statement is true about the new and malloc?
I. The pointer object initialization of a specific class using "malloc" also needs to include constructor calls; on the other hand, doing so with the "new" keyword does not include any constructor calls.
II. The pointer object initialization of a specific class using the "new" keyword also needs to include a constructor call. On the other hand, doing so with the "malloc" does not need to include any constructor calls.
III. Pointer object initialization of a specific class using either "new" or "malloc" involves the constructor call.
Only II
Both II and III
Only I
None of the above
Answer: A
Explanation: In object initialization of a class using the "new" keyword also involves a constructor call, while doing so with the "malloc" does not require any involvement of the constructor call. This is the main reason why the "new" is added explicitly in the C++ language. However, the "malloc" function is used to assign the memory to any specific pointer, so it assigns an equal amount of memory to the class size. At the same time, the "new" keyword involves the initialization, hence invokes the Constructor of that particular class.
3) Which of the following statement is correct about Virtual Inheritance?
It is a technique to ensure that a private member of a base class can be accessed
It is a technique to optimize the multiple inheritances
It is a technique to avoid the multiple inheritances of the classes
It is a C++ technique to avoid multiple copies of the base class into the derived or child classes
Answer: D
Explanation:
4) Which one of the following statements correctly refers to the Delete and Delete[] in C++ programming language?
Delete is syntactically correct although, if the Delete[] is used, it will obtain an error.
The "Delete" is used for deleting the standard objects, while on the other hand, the "Delete[]" is used to delete the pointer objects
The "Delete" is a type of keyword, whereas the "Delete[]" is a type of identifier
The "Delete" is used for deleting a single standard object, whereas the "Delete[]" is used for deleting an array of the multiple objects
Answer: D
Explanation: The "Delete" is used with the single general object, while on the other hand, the "Delete[]" is used with the array of the multiple objects initiated with the new operator.
5) Consider the following given program and choose the most appropriate output from the given options:
#include <iostream>
using namespace std;
class A{
public:
A(){
cout<<"Constructor called\n";
}
~A(){
cout<<"Destructor called\n";
}
};
int main(int argc, char const *argv[])
{
A *a = new A[5];
delete[] a;
return 0;
} #include <iostream> using namespace std; class A{ public: A(){ cout<<"Constructor called\n"; } ~A(){ cout<<"Destructor called\n"; } }; int main(int argc, char const *argv[]) { A *a = new A[5]; delete[] a; return 0; }
Segmentation failure.
Error.
The Constructor will be invoked five times, and after that destructor will be invoked only once.
The Constructor will be invoked five times, and after that destructor will also be invoked five times.
Answer: D
Explanation: As mentioned in the above-given program, five-pointer variables are initiated using the "new" keyword so that the Constructor will be invoked five times. In addition, the destructor "Delete[]" is used( that is used for deleting the group of the several objects) for terminating variables. Hence all of the variables will be terminated completely, and while terminating the variables, the destructor will be invoked five times.
6) Consider the following given program and choose the most appropriate output from the given options:
#include<iostream>
using namespace std;
class Base {
public:
Base()
{ cout<<"Constructing Base \n"; }
~Base()
{ cout<<"Destructing Base \n"; }
};
class Derived: public Base {
public:
Derived()
{ cout<<"Constructing Derived \n"; }
~Derived()
{ cout<<"Destructing Derived \n"; }
};
int main(void)
{
Derived *d = new Derived();
Base *b = d;
delete b;
return 0;
} #include<iostream> using namespace std; class Base { public: Base() { cout<<"Constructing Base \n"; } ~Base() { cout<<"Destructing Base \n"; } }; class Derived: public Base { public: Derived() { cout<<"Constructing Derived \n"; } ~Derived() { cout<<"Destructing Derived \n"; } }; int main(void) { Derived *d = new Derived(); Base *b = d; delete b; return 0; }
Constructing base, Constructing Derived, Destructing Base, Destructing Derived
Constructing base, Constructing Derived, Destructing Base
Constructing base, Constructing Derived, Destructing Derived, Destructing Base
None of the above
Answer: B
Explanation: In the above-given program, the derived class's object is storing in the base class pointer; while the object is destroyed, the program has never invoked the destructor of the Derived class. Therefore, it shows that the object is not destroyed at all; hence, in this case, the program may also behave undesirably.
7) Which of the following can be considered as the correct syntax for declaring an array of pointers of integers that has a size of 10 in C++?
int arr = new int[10];
int *arr = new int*[10]
int **arr = new int*[10];
int *arr = new int[10];
Answer: C
Explanation: To declare an array of integers' pointer, here we need to use the double-pointer array where every element is set of the pointer to the integers. Therefore the correct option is C.
8) Which of the following can be considered as the members that can be inherited but not accessible in any class?
Public
Protected
Private
Both A and C
Answer: C
Explanation: The "Private" member of a class can be inherited by the child class. Still, the child class cannot access them (Or we can say that they are not accessible from the child class).
9) Which of the following can be used to create an abstract class in the C++ programming language?
By using the pure virtual function in the class
By declaring a virtual function in the base class
By declaring the virtual keyword afterward, the class Declaration
None of the above
Answer: A
Explanation: A class must contain at least one declaration of the pure virtual function in itself to be called an abstract class. Therefore to make an abstract class, one has to declare at least one pure virtual function in that class.
10) Which of the following statements is correct about the class?
An object is an instance of its class
A class is an instance of its object
An object is the instance of the data type of that class
Both A and C
Answer: A
Explanation: Generally, an object is an instance of a class, e.g., an object represents the class.
11) Which of the following statements is correct about the friend function in C++ programming language?
A friend function is able to access private members of a class
A friend function can access the private members of a class
A friend function is able to access the public members of a class
All of the above
Answer: D
Explanation: A friend function can access any member of a class without caring about the type of member it contains, such as public, private, and protected.
12) Which of the following statement is not true about C++?
Members of a class are public by default
A class cannot have the private members
A structure can have the member functions
All of the above
Answer: C
Explanation: In C, structures are not allowed to have member functions; while on the other hand, C++ allows the structure to have the member functions. Members of the class are generally private by default, and those of the structures are public. So it is a completely false statement that classes can not private members.
13) Which of the following given statement is completely true?
I. In an object-oriented programming language, all the function calls are resolved at compile-time.
II. In a procedure programming language, all the function calls are resolved at compile-time
Only II
Only I
Both I & II
None of the above
Answer: B
Explanation: In a procedure programming language such as C, we do not have the concept of Polymorphism, so all function calls are resolved at the compile-time while, on the other hand, In an Object-Oriented language, through the concept of Polymorphism, all function calls cannot be resolved at the compile-time.
14) Which one of the following cannot be used with the virtual keyword?
Constructor
Destructor
Member function
None of the above
Answer: A
Explanation: In C++, we cannot use the virtual keyword with the Constructor because constructors are generally defined to initialize the object of a specific class; hence no other class requires the other class's object.
15) Which of the following is used for implementing the late binding?
Operator Functions
Constant Functions
Virtual Functions
Both A and B
Answer: C
Explanation: A virtual function can be used for implementing the concept of late binding. For example - Binding the actual functions to their corresponding calls.
16) Which of the following statements supports that reusable code should be one of the desirable features of any language?
It helps in reducing the maintenance cost
It helps in reducing the testing time
It helps in reducing both the maintenance time and testing time
It helps in reducing the compile time
Answer: C
Explanation: While reusing the existing code, we are not required to test/check that code, again and again, because it was already tested while it was written for the very first time. So the reusing the code defiantly helps in reducing the maintenance and testing time.
While reusing the existing code, the compile-time most likely to be increased or remain the same, and it is obvious because we use the existing code in our program to save our time( or to include any specific functionality) by which the size of the overall program gets grows which is natural.
50) Which of the following statement is correct about the C++ programming language?
In C++, both the Static and Dynamic type checking are allowed
In C++, member function are allowed to be of the type canst
In C++, Dynamic checking is allowed
None of the above
Answer: A
Explanation: In C++, both types of static and dynamic checking are allowed.
18) Which of the following is not a kind of inheritance?
Distributed
Multiple
Multi-level
Hierarchal
Answer: A
Explanation: Among the above options, Distributed is only, which is not a type of inheritance, while Multiple, Multi-level, and the Hierarchal are the types of inheritance.
19) What will happen if "In a C++ program a class has no name"?
It is not even allowed in C++
It will not have the Constructor
It will not have the destructor
Both B and C
Answer: C
Explanation: In the C++ program, if we use a class without assigning a name. As a result, it will not be going to have a destructor, but it will have the object. To understand it in more detail, consider the following program:
Program
#include using namespace std; class { public: void func() { cout<<"Hello world"; } }a; int main(int argc, char const *argv[]) { a.func(); return 0; }
20) Which type of approach is used by the C++ language?
Right to left
Left to right
Top to bottom
Bottom-up
Answer: D
Explanation: Generally, C++ uses the Bottom-up approach while other programming languages like C use the top-down approach.
21) Which of the following concept refers to adding new components to the program at the run time?
Dynamic Loading
Dynamic binding
Data hiding
Both A & B
Answer: C
Explanation: The dynamic loading is referred to as the concept of adding a new component to the program as it runs.
22) How can one implement the compile-time Polymorphism in the C++ programming language?
By using the Template
By using the concepts of inheritance
By using both the virtual functions and inheritance
By using only the virtual functions
Answer: A
Explanation: One can easily implement the Compile-time Polymorphism using the Template.
23) How can one implement the run-time Polymorphism in the C++ programming language?
By using the Template
By using the concepts of inheritance
By using both the virtual functions and inheritance
By using only the virtual functions
Answer: In C++, one can implement the run-time Polymorphism by using the virtual functions and inheritance where the object decides which function to call.
Explanation: C
24) Which of the following offers a programmer the facility of using a specific class object into other classes?
Polymorphism
Abstraction
Inheritance
Composition
Answer: D
Explanation: The composition is referred to as the concept of using objects of a specific class into several other classes.
25) Which one of the following cannot be a friend in C++ languages?
A Class
A Function
An Object
None of the above
Answer: C
Explanation: In general, an object of any class cannot be a friend of the same and any other class as well. However, there are some functions, operator, and classes which can be made a friend.
26) How are the references different from the pointer?
A reference cannot be modified once it initialized
There is no need of an extra operator for dereferencing of a reference
A reference cannot be NULL
All of the above
Answer: D
Explanation: These are some basic reasons why the references are far different from the pointers.
In the case of pointers "*" operator is required for dereferencing the value contain by it. However, the reference does not need any type of operator for deference.
A Reference cannot be modified once it is initialized, but it is possible to do so in the case of a pointer.
A pointer can be made Null, whereas a reference cannot be NULL.
27) Among the following given options, which can be considered as a member of a class?
Class variable
Member variable
Class functions
Both A and B
Answer: B
Explanation: Generally, the functions of any class are also considered as the member function of that class.
28) Which of the following refers to the wrapping of data and its functionality into a single individual entity?
Modularity
Abstraction
Encapsulation
None of the above
Answer: C
Explanation: In C++, the property of wrapping the data and its functionality into a single entity is known as Encapsulation ( e.g., Classes).
29) Which of the following refers to using the existing code instead of rewriting it?
Inheritance
Encapsulation
Abstraction
Both A and B
Answer: A
Explanation: With the help of the inheritance concept, one can use existing code instead of rewriting. We can do this by inheriting the properties of pre-written code in other parts of the code blocks. Therefore the user is not required to write the same code repeatedly.
30) Among the following, which shows the Multiple inheritances?
X,Y->Z
X->Y->Z
X->Y;X->Z
None of the above
Answer: A
Explanation: In multiple inheritances, a single class can inherit properties form more than one base class.
31) Which of the following statements is true about the C++ programming language?
C++ is an object-oriented programming language
C++ is a procedural programming language
C++ is a functional programming language
C++ is both procedural and object-oriented language
Answer: D
Explanation: C++ is a type of programming language which supports both the procedural ( or step by step instructions) and object-oriented through the concept of classes.
32) Among the following, which statement is correct about the Modularity?
Modularity means hiding the parts of the program
Modularity refers to dividing a program into subsequent small modules or independent parts
It refers to overloading the program's part
Modularity refers to wrapping the data and its functionality into a single entity
Answer: B
Explanation: Modularity refers to dividing a program into small independent code blocks or modules so that they can be easily called anywhere in the entire program where it is required. The concept of Modularity is very efficient and helpful for developers because it makes the program well structured and easy to understand. Hence the correct option is D.
33) Read the following program carefully and find out which concept from the given options is not used or missing in the following program?
Program
class A
{
int x;
public:
void print(){cout<<"hello"<<x;}
}
class B: public A
{
int y;
public:
void assign(int a){y = a;}
} class A { int x; public: void print(){cout<<"hello"<<x;} } class B: public A { int y; public: void assign(int a){y = a;} }
Polymorphism
Encapsulation
Inheritance
Abstraction
Answer: A
Explanation: As we can see in the above-given program, the variables X and Y both are private members, which means they both are hidden from the outside world of the class, so here the concept of abstraction is used.
The other data members and their corresponding functions are stored in an individual class, so here the concept of Encapsulation is also used. In addition, Class B is derived from Class A, which means the concept of inheritance is used as well, but still, we didn't find and an overloaded function in any of the classes. Therefore the concept of Polymorphism is missing or not used in the given program.
34) Which of the following options correctly explains the concept of Polymorphism?
a.
int func(float);
float func(int, int, char); int func(float); float func(int, int, char);
b.
int func(int);
int func(int); int func(int); int func(int);
c.
int func(int, int);
float func1(float, float); int func(int, int); float func1(float, float);
d. None of the above
Answer: A
Explanation: The concept of Polymorphism refers to the overloading a function by changing either its number of passed arguments or changing its type. So you have only two options in which the function has named the same. Therefore one can easily find out that in option C, the type and the number of the argument all are the same, which definitely leads to an error that is wrong. However, the option in which the name of the function is the same has a different number of arguments, or different types is the correct option. So the correct answer is A.
Comments
Post a Comment