Python Data Science Jobs & Interviews
20.3K subscribers
188 photos
4 videos
25 files
326 links
Your go-to hub for Python and Data Science—featuring questions, answers, quizzes, and interview tips to sharpen your skills and boost your career in the data-driven world.

Admin: @Hussein_Sheikho
Download Telegram
❓️ Question 20: #CPP
The length of an array in C++ is significant because it determines ____.

1. the number of elements that can be stored in the array

2. the memory allocated for the array

3. the maximum size of the array

4. All of the above

Correct Response: 1

Explanation: The length of an array in C++ is significant because it determines the number of elements that can be stored in the array. It affects the memory allocation for the array and defines the maximum size of the array. Understanding the length of an array is important for proper memory management, accessing array elements, and avoiding potential issues such as accessing out-of-bounds indices.

https://t.iss.one/DataScienceQ
Please open Telegram to view this post
VIEW IN TELEGRAM
👍155
Question 21: #CPP
To access elements in a structure in C++, you use the structure variable name followed by ___.

1. A dot operator (.)

2. An arrow operator (->)

3. A hash sign (#)

4. A dollar sign ($)

Correct Response: 1

Explanation: In C++, to access elements in a structure, you use the structure variable name followed by a dot operator (.) and the name of the structure member. For example, if you have a structure 'Person' with a member 'name', you can access it using 'person.name', assuming 'person' is an instance of the 'Person' structure. The dot operator is used to access individual members of the structure.

https://t.iss.one/DataScienceQ
Please open Telegram to view this post
VIEW IN TELEGRAM
👍234
Top 50 C++ Keywords & Functions

#CPP #Basics #IO

#1. #include <iostream>
A preprocessor directive that includes the input/output stream library.

#include <iostream>

int main() {
std::cout << "This requires iostream!";
return 0;
}

This requires iostream!


#2. int main()
The main function where program execution begins.

#include <iostream>

int main() {
std::cout << "Program starts here.";
return 0;
}

Program starts here.


#3. std::cout
Used to output data (print to the console).

#include <iostream>

int main() {
std::cout << "Hello, C++!";
return 0;
}

Hello, C++!


#4. std::cin
Used to get input from the user.

#include <iostream>
#include <string>

int main() {
int age;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "You are " << age << " years old.";
return 0;
}

Enter your age: 25
You are 25 years old.


#5. using namespace std;
Tells the compiler to use the std (standard) namespace. Avoids prefixing std:: to every standard function.

#include <iostream>
using namespace std;

int main() {
cout << "No std:: prefix needed.";
return 0;
}

No std:: prefix needed.

---
#CPP #DataTypes #Variables

#6. int
Declares an integer variable.

#include <iostream>

int main() {
int number = 100;
std::cout << "The number is: " << number;
return 0;
}

The number is: 100


#7. double
Declares a floating-point number variable (can hold decimals).

#include <iostream>

int main() {
double price = 19.99;
std::cout << "The price is: " << price;
return 0;
}

The price is: 19.99


#8. char
Declares a character variable.

#include <iostream>

int main() {
char grade = 'A';
std::cout << "Your grade is: " << grade;
return 0;
}

Your grade is: A


#9. bool
Declares a boolean variable, which can only have the value true or false.

#include <iostream>

int main() {
bool isRaining = false;
std::cout << "Is it raining? " << isRaining; // Outputs 0 for false
return 0;
}

Is it raining? 0


#10. std::string
Declares a variable that can hold a sequence of characters. Requires #include <string>.

#include <iostream>
#include <string>

int main() {
std::string greeting = "Hello, World!";
std::cout << greeting;
return 0;
}

Hello, World!

---
#CPP #Keywords #Operators

#11. const
Declares a variable as a constant, meaning its value cannot be changed.

#include <iostream>

int main() {
const double PI = 3.14159;
std::cout << "The value of PI is: " << PI;
// PI = 4; // This would cause a compile error
return 0;
}

The value of PI is: 3.14159


#12. sizeof()
An operator that returns the size (in bytes) of a data type or variable.

#include <iostream>

int main() {
int myInt;
std::cout << "Size of int is: " << sizeof(myInt) << " bytes.";
return 0;
}

Size of int is: 4 bytes.
#13. auto
A keyword that lets the compiler automatically deduce the data type of a variable at compile-time.

#include <iostream>

int main() {
auto number = 10; // Compiler deduces int
auto pi = 3.14; // Compiler deduces double
std::cout << "Type of 'number' is deduced.";
return 0;
}

Type of 'number' is deduced.


#14. & (Address-of Operator)
Returns the memory address of a variable.

#include <iostream>

int main() {
int var = 20;
std::cout << "Memory address of var: " << &var;
return 0;
}

Memory address of var: 0x61ff08 
(Note: Address will vary)


#15. * (Dereference Operator)
Accesses the value stored at a memory address held by a pointer.

#include <iostream>

int main() {
int var = 50;
int* ptr = &var; // ptr holds the address of var
std::cout << "Value at address " << ptr << " is " << *ptr;
return 0;
}

Value at address 0x61ff04 is 50
(Note: Address will vary)

---
#CPP #ControlFlow #Conditional

#16. if
Executes a block of code if a specified condition is true.

#include <iostream>

int main() {
int age = 18;
if (age >= 18) {
std::cout << "You are an adult.";
}
return 0;
}

You are an adult.


#17. else
Executes a block of code if the condition in the if statement is false.

#include <iostream>

int main() {
int age = 16;
if (age >= 18) {
std::cout << "You are an adult.";
} else {
std::cout << "You are not an adult.";
}
return 0;
}

You are not an adult.


#18. else if
Specifies a new condition to test, if the first if condition is false.

#include <iostream>

int main() {
int score = 85;
if (score >= 90) {
std::cout << "Grade: A";
} else if (score >= 80) {
std::cout << "Grade: B";
} else {
std::cout << "Grade: C";
}
return 0;
}

Grade: B


#19. switch / case
Selects one of many code blocks to be executed.

#include <iostream>

int main() {
int day = 3;
switch (day) {
case 1:
std::cout << "Monday";
break;
case 2:
std::cout << "Tuesday";
break;
case 3:
std::cout << "Wednesday";
break;
}
return 0;
}

Wednesday


#20. break
Used to exit a switch statement or a loop.

#include <iostream>

int main() {
for (int i = 0; i < 10; ++i) {
if (i == 5) {
break; // Exit the loop when i is 5
}
std::cout << i << " ";
}
return 0;
}

0 1 2 3 4

---
#CPP #Loops

#21. for
Executes a block of code a specified number of times.

#include <iostream>

int main() {
for (int i = 0; i < 5; ++i) {
std::cout << i << " ";
}
return 0;
}

0 1 2 3 4


#22. while
Loops through a block of code as long as a specified condition is true.

#include <iostream>

int main() {
int i = 0;
while (i < 5) {
std::cout << i << " ";
i++;
}
return 0;
}

0 1 2 3 4
#23. do-while
Similar to a while loop, but the code block is executed at least once before the condition is tested.

#include <iostream>

int main() {
int i = 5;
do {
std::cout << "This will run once.";
i++;
} while (i < 5);
return 0;
}

This will run once.


#24. Range-based for loop
Iterates over all elements in a range, such as an array or vector, without using an index.

#include <iostream>
#include <vector>

int main() {
int numbers[] = {10, 20, 30};
for (int num : numbers) {
std::cout << num << " ";
}
return 0;
}

10 20 30


#25. continue
Skips the current iteration of a loop and continues with the next iteration.

#include <iostream>

int main() {
for (int i = 0; i < 5; ++i) {
if (i == 2) {
continue; // Skip printing 2
}
std::cout << i << " ";
}
return 0;
}

0 1 3 4

---
#CPP #Functions

#26. Function Declaration (Prototype)
Declares a function's name, return type, and parameters, allowing it to be used before it's defined.

#include <iostream>

void sayHello(); // Function declaration

int main() {
sayHello(); // Call the function
return 0;
}

void sayHello() { // Function definition
std::cout << "Hello from function!";
}

Hello from function!


#27. void
A keyword specifying that a function does not return any value.

#include <iostream>

void printMessage(std::string message) {
std::cout << message;
}

int main() {
printMessage("This function returns nothing.");
return 0;
}

This function returns nothing.


#28. return
Terminates a function and can return a value to the caller.

#include <iostream>

int add(int a, int b) {
return a + b; // Return the sum
}

int main() {
int result = add(5, 3);
std::cout << "Result: " << result;
return 0;
}

Result: 8


#29. #include <cmath>
Includes the C++ math library for complex mathematical operations.

#include <iostream>
#include <cmath> // Include for sqrt()

int main() {
double number = 25.0;
std::cout << "Square root is: " << sqrt(number);
return 0;
}

Square root is: 5


#30. pow()
A function from <cmath> that returns the base raised to the power of the exponent.

#include <iostream>
#include <cmath>

int main() {
double result = pow(2, 3); // 2 to the power of 3
std::cout << "2^3 is: " << result;
return 0;
}

2^3 is: 8

---
#CPP #STL #Vector

#31. #include <vector>
Includes the library for std::vector, a dynamic array.

#include <iostream>
#include <vector>

int main() {
std::vector<int> myVector;
myVector.push_back(1);
std::cout << "Vector is ready.";
return 0;
}

Vector is ready.


#32. std::vector
A sequence container that encapsulates dynamic size arrays.

#include <iostream>
#include <vector>

int main() {
std::vector<int> numbers = {10, 20, 30};
std::cout << "First element: " << numbers[0];
return 0;
}

First element: 10


#33. .push_back()
Member function of std::vector that adds an element to the end.
#include <iostream>
#include <vector>

int main() {
std::vector<int> numbers;
numbers.push_back(5);
numbers.push_back(10);
std::cout << "Vector size: " << numbers.size();
return 0;
}

Vector size: 2


#34. .size()
Member function that returns the number of elements in a container like std::vector or std::string.

#include <iostream>
#include <vector>

int main() {
std::vector<std::string> fruits = {"Apple", "Banana"};
std::cout << "There are " << fruits.size() << " fruits.";
return 0;
}

There are 2 fruits.


#35. .length()
A member function of std::string that returns its length. It's synonymous with .size().

#include <iostream>
#include <string>

int main() {
std::string text = "C++";
std::cout << "The length of the string is: " << text.length();
return 0;
}

The length of the string is: 3

---
#CPP #STL #Algorithms

#36. #include <algorithm>
Includes the standard library algorithms, like sort, find, copy, etc.

#include <iostream>
#include <vector>
#include <algorithm> // Required for std::sort

int main() {
std::vector<int> nums = {3, 1, 4};
std::sort(nums.begin(), nums.end());
std::cout << "Sorting is possible with <algorithm>.";
return 0;
}

Sorting is possible with <algorithm>.


#37. std::sort()
Sorts the elements in a range (e.g., a vector).

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
std::vector<int> nums = {50, 20, 40, 10, 30};
std::sort(nums.begin(), nums.end());
for(int n : nums) {
std::cout << n << " ";
}
return 0;
}

10 20 30 40 50


#38. .begin()
Returns an iterator pointing to the first element in a container.

#include <iostream>
#include <vector>

int main() {
std::vector<int> nums = {100, 200, 300};
auto it = nums.begin();
std::cout << "First element: " << *it;
return 0;
}

First element: 100


#39. .end()
Returns an iterator referring to the past-the-end element in the container.

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
std::vector<int> nums = {1, 2, 3};
// .end() points after the last element, used as a boundary
std::cout << "Vector has elements until the end.";
return 0;
}

Vector has elements until the end.


#40. #define
A preprocessor directive used to create macros or symbolic constants.

#include <iostream>
#define PI 3.14159

int main() {
std::cout << "The value of PI is " << PI;
return 0;
}

The value of PI is 3.14159

---
#CPP #OOP #Classes

#41. class
A keyword used to declare a class, which is a blueprint for creating objects.

#include <iostream>

class Dog {
public:
void bark() {
std::cout << "Woof!";
}
};

int main() {
Dog myDog;
myDog.bark();
return 0;
}

Woof!


#42. struct
Similar to a class, but its members are public by default.
#include <iostream>

struct Point {
int x;
int y;
};

int main() {
Point p;
p.x = 10;
p.y = 20;
std::cout << "Point: (" << p.x << ", " << p.y << ")";
return 0;
}

Point: (10, 20)


#43. public
An access specifier that makes class members accessible from outside the class.

#include <iostream>

class MyClass {
public: // Accessible from anywhere
int myNum = 10;
};

int main() {
MyClass obj;
std::cout << obj.myNum;
return 0;
}

10


#44. private
An access specifier that makes class members accessible only from within the class itself.

#include <iostream>

class MyClass {
private:
int secret = 42;
public:
void printSecret() {
std::cout << secret; // Accessible from within the class
}
};

int main() {
MyClass obj;
// std::cout << obj.secret; // This would cause a compile error
obj.printSecret();
return 0;
}

42


#45. Constructor
A special member function of a class that is executed whenever a new object of that class is created.

#include <iostream>

class Car {
public:
// Constructor
Car() {
std::cout << "Car object created.";
}
};

int main() {
Car myCar; // Constructor is called here
return 0;
}

Car object created.

---
#CPP #OOP #MemoryManagement

#46. Destructor
A special member function that is executed automatically when an object is destroyed.

#include <iostream>

class MyClass {
public:
// Destructor
~MyClass() {
std::cout << "Object destroyed.";
}
};

int main() {
MyClass obj;
// Destructor is called when main() ends
return 0;
}

Object destroyed.


#47. this
A keyword that refers to the current instance of the class.

#include <iostream>

class Box {
private:
int length;
public:
Box(int length) {
this->length = length; // Use 'this' to distinguish member from parameter
}
void printLength() {
std::cout << "Length: " << this->length;
}
};

int main() {
Box b(10);
b.printLength();
return 0;
}

Length: 10


#48. new
An operator that allocates memory on the heap and returns a pointer to it.

#include <iostream>

int main() {
int* ptr = new int; // Allocate an integer on the heap
*ptr = 100;
std::cout << "Value from heap: " << *ptr;
delete ptr; // Must deallocate memory
return 0;
}

Value from heap: 100


#49. delete
An operator that deallocates memory previously allocated with new.

#include <iostream>

int main() {
int* ptr = new int(55);
std::cout << *ptr << " allocated. ";
delete ptr; // Deallocate the memory
std::cout << "Memory freed.";
// Accessing ptr now is undefined behavior
return 0;
}

55 allocated. Memory freed.


#50. nullptr
Represents a null pointer literal. It indicates that a pointer does not point to any valid memory location.

#include <iostream>

int main() {
int* ptr = nullptr;
if (ptr == nullptr) {
std::cout << "The pointer is null.";
}
return 0;
}

The pointer is null.


━━━━━━━━━━━━━━━
By: @DataScienceQ