B. Linker During Linking - Correct
B. Linker During Linking - Correct
Mark: 2
Q2. Which of the following is the binary representation (LSB on right) of 0xA49C
Mark:2
- takes one parameter named ‘arr’ which is reference to a 2-dimensional char array of 5 rows
and 6 columns
Marks: 4
std::pair test(char(&arr)[5][6]);
Q4. Use range based for loop in program which uses the following array and updates it such that the
even numbers get doubled, but no change is done to odd numbers.
Then, use another range based for loop to print the arr array to console. The output when this
program is run should look like:
20 21 13 68 100 65 144
Marks: 6
void Q4()
{
Q5. For a 2-d matrix, the right most column should have sum of respective rows and bottom row
should have sum of respective columns. eg:
20 -10 20 30
20 20 20 60
30 -10 60 80
Write a program to do so. Use following hardcoded matrix ‘m’ as input. Use another matrix for
preparing the result as above. You do not need to print anything to output.
{20,20,20},
{-10,-20,20}};
Marks: 8
void Q5()
{20,20,20},
{-10,-20,20} };
int n[4][4];
n[i][j] = m[i][j];
Q6. Write a program to create a 2-d array of 4 rows x 50 columns and copy following strings to it in
order of their length, first being the string with least length. Your program should work correctly
regardless of order of the strings in array below.
"Small",
"Medium string"};
Marks: 9
void Q6()
"Small",
"Medium string" };
char output[rows][50];
std::pair lenIndex[rows]; // array of pairs, each pair stores length of string and its
position/index in input array
lenIndex[i].first = strlen(input[i]);
lenIndex[i].second = i;
auto t = lenIndex[i];
lenIndex[i] = lenIndex[j];
lenIndex[j] = t;
strcpy_s(output[i], strlen(input[lenIndex[i].second]) + 1,
input[lenIndex[i].second]);
Q7. Complete the following function for bitwise operations according to the comments provided. In
you answer, write the whole function. You are not allowed to modify the code already written and it
should be used in the portion that you write.
void bitwiseTest ()
uint bitVal{0};
uint bcv{0};
uint pos{0};
//<write code>
//<write code>
30117
30629
30597
Marks: 9
void Q7()
pos = 2;
pos = 9;
op = op | bcv;
pos = 5;
op = op & ~bcv;
void main()
Q7();
return (i + j * i % j);
Ans:
return (i + j * i % j);
}
2. These days vehicles in India have two kinds of number plates - HSRP (high security registration
plate) and regular. HSRP has an unique ID (integer), which regular one doesn't have. We want to
capture information of both number plates in a single structure. For regular plate, we want to store
the word "regular" instead of ID (incase of HSRP). How will you do that?
Ans:
HSRP,
REG,
struct RC
RCType type;
char* rcNum;
union hsrp
int id;
char* regular;
};
3. Use the RC struct declared above to create few number plates on the Free Store by allocating
memory properly and then deallocating at the end of the function / program.
void main()
//consts
//input
RC* rcs[SIZE];
auto l = strlen(rcValues[i])+1;
strcpy_s(rcs[i]->rcNum, l, rcValues[i]);
if (rcs[i]->type == RCType::HSRP)
rcs[i]->hsrp.id = rand()%100000;
else
auto rl = strlen(reg)+1;
// ...
// done with rcs. delete memory allocated on FS
if (rcs[i]->type == RCType::REG)
delete[] rcs[i]->hsrp.regular;
delete[] rcs[i]->rcNum;
delete rcs[i];
Q2. Write a program to count the number of words in a sentence. You do not need to take input from user.
Use the following and print the result to console. The program should not count extra spaces eg. around
8
word ‘one’.
For simplicity, assume that you do not need to deal with sentences with leading/trailing spaces.
void main(){
char str[] = "The multi - word sentence could have more than one spaces between words";
char c;
int word = 1, check = 0,i = 0;
while (str[i]) {
c = str[i];
if (isspace(c) && check == 0) {
word++;
check = 1;
}
else {
check = 0;
}
i++;
}
cout << "Number of words : " << word << endl;
}
Q1. See the program below and find out data type and qualifier you think is most appropriate for the
variables. Take hints from comments. When run, the program gives following output:
4
50000 with interest of 8.3 compounded quarterly, will become 96469.73 after 8
years.
void Q1()
// example input:
p = 50'000.00;
t = 8;
cout << p << " with interest of " << r << " compounded quarterly, will
become " << fixed << setprecision(2) << a <<" after " << t << " years.";
void main() {
const float r{ 8.3 }; // interest in percent, fraction, constant value
unsigned int p{}; // principal, large, only +ve amount, fraction
int t{}; // duration in complete years, no fraction
const int n{ 4 }; // compounded quarterly, constant value
// example input:
p = 50'000.00;
t = 8;
cout << p << " with interest of " << r << " compounded quarterly, will become " << fixed << a << " after
" << t << " years.";
}
if (titletoCost.count(input) == 0) {
else {
cin.get();
system("cls");
Q7. In C++ if a file is to be opened for output to, in append mode and the data to be written is binary how
will you do that? Use example.bin as file name and myFile as variable name. Assume no path is required, just
Q8. Complete the Compact ByXOR function in the following program. It bitwise XORs-first, all the pages
together
to first page-1.e. first cells of all pages are XORed and the result is stored in the first cell of first page
and so on. After that it takes the first page and compacts (l.e. XORs) all rows of the first page into first
row-l.e. all first cells of each row in the page are XORed and result stored in the first cell of first row and
so on. Then, in 3d loop, it XORs all cells of the first row to first cell of the row. Use concepts of page, row
and column pointers.
const int R = 4, C = 3, P = 3;
//bitwise XOR the pages to compact them to 2d (in 1st page itself) // i.e. each cell of 1st page is bitwise-xored
with same cell of the // other two pages and result is stored in the 1st page in that cell.
// bitwise XOR the rows to compact them to 1d (in 1st row of 1st page itself)
// bitwise XOR the cells of 1st row of 1st page to get the result in // first cell and return that
int result = 0;
Q5. If you have in your program a function Twice that matches the signature of the
function pointer declared in Q4, then how would you let the function pointer of Q4 point to Twice function?
fp = (fp)Twise;
Q6. Imagine that you have Student structure. Fill in the code below for proper allocation and disposal of
Student *p[10];
// write for loop to create 10 Student nodes on free store and keep them in the array created above
/* imagine that the Student objects are getting used by code represented
by this comment */
// we are done with using the Student objects. Dispose all Student objects
delete[] p[i];
p[i] = nullptr;
delete[] p;
14. Complete the function CpiCount below. The idea is to efficiently know how many students have a
particular
cpi (like, 1.6 in the code below) and print the student's info of all such students. Use appropriate STL container.
void CpiCount()
auto v = CreateSomeStudents();
unordered_multimap<Student> cpis;
for(auto s : v)
cpis.insert(s);
// create "empty" Student object but with the CPI we want to find Student checkCpl -("", 0, 8.6);
auto p= cpis.equal_range(checkCpi);
PrintStudentinfo(*itr);
Q12. Complete the following function which iterates over the vector v to create another sequence in which all
female students are in the front of the queue and male students are placed after them efficiently. Iterate over
void FM()
auto v= CreateSomeStudents();
deque<student> dq;
for (auto s: v) {
dq push_front(s);
else
dq.push_back(s);
PrintStudentInfo (*itr);
how would you call this function passing it two string params a and b?
b) template<string>larger(a, b);
d) <string>larger(a, b);
Q6. For the template function defined in Q5, you specialize it for a type named Player. The prototype of this
Q7. How would you declare an iterator itr to a vector of elements where each element is a pair of string and
integer?
a) iterator<vector<pair<string, int>::itr;
b) vector<pair<string, Int>>::iterator itr; - correct
c) auto itr;
void main()
string s{"abcdef"};
s[3] = 'z';
a[0] = 'x';
a) bcde abcdez
c) xcdz xbczef
d) abcd xbczef
09. Someone wrote the following program. You observed that it can be improved in terms of avoiding
unnecessary
copies of the string objects. So, you commented the PrintSubString() function out:
/*
{
if (fullString.length() >= startindex + count)
*/
void main()
PrintSubString(s, 4, 10);
Rewrite the PringSubString() function that you as a programmer would write as an improvement to avoid
copies of
strings.
For Q10 to 014, we'll use following partial program and you will be asked to complete / write the rest of the
enum Gender{
female, male
};
struct Student{
string name;
long id;
float cpi;
Gender gender;
};
vold PrintStudentInfo(Student s)
cout << s.id << '' << s.cpl << '' << s.name << '' << (s.gender == female ? "F" : "M") << endl;
vector<Student> CreateSomeStudents()
vector<Student> v;
return v;
Q10. Create a struct functor CompareByCpi which compares cpi of two students and returns true if student in
first
Struct CompareByCpi {
}
};
Q11. Assuming struct CompareByCpi functor is available from Q10 above, complete the SortByCpi function
below which
uses priority queue container and the CompareByCpi functor to print the list of students in vector v sorted by
vold SortByCpi()
auto v = CreateSomeStudents();
for(auto s : v)
sq.push(s);
PrintStudentInfo(sq.top());
sq.poll();
// the authors in Book object could be comma separated list. So could be keywords.
// so we write a common function to extract list of sub-strings comma separated in given string
//i.e. original is like "a,b c,d" and not like "a, b c, d"
vector<string> list;
for(;;) {
if (startPos != original.length()) {
list.push_back(original.Substr(startPos));
list.push_back(original.substr(startPos, pos));
startPos = pos + 1;
return list;