Expert c++ quiz

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

### Expert C++ Questions

21. What is the use of the `static` keyword in C++?


- A) To declare a variable that can only be used within the current function
- B) To declare a global variable
- C) To declare a variable that retains its value between function calls
- D) To declare a constant variable
- Answer: C) To declare a variable that retains its value between function calls

22. Which of the following is a correct way to create a pointer to an integer variable in C++?
- A) int *p;
- B) int &p;
- C) int p*;
- D) int p&;
- Answer: A) int *p;

23. What is the output of the following code?


```cpp
int x = 5;
int y = 10;
swap(x, y);
cout << x << " " << y;
```
- A) 5 10
- B) 10 5
- C) Error
- D) Unde ned
- Answer: C) Error

24. Which of the following operators cannot be overloaded in C++?


- A) +
- B) -
- C) ::
- D) /
- Answer: C) ::

25. What is the purpose of the `this` pointer in C++?


- A) To refer to the current object
- B) To refer to the base class
- C) To refer to the derived class
- D) To refer to a global variable
- Answer: A) To refer to the current object

26. What is the output of the following code?


```cpp
int x = 10;
int &y = x;
y = 20;
cout << x;
```
- A) 10
- B) 20
- C) Error
- D) Unde ned
- Answer: B) 20

27. What is the di erence between `delete` and `delete[]` in C++?


- A) `delete` is used for single objects, `delete[]` is used for arrays
- B) `delete` is used for arrays, `delete[]` is used for single objects
- C) `delete` frees memory, `delete[]` does not
fi
fi
ff
- D) `delete[]` frees memory, `delete` does not
- Answer: A) `delete` is used for single objects, `delete[]` is used for arrays

28. Which of the following is true about destructors in C++?


- A) A destructor has the same name as the class but with a tilde (~) pre x
- B) A destructor can have parameters
- C) A destructor can have a return type
- D) A destructor is called manually
- Answer: A) A destructor has the same name as the class but with a tilde (~) pre x

29. What is the output of the following code?


```cpp
int x =

5;
cout << (x > 3 ? x < 4 ? 10 : 8 : 7);
```
- A) 10
- B) 8
- C) 7
- D) Error
- Answer: B) 8

30. Which of the following is not a valid way to declare a string in C++?
- A) string str = "Hello";
- B) char str[] = "Hello";
- C) char *str = "Hello";
- D) String str = "Hello";
- Answer: D) String str = "Hello";
fi
fi

You might also like