C++ Flashcards

1
Q

Difference b/w \n and endl

A

\n - Just puts a newline

endl - Will also flush the buffer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What happens when two operators of equal precedence occur in an expression?

A

The Associativity will apply from left to right.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a Comma(,) operator in C++?

A

In C and C++, comma (, ) can be used in two contexts:
1) Comma as an operator:
The comma operator (represented by the token, ) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type). The comma operator has the lowest precedence of any C operator and acts as a sequence point.

/* comma as an operator */
int i = (5, 10); /* 10 is assigned to i*/
int j = (f1(), f2()); /* f1() is called (evaluated) first followed by f2().
The returned value of f2() is assigned to j */

2) Comma as a separator:
Comma acts as a separator when used with function calls and definitions, function-like macros, variable declarations, enum declarations, and similar constructs.

/\* comma as a separator \*/
int a = 1, b = 2;
void fun(x, y);

More info: Comma Operator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
What is the output of following program? 

#include 

using namespace std;

int main()
{
~~~

int a = 5;
a = 2, 3, 4;
cout << a;
return 0; } ~~~
A

Output: 2

The following expression in the code:

a = 2, 3, 4;

is evaluated as:

(((a = 2), 3), 4);

This is because the reason that assignment operator has high precedence over the comma operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
What is the output?

#include
using namespace std;
~~~

int main()
{
    int arr[2] = {10, 20, 30};
return 0; } ~~~
A

Compile error:

prog.cpp: In function ‘int main()’: prog.cpp:6:29: error: too many initializers for ‘int [2]’ int arr[2] = {10, 20, 30}; ^

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
What is the output?

#include
using namespace std;
~~~

int main()
{
    int arr[2] = {10, 20};
cout << arr[0] << " " << arr[1] << " " << arr[2];

return 0; } ~~~
A

10 20 1294526976

If trying to access the array out of bound(arr[2]), it prints a random value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
What is the output?

#include
using namespace std;
~~~

int main()
{
    int arr1[2] {10, 20};
    int arr2[6] {10, 20};
cout << arr1[0] << " " << arr1[1] << " ";

cout << arr2[0] << " " << arr2[1] << " " << arr2[2];

return 0; } ~~~
A

10 20 10 20 0

When Array is partially initialized, it stores 0 values in the non-initialized values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
What is the output? 

#include
using namespace std;
~~~

int main()
{
    int arr[] = {10, 20, 30, 40};
cout << sizeof(arr);

return 0; } ~~~
A

16

Size of 4 int elements

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
What is the output?

#include
using namespace std;
~~~

int main()
{
    int arr[] = {10, 20, 30, 40};
cout << sizeof(arr)/sizeof(arr[0]);

return 0; } ~~~
A

4

Size of 4 int elements = 16 / Size of single int element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
Output?

#include < iostream >
using namespace std;
~~~

int main()
{
char str[] = “gfg”;
cout &laquo_space;str;
return 0;
}
~~~

A

O/p: gfg

This is a C-style string with a null termination at the end. It stores as “gfg\0”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
Output?

#include < iostream >
using namespace std;
~~~

int main()
{
    char str[] = "gfg";
    cout << sizeof(str);
    return 0;
}

~~~

A

O/p: 4

C-style strings include the null character also in the size.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
Output? 

#include < iostream >
using namespace std;
~~~

int main()
{
    char str[3] = "gfg";
    cout << sizeof(str);
    return 0;
}

~~~

A

Compile error:

prog.cpp: In function ‘int main()’: prog.cpp:6:19: error: initializer-string for array of chars is too long [-fpermissive] char str[3] = “gfg”; ^

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
Output?

#include < iostream >
using namespace std;
~~~

int main()
{
char str[] = {‘g’, ‘f’, ‘g’};
cout &laquo_space;str;
return 0;
}
~~~

A

O/p: gfg¸ü

Some random characters at the end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
#include
using namespace std;

int main()
{
char str[] = {‘g’, ‘f’, ‘g’, ‘\0’};
cout &laquo_space;str &laquo_space;endl;
cout &laquo_space;sizeof(str) &laquo_space;endl;
return 0;
}
~~~
~~~

A

O/p:

gfg

4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
Output?

#include < iostream >
#include < cstring >
using namespace std;
~~~

int main()
{
char s1[] = “abc”;
char s2[] = “bcd”;
int res = strcmp(s1, s2);
if (res > 0)
cout &laquo_space;“Greater”;
else if(res == 0)
cout &laquo_space;“Same”;
else
cout &laquo_space;“Smaller”;
}
~~~

A

O/p: Smaller

strcmp compares the string lexicographically, i.e “aa” is greater than “a”.

“ab” is greater than “aa”. “bcc” is smaller than “bcd”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
#include < iostream >
#include < cstring >
using namespace std;

int main()
{
char str[5];
str = “gfg”;
//strcpy(str, “gfg”);
cout &laquo_space;str;
return 0;
}
~~~
~~~

A

o/p:

Compile error:

prog.cpp: In function ‘int main()’: prog.cpp:8:9: error: incompatible types in assignment of ‘const char [4]’ to ‘char [5]’ str = “gfg”; ^

Resolution:

To resolve the above error, use strcpy to copy the string.

17
Q
Output?

#include < iostream >
#include < cstring >
using namespace std;
~~~

int main()
{
string str = “geeksforgeeks”;
cout &laquo_space;str.length() &laquo_space;” “;
str = str + “xyz”;
cout &laquo_space;str &laquo_space;” “;
cout &laquo_space;str.substr(1, 3) &laquo_space;” “;
cout &laquo_space;str.find(“eek”) &laquo_space;” “;
return 0;
}
~~~

A

O/p:

13 geeksforgeeksxyz eek 1

string is a class in C++. So, it will have members and functions. Here str is an object and length, substr, find are functions. '+ "xyz"' is a operator overload, where it concatenates the string.

find() here will find the string in the given string and return the starting index of the found string, else it returns string::npos which sopecifies no position/string found.

18
Q
#include < iostream >
#include < cstring >
using namespace std;

int main()
{
    string str;
    cout << "Enter your name";
    getline(cin, str);
    cout << "\nYour name is " << str;
    return 0;
}
A

Using getline, we can read the input in a continuous string with spaces, until an enter is hit.

19
Q
#include < iostream >
#include < cstring >
using namespace std;

int main()
{
    string str;
    cout << "Enter your name";
    getline(cin, str, 'e');
    cout << "\nYour name is " << str;
    return 0;
}

Input: Sandeep

A

O/p: Sand

20
Q
#include < iostream >
using namespace std;

int main()
{
    int x = 10;
    int &y = x;
    cout << y << " ";
    x = x + 5;
    cout << y << " ";
    y = y + 5;
    cout << x << " ";
    return 0;
}
A

O/p:

10 15 20

  • References create an alias
  • Must be assigned when declared
  • Cannot refer to another location once assigned
  • Cannot be NULL
  • Safer than pointers
  • Easy to use(without any dereference, etc., like pointers)
21
Q
#include < iostream >
using namespace std;

void swap(int &x, int &y)
{
int temp = x;
x = y;
y = temp;
}
~~~

int main()
{
int x = 10, y = 15;
swap(x, y);
cout &laquo_space;x &laquo_space;” “ &laquo_space;y;
return 0;
}
~~~

A

O/P:

15 10

  • When references are removed in the swap() here the exchange will fail
22
Q
Output?

#include < iostream >
#include < bits stdc++.h >
~~~

using namespace std;

void Print(vector &v)
{
    for(auto x: v)
        cout << x << " ";
}
int main()
{
    vector < int > v;
    for (int i = 0; i < 1000; i++)
        v.push_back(i);
    Print(v);
    return 0;
}

~~~

A

O/P:

  • Prints from 1 to 1000. But, an important point to note here is when we use a reference the copy of vector is not created when Print() is called unless regular variables do.
  • So, references are good and easy to use to refer to large objects like here in this example, which avoids unwanted copies.
23
Q
Output?

#include < iostream >
#include < bits/stdc++.h >
using namespace std;
~~~

int main() 
{
    vector < int > vect{10, 20, 30, 40};
    for(auto x: vect)
        x = x+5;
    for (auto x: vect)
        cout << x << " ";
    return 0;
}

~~~

A

O/p:

10 20 30 40

  • When reference is not used a copy of vector is created by x variable in for each loop here.
  • So, as x is not referenced to original vector, values are not modified in original vector.
  • By using reference here, the variable x will be referenced and avoid copy as below:
int main() 
{
    vector < int > vect{10, 20, 30, 40};
    for(auto &x: vect)
        x = x+5;
    for (auto x: vect)
        cout << x << " ";
    return 0;
}

O/p: 15 25 35 45

24
Q
#include < iostream >
#include < bits/stdc++.h >  
using namespace std;

int main()
{
vector vect{“geeksforgeeks practice”, “geeksforgeeks ide”, “geeksforgeeks write”};
for (const auto &x: vect)
cout &laquo_space;x &laquo_space;” “;
return 0;
}
~~~
~~~

A

O/p:

geeksforgeeks practice geeksforgeeks ide geeksforgeeks write

  • Having const reference will avoid modifying the original variable referenced.
25
Q
#include < iostream >
#include < bits/stdc++.h >  
using namespace std;

int main()
{
vector vect{10, 20, 30, 40};
for(const auto &x: vect)
x = x+5;
for (auto x: vect)
cout &laquo_space;x &laquo_space;” “;
return 0;
}
~~~
~~~

A

O/p:

Compile error:

In function ‘int main()’: prog.cpp:9:11: error: assignment of read-only reference ‘x’ x = x+5;

26
Q
#include < iostream >
using namespace std;

int main()
{
int x = 10, z = 20;
int &y = x;
y = z;
y = y+5;
cout &laquo_space;x &laquo_space;” “ &laquo_space;y &laquo_space;” “ &laquo_space;z;
return 0;
}
~~~
~~~

A

O/P:

25 25 20

  • References once assigned, cannot be referenced to other
  • Here value of z is assigned to both x and y, as they are referencing to same location, but z will not be referenced again.
27
Q
Output?

#include < iostream >
using namespace std;
~~~

int &fun()
{
    static int x = 10;
    return x;
}
int main()
{
    int &z = fun();
    cout << fun() << " ";
    z = 30;
    cout << fun();
    return 0;
}

~~~

A

O/p:

10 30

  • Note: Never return non-static variable as a reference, as the value will be lost. Only with static, the value is still retained even after function return.
28
Q
Output?

#include < iostream >
using namespace std;
~~~

int main() {
int arr[] = {10, 20};
int p1 = arr;
++
p1;
cout &laquo_space;arr[0] &laquo_space;” “ «
arr[1] &laquo_space;” “ &laquo_space;*p1;
return 0;
}
~~~

A

O/P:

10 20 11

Here, * ++ have the same precedence, so associativity applies from left to right, and expression is read as ++(*p1).

29
Q
Output?

#include < iostream >
using namespace std;
~~~

int main() {
int arr[] = {10, 20};
int *p2 = arr;
*p2++;
cout &laquo_space;arr[0] &laquo_space;” “ «
arr[1] &laquo_space;” “ &laquo_space;*p2;
return 0;
}
~~~

A

O/P:

10 20 20

30
Q
Output?

#include < iostream >
using namespace std;
~~~

int main() {
int arr[] = {10, 20};
int *p2 = arr;
cout &laquo_space;*p2++ &laquo_space;” “;
cout &laquo_space;arr[0] &laquo_space;” “ «
arr[1] &laquo_space;” “ &laquo_space;*p2;
return 0;
}
~~~

A

O/p:

10 10 20 20

31
Q
Output?

#include < iostream >
using namespace std;
~~~

int main() {
int arr[] = {10, 20};
int *p3 = arr;
*++p3;
cout &laquo_space;arr[0] &laquo_space;” “ «
arr[1] &laquo_space;” “ &laquo_space;*p3;
return 0;
}
~~~

A

O/P:

10 20 20

32
Q
Output?

#include < iostream >
using namespace std;
~~~

int main() {
int arr[] = {10, 20};
int *p3 = arr;
cout &laquo_space;*++p3 &laquo_space;” “;
cout &laquo_space;arr[0] &laquo_space;” “ «
arr[1] &laquo_space;” “ &laquo_space;*p3;
return 0;
}
~~~

A

O/P:

20 10 20 20

33
Q
Output?

#include < iostream >
using namespace std;
~~~

int main() {
    char arr[] = {1, 2, 3};
    char *ptr = arr;
    cout << sizeof(arr) << " ";
    cout << sizeof(ptr) << " ";
    return 0;
}

~~~

A

O/P:

3 8

34
Q
Output?

#include < iostream >
using namespace std;
~~~

int main() {
    int arr[] = {1, 2, 3};
    int *ptr = arr;
    cout << sizeof(arr) << " ";
    cout << sizeof(ptr) << " ";
    return 0;
}

~~~

A

O/p:

12 8

35
Q
Output?

#include < iostream >
using namespace std;
~~~

int main() {
int arr[] = {20, 30, 40, 50};
int *ptr = arr;
cout &laquo_space;*(ptr + 2);
}
~~~

A

O/P:

40

36
Q
Output?

#include < stdio.h >

struct Point
{
  int x;
  int y;
};

int main()
{
struct Point p = {.y = 10};
struct Point t;
printf(“%d %d”, t.x, p.x);
return 0;
}
~~~
~~~

A

O/p:

Random value(121332) 0

Struct initialized partially gets default values as 0 for uninitialized variables in the struct.

If the struct is not initialized at all, it will get random values.

37
Q
#include < iostream >
using namespace std;

struct Student
{
  int roll;
  int age;
  string name;
  string address;
};

void print(const Student &s)
{
   cout << s.roll << ' '
        << s.name << ' '
        << s.age << ' '
        << s.address;
}

int main()
{
Student s = {2323, 38, “Sandeep”, “GFG”};
print(s);
return 0;
}
~~~
~~~

A

O/p:

2323 Sandeep 38 GFG

38
Q
#include < iostream >
using namespace std;

class BaseClass
{
public:
//make private variables public
int x;
};
~~~

//make derivation public also
class DerivedClass : public BaseClass {};
struct BaseStruct
{
    int x;
};
// Need not specify public keyword
struct DerivedStruct : BaseStruct {};

int main()
{
{
DerivedStruct d;
d.x = 20;
cout &laquo_space;d.x &laquo_space;‘\n’;
}

{
    DerivedClass d;
    d.x = 20;
    cout << d.x << '\n';
}

return 0; } ~~~
A

O/p:

20 20

  • Structures have same functionality as classes, except that structure members are public by default and classes have private members by default.
  • A structure can have cconstructor, member functions exactly like classes, if we have to make default members to be private, can be declared as private.
39
Q

When to use a Struct and Class?

A
  • When purely object-oriented software is being designed, it is recommended to use Class to leverage the pure OO behavior of classes.
  • When we are designing a mathematical or functional behaved software, we can use Struct