[go: up one dir, main page]

0% found this document useful (0 votes)
9 views9 pages

C

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)
9 views9 pages

C

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/ 9

C++

1. Difference between c and C++?


2. What kind of storage class in C++? Also Give example.
3. What is a variable?
4. What is data type?
5. What is typecas ng?
6. How to reuse code in C++?
7. What is a pointer?
8. What is user defined data type?
9. What is func on?
10. Can func on return mul ple values at a me?
11. Difference between for loop, while and do while loop?
12. What is applica on of switch case?
13. What is the importance of main() in C++?
14. What are the jumping statements?
15. What are the extrac on and inser on operator in C++?
16. What is call by value and call by reference and call by address?
17. What is excep on handling in C++?
18. What do you mean by file handling?
19. How to allocate dynamic memory in C and C++?
20. What is stack overflow error and when does it occur?
21. What is namespace?
22. Explain the diamond problem in C++.
23. What is the use of the inline keyword in C++?
24. What is a friend func on in C++?
25. What is the difference between struct and class in C++?
26. What is the difference between malloc() and new in C++?
27. What is dynamic memory alloca on in C++?
28. What are the differences between pointers and references in C++?
29. What is the difference between delete and delete[] in C++?
30. What is the difference between stack and heap memory in C++?
31. How is excep on handling implemented in C+ +?
32. What is the purpose of the typedef keyword in C++?
33. What are the uses of the const keyword in C++?
34. Explain memory leaks in C++ and how to avoid them.
35.Explain the use of smart pointers in C++.
36. What is the use of the "mutable" keyword in C++?
37. What are key words in C++?
38.What are data types in C++?

6. What are preprocessor direc ves in C++?


7. What is the purpose of the explicit keyword in C++?
14. What is a virtual func on in C++?
15. What is a template in C++?
26. What are rvalue references in C++?
33. How does C++ handle memory management differently than Java?
23. What is the use of the inline keyword in C++?
The inline keyword tells the compiler to replace a func on call with the
actual code of the func on at the point where it's called. This can help
improve performance by reducing the overhead of func on calls,
especially for small, func ons that are called o en.

24. What is a friend func on in C++?


A friend func on in C++ is a func on that is not part of a class, but it is
allowed to access the private and protected members of that class.
It's declared inside the class using the friend keyword but works from
outside the class.

25. What is the difference between struct and class in C++?


In C++, the key difference is that in a struct, members are public by
default, while in a class, they are private by default.
Struct mainly used for simple data grouping but class is used for complex
objects that not only hold data but also include func ons to manipulate
that data.
26. What is dynamic memory alloca on in C++?
Dynamic memory alloca on in C++ is the process of alloca ng memory
at run me instead of at compile me.
Dynamic memory is allocated using operator like New and func on like
malloc() and this allocated memory released using delete keyword and
free() func on.
It allows programs to allocate memory based on run me requirements.
This means we can create data structures that can grow or shrink as
needed, such as linked lists, trees, and dynamic arrays.
Memory can be allocated only when necessary, This helps prevent
memory wastage.
27. What are the differences between pointers and references in C++?
Pointers Declared using the * symbol and can be reassigned to different
memory addresses.
References Declared with the & symbol, must be assigned it to an
exis ng variable when created, and cannot be changed to refer to
another object.
Pointers can be set to null, meaning they can point to nothing.
References cannot be null; they must always refer to a valid object.
28. What is the difference between delete and deletell in C++?
delete: Used to free memory allocated for a single object created with
new.
delete[]: Used to free memory allocated for an array of objects created
with new[]

29. What is the difference between stack and heap memory in C++?
Stack memory is automa cally managed and used for local variables,
while heap memory is manually managed and used for dynamic memory
alloca on. Stack memory is faster and has a limited size, whereas heap
memory is larger and allows for longer life mes of variables.
30. How is excep on handling implemented in C+ +?
Excep on handling in C++ is a way to manage errors during the execu on
of a program. Instead of allowing the program to crash when an error
occurs. excep on handling helps to "catch" the error and manage it, so
the program can con nue running properly.
For excep on handling try block is used.
In try block we place code that might throw an error.
The throw statement is used to indicate that an error or excep on has
happened.
In catch block we handle the excep on and define what to do if an error
happens.
31. What is the purpose of the typedef keyword in C++?
The typedef keyword in C++ is used to give a new name (or alias) to an
exis ng data type, making the code more readable or easier to maintain.

32. What are the uses of the const keyword in C++?


The const keyword in C++ is used to define constants, which means that
the value cannot be changed a er it is ini alized.
const int MAX_SIZE = 100;
33. Explain memory leaks in C++ and how to avoid them.
Memory leakage occurs in C++ when program dynamically allocate
memory by using new keyword or malloc and forgets to deallocate the
memory by using delete or operator free.
We can avoid them by always releasing memory using delete, using
smart pointers.
34. Explain the use of smart pointers in C++?
Smart pointers automa cally manage memory and free it when it's no
longer needed.
 unique_ptr
 shared_ptr
 weak_ptr

35. What is the use of the "mutable" keyword in C++?


In C++, the
mutable keyword
The mutable keyword in C++ allows a class member variable to be
is used to allow a modified even if the enclosing object is declared as const. This can be
member of a class useful when you want to allow certain variables to change while keeping
to be modified
even if the object the overall object logically constant.
of the class is
const

36. What are key words in C++?


List of Common C++ Keywords:
1. Control Flow:
o if: Condi onal statement.
o else: Defines an alternate branch in an if statement.
o switch: Mul -way branch statement.
o case: A branch in a switch statement.
o default: The default branch in a switch statement.
o break: Exit a loop or switch statement.
o con nue: Skip the current itera on of a loop.
o return: Exit a func on and op onally return a value.
Modifiers:
 const: Defines a constant value.
 vola le: Indicates that a variable may change at any me.
 sta c: Limits the visibility of a variable or func on.
 extern: Declares a variable or func on that is defined elsewhere.
 signed / unsigned: Defines the sign of integral types.
Class and Object-Oriented Programming:
 class: Defines a class.
 struct: Defines a structure.
 public: Access specifier for class members.
 private: Access specifier restric ng access to class members.
 protected: Access specifier that allows access to derived classes.
 virtual: Declares a virtual func on for dynamic polymorphism.
 override: Indicates that a method is intended to override a virtual
method in a base class.
 final: Prevents further deriva on of a class or overriding of a method.
Memory Management:
 new: Allocates memory dynamically.
 delete: Frees dynamically allocated memory.
 new[]: Allocates an array dynamically.
 delete[]: Frees a dynamically allocated array.
Excep on Handling:
 try: Starts a block of code to be tested for excep ons.
 catch: Defines a block of code to handle excep ons.
 throw: Used to signal the occurrence of an excep on.
Namespace and Type Cas ng:
 namespace: Defines a scope for iden fiers.
 using: Brings names from a namespace into current scope.
 sta c_cast: Performs a safe type conversion.
 dynamic_cast: Performs a safe downcast of polymorphic types.
 reinterpret_cast: Converts any pointer type to any other pointer type.
 const_cast: Casts away the constness of a variable.
Other Keywords:
 this: A pointer to the current object.
 sizeof: Returns the size of a data type or object.
 typeid: Returns the type informa on at run me.
 alignas: Specifies the alignment of a variable.
 alignof: Returns the alignment of a type.
 noexcept: Specifies whether a func on can throw excep ons.

37. What are the data types in C++?


Basic Data Types:
 int: Stores integers (whole numbers), e.g., 5 or -10.
 float: Stores single-precision floa ng-point numbers (decimals), e.g.,
3.14.
 double: Stores double-precision floa ng-point numbers, e.g.,
3.14159.
 char: Stores a single character, e.g., 'A'.
 bool: Stores true or false values.
Derived Data Types:
 Arrays, pointers, func ons, and references.
User-Defined Data Types:
 struct, class, and enum allow crea ng custom types.
Void:
 Represents no data or used in func ons that do not return a value."

Virtual Function

Declaring a function with the keyword virtual in the base class allows the derived class to override
it.

You might also like