1.
What is the final value of x when the code int x; for(x=0; x<10; x++) {} is run?
Correct Answer
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.
2.
In an assignment statement a=b Which of the following statement is true?
Correct Answer
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.
3.
Which of the following type of class allows only one object of it to be created?
Correct Answer
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.
4.
Which of the following statements is correct?
Correct Answer
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.
5.
Which of the following provides a reuse mechanism?
Correct Answer
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.
6.
What is the output of the following code
char symbol[3]={‘a’,‘b’,‘c’};
for (int index=0; index<3; index++)
cout << symbol [index];
Correct Answer
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".
7.
The process of building new classes from existing one is called _________.
Correct Answer
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.
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
Correct Answer
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.
9.
To perform stream I/O with disk files in C++, you should
Correct Answer
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.
10.
Overloading the function operator
Correct Answer
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.
11.
RunTime Polymorphism is achieved by ______
Correct Answer
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.
12.
In C++, dynamic memory allocation is accomplished with the operator ____
Correct Answer
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.
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;
}
Correct Answer
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.
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); }
Correct Answer
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.
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;
}
Correct Answer
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.
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();
}
Correct Answer
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.
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;
}
Correct Answer
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".
18.
How the template class is different from the normal class?
Correct Answer
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.
19.
Which of the following accesses a variable in structure *b?
Correct Answer
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".
20.
The operator used for dereferencing or indirection is ____
Correct Answer
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.