Career Tips
Top 50 C and C++ Interview Questions and Answers
Q-1: What is the syntax to define a macro in C?
Answer: #define MACRO_NAME value
What is the difference between malloc() and calloc()?
Answer: malloc() allocates memory but does not initialize it, whereas calloc() allocates memory and initializes all bits to zero.
Explain the purpose of the sizeof operator in C.
Answer: sizeof operator returns the size in bytes of its operand.
What is the significance of the static keyword in C?
Answer: In C, static has several uses. It can be used to declare variables with a fixed memory location, make functions and variables local to a file, and preserve variable values between function calls.
What is a pointer in C?
Answer: A pointer is a variable that holds the memory address of another variable.
Explain the difference between ++i and i++.
Answer: ++i is the pre-increment operator, which increments the value of i before using it. i++ is the post-increment operator, which increments the value of i after using it.
What is the purpose of the malloc() function in C?
Answer: malloc() is used to dynamically allocate memory during program execution.
What is a structure in C?
Answer: A structure is a collection of variables of different data types under a single name.
How do you access structure members in C?
Answer: You access structure members using the dot (.) operator.
What is a header file in C?
Answer: A header file in C contains function declarations and macro definitions to be shared across multiple source files.
What is the difference between malloc() and realloc()?
Answer: malloc() is used for memory allocation, while realloc() is used for resizing dynamically allocated memory.
Explain the #include directive in C.
Answer: #include directive is used to include contents of another file into the source file during compilation.
What does void indicate in function declarations in C?
Answer: void indicates that the function does not return any value.
What is the purpose of the const keyword in C?
Answer: The const keyword is used to define constants or to indicate that a variable’s value cannot be changed.
What are the storage classes in C?
Answer: The storage classes in C are auto, register, static, and extern.
What is a file pointer in C?
Answer: A file pointer is a pointer that points to a file. It is used to perform operations like reading, writing, and positioning within the file.
How do you dynamically allocate memory in C++?
Answer: In C++, you use the new operator to dynamically allocate memory.
What is the purpose of the this pointer in C++?
Answer: The this pointer in C++ is used to refer to the current object.
What is a virtual function in C++?
Answer: A virtual function is a member function of a class that can be overridden in derived classes.
Explain the concept of inheritance in C++.
Answer: Inheritance is a feature of object-oriented programming where a class inherits properties and behavior (methods) from another class.
What is the purpose of the delete operator in C++?
Answer: The delete operator in C++ is used to deallocate memory allocated dynamically using the new operator.
What is function overloading in C++?
Answer: Function overloading is a feature in C++ that allows multiple functions with the same name but different parameter lists to be defined.
What is the difference between private, protected, and public access specifiers in C++ classes?
Answer: private members are accessible only within the same class, protected members are accessible within the same class and derived classes, and public members are accessible from anywhere.
What is a reference variable in C++?
Answer: A reference variable is an alias for another variable.
Explain the difference between new and malloc() in C++.
Answer: new is an operator in C++ for dynamic memory allocation, while malloc() is a function in C.
What is a namespace in C++?
Answer: A namespace in C++ is a declarative region that provides a scope to the identifiers within it.
What is the difference between struct and class in C++?
Answer: In C++, the only difference between struct and class is that members of a struct are by default public, while members of a class are by default private.
What is an abstract class in C++?
Answer: An abstract class in C++ is a class that has at least one pure virtual function.
What is the std::cin object in C++ used for?
Answer: std::cin is used for input operations in C++.
What is the std::cout object in C++ used for?
Answer: std::cout is used for output operations in C++.
What is the purpose of the friend keyword in C++?
Answer: The friend keyword in C++ allows a function or another class to access private and protected members of a class.
What is the difference between delete and delete[] in C++?
Answer: delete is used to deallocate memory for a single object allocated using new, while delete[] is used to deallocate memory for arrays allocated using new[].
What is a constructor in C++?
Answer: A constructor is a special member function of a class that is automatically called when an object of that class is created.
What is a destructor in C++?
Answer: A destructor is a special member function of a class that is automatically called when an object goes out of scope or is deleted.
What is operator overloading in C++?
Answer: Operator overloading is a feature in C++ that allows operators to be redefined for user-defined types.
What is the purpose of the inline keyword in C++?
Answer: The inline keyword in C++ is used to suggest to the compiler that a function should be expanded in place rather than called.
Explain the try, catch, and throw keywords in C++.
Answer: try is used to enclose code that might throw an exception, catch is used to handle exceptions, and throw is used to throw an exception.
What is a template in C++?
Answer: A template in C++ is a mechanism for generic programming that allows functions and classes to operate with generic types.
What is the difference between std::vector and std::array in C++?
Answer: std::vector is a dynamically resizable array, while std::array is a fixed-size array.
What is a lambda function in C++?
Answer: A lambda function in C++ is an anonymous function defined using a concise syntax.
What is the purpose of the std::move function in C++?
Answer: The std::move function in C++ is used to convert an lvalue into an rvalue, enabling move semantics.
Explain the difference between const and constexpr in C++.
Answer: const is used to declare variables that cannot be modified, while constexpr is used to declare constants that must be evaluated at compile time.
What is the difference between std::shared_ptr and std::unique_ptr in C++?
Answer: std::shared_ptr allows multiple pointers to own the same object, while std::unique_ptr ensures that only one pointer owns the object.
What is a smart pointer in C++?
Answer: A smart pointer in C++ is a class that wraps a raw pointer and provides automatic memory management.
What is the purpose of the std::mutex class in C++?
Answer: The std::mutex class in C++ is used to provide mutual exclusion in multithreaded programs.
What is the purpose of the std::condition_variable class in C++?
Answer: The std::condition_variable class in C++ is used to block a thread until notified to resume.
What is a lambda capture in C++?
Answer: A lambda capture in C++ allows lambda functions to capture and use variables from their enclosing scope.
What is the purpose of the std::tuple class in C++?
Answer: The std::tuple class in C++ is used to create a heterogeneous collection of elements.
What is the std::initializer_list class in C++ used for?
Answer: std::initializer_list is used to initialize objects of standard library containers.
What is a functor in C++?
Answer: A functor in C++ is an object that can be called as if it were a function. It is often implemented as a class with an overloaded operator().
These questions cover various aspects of both C and C++ programming languages, ranging from basic concepts to advanced features.
-
Exam Schedule 20235 months ago
Avadh University M.A, M.Sc, M.Com First Semester Exam Time Table 2024
-
Current Affairs3 months ago
Current Affairs February 2024
-
Current Affairs9 months ago
Latest Current Affairs from 9 to 15 August 2023
-
Current Affairs1 year ago
Latest Current Affairs from 5 April to 11 April 2023
-
University3 months ago
वीर बहादुर सिंह पूर्वांचल यूनिवर्सिटी में 2024 में मिलेंगी इतनी छुट्टियां
-
Current Affairs9 months ago
Latest Current Affairs from 26 July to 1 August 2023
-
Current Affairs9 months ago
Latest Current Affairs from 19 to 25 July 2023
-
Current Affairs9 months ago
Latest Current Affairs from 2 to 8 August 2023