The Wayback Machine - https://web.archive.org/web/20240917115752/https://www.geeksforgeeks.org/c-plus-plus-gq/
Open In App

C ++

Last Updated : 08 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Similar Reads

C ++
Quizzes on C++ LanguageConstructors(17)References(6)Class and Object(6)Misc C++(7)Destructors(5)Operator Overloading(10)this pointer(5)Function Overloading and Default Arguments(5)Static Keyword(6)friend keyword(2)Inheritance(14)const keyword(5)new and delete(5)Virtual Functions(14)Templates(10)Exception Handling(12)Assorted MCQs on C++(60)Please d
1 min read
Accolite Digital interview Experience | On-Campus
Round 1: Aptitude Round The process started with the aptitude test in which basic C++ code snippets were given and few questions were from oops and DBMS concepts too. Round 2: Coding Round Then those who cleared this round were allowed to appear in the next round which was a coding round. It has a basic question of recursion. I don't remember the e
3 min read
How to find Size of std::forward_list in C++ STL
Forward list in standard template library of C++. It comes under #include<forward_list> header file. It is implemented as a singly linked list. It was introduced in C++ 11 for the first time. Forward lists are sequence containers that allow constant time insert and erase operations from anywhere within the sequence. In the case of a forward l
3 min read
Test Case Generation | Set 6 (Random Unweighted Binary Tree)
Generating Random Unweighted Binary Tree: Since this is a tree, the test data generation plan is such that no cycle gets formed.The number of edges is one less than the number of vertices.For each RUN, first print the count of nodes say, N and the next N - 1 lines are of the form (a, b) where a is the parent of b.Each node contains at most 2 childr
10 min read
Generation Google Scholarship (APAC) Online Challenge Experience
I had applied for an APAC scholarship in March 2021 through their Google Career Page and I got a mail that there will be an Online Challenge from 9th April to 10th April. In the challenge, there were two sections designed with multiple-choice questions (1) General Cognitive Ability and (2) Role Related Knowledge both have to be completed within 60
6 min read
Convert a Binary String to another by flipping prefixes minimum number of times
Given two binary strings A and B of length N, the task is to convert the string A to B by repeatedly flipping a prefix of A, i.e. reverse the order of occurrence of bits in the chosen prefix. Print the number of flips required and the length of all prefixes. Examples: Input: A = "01", B = "10"Output:31 2 1Explanation:Operation 1: Select the prefix
8 min read
Difference between int* p() and int (*p)()?
A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, a pointer must be declare before storing any variable address. The general form of a pointer variable declaration is: Syntax: type *var_name; Here, type is the pointers base type. It must be a valid C/C
2 min read
Examples of Big-O analysis
Prerequisite: Analysis of Algorithms | Big-O analysis In the previous article, the analysis of the algorithm using Big O asymptotic notation is discussed. In this article, some examples are discussed to illustrate the Big O time complexity notation and also learn how to compute the time complexity of any program. There are different asymptotic nota
13 min read
How to access private/protected method outside a class in C++
Prerequisites: Access Modifiers in C++, Runtime Polymorphism Private: The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members
5 min read
Search by value in a Map in C++
Given a set of N pairs as a (key, value) pairs in a map and an integer K, the task is to find all the keys mapped to the given value K. If there is no key value mapped to K then print "-1".Examples: Input: Map[] = { {1, 3}, {2, 3}, {4, -1}, {7, 2}, {10, 3} }, K = 3 Output: 1 2 10 Explanation: The 3 key value that is mapped to value 3 are 1, 2, 10.I
2 min read
Count of square free divisors of a given number
Given an integer N, the task is to count the number of square-free divisors of the given number. A number is said to be square-free, if no prime factor divides it more than once, i.e., the largest power of a prime factor that divides N is one. Examples: Input: N = 72 Output: 3 Explanation: 2, 3, 6 are the three possible square free numbers that div
6 min read
Reversed Range-based for loop in C++ with Examples
Range-based for loops is an upgraded version of for loops. It is quite similar to for loops which is use in Python. Range-based for loop in C++ is added since C++ 11. We can reverse the process of iterating the loop by using boost::adaptors::reverse() function which is included in boost library Header. Header File: #include <boost/range/adaptor/
2 min read
Length of race track based on the final distance between participants
Given three integers A, B, and C, the task is to find the length of a race track if 3 racers are competing in a race where the first racer beats the second racer by A meters, the first racer beats the third racer by B meters and the second racer beats the third by C meters. Examples: Input: A = 11, B = 90, C = 80 Output: 880 Input: A = 10, B = 20,
3 min read
cerr - Standard Error Stream Object in C++
Standard output stream(cout): cout is the instance of the ostream class. cout is used to produce output on the standard output device which is usually the display screen. The data needed to be displayed on the screen is inserted in the standard output stream(cout) using the insertion operator(<<). Standard error stream (cerr): cerr is the sta
2 min read
Check if its possible to make sum of the array odd with given Operations
Given an array arr[], the task is to check if it is possible to make the sum of array odd such that any two indices i and j can be chosen and arr[i] can be set equal to arr[j] given that i != j. Examples: Input: arr[] = { 5, 4, 4, 5, 1, 3 } Output: Yes Explanation: The sum of the array is 22. Put arr[0] = arr[1] where i=0 and j=1. The sum of this n
7 min read
CamelCase Pattern Matching
Given a list of words where each word follows CamelCase notation, the task is to print all words in the dictionary that match with a given pattern consisting of uppercase characters only. Examples Input: arr[] = [ "WelcomeGeek", "WelcomeToGeeksForGeeks", "GeeksForGeeks" ], pattern = "WTG" Output: WelcomeToGeeksForGeeks Explanation: There is only on
15+ min read
Minimum cost of choosing 3 increasing elements in an array of size N
Given two arrays arr[] and cost[] where cost[i] is the cost associated with arr[i], the task is to find the minimum cost of choosing three elements from the array such that arr[i] < arr[j] < arr[k]. Examples: Input: arr[] = {2, 4, 5, 4, 10}, cost[] = {40, 30, 20, 10, 40} Output: 90 (2, 4, 5), (2, 4, 10) and (4, 5, 10) are the only valid tripl
12 min read
Comparing X^Y and Y^X for very large values of X and Y
Given two integer X and Y, the task is compare XY and YX for large values of X and Y.Examples: Input: X = 2, Y = 3 Output: 2^3 < 3^2 23 < 32Input: X = 4, Y = 5 Output: 4^5 > 5^4 Naive approach: A basic approach is to find the values XY and YX and compare them which can overflow as the values of X and Y can be largeBetter approach: Taking l
4 min read
Increment odd positioned elements by 1 and decrement even positioned elements by 1 in an Array
Given an array arr[], the task is increment all the odd positioned elements by 1 and decrement all the even positioned elements by 1. Examples: Input: arr[] = {3, 6, 8} Output: 4 5 9 Input: arr[] = {9, 7, 3} Output: 10 6 4 Approach: Traverse the array element by element and if the current element's position is odd then increment it by 1 else decrem
5 min read
Replace the odd positioned elements with their cubes and even positioned elements with their squares
Given an array arr[] of n elements, the task is to replace all the odd positioned elements with their cubes and even positioned elements with their squares i.e. the resultant array must be {arr[0]3, arr[1]2, arr[2]3, arr[3]2, ...}.Examples: Input: arr[]= {2, 3, 4, 5} Output: 8 9 64 25 Updated array will be {23, 32, 43, 52} -> {8, 9, 64, 25}Input
5 min read
Program to print 'N' alphabet using the number pattern from 1 to n
Given an integer N, the task is to print the Alphabet N Pattern as given below: 1 1 2 2 2 3 3 3 * * * * * * * * * N N Examples: Input: N = 6 Output: 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 Input: N = 5 Output: 1 1 2 2 2 3 3 3 4 4 4 5 5 Approach: Except the first and the last row, every other row will follow the following: Print the first value as index + 1
7 min read
Ittiam Written Test Questions
Round 1(30 mins): Written Test It was a written round (pen-paper). It started just after the company’s ppt (pre-placement talk). There were a total of 20 Mcqs (with negative marking), 10 from quantitative aptitude and 10 from technical aptitude to be solved in 30 mins. Quant questions were from time&work, speed-distance(tricky one), few maths q
4 min read
Why are negative numbers stored as 2's complement?
When doing addition/subtraction on binary numbers in other representations we need to apply different logics (circuits) to perform addition and subtraction. In 2s-complement representation, we represent a positive number as it is and negative number by its corresponding 2s-complement, so we can use the same circuit to perform addition and subtracti
1 min read
ungetc() in C/C++
The ungetc() function takes a single character and shoves it back onto an input stream. It is the opposite of the getc() function, which reads a single character from an input stream. Also, ungetc() is an input function, not an output function. Syntax: int ungetc(int char, FILE *stream) Parameters: char: specifies the int promotion of the character
3 min read
Largest of two distinct numbers without using any conditional statements or operators
Given two positive and distinct numbers, the task is to find the greatest of two given numbers without using any conditional statements(if...) and operators(?: in C/C++/Java). Examples: Input: a = 14, b = 15 Output: 15 Input: a = 14, b = 14 Output: 14 Input: a = 1233133, b = 124 Output: 1233133The Approach is to return the value on the basis of the
3 min read
Check if all occurrences of a character appear together
Given a string s and a character c, find if all occurrences of c appear together in s or not. If the character c does not appear in the string at all, the answer is true. Examples Input: s = "1110000323", c = '1' Output: Yes All occurrences of '1' appear together in "1110000323" Input: s = "3231131", c = '1' Output: No All occurrences of 1 are not
7 min read
C++ | this pointer | Question 4
Predict the output of following C++ program #include<iostream> using namespace std; class Test { private: int x; int y; public: Test(int x = 0, int y = 0) { this->x = x; this->y = y; } static void fun1() { cout << "Inside fun1()"; } static void fun2() { cout << "Inside fun2()"; this->fun1(); } }; int m
1 min read
C++ | this pointer | Question 3
Predict the output of following C++ program. #include<iostream> using namespace std; class Test { private: int x; public: Test(int x = 0) { this->x = x; } void change(Test *t) { this = t; } void print() { cout << "x = " << x << endl; } }; int main() { Test obj(5); Test *ptr = new Test (10); obj.change(ptr); obj.
1 min read
C++ | Operator Overloading | Question 10
How can we restrict dynamic allocation of objects of a class using new? (A) By overloading new operator (B) By making an empty private new operator. (C) By making an empty private new and new[] operators (D) By overloading new operator and new[] operators Answer: (C) Explanation: If we declare new and [] new operators, then the objects cannot be cr
1 min read
C++ | Constructors | Question 2
When a copy constructor may be called? (A) When an object of the class is returned by value. (B) When an object of the class is passed (to a function) by value as an argument. (C) When an object is constructed based on another object of the same class (D) When compiler generates a temporary object. (E) All of the above Answer: (E) Explanation: See
1 min read
Article Tags :