C++ Basics and Arrays Quiz

C++ Basics and Arrays Quiz

Multiple Choice Questions

1. What is the correct way to declare a variable in C++?
2. Which of the following is the correct file extension for a C++ source file?
3. Which keyword is used to define a constant variable in C++?
4. What is the output of the following code?
int a = 5;
      int b = 3;
      cout << a + b;
5. Which of the following is the correct way to include a header file in C++?
6. What is the purpose of the 'using namespace std;' line?
7. Which of the following is the correct way to declare an array of 5 integers?
8. What is the index of the first element in a C++ array?
9. What will be the output of the following code?
int arr[3] = {1, 2, 3};
      cout << arr[1];
10. Which data type is used to represent single characters in C++?

Exercises

1. Digit Sum Function
Problem: Write a function that takes an integer and returns the sum of its digits.
Input: 123
Output: 15
2. Reverse a Number
Problem: Write a function that reverses the digits of a number and returns it.
Input: 5602
Output:2065
3. Count Even Digits
Problem: Write a function that counts how many even digits are in a number.
Input: 82471
Output: 2 *(8 and 2)
4. Power Without Using pow()
Problem: Write a function that calculates a^b using multiplication
Input: a = 3, b = 4
Output: 81
5. ArrayProblem:
Problem: Write a function that returns the product of all element.
Input: [2, 3, 4]
Output: 24
6 Maximum Difference in Array
Problem: Find the maximum difference between any two elements in the array.
Input: [1, 10, 5, 3]
Output: 9
7. Sum of Elements at Even Indices
Problem: Write a function that sums the elements of the array found at even indices.
Input: [5, 4, 8, 2, 7]
Output: 5 + 8 + 7 = 20
8. Swap First and Last Element of Array
ProblemWrite a function that swaps the first and last elements of an array.
Input: [9, 3, 7, 1]
Output: [1, 3, 7, 9]
9. Count Unique Elements in Array
Problem: Write a function that counts the number of distinct elements in an array (no STL).
Input: [1, 2, 2, 3, 4, 1]
Output: 4(1, 2, 3, 4)
10. Circular Left Shift of Array
Problem: Implement a function that circularly shifts the array one position to the left.
Input: [1, 2, 3, 4]
Output: [2, 3, 4, 1]