C++ Operators
C++ Operators Interview with follow-up questions
Interview Question Index
- Question 1: What are the different types of operators available in C++?
- Follow up 1 : Can you explain the difference between unary and binary operators?
- Follow up 2 : How does the ternary operator work in C++?
- Follow up 3 : Can you give an example of using the assignment operator in C++?
- Question 2: How does the precedence of operators work in C++?
- Follow up 1 : What happens when operators of the same precedence appear in an expression?
- Follow up 2 : Can you give an example of an expression where operator precedence plays a crucial role?
- Follow up 3 : How can parentheses be used to change the precedence of operators in an expression?
- Question 3: What is the purpose of the 'sizeof' operator in C++?
- Follow up 1 : Can you give an example of using the 'sizeof' operator?
- Follow up 2 : Can 'sizeof' be used on user-defined types?
- Follow up 3 : What is the return type of the 'sizeof' operator?
- Question 4: What are bitwise operators in C++?
- Follow up 1 : Can you explain the difference between bitwise AND and bitwise OR operators?
- Follow up 2 : How does the bitwise NOT operator work?
- Follow up 3 : Can you give an example of using bitwise shift operators?
- Question 5: What are the special operators in C++?
- Follow up 1 : What is the purpose of the scope resolution operator?
- Follow up 2 : How does the member selection operator work?
- Follow up 3 : Can you give an example of using the new and delete operators?
Question 1: What are the different types of operators available in C++?
Answer:
C++ provides several types of operators, including arithmetic operators, relational operators, logical operators, bitwise operators, assignment operators, and more.
Follow up 1: Can you explain the difference between unary and binary operators?
Answer:
Unary operators operate on a single operand, while binary operators operate on two operands. Unary operators include increment (++), decrement (--), logical negation (!), and more. Binary operators include addition (+), subtraction (-), multiplication (*), division (/), and more.
Follow up 2: How does the ternary operator work in C++?
Answer:
The ternary operator in C++ is a conditional operator that takes three operands. It is written in the form: condition ? expression1 : expression2
. If the condition is true, the operator evaluates to expression1; otherwise, it evaluates to expression2.
Follow up 3: Can you give an example of using the assignment operator in C++?
Answer:
Certainly! Here's an example of using the assignment operator in C++:
int x = 5;
x += 3; // equivalent to x = x + 3
Question 2: How does the precedence of operators work in C++?
Answer:
In C++, operators have different levels of precedence, which determines the order in which they are evaluated in an expression. Operators with higher precedence are evaluated before operators with lower precedence. For example, the multiplication operator (*) has higher precedence than the addition operator (+), so in the expression 2 + 3 * 4
, the multiplication is performed first, resulting in 2 + 12
, which equals 14
.
Follow up 1: What happens when operators of the same precedence appear in an expression?
Answer:
When operators of the same precedence appear in an expression, the associativity of the operators determines the order in which they are evaluated. Most operators in C++ have left-to-right associativity, which means they are evaluated from left to right. For example, in the expression 2 + 3 - 4
, the addition and subtraction operators have the same precedence, but since they have left-to-right associativity, the addition is performed first, resulting in 5 - 4
, which equals 1
.
Follow up 2: Can you give an example of an expression where operator precedence plays a crucial role?
Answer:
Sure! Consider the expression 2 + 3 * 4
. If the operators were evaluated from left to right without considering precedence, the result would be 10
. However, due to the higher precedence of the multiplication operator, the expression is evaluated as 2 + (3 * 4)
, resulting in 2 + 12
, which equals 14
.
Follow up 3: How can parentheses be used to change the precedence of operators in an expression?
Answer:
Parentheses can be used to explicitly specify the order in which operators are evaluated in an expression, overriding the default precedence and associativity. For example, in the expression (2 + 3) * 4
, the addition inside the parentheses is performed first, resulting in 5 * 4
, which equals 20
.
Question 3: What is the purpose of the 'sizeof' operator in C++?
Answer:
The 'sizeof' operator in C++ is used to determine the size in bytes of a data type or a variable. It returns the size as an unsigned integer value.
Follow up 1: Can you give an example of using the 'sizeof' operator?
Answer:
Sure! Here's an example:
#include
int main() {
int arr[5];
std::cout << "Size of arr: " << sizeof(arr) << " bytes" << std::endl;
return 0;
}
Output:
Size of arr: 20 bytes
Follow up 2: Can 'sizeof' be used on user-defined types?
Answer:
Yes, 'sizeof' can be used on user-defined types. It returns the size of the object or type in bytes.
Follow up 3: What is the return type of the 'sizeof' operator?
Answer:
The return type of the 'sizeof' operator is 'size_t', which is an unsigned integer type defined in the header.
Question 4: What are bitwise operators in C++?
Answer:
Bitwise operators in C++ are used to perform operations on individual bits of binary numbers. These operators work at the bit level and are used to manipulate and extract specific bits from integers. The bitwise operators in C++ are:
Bitwise AND (&): This operator performs a bitwise AND operation between the corresponding bits of two integers. It returns a new integer with bits set to 1 only if both corresponding bits are 1.
Bitwise OR (|): This operator performs a bitwise OR operation between the corresponding bits of two integers. It returns a new integer with bits set to 1 if at least one of the corresponding bits is 1.
Bitwise XOR (^): This operator performs a bitwise XOR (exclusive OR) operation between the corresponding bits of two integers. It returns a new integer with bits set to 1 if the corresponding bits are different.
Bitwise NOT (~): This operator performs a bitwise NOT operation on an integer. It flips all the bits of the integer, i.e., it changes 0 to 1 and 1 to 0.
Left Shift (<>): This operator shifts the bits of the left operand to the right by a specified number of positions. The vacant positions are filled with the sign bit (for signed integers) or with zeros (for unsigned integers).
Follow up 1: Can you explain the difference between bitwise AND and bitwise OR operators?
Answer:
The bitwise AND (&) operator performs a bitwise AND operation between the corresponding bits of two integers. It returns a new integer with bits set to 1 only if both corresponding bits are 1. On the other hand, the bitwise OR (|) operator performs a bitwise OR operation between the corresponding bits of two integers. It returns a new integer with bits set to 1 if at least one of the corresponding bits is 1.
Here's an example to illustrate the difference:
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
int resultAnd = a & b; // Binary: 0001 (1 in decimal)
int resultOr = a | b; // Binary: 0111 (7 in decimal)
Follow up 2: How does the bitwise NOT operator work?
Answer:
The bitwise NOT (~) operator performs a bitwise NOT operation on an integer. It flips all the bits of the integer, i.e., it changes 0 to 1 and 1 to 0. This operator is a unary operator, meaning it operates on a single operand.
Here's an example to illustrate how the bitwise NOT operator works:
int a = 5; // Binary: 0101
int result = ~a; // Binary: 1010 (-6 in decimal)
Follow up 3: Can you give an example of using bitwise shift operators?
Answer:
Bitwise shift operators in C++ are used to shift the bits of an integer to the left or right by a specified number of positions. There are two types of bitwise shift operators: left shift (<>).
Here's an example to illustrate the usage of bitwise shift operators:
int a = 5; // Binary: 0101
int resultLeftShift = a << 2; // Binary: 010100 (20 in decimal)
int resultRightShift = a >> 1; // Binary: 0010 (2 in decimal)
Question 5: What are the special operators in C++?
Answer:
Some of the special operators in C++ include:
- Arithmetic operators: +, -, *, /, %
- Relational operators: ==, !=, >, =, <=
- Logical operators: &&, ||, !
- Bitwise operators: &, |, ^, ~, <>
- Assignment operators: =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
- Increment and decrement operators: ++, --
- Conditional operator: ? :
- Sizeof operator
- Comma operator
- Pointer operators: *, ->
- Scope resolution operator: ::
- Member selection operator: ., ->
- New and delete operators
- Type casting operators: static_cast, dynamic_cast, reinterpret_cast, const_cast
Follow up 1: What is the purpose of the scope resolution operator?
Answer:
The scope resolution operator (::) is used to access global variables, functions, or static members of a class. It allows you to specify the scope in which a particular identifier is defined.
For example, if you have a global variable named 'x' and a local variable with the same name inside a function, you can use the scope resolution operator to access the global variable as follows:
int x = 10;
void foo() {
int x = 20;
cout << ::x; // Accessing the global variable
}
Follow up 2: How does the member selection operator work?
Answer:
The member selection operator (.) is used to access members of an object or a structure. It is used when you have an instance of a class or a structure and you want to access its member variables or member functions.
For example, if you have a class named 'Person' with a member variable 'name', you can access it using the member selection operator as follows:
class Person {
public:
string name;
};
Person p;
p.name = "John"; // Accessing the member variable
Follow up 3: Can you give an example of using the new and delete operators?
Answer:
Yes, the new and delete operators are used for dynamic memory allocation and deallocation in C++.
The new operator is used to allocate memory for an object or an array, and it returns a pointer to the allocated memory. Here's an example:
int* p = new int; // Allocating memory for an integer
*p = 10;
delete p; // Deallocating memory
The delete operator is used to deallocate memory that was previously allocated with the new operator. It frees the memory and makes it available for reuse.
Note: It's important to always deallocate memory that was allocated with the new operator to avoid memory leaks.