KEMBAR78
C++ Solution | PDF | Class (Computer Programming) | Method (Computer Programming)
0% found this document useful (0 votes)
5 views10 pages

C++ Solution

Uploaded by

riddhimalviya757
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

C++ Solution

Uploaded by

riddhimalviya757
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Viva Questions of C++ With Answer

Q.1 What is the basic structure of a C++ program?


Answer: The basic structure of a C++ program is shown below:
#include<iostream.h>
#include<conio.h>
Void main()
{
clrscr();
cout<<”Hello World!”;
getch();
}
Q.2 What do you mean by # in C++?
Answer:
➢ # is known has Pre-processor.
➢ It gives instructions to the compiler to preprocess the information
before actual compilation starts.
➢ All preprocessor directives begin with # such that #include, #define etc.
➢ #include is used to include header files in the current program.
➢ #define is used to define global constant for the current program.

Q.3 Define comments.


Answer:
➢ Comments are those portion of code which is ignored by compiler.
➢ It is used to increase the understandability of the program.
➢ They are only helpful for a programmer to add a description or
additional information about their source code.
➢ There are two types of comments:
➢ Single Line Comment:- for single line. start with // and continue until
the end of the line.
➢ Multiline Line comment:- for multiple line. start with /* and end with *.
Q.4 what is variable in C++?
Answer:
➢ Variable is the container that stores the data.
➢ Variable is the name of memory location that is used to store data .

Q.5 What is the difference between variable and constant?


Answer:
➢ The value of a variable can be change while the value of a constant can
not be change.
➢ Syntax:
int a; // variable
const int a=20; //constant

Q.6 What is Local and Global scope of varables?


Answer:
➢ The scope of a variable is defined as the extent of the program code
within which the variable remains active i.e. it can be declared, defined
or worked with.
➢ There are two types of scope in C++:
1. Local Scope: A variable is said to have a local scope or is local
when it is declared inside a code block/function. The variable
remains active only inside the block and is not accessible outside
the code block/function.
2. Global Scope: A variable has a global scope when it is accessible
throughout the program. A global variable is declared on top of
the program before all the function definitions.

#include<iostream.h>
#include<conio.h>
int a=10; // global variable
void main()
{
int b=20; local variable
}
Q.7 What is the precedence when there are a Global variable and a
Local variable in the program with the same name?
Answer:
➢ Whenever there is a local variable with the same name as that of a
global variable, the compiler gives precedence to the local variable.

#include<iostream.h>
#include<conio.h>
int a=10; // global variable→ Low priority
void main()
{
int a=20; local variable→High priority
}

Q.8 What do you mean by << and >> in C++.


Answer:
➢ << is known as insertion operator that is used with cout to write the
data.
➢ >> is known as extraction operator that is used with cin to read the
data.
➢ For example:
cout<<”Hello world!”;
cin>>variablename;

Q.9 What is the difference between equal to (==) and Assignment


Operator (=)?
Answer:
➢ In C++, equal to (==) and assignment operator (=) are two completely
different operators.
➢ Equal to (==) is an equality relational operator that evaluates two
expressions to see if they are equal and returns true if they are equal
and false if they are not.
➢ The assignment operator (=) is used to assign a value to a variable.
Q.10 What is a Class?
Answer:
➢ Class is a user-defined data type in C++.
➢ Class is a logical structure/blueprint that has some properties and
behaviour.
➢ Logical means it does not have any physical implementation in memory.
Q.11 Difference between Class and Structure.
Answer:
➢ Structure: In C language, the structure is used to bundle different types
of data types together. The variables inside a structure are called the
members of the structure. These members are by default public and can
be accessed by using the structure name followed by a dot operator and
then the member name.
➢ Class: Class is a successor of the Structure. C++ extends the structure
definition to include the functions that operate on its members. By
default all the members inside the class are private.
Q.12 Define Object in C++?
Answer:
➢ Object is an instance of the class. An object can have fields, methods,
constructors and related. For example, a bike in real life is an object,
but it has various features such as brakes, color, size, design, and others
which are instances of its class.

Q.13 What is an Inline function in C++?


Answer:
➢ Inline function is a function that is compiled by the compiler as the
point of calling the function and the code is substituted at that point.
This makes compiling faster. This function is defined by prefixing the
function prototype with the keyword “inline”.

Q.14 What is the difference between an Object and a Class?


Answer:
➢ Class is a blueprint of a project or problem to be solved and consists of
variables and methods. These are called the members of the class. We
cannot access methods or variables of the class on its own unless they
are declared static.
➢ In order to access the class members and put them to use, we should
create an instance of a class which is called an Object. The class has an
unlimited lifetime whereas an object has a limited lifespan only.
Q.15 Define Encapsulation in C++?
Answer:
➢ Encapsulation is the process of binding together the data and functions
in a class. It is applied to prevent direct access to the data for security
reasons. The functions of class are applied for this purpose. For
example, the net banking facility to the customers allows only the
authorized person with the required login id and password to get access
and that too only for his/her part of the information in the bank
datasource.
Q.16 What is an abstraction in C++?
Answer:
➢ An abstraction in C++ is the process of hiding the internal
implementations and displaying only the required details. For example,
when you send an important message through email, at that time only
writing and clicking the send option is used. This outcome is just the
success message that is displayed to confirm you that your email has
been sent. However, the process followed in transferring the data
through email is not displayed because it is of no use to you.

Q.17 What are the various Access Specifiers in C++?


Answer:
➢ C++ supports the following access specifiers:
➢ Public: Data members and functions are accessible outside the class.
➢ Private: Data members and functions are not accessible outside the
class. The exception is the usage of a friend class.
➢ Protected: Data members and functions are accessible only to the
derived classes.

Q.18 What is a Constructor?


Answer:
➢ Constructor is a member function of the class having the same name as
the class. It is mainly used for initializing the members of the class.
Q.19 What is the role of the Static keyword for a class member
variable?
Answer:
The static member variable shares a common memory across all the objects
created for the respective class. We need not refer to the static member
variable using an object. However, it can be accessed using the class name
itself.

Q.20 Explain Function Overloading and Operator Overloading.


Answer:
➢ C++ supports OOPs concept Polymorphism which means “many forms”.
➢ In C++ we have two types of polymorphism, i.e. Compile-time
polymorphism, and Run-time polymorphism. Compile-time polymorphism
is achieved by using an Overloading technique.
➢ C++ supports two types of overloading:
➢ Function Overloading:
➢ Function overloading is a technique that allows the programmer to have
more than one function with the same name but different parameter list. In
other words, we overload the function with different arguments i.e. be it the
type of arguments, number of arguments or the order of arguments.
➢ Operator Overloading:
➢ This is yet another type of compile-time polymorphism that is supported by
C++. In operator overloading, an operator is overloaded, so that it can
operate on the user-defined types as well with the operands of the standard
data type. But while doing this, the standard definition of that operator is
kept intact.
➢ For Example, an Addition operator (+) that operates on numerical data
types can be overloaded to operate on two objects just like an object of
complex number class.

Q.21 What is the difference between Method Overloading and


Method Overriding in C++?
Answer:
➢ Method overloading is having functions with the same name but
different argument lists. This is a form of compile-time polymorphism.
➢ Method overriding comes into picture when we rewrite the method that
is derived from a base class. Method overriding is used while dealing
with run-time polymorphism or virtual functions.
Q.22 Name the Operators that cannot be Overloaded.
Answer:
➢ sizeof – sizeof operator
➢ . – Dot operator
➢ .* – dereferencing operator
➢ -> – member dereferencing operator
➢ :: – scope resolution operator
➢ ?: – conditional operator

Q.23 What is Inheritance?


Answer:
➢ Inheritance is a process by which we can acquire the characteristics of
an existing entity and form a new entity by adding more features to it.
➢ In terms of C++, inheritance is creating a new class by deriving it from
an existing class so that this new class has the properties of its parent
class as well as its own.

Q.24 What are the advantages of Inheritance?


Answer:
➢ Inheritance allows code re-usability, thereby saving time on code
development.
➢ By inheriting, we make use of a bug-free high-quality software that
reduces future problems.

Q.25 Define super and Sub class.


Answer:
➢ An existing class who is inherited by a new class called super
class/parent class.
➢ A class which inherits an existing class called sub/child class;

Q.26 What is Polymorphism?


Answer:
➢ The basic idea behind polymorphism is in many forms. In C++, we have two
types of Polymorphism:
➢ Compile-time Polymorphism
➢ In compile-time polymorphism, we achieve many forms by overloading.
Hence, we have an Operator overloading and function overloading. (We
have already covered this above)
➢ Run-time Polymorphism
➢ This is the polymorphism for classes and objects. General idea is that a base
class can be inherited by several classes. A base class pointer can point to its
child class and a base class array can store different child class objects.
➢ This means, that an object reacts differently to the same function call. This
type of polymorphism can use a virtual function mechanism.

Q.27 What do you mean by Pure Virtual Functions?


Answer:
➢ A Pure Virtual Member Function is a member function in which the
base class forces the derived classes to override. Normally this member
function has no implementation. Pure virtual functions are equated to
zero.
➢ Example:
class Shape { public: virtual void draw() = 0; };
Q.28 What is a friend function?
Answer:
➢ C++ class does not allow its private and protected members to be
accessed outside the class. But this rule can be violated by making use of
the “Friend” function.
➢ As the name itself suggests, friend function is an external function that
is a friend of the class.
➢ For friend function to access the private and protected methods of the
class, we should have a prototype of the friend function with the
keyword “friend” included inside the class.

Q.29 What is a template?


Answer:
➢ Templates allow creating functions that are independent of data type
(generic) and can take any data type as parameters and return value
without having to overload the function with all the possible data types.
Templates nearly fulfill the functionality of a macro.
➢ Its prototype is any of the following ones:
template <class identifier> function_declaration;
template <typename identifier> function_declaration;
➢ The only difference between both the prototypes is the use of keyword
class or typename. Their basic functionality of being generic remains
the same.
Q.30 What are the most important differences between C and C++?
Answer:
➢ C++ supports references while C doesn’t
➢ Features like friend functions, function overloading, inheritance,
templates, and virtual functions are inherent to C++. These are not
available in C programming language
➢ In C, exception handling is taken care of in the traditional if-else style.
On the other hand, C++ offers support for exception handling at the
language level
➢ Mainly used input and output in C are scanf() and printf(), respectively.
In C++, cin is the standard input stream while cout serves as the
standard output stream
➢ While C is a procedural programming language, C++ provides support
for both procedural and object-oriented programming approaches
Q.31 Define access specifier and its various types in C++
Answer:
➢ An access specifier offers the means by which it is possible to define
how the class members, i.e. functions and variables, will be accessed
outside the scope of the class. There are three types of access specifier in
C++:
➢ Private – Such class members can’t be accessed outside the class in
which they are declared and are only accessible within the same class.
Even child classes are disabled to access private members of its parent
class
➢ Protected – In addition to the class in which they are declared, the child
classes can access the protected members of its parent class
➢ Public – Class members declared as public can be accessed throughout
the program (code)

Q.32 Define a namespace?


Answer:
➢ A namespace is used for resolving the name conflict of the identifier,
which is accomplished by placing them under various namespaces. This
way, it helps in the logical division of the different codes.
Q.33 What do you mean by this pointer?
Answer:
➢ The this pointer holds the address of current object, in simple words
you can say that this pointer points to the current object of the class.

You might also like