0% found this document useful (0 votes)
21 views11 pages

C++ Common Questions

The document contains a series of multiple-choice questions and answers related to C++ programming concepts, including data types, variable declaration, operators, memory management, class definitions, and control structures. Each question is followed by four answer options, with the correct answer indicated. The content serves as a quiz or study guide for individuals learning or testing their knowledge of C++.

Uploaded by

x21e0day
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views11 pages

C++ Common Questions

The document contains a series of multiple-choice questions and answers related to C++ programming concepts, including data types, variable declaration, operators, memory management, class definitions, and control structures. Each question is followed by four answer options, with the correct answer indicated. The content serves as a quiz or study guide for individuals learning or testing their knowledge of C++.

Uploaded by

x21e0day
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1. Which of the following is not a fundamental data type in C++?

A. int
B. float
C. char
D. string
Answer: D

2. What is the correct way to declare a variable in C++?


A. var x;
B. x = 10;
C. int x;
D. x := 10;
Answer: C

3. What is the output of the following code snippet?


```
int x = 5;
int y = x++;
cout << y;
```
A. 4
B. 5
C. 6
D. Compilation error
Answer: A

4. Which operator is used for dynamic memory allocation in C++?


A. new
B. malloc
C. alloc
D. allocate
Answer: A

5. What is the correct syntax to define a class in C++?


A. class MyClass {}
B. class MyClass;
C. MyClass class;
D. define class MyClass;
Answer: A

6. What is the output of the following code snippet?


```
int arr[] = {1, 2, 3, 4, 5};
cout << arr[2];
```
A. 1
B. 2
C. 3
D. Compilation error
Answer: C
7. Which keyword is used to define a constant in C++?
A. constant
B. const
C. final
D. static
Answer: B

8. What is the correct way to access a member function of a class in C++?


A. objectName.memberFunction()
B. memberFunction.objectName()
C. objectName->memberFunction()
D. memberFunction->objectName()
Answer: A

9. What is the output of the following code snippet?


```
int x = 5;
int y = ++x;
cout << y;
```
A. 4
B. 5
C. 6
D. Compilation error
Answer: C

10. What is the purpose of the "sizeof" operator in C++?


A. To return the size of a data type in bytes
B. To return the size of an array in bytes
C. To return the size of a class or structure in bytes
D. All of the above
Answer: D

11. What is the correct syntax to define a function in C++?


A. function myFunction() {}
B. myFunction() {}
C. void myFunction() {}
D. define myFunction() {}
Answer: C

12. What is the output of the following code snippet?


```
string name = "John";
cout << "Hello, " << name << "!";
```
A. Hello, name!
B. Hello, John!
C. Compilation error
D. Runtime error
Answer: B
13. Which loop is used to iterate over a range of values in C++?
A. for loop
B. while loop
C. do-while loop
D. foreach loop
Answer: A

14. What is the correct way to read input from the user in C++?
A. cin << x;
B. cin >> x;
C. x << cin;
D. x >> cin;
Answer: B

15. What is the output of the following code snippet?


```
int x = 10;
if (x > 5) {
cout << "x is greater than 5";
} else {
cout << "x is less than or equal to 5";
}
```
A. x is greater than 5
B. x is less than or equal to 5
C. Compilation error
D. Runtime error
Answer: A

16. Which operator is used for pointer dereferencing in C++?


A. &
B. *
C. ->
D. ::
Answer: B

17. What is the correct way to include a standard library header file in C++?
A. include <iostream>
B. include "iostream"
C. import <iostream>
D. import "iostream"
Answer: A
18. What is the output of the following code snippet?
```
int x = 10;
int y = 5;
int result = x % y;
cout << result;
```
A. 0
B. 1
C. 2
D. Compilation error
Answer: A

19. Which keyword is used to define a class that inherits from another class in C++?
A. extend
B. inherits
C. derive
D. :
Answer: D

20. What is the purpose of the "break" statement in C++?


A. To exit the current loop or switch statement
B. To continue to the next iteration of a loop
C. To return a value from a function
D. To throw an exception
Answer: A

21. What is the output of the following code snippet?


```
int arr[3] = {1, 2, 3};
cout << arr[3];
```
A. 1
B. 2
C. 3
D. Compilation error
Answer: D

22. Which keyword is used to define a class member that is accessible without creating an
instance of the class?
A. static
B. const
C. final
D. public
Answer: A
23. What is the output of the following code snippet?
```
int x = 5;
int y = 2;
int result = x / y;
cout << result;
```
A. 2
B. 2.5
C. 3
D. Compilation error
Answer: C

24. What is the purpose of the "continue" statement in C++?


A. To exit the current loop or switch

statement
B. To continue to the next iteration of a loop
C. To return a value from a function
D. To throw an exception
Answer: B

25. What is the output of the following code snippet?


```
int arr[3] = {1, 2, 3};
cout << *arr;
```
A. 1
B. 2
C. 3
D. Compilation error
Answer: A

26. Which keyword is used to define a class member that cannot be changed once assigned a
value?
A. const
B. static
C. final
D. private
Answer: A
27. What is the output of the following code snippet?
```
int x = 10;
int y = 5;
int result = x + y * 2;
cout << result;
```
A. 20
B. 25
C. 30
D. Compilation error
Answer: C

28. What is the purpose of the "const" keyword in C++?


A. To prevent a class from being inherited
B. To define a constant variable
C. To declare a variable
D. To define a constant function that cannot modify class members
Answer: B

29. What is the correct syntax to define a constructor in C++?


A. class MyClass { constructor() {} }
B. class MyClass { void constructor() {} }
C. class MyClass { MyClass() {} }
D. class MyClass { define constructor() {} }
Answer: C

30. What is the output of the following code snippet?


```
int x = 5;
int y = 2;
int result = x % y;
cout << result;
```
A. 0
B. 1
C. 2
D. Compilation error
Answer: B

31. Which operator is used for logical NOT in C++?


A. !
B. &&
C. ||
D. !=
Answer: A
32. What is the output of the following code snippet?
```
int x = 5;
int y = 10;
if (x > y) {
cout << "x is greater than y";
} else if (x < y) {
cout << "x is less than y";
} else {
cout << "x is equal to y";
}
```
A. x is greater than y
B. x is less than y
C. x is equal to y
D. Compilation error
Answer: B

33. Which keyword is used to define a variable that can hold the memory address of another
variable in C++?
A. address
B. reference
C. pointer
D. memory
Answer: C

34. What is the output of the following code snippet?


```
int x = 10;
int y = 5;
int result = x - y;
cout << result;
```
A. 5
B. 10
C. 15
D. Compilation error
Answer: A

35. What is the purpose of the "break" statement in C++?


A. To exit the current loop or switch statement
B. To continue to the next iteration of a loop
C. To return a value from a function
D. To throw an exception
Answer: A
36. What is the output of the following code snippet?
```
string name = "John";
cout << name.length();
```
A. 4
B. 5
C. Compilation error
D. Runtime error
Answer: B

37. Which operator is used for pointer arithmetic in C++?


A. +
B. -
C. *
D. /
Answer: B

38. What is the output of the following code snippet?


```
int x = 5;
cout << x++;
```
A. 4
B. 5
C. 6
D. Compilation error
Answer: B

39. Which keyword is used to define a class member that can be accessed from any class?
A. public
B. private
C. protected
D. static
Answer: A

40. What is the purpose of the "return" statement in C++?


A. To exit the current loop or switch statement
B. To continue to the next iteration of a loop
C. To return a value from a function
D. To throw an exception
Answer: C
41. What is the output of the following code snippet?
```
int arr[] = {1, 2, 3, 4, 5};
cout << arr[0] + arr[4];
```
A. 1
B. 2
C. 3
D. 6
Answer: D

42. Which

operator is used for logical AND in C++?


A. &&
B. ||
C. !
D. ==
Answer: A

43. What is the output of the following code snippet?


```
int x = 5;
int y = 2;
int result = x * y;
cout << result;
```
A. 5
B. 2
C. 10
D. Compilation error
Answer: C

44. What is the purpose of the "continue" statement in C++?


A. To exit the current loop or switch statement
B. To continue to the next iteration of a loop
C. To return a value from a function
D. To throw an exception
Answer: B
45. What is the output of the following code snippet?
```
int x = 5;
int y = 10;
if (x > y) {
cout << "x is greater than y";
} else if (x < y) {
cout << "x is less than y";
} else {
cout << "x is equal to y";
}
```
A. x is greater than y
B. x is less than y
C. x is equal to y
D. Compilation error
Answer: B

46. Which operator is used for pointer dereferencing in C++?


A. *
B. &
C. ->
D. ::
Answer: A

47. What is the output of the following code snippet?


```
int arr[] = {1, 2, 3, 4, 5};
cout << sizeof(arr) / sizeof(arr[0]);
```
A. 1
B. 2
C. 3
D. 5
Answer: D

48. Which keyword is used to define a variable that cannot be changed once assigned a value?
A. const
B. static
C. final
D. private
Answer: A

49. What is the purpose of the "const" keyword in C++?


A. To prevent a class from being inherited
B. To define a constant variable
C. To declare a variable
D. To define a constant function that cannot modify class members
Answer: B
50. What is the output of the following code snippet?
```
int x = 10;
int y = 5;
int result = x / y;
cout << result;
```
A. 2
B. 2.5
C. 3
D. Compilation error
Answer: A

You might also like