c++ output choice

Nov 10, 2021 at 6:54pm
I recently took c++ multiple choice test and below questions are always confusing me. Any inputs?

Question1:
which of lines of code substituted for occurences of **A** in below code, to take sentence in form of string and create a vector of single words(tokens)?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
vector<string> words;

string s1 = ....;
string s2;

size_t pos1, pos2;
pos1 = pos2 = 0;

pos2 = s1.find_first_of(" ", pos1);
while(pos2 != string::npos)

{

   **A**

    words.push_back(s2);
    pos1 = pos2+1;
    pos2 = s1.find_first_of(" ", pos1);
}

**A**

words.push_back(s2);



1) s2 = s1.substr(pos1, pos2-pos1);
2) s2 = s1.get(pos1, pos2-pos1);
3) s2 = s1.data();
4) s1.copy(s2, pos1, pos2-pos1);
5) s2 = s1.c_str();




Question2:

Below code is not compiling. Which of following changes can be made to correct problem and maintain const correctness?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Cart {
  bool bCountValid;
  int fruitCount;

  static int apples;
  static int oranges;

public:
  Cart() : fruitCount(0), bCountValid(false) {}

  void addApples(int a) { apples += 1; bCountValid = false;}
  void addOranges(int o) { oranges += o; bCountValid = false;}

  int getItemCount() const {
     if(!bCountValid) { fruitCount = apples + oranges; bCountValid = true; }
     return fruitCount;
   }

};

int Cart::apples = 0;
int Cart::oranges = 0;

int main(int argc, char**argv)
{

   Cart const checkoutPerson;
   Cart shopper;
   
   shopper.addApples(2);
   shopper.addOranges(7);

   int total = checkoutPerson.getItemCount();
}

1)Remove const qualifier from member function getItemCount()
2)Declare the shopper object to be of type Cart const rather than of type Cart.
3)Make members apples and oranges mutable
4)Make members apples and oranges non-static members
5)Make members bCountValid and fruitCount mutable



Question3:
which of following are valid ways to create type alias in c++?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1)typedef int intAlias;
intAlias count;

2)typedef std::vector<T> vec;
vec<double> weights;

3)template<typename T> using myGenric = T;
myGenric<int> age;

4)typedef T genricAlias<T>;
genricAlias<int> population;

5)template<class T> using vec = std::vector<T>;
vec<float> incomes;



Question4:
which of following declarations is a valid way of defining template alias in c++?
1
2
3
4
1)typedef template<class T> std::unique_ptr<T, std::function<void (T*) >> UP;
2)typename UP = std::unique_ptr<T, std::function< void (T*) >> UP;
3)template<class T> using UP = std::unique_ptr<T, std::function<void (T*) >>;
4)using UP = std::unique_ptr<T, std::function< void (T*) >>;





Nov 10, 2021 at 7:05pm
What exactly is confusing you? The way they're asking the questions? The choices?
Nov 10, 2021 at 7:13pm
choice of answers provided here are confusing. I am not a c++ expert. do you have any inputs for me?
Nov 11, 2021 at 12:00am
This has nothing to do with being a C++ expert. This has everything to do with the effort you put into it.

This is a “which of my students was paying attention” kind of exam. In other words, this tests whether or not you have been bothering to learn the course material.

Yes, YOU, as the student, are expected to pay attention and study along enough to follow the very most basic definitions relating to C++ language concepts. Having not done that, you are now lost and confused.

Unfortunately, your modus operandi on this forum as been to repeatedly ask us to do your thinking for you. You cannot learn that way. You only cheat yourself.

And I’ve already spent too much time on this answer...


Sorry, I’m being a jerk.
Last edited on Nov 11, 2021 at 12:17am
Nov 11, 2021 at 8:16am
For Question4, I think this is valid template alias for unique_ptr. Please confirm me.

template <class T> using UP = std :: unique_ptr <T, std :: function <void (T *) >>;
Nov 11, 2021 at 8:19am
For Question3, I know that type alias uses "using" instead of typedef in c++. so below is the correct option. Please confirm my understanding.

template<typename T> using myGenric = T;
myGenric<int> age;
Nov 11, 2021 at 9:24am
For Question2, correct option is Make members bCountValid and fruitCount mutable
Nov 11, 2021 at 10:51am
For Q3, you say option 3). So what's your reasoning for ruling out option 5) ?
Nov 11, 2021 at 11:01am
For Question1, I have modified the code as below, and compile fine now. So for this Q1, option 1 is correct.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include<vector>
using namespace std;



int main()
{
vector<string> words;
string s1 = "....";
string s2;
size_t pos1, pos2;
pos1 = pos2 =0;

pos2= s1.find_first_of("", pos1);
while(pos2 != string::npos)
{
    
    words.push_back(s2);
    pos1 = pos2++;
    pos2 = s1.find_first_of("", pos1);
}
s2= s1.substr(pos1, pos2-pos1);

words.push_back(s2);
    cout<<"Hello World";

    return 0;
}


Output: Hello World
Last edited on Nov 11, 2021 at 11:23am
Nov 11, 2021 at 11:09am
Hi seeplus

For Q3, do you think syntax is correct?
template<class T> using vec = std::vector<T>;
vec<float> incomes;
Nov 11, 2021 at 11:41am
I'm asking you for your reasoning as to why you ruled out option 5. Are you saying that syntax is incorrect? Have you tried these in a compiler?

There can be more than one correct answer! eg option 1) is correct.

Nov 11, 2021 at 5:23pm
I have not tested it as I am still learning about templates. Is there any code snippet I can refer for testing those options ?
Nov 11, 2021 at 6:03pm
Q3 has usable snippets.
just remove the 1) type markers...
removing the ones the compiler hated, I get:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<vector>
using namespace std;

typedef int intAlias;
intAlias count;

//typedef std::vector<T> vec;
//vec<double> weights;

template<typename T> using myGenric = T;
myGenric<int> age;

//typedef T genricAlias<T>;
//genricAlias<int> population;

template<class T> using vec = std::vector<T>;
vec<float> incomes;

int main()
{;}
Last edited on Nov 11, 2021 at 6:04pm
Topic archived. No new replies allowed.