A few c++ questions | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 4

A few c++ questions

1) In order to use the string data type in a program, you must include the ____ directive in your program. 1. #include <iostream> 2. #include <iomanip> 3. #include <string> 4. using namespace std; 2) The _____ function allows you to access any number of characters contained in a string variable. 1. remove 2. getline 3. substr 4. partial 3) Which one of the following options represents the output of the program below? struct pixel { int c, r; }; void display(pixel p) { cout << "col "<< p.c << " row " << p.r << endl; } int main() { pixel x = {40,50}, y, z; z = x; x.c += 10; y = z; y.c += 10; y.r += 20; z.c -= 15; display(x); display(y); display(z); return 0; } 1. Col 50 Row 50 Col 50 Row 70 Col 25 Row 50 2. Col 40 Row 40 Col 40 Row 70 Col 25 Row 50 3. Col 60 Row 50 Col 60 Row 80 Col 25 Row 50 4. Col 50 Row 70 Col 50 Row 55 Col 25 Row 50 4) Consider the following statements: struct studentType1 { string name; int ID; float gpa; }; struct studentType2 { string name; int ID; float gpa; }; studentType1 student1, student2; studentType2 student3, student4; Which of the following statements is valid in C++? 1. student2.ID = ID; 2. student2 = student3; 3. student1 = student4; 4. student1.ID = student3.ID; 5) Consider the following statements: struct rectangleData { float length; float width; float area; float perimeter; }; rectangleData bigRect; rectangleData smallRect; Which of the following statements is legal in C++? 1. if (bigRect == smallRect) 2. if (bigRect != smallRect) 3. if (bigRect.length == width) 4. if (bigRect.length == smallRect.width) 6) Consider the following statements: struct circleData { float radius; float area; float circumference; }; circleData circle; Which of the following statements is valid in C++? 1. cin >> circle.radius; circle.a

28th Sep 2017, 9:15 PM
gorgamin
gorgamin - avatar
7 Answers
+ 2
1)3
25th Sep 2017, 6:54 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 1
Are these all challenge questions?
25th Sep 2017, 6:35 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 1
Just homework questions I'm not sure of.
25th Sep 2017, 6:55 AM
gorgamin
gorgamin - avatar
+ 1
Okay
25th Sep 2017, 6:56 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 1
Would you agree with my solutions? 1) ? 2) 2 3) 1 4) 4 5) 4 6) 4 7) ? 8) 2 9) 2 10) 4 11) 2 12) 3
25th Sep 2017, 6:59 AM
gorgamin
gorgamin - avatar
+ 1
Thanks for the feedback. I updated this question and deleted the other one.
25th Sep 2017, 7:02 AM
gorgamin
gorgamin - avatar
0
A few more.... 7) Comment on the output of this C++ code? #include <iostream> struct temp { int a; int b; int c; }; int main() { struct temp p[] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; return 0; } 1. No Compile time error, generates an array of structure of size 3 2. Compile time error, illegal declaration of a multidimensional array 3. No Compile time error, generates an array of structure of size 9 4. Compile time error, illegal assignment to members of structure 8) Consider the following array declaration: int sales[5] = {10000, 12000, 900, 500, 20000}; The statement sales[3] = sales[3] + 10; will replace the number _____ 1. 500 with 10 2. 500 with 510 3. 900 with 10 4. 900 with 910 9) Consider the following array declaration: int sales[5] = {10000, 12000, 900, 500, 20000}; The statement sales[4] = sales[4-2]; will replace the number _____ 1. 500 with 498 2. 20000 with 900 3. 20000 with 19998 4. 500 with 12000 10) Consider the following array declaration: int sales[5] = {10000, 12000, 900, 500, 20000}; Which of the following if-statements verifies that the array subscript stored in the x variable is valid for the sales array? 1. if (x >= 0 && x < 4) 2. if (x >= 0 && x <= 4) 3. if (sales[x] >= 0 && sales[x] < 4) 4. if (sales[x] >= 0 && sales[x] <= 4) 11) Which of the following function header correctly includes a two dimensional array as its parameter? 1. void display(int [][COLS]); 2. void display(int [ROWS][]); 3. void display(int [][]); 4. void display(int [ROWS][COLS]); 12) Suppose that gamma is an array of 50 elements of type int and j is an int variable. Which of the following for loops sets the subscript of gamma out-of-range? 1. for (j = 0; j <= 49; j++) cout << gamma[j] << " "; 2. for (j = 1; j < 50; j++) cout << gamma[j] << " "; 3. for (j = 0; j <= 50; j++) cout << gamma[j] << " "; 4. for (j = 0; j <= 48; j++) cout << gamma[j] << " ";
25th Sep 2017, 6:58 AM
gorgamin
gorgamin - avatar