InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
File handling |
|
Answer» int main() { // Create and open a text file ofstream MyFile("filename.txt"); // Write to the file MyFile << "File Handling in C++"; // Close the file MyFile.close(); }. File handling refers to reading and writing data in files. C++ provides us with functions that allow us to do so. The above code creates a file and writes text in it. getline();The above function allows us to read the file line by line. void open(const char* file_name,ios::openmode mode);The above function opens a file. ConclusionThere has been a lot of change in the world of C++ programming since its inception, and it is becoming ever more important to be aware of the new syntax that is being introduced. This article provides a summary of the most popular syntaxes in C++ and has been designed to lay out all the basics for those who are early on in their programming journey. For those who are more experienced, this article will provide an overview of what is happening in the world of C++. We wish you a happy coding journey! Useful Resources
|
|
| 2. |
Object-Oriented Programming |
Answer»
// Attribute string name; int population; public: // Method void increase_population() { population++; } }; In C++, a class is the fundamental building block of Object-Oriented programming. A class typically consists of:
An object is an instance of a class that encapsulates its data and member functions. The above code creates an instance of the class City named Mumbai. |
|
| 3. |
Iterative statements |
|
Answer» Suppose you want to write “Write it hundred times” hundred times. One way to do this is to use cout 100 times. But don’t worry, there’s an easy way. You can use iterative statements to write the same statements 100 times.
while(i <=100){ cout << "Write it hundred times \n"; i++; } The above code will print the statement 100 times (i.e. until the value of i is less than 101).
do{ cout << "Write it hundred times \n"; i++; } while(i<=100); The do-while loop is very similar to the while loop. The difference is that in the while loop the condition is checked first; and in the do-while loop, the condition is checked after certain tasks have been performed.
A for loop repeats a code block for a set number of times. It is divided into three sections:
cout << i << "\n"; } The numbers 0 to 9 are printed on the screen in this example. |
|
| 4. |
Math Functions |
|
Answer» cout << max(100, 110); // Returns max value The above function returns the maximum value. cout << min(1, 0); // Returns min valueThe above function returns the minimum value. cout << sqrt(625); // Returns square rootThe above function returns the square root of the given value. cout << ceil(a); // Returns ceil valueThe above function returns the ceil value. cout << floor(a); // returns floor valueThe above function returns the floor value. cout <<pow(a, b) // returns a raised to the power bThe above function returns a to power b. |
|
| 5. |
String Functions |
Answer»
string lastName = "Babu"; string fullName = firstName.append(lastName); cout << fullName; This function concatenates one string after another string. The output of the above code will be: Ramesh Babu You can also change any individual character of a string in C++. string variable1 = "Hello World";variable1[1] = 'i'; cout << variable1;
cout << "The length of the string is: " << variable1.length(); It returns the length of the string as an integer. |
|
| 6. |
Functions |
|
Answer» int sum (int a, int b){ // Declaration return a+b; } int main(){ int first_number= 10; int second_number = 20; cout<< sum(first_number, second_number); // Calling a function } When a function is called, it is a collection of statements that are all executed at the same time. Every function has a name that is used to refer to it when it is called. A function typically contains the following parts:
|
|
| 7. |
References and Pointers |
|
Answer» string variable1 = "Value1"; // a variable string &variable2 = variable1; // reference to variable1 Reference is an alias for an already existing variable. Once it is initialized to a variable, it cannot be changed to refer to another variable. In the above example, variable2 is a reference to variable1. A pointer is a variable that contains the memory address of another variable. int var = 2, *p;p = &var;// The variable p holds the address of the variable var int *const ptr; ptr in the above code is a constant pointer. const int* ptr;The above code shows how to declare a pointer to a constant. |
|
| 8. |
Vectors |
|
Answer» #include <vector> int main() { vector<int> grade(3); grade[0] = 90; grade[1] = 80; grade[2] = 70; return 0; } A vector in C++ is a dynamic list of things that can expand and shrink in size. It can only hold values of the same type. It is important to #include the vector library in order to use vectors. vector<string> wishlist;wishlist.push_back("Furniture"); wishlist.push_back("Basket"); wishlist.pop_back(); cout << wishlist.size(); // returns the output 1
|
|
| 9. |
Arrays |
|
Answer» int main() { string str[4] = {"Volvo", "BMW", "Volkswagen", "Ford"}; for(int i=0;i<4;i++){ cout << str[i]+ " "; } return 0; } Instead of defining individual variables for each item, arrays are used to hold these values of the same data type in a single variable. It stores the values in a contagious block of memory, that’s why we need to specify the number of values it is going to hold beforehand. Declare an array by specifying the variable type, the array name enclosed in square brackets, and the number of elements it should store. |
|
| 10. |
Conditions and If Statements |
Answer»
// This block of code will get executed if the condition is True } If statement belongs to the category of decision-making statements. These statements make decisions based on a condition. If the condition in the condition block is true, the statements in the curly brackets { } are executed. Let’s see the example given below. if(2<3){cout << "2 is less than three"; }
If-else is an extension of the if statement. If the conditions provided with if are not true, the statements in the else block are executed. if(2>3){cout<< "2 is greater than 3"; } else{ cout<< "3 is greater than 2"; }
if can be paired with else if for additional conditions. if(2>3){cout<< "2 is greater than 3"; } else if(2==3){ cout<< "2 is equal to 3"; } else{ cout<< "3 is greater than 2"; }
case 9: cout << "Freshman\n"; break; case 10: cout << "Sophomore\n"; break; case 11: cout << "Junior\n"; break; case 12: cout << "Senior\n"; break; default: cout << "Invalid\n"; break; } A switch statement allows you to test an expression against a variety of cases. If a match is found, the code within begins to run. A case can be ended with the break keyword. When no case matches, default is used. |
|
| 11. |
Comments |
|
Answer» //This is a comment /* C++ comments can also * span multiple lines */ Anything after // is ignored by the compiler. Multiple lines can be commented using /*......*/. |
|
| 12. |
Input |
|
Answer» int variable; cin >> variable;cin takes the input from the screen and stores it in the variable. |
|
| 13. |
Output |
|
Answer» cout << "Hello World"; cout prints anything under the “ ” to the screen. |
|
| 14. |
Basic Syntax |
|
Answer» #include <iostream> using namespace std; // main() is where program execution begins. int main() { // This is where you write your code return 0; } This is the basic structure of C++. You will have to use this structure almost every time you write a C++ code. It contains the main function where the program execution begins. |
|