C/C++ Questions

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Difference between C/C++

- C is a procedural programming language, does not support classes and objects - C++ is a multi-paradigm language that supports procedural, object-oriented, and generic programming, C++ extends C with OOP support (classes, objects, inheritence, polymorphism, abstraction, and encapsulation)

Discuss the role of constructors and deconstructors in C++.

- Constructors are special member functions that initialize objects of a class. - Deconstructors are special member functions that clean up resources when objects go out of scope.

What is a header file in C/C++? Why are they used?

- Header files are files that contain declarations of functions, classes, and variables. They allow code to be shared across multiple files

What are namespaces in C++? Why are they useful?

- Namespaces are used to organize code into logical groups. - They help prevent name conflicts.

Explain Pass by Value vs Pass by Reference

- Pass by value creates a copy of the argument, does not modify the original value - Pass by reference passes the address of the argument, allows modifications to original value.

What are pointers in C/C++

- Pointers are variables that store memory addresses. - They allow direct manipulation of memory and dynamic memory allocation.

What is OOP and how does C++ support it?

- Programing paradigm based on concepts of objects, which can contain data and code - C++ supports OOP through abstraction, encapsulation, inheritance, polymorphism,

Explain stack and heap memory

- Stack: the OS continuously manages and uses to store local variables in LIFO order - Heap: type of dynamic memory allocation used for storing objects and data structures that require longer lifespan than stack memory

What are virtual functions and how are they used in C++?

- Virtual functions allow a function in a base class to be overridden in a derived class. - They enable runtime polymorphism.

What is the difference between delete and delete[] in C++?

- delete is used to deallocate memory allocated with new, while delete[] is used to deallocate memory allocated with new[] for arrays.

Describe the differences between malloc() and new in C++ for memory allocation

- malloc() is a function in C for dynamic memory allocation, malloc() returns a void pointer (void*) . - new is an operator in C++, new returns a typed pointer.

How does C++ handle exceptions? Describe try, catch, and throw.

-Exception handling allows for graceful error handling. try, catch, and throw are keywords used for exception handling. - Try: Tries running the code inside the try brackets - Catch: If an error happens, you would catch the error instead of the program ending - Throw: throws the exception that is caught to the user

Compiletime-Polymorphism

Function Overloading: functions by using the same name but with different number of arguments and different types of arguments Operator overloading: overloading operators (+,-,*) to do what the developers want

Example of Pass by Reference

void passByReference(int& x) { x = 20; // Changes original value }

Example of Pass by Value

void passByValue(int x) { x = 20; // Changes local copy, not original value }

What is function overloading? How does it work in C++?

Allows multiple functions to have the same name, but each function has a different parameters (number or types of parameters) Ex. print function with a parameter of a string vs print function with a parameter of an int

What is inheritance? Why is it useful? Give an example

Concept of deriving a class that can inherit the properities and behaviors of a parent or base class Encourages code reusability, maintainability, polymorphism Ex. Shape parent class, circle and rectangle child classes that inherit a calculateArea() method

What is Abstraction? Why is it useful? Give an example

Concept of hiding the complex implementation details and only showing the essential features Ex. Users can use a shape class without needing to know the details for the Circle or Rectangle classes

What is Encapsulation? Why is it useful? Give an example:

Concept of storing data and methods in objects. Helpful in restricting access to keep from accidental modification from outside Ex. Person object with name, age, and occupation attributes

What is polymorphism? Why is it useful? Give an example

Concept that a class can be treated as instances of the parent class rather than the class itself. Uses method overriding from method in the superclass. Encourages code reusability and maintainablilty Ex. Animal is Animal, Dog is Animal, Tiger is Animal

Explain the role of the const keyword in C/C++. When would you use them?

Makes a variable as immutable. A good use case would be for variables that should not change after initialization Ex. const int MAX_USERS = 10000

Runtime-Polymorphism

Method Overriding: process of creating a new definition of a function in a derived class that is already defined in its base class. Method names and parameters must be the same Virtual Functions: a member function that is declared as virtual in the base class and can be overwritten in the derived classes that inherit the base class

Discuss synchronization mechanisms.

Mutexes are important for multithreading because it makes sure that the threads can't run over eachother.

What is a reference variable in C++? How is it different from a pointer?

Reference: An alias for another pre-existing variable and does not have a memory of its own. - Pointers hold the address of a variable, can hold their own memory Ex. int* p = new int; - Cannot be null and cannot reassign to another piece of memory

Describe the role of sizeof() operator in C/C++.

Returns the amount of bytes of a type or object

What are the differences between struct and class in C++?

Struct members are public by default where class members are private by default


संबंधित स्टडी सेट्स

Brunner Chapter 70 Oncologic and Degenerative Neuro Disorders

View Set

Thema #16: Termine vereinbaren und verschieben

View Set

Psych 2070 Chapter 9 (social and emotional development)

View Set

Regular Homework #6 Bio 111 - Gen Mager BSU (Mastering Biology)

View Set