C++

Variables and Data Types

C++ supports various data types: int, float, double, char, and bool. Variables must be declared before use.

int age = 20;
float height = 5.8;
char grade = 'A';
bool isStudent = true;
std::cout << "Age: " << age << ", Height: " << height << "\\n";
>

Input/Output

C++ uses std::cin for input and std::cout for output.

int num;
std::cout << "Enter a number: ";
std::cin >> num;
std::cout << "You entered: " << num << "\\n";
>

Conditionals

C++ supports if, else if, and else for conditional execution.

int n;
std::cin >> n;
if(n > 0)
  std::cout << "Positive\\n";
else if(n < 0)
  std::cout << "Negative\\n";
else
  std::cout << "Zero\\n";
>

Loops

C++ supports for, while, and do-while loops.

for(int i = 1; i <= 5; i++) {
  std::cout << "i = " << i << "\\n";
}
int j = 1;
while(j <= 5) {
  std::cout << "j = " << j << "\\n";
  j++;
}
>

Functions

Functions allow reusable code blocks. Declare, define, and call them.

int add(int a, int b) {
  return a + b;
}
int result = add(5, 3);
std::cout << "Sum: " << result << "\\n";
>

Final Example — Collatz Sequence in C++

This program generates the Collatz sequence for a given number. Save as collatz.cpp.

#include <iostream>
using namespace std;
int main() {
  int n;
  cout << "Enter a number: ";
  cin >> n;
  cout << "Collatz sequence: ";
  while(n != 1) {
    cout << n << " ";
    if(n % 2 == 0)
      n = n / 2;
    else
      n = 3 * n + 1;
  }
  cout << "1\\n";
  return 0;
}
>

MCQs

1. Which keyword is used to define a variable in C++?

(a) var

(b) let

(c) int

(d) dim

► (c) int

2. Which header file is required for input/output in C++?

(a) <iostream>

(b) <stdio.h>

(c) <conio.h>

(d) <math.h>

► (a) <iostream>

3. How do you print output to the console in C++?

(a) print()

(b) cout

(c) echo

(d) output()

► (b) cout

4. Which operator is used for input in C++?

(a) <<

(b) >>

(c) =

(d) ==

► (b) >>

5. Which of the following is a valid C++ comment?

(a) // comment

(b) # comment

(c) <!-- comment -->

(d) /* comment */

► (a) // comment

6. Which loop is guaranteed to execute at least once?

(a) for

(b) while

(c) do-while

(d) foreach

► (c) do-while

7. Which of the following is used to define a function?

(a) func

(b) void

(c) define

(d) function

► (b) void

8. What is the correct syntax to start the main function?

(a) main() { }

(b) int main() { }

(c) function main() { }

(d) void main() { }

► (b) int main() { }

9. Which symbol is used to end a statement in C++?

(a) .

(b) ;

(c) :

(d) ,

► (b) ;

10. Which operator is used for equality comparison?

(a) =

(b) ==

(c) !=

(d) <>

► (b) ==

11. Which data type is used to store a single character?

(a) string

(b) char

(c) int

(d) bool

► (b) char

12. Which data type stores true or false values?

(a) int

(b) bool

(c) char

(d) float

► (b) bool

13. What will the expression 5 % 2 return?

(a) 2

(b) 1

(c) 0

(d) 5

► (b) 1

14. Which keyword is used to create a constant variable?

(a) constant

(b) const

(c) final

(d) fixed

► (b) const

15. How do you exit a program in C++?

(a) stop();

(b) exit(0);

(c) return 1;

(d) quit();

► (b) exit(0);

16. Which keyword is used for conditional branching?

(a) switch

(b) loop

(c) case

(d) break

► (a) switch

17. Which header is used for math functions like sqrt()?

(a) <math.h>

(b) <cmath>

(c) <mathcpp>

(d) <algorithm>

► (b) <cmath>

18. What type of loop is used when the number of iterations is known?

(a) while

(b) do-while

(c) for

(d) loop

► (c) for