0% found this document useful (0 votes)
3 views8 pages

Extra Questions

The document outlines key differences between C and C++, explaining concepts such as classes, objects, access modifiers, and various operators in C++. It also covers advanced topics like polymorphism, virtual functions, friend classes, and memory management techniques. Additionally, it distinguishes between shallow and deep copies, providing a comprehensive overview of C++ programming principles.

Uploaded by

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

Extra Questions

The document outlines key differences between C and C++, explaining concepts such as classes, objects, access modifiers, and various operators in C++. It also covers advanced topics like polymorphism, virtual functions, friend classes, and memory management techniques. Additionally, it distinguishes between shallow and deep copies, providing a comprehensive overview of C++ programming principles.

Uploaded by

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

1. What is the difference between C and C++?

C C++

C is a procedure-oriented programming C++ is a partially object-


language oriented programming language

It follows a top-down approach It follows a bottom-up approach

C doesn’t support function or operator C++ supports function as well as function


overloading overloading

C language doesn’t support virtual and C++ language supports both virtual and
friend function friend functions.

C language has 32 keywords C++ language contains 52 keywords

2. What are classes and objects in C++?


A class is like a blueprint of an object. You define objects as an instance of a class.
Once it creates the object, then it can operate on both data members and member
functions.

3. What are access modifiers?

You use access modifiers to define accessibility for the class members. It defines how
to access the members of the class outside the class scope.

There are three types of access modifiers:

 Private

 Public

 Protected

4. Difference between equal to (==) and assignment operator(=)?

The equal to operator == checks whether two values are equal or not. If equal, then it’s
true; otherwise, it will return false.

The assignment operator = allots the value of the right-side expression to the left
operand.

7. Which among the following operators cannot be overloaded?

1. -

2. +

3. ?:

4. %

3 - ?: operator cannot be overloaded because it is not syntactically possible.

9. Discuss the difference between prefix and postfix?

In prefix (++i), first, it increments the value, and then it assigns the value to the
expression.
In postfix (i++), it assigns the value to the expression, and then it increments the
variable's value.

11. What is std in C++?

1. std is a standard class in C++

2. std is a standard file reading header

3. std is a standard header file

4. std is a standard namespace

4 - std is a standard namespace in C++

12. What are the four different data types in C++?

 Primitive/Basic: Char, int, short, float, double, long, bool, etc.

 Derived: Array, pointer, etc.

 Enumeration: Enum

 User-defined: Structure, class, etc.

15. Compare compile time and runtime polymorphism.

Compile-time Polymorphism Runtime Polymorphism

The method to be executed is


The method to be executed is known at compile time.
known at run time. The compiler
And the call is resolved by the compiler.
does not resolve the call.

Provides quicker execution because it is known at the Provides slower execution


because it is known at the run
compile time.
time.

Achieved by operation or function overloading. Achieved by function overriding.

17. What is a virtual function?

A member function in the base class redefined in a derived class is a virtual function. It
is declared using the virtual keyword. It ensures that the correct function is called for an
object, irrespective of the type of reference/pointer used for the function call. Virtual
functions are mainly used for runtime polymorphism.

18. What do you understand about friend class and friend function?

A friend class is capable of accessing protected, private, and public members of other
classes in which it is declared as friends.

A friend function can also access protected, private, and public members, but it is not a
member function.

19. What are the three different types of C++ access specifiers?

 Public: All member functions and data members are accessible outside the class.

 Protected: All member functions and data members are accessible within the class
and to the derived class.

 Private: All member functions and data members cannot be accessed outside the
class.

23. What is an abstract class? When is it used?


An abstract class is a class whose objects cannot be created. It serves as a parent for
the derived classes. Placing a pure virtual function in the class makes it an abstract
class.

26. When is void() return type used?

You use the void() return type when you don’t want to return any value. It specifies that
the function doesn’t return a value. A function with a void return type completes its task
and then returns the control to the caller.

27. What is call by value and call by reference in C++?

In the call by value method, you pass the copies of actual parameters to the function's
formal parameters. This means if there is any change in the values inside the function,
then that change will not affect the actual values.

In the call-by-reference method, the reference or address of actual parameters is sent to


the function's formal parameters. This means any change in values inside the function
will be reflected in the actual values.

28. What is an inline function?

An inline function when called expands in line. When you call this function, the whole
code of the inline function gets inserted or substituted at the inline function call.

Syntax:

Inline return-type function-name(parameters)

29. What are pointers in C++?

Pointers are the variables that store the memory address of another variable. The type
of the variable must correspond with the type of pointer.

Syntax: type *name


30. What is a scope resolution operator?

A scope resolution operator is represented as ::

This operator is used to associate function definition to a particular class.

The scope operator is used for the following purposes:

 To access a global variable when you have a local variable with the same name.

 To define a function outside the class.

35. Discuss the difference between new and malloc.

New Malloc

new is an operator malloc() is a function

The malloc function doesn’t call


It calls the constructor
the constructor

There is no need to specify memory size while using You have to specify the memory
new() size

new operator can be overloaded malloc() can never be


overloaded

36. What is operator overloading?

Operator overloading is a mechanism in which a special meaning is given to an


operator.

For example, you can overload the ‘+’ operator in a class-like string to concatenate two
strings by only using ‘+.’

41. What is a friend function?

You can define a friend function as a function that can access private, public and protect
members of the class. You declare the friend function with the help of the friend
keyword. You declare this function inside the class.

54. How is a shallow copy different from a deep copy?

Shallow Copy Deep Copy

It makes a fresh and separate


It stores the references of objects to the
copy of an entire object and its
original memory address.
unique memory address.

Faster Comparatively slower


It doesn’t reflect changes made
It reflects changes made to the
to the new/copied object in the
new/copied object in the original object.
original object.

You might also like