20 C++ questions and answers
Q: What is the difference between a variable and a constant in C++?
A: A variable can be assigned a new value during program execution, while a constant has a fixed value that cannot be changed.
Q: What is the syntax for declaring a variable in C++?
A: The syntax for declaring a variable in C++ is: <data_type> <variable_name>;
Q: What are the basic data types in C++?
A: C++ supports several basic data types, including int, float, double, char, and bool.
Q: What is the difference between an int and a float data type?
A: An int data type stores whole numbers, while a float data type stores decimal numbers with a limited precision.
Q: How can you determine the size of a data type in C++?
A: You can use the sizeof
operator to determine the size of a data type in bytes. For example, sizeof(int)
returns the size of an int.
Q: What is the purpose of an if statement in C++?
A: An if statement allows you to perform certain actions based on a condition. If the condition is true, the code within the if block is executed.
Q: What is the difference between the pre-increment and post-increment operators?
A: The pre-increment operator (++i) increments the value of the variable and returns the updated value, while the post-increment operator (i++) returns the current value and then increments it.
Q: How do you define a function in C++?
A: The syntax for defining a function in C++ is: <return_type> <function_name>(<parameter_list>) { <function_body> }
Q: What is recursion in programming?
A: Recursion is a technique where a function calls itself to solve a problem. It involves breaking a problem into smaller subproblems until a base case is reached.
Q: What is the purpose of the return statement in a function?
A: The return statement is used to return a value from a function back to the caller. It also terminates the function’s execution.
Q: What is a pointer in C++?
A: A pointer is a variable that stores the memory address of another variable. It allows you to indirectly access and manipulate the value of that variable.
Q: How do you declare a pointer variable in C++?
A: The syntax for declaring a pointer variable in C++ is: <data_type>* <pointer_name>;
Q: What is the difference between the * and & operators when working with pointers?
A: The * operator is used to declare a pointer and to dereference a pointer (access the value it points to). The & operator is used to get the address of a variable.
Q: What is the purpose of the while loop in C++?
A: The while loop repeatedly executes a block of code as long as a given condition is true. It is used when the number of iterations is not known in advance.
Q: What is the switch statement used for in C++?
A: The switch statement allows you to select one of many possible paths of execution based on the value of a variable or an expression.
Q: What is the scope of a variable in C++?
A: The scope of a variable defines where it can be accessed within a program. Variables can have local scope (limited to a specific block) or global scope (accessible throughout the program).
Q: Can you pass an array to a function in C++?
A: Yes, you can pass an array to a function in C++. You can either pass it by reference or by pointer.
Q: What is the purpose of the break statement in a loop or switch statement?
A: The break statement is used to exit a loop or switch statement prematurely. It transfers control to the statement immediately following the loop or switch.
Q: How do you dynamically allocate memory in C++?
A: You can dynamically allocate memory in C++ using the new
keyword. For example, int* ptr = new int;
allocates memory for an integer.
Q: What is function overloading in C++?
A: Function overloading allows you to define multiple functions with the same name but different parameter lists. The compiler determines which function to call based on the arguments used.