Software Engineering

Ace your homework & exams now with Quizwiz!

You will be given an array of N integers and you have to print the integers in the reverse order.

CORRECT ANSWER: A The answer is A: #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; for (int i = n - 1; i >= 0; i--) cout << arr[i] << ' '; return 0; }

A C++ program to print out the phrase "Hello World!" may be which one of the following?

CORRECT ANSWER: A The answer is A: #include <iostream> #include <cstdio> using namespace std; int main() { printf("Hello, World!"); return 0; }

You have to write a function int max_of_four(int a, int b, int c, int d) which reads four arguments and returns the greatest of them.

CORRECT ANSWER: A The answer is: #include <bits/stdc++.h> using namespace std; int max_of_four(int a, int b, int c, int d) { return max(max(a, b), max(c, d)); } int main() { int a, b, c, d; scanf("%d %d %d %d", &a, &b, &c, &d); int ans = max_of_four(a, b, c, d); printf("%d", ans); return 0; }

A C++ program requirement: Input format: You will be given two positive integers, a and b (a <= b), separated by a newline.Output format: For each integer n in the interval [a,b]: If 1 <= n <= 9, then print the lowercase English word corresponding to the number (e.g., one for 1, two for 2), else if n > 9 and it is an even number, then print "even," else if n > 9 and it is an odd number, then print "odd."

CORRECT ANSWER: A The answer is: #include <bits/stdc++.h>using namespace std; int main() { string s[] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; int a, b; cin >> a >> b; for (int i = a; i <= b; ++i) if (1 <= i and i <= 9) cout << s[i - 1] << endl; else cout << (i % 2 ? "odd" : "even") << endl; return 0;} The answer is NOT: #include <bits/stdc++.h> int main() { string s[] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; int a, b; cin >> a >> b; for (int i = a; i <= b; ++i) if (1 <= i and i <= 9) cout << s[i - 1] << endl; else cout << (i % 2 ? "odd" : "even") << endl; return 0;}

A C++ program to read three numbers from stdin and write their sum to stdout may be which of the following?

CORRECT ANSWER: C The answer is C: #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int s = 0; int t; while(cin>>t){ s+=t; } cout<<s; return 0; };

Use the following code segment for this question: #include <iostream> using namespace std; class Shape { public: // pure virtual function providing interface framework. void setWidth(int w) { width = w; } protected: int width; int height;}; class Rectangle { public: int getArea() { return (width * height); }}; class Triangle: public Shape { public: int getArea() { return (width * height)/2; }}; int main(void) { Rectangle Rect; Triangle Tri; Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object. cout << "Total Rectangle area: " << Rect.getArea() << endl; // Print the area of the object. cout << "Total Triangle area: " << Tri.getArea() << endl; return 0;} In order for Rectangle to use the getArea() method, its declaration must be which of the following?

CORRECT ANSWER: Class Rectangle: public Shape

Using C++, print each element on a new line in the same order it was received as input. Note that the floating-point value should be correct up to three decimal places and the double to nine decimal places.Sample Input3 12345678912345 a 334.23 14049.30493Sample Output312345678912345a334.23014049.304930000

CORRECT ANSWER: D The answer is NOT A: #include <iostream> #include <cstdio> using namespace std; int main() { short a; long b; long long c; char d; float e; double f; cin >> a >> b >> c >> d >> e >>f; cout << a << endl << b << endl << c << endl << d << endl; printf("%f\n%lf\n", e, f); return 0; } The answer is NOT B: #include <iostream> #include <cstdio> using namespace std; int main() { int a; long b; long c; char d; float e; double f; cin >> a >> b >> c >> d >> e >>f; cout << a << endl << b << endl << c << endl << d << endl; printf("%f\n%lf\n", e, f); return 0; } The answer is NOT C : #include <iostream> #include <cstdio> using namespace std; int main() { int *a; long *b; long long *c; char *d; float *e; double *f; cin >> a >> b >> c >> d >> e >>f; cout << a << endl << b << endl << c << endl << d << endl; printf("%f\n%lf\n", e, f); return 0; }

Use this code for this question: #include <iostream>using namespace std; class Box { public: double getVolume(void) { return length * breadth * height; } void setLength( double len ) { length = len; } void setBreadth( double bre ) { breadth = bre; } void setHeight( double hei ) { height = hei; } Box operator+(const Box& b) { Box box; box.length = this->length + b.length; box.breadth = this->breadth + b.breadth; box.height = this->height + b.height; return box; } private: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box}; // Main function for the programint main() { Box Box1; // Declare Box1 of type Box Box Box2; // Declare Box2 of type Box Box Box3; // Declare Box3 of type Box double volume = 0.0; // Store the volume of a box here // box 1 specification Box1.setLength(6.0); Box1.setBreadth(7.0); Box1.setHeight(5.0); // box 2 specification Box2.setLength(12.0); Box2.setBreadth(13.0); Box2.setHeight(10.0); volume = Box1.getVolume(); cout << "Volume of Box1 : " << volume <<endl; volume = Box2.getVolume(); cout << "Volume of Box2 : " << volume <<endl; Box3 = Box1 + Box2; return 0;} The statement "Box3 = Box1 + Box2;" is an example of ______.

CORRECT ANSWER: operator overloading INCORRECT ANSWER: a syntax error

You have to create a struct, named Student, representing the student's details, as mentioned below, and store the data of a student.Input format Input format: Input will consist of four lines. The first line will contain an integer, representing age. The second line will contain a string, consisting of lower-case Latin characters ('a'-'z'), representing the first_name of a student. The third line will contain another string, consisting of lower-case Latin characters ('a'-'z'), representing the last_name of a student. The fourth line will contain an integer, representing the standard of student. Note: The number of characters in first_name and last_name will not exceed 50. Output format: Output will be of a single line, consisting of age, first_name, last_name and standard, each separated by one white space. Sample input: 15 john carmack 10 Sample output: 15 john carmack 10

INCORRECT ANSWER A: #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; struct Student{ int age, standard; string first_name, last_name; } st; int main() { Student st; cin >> st.age >> st.first_name >> st.last_name >> st.standard cout << st.age << " " << st.first_name << " " << st.last_name << " " << st.standard; return 0; }

Use this code for this question: #include <iostream> using namespace std; class Box { public: double getVolume(void) { return length * breadth * height; } void setLength( double len ) { length = len; } void setBreadth( double bre ) { breadth = bre; } void setHeight( double hei ) { height = hei; } Box operator+(const Box& b) { Box box; box.length = this->length + b.length; box.breadth = this->breadth + b.breadth; box.height = this->height + b.height; return box; } private: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box }; // Main function for the program int main() { Box Box1; // Declare Box1 of type Box Box Box2; // Declare Box2 of type Box Box Box3; // Declare Box3 of type Box double volume = 0.0; // Store the volume of a box here // box 1 specification Box1.setLength(6.0); Box1.setBreadth(7.0); Box1.setHeight(5.0); // box 2 specification Box2.setLength(12.0); Box2.setBreadth(13.0); Box2.setHeight(10.0); volume = Box1.getVolume(); cout << "Volume of Box1 : " << volume <<endl; volume = Box2.getVolume(); cout << "Volume of Box2 : " << volume <<endl; Box3 = Box1 + Box2; return 0; } What is the volume of Box3?

INCORRECT ANSWER: Cannot be determined INCORRECT ANSWER: 885

cYou have to complete the function void update(int *a,int *b), which reads two integers as arguments, and sets the sum of them and the absolute difference of them.a' = a + bb' = |a - b|

INCORRECT ANSWER: D The answer is NOT A: #include <stdio.h> #include <algorithm> void update(int *a,int *b) { // Complete this function int tmp = (*a)+(*b); int tmp2 = abs((*a)-(*b)); *a = tmp; *b = tmp2; } int main() { int a, b; int *pa = &a, *pb = &b; scanf("%d %d", a, b); update(pa, pb); printf("%d\n%d", a, b); return 0; } The answer is NOT B: #include <stdio.h> #include <algorithm> void update(int a, int b) { // Complete this function int tmp = (*a)+(*b); int tmp2 = abs((*a)-(*b)); *a = tmp; *b = tmp2; } int main() { int a, b; int *pa = &a, *pb = &b; scanf("%d %d", &a, &b); update(pa, pb); printf("%d\n%d", a, b); return 0; } The answer is NOT C: #include <stdio.h> #include <algorithm> void update(int *a,int *b) { // Complete this function int tmp = (*a)+(*b); int tmp2 = abs((*a)-(*b)); *a = tmp; *b = tmp2; } int main() { int a, b; int *pa = &a, *pb = &b; scanf("%d %d", a, b); update(pa, pb); printf("%d\n%d", a, b); return 0; }

Consider the following code example:#include <iostream>#include <vector>#include <cstdlib>#include <string>#include <stdexcept> using namespace std; template <class T>class Stack { private: vector<T> elems; // elements public: void push(T const&); // push element void pop(); // pop element T top() const; // return top element bool empty() const { // return true if empty.return elems.empty(); } }; template <class T>void Stack<T>::push (T const& elem) { // append copy of passed element elems.push_back(elem); } template <class T>void Stack<T>::pop () { if (elems.empty()) { throw out_of_range("Stack<>::pop(): empty stack"); }// remove last element elems.pop_back(); } template <class T>T Stack<T>::top () const { if (elems.empty()) { throw out_of_range("Stack<>::top(): empty stack"); }// return copy of last element return elems.back(); } int main() { try { // manipulate int stack intStack.push(7); cout << intStack.top() <<endl; // manipulate string stack stringStack.push("hello"); cout << stringStack.top() << std::endl; stringStack.pop(); stringStack.pop(); } catch (exception const& ex) { cerr << "Exception: " << ex.what() <<endl; return -1;} }What statement(s) is/are missing in the main method?

INCORRECT ANSWER: There are no missing statements. The code will run correctly as-is. INCORRECT ANSWER: Stack<string> stringStack; // stack of strings

Use the following code segment for this question: #include <iostream> using namespace std; class Shape { public: // pure virtual function providing interface framework. void setWidth(int w) { width = w; } protected: int width; int height;}; class Rectangle { public: int getArea() { return (width * height); }}; class Triangle: public Shape { public: int getArea() { return (width * height)/2; }}; int main(void) { Rectangle Rect; Triangle Tri; Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object. cout << "Total Rectangle area: " << Rect.getArea() << endl; // Print the area of the object. cout << "Total Triangle area: " << Tri.getArea() << endl; return 0;} ______ is a base class and ______ is a derived class.

INCORRECT ANSWER: Triangle, Shape INCORRECT ANSWER: Shape, getArea INCORRECT ANSWER: Triangle, rectangle CORRECT ANSWER: Shape, Triangle

Use this code for this question: #include <iostream> using namespace std; class Box { public: double getVolume(void) { return length * breadth * height; } void setLength( double len ) { length = len; } void setBreadth( double bre ) { breadth = bre; } void setHeight( double hei ) { height = hei; } Box operator+(const Box& b) { Box box; box.length = this->length + b.length; box.breadth = this->breadth + b.breadth; box.height = this->height + b.height; return box; } private: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box }; // Main function for the program int main() { Box Box1; // Declare Box1 of type Box Box Box2; // Declare Box2 of type Box Box Box3; // Declare Box3 of type Box double volume = 0.0; // Store the volume of a box here // box 1 specification Box1.setLength(6.0); Box1.setBreadth(7.0); Box1.setHeight(5.0); // box 2 specification Box2.setLength(12.0); Box2.setBreadth(13.0); Box2.setHeight(10.0); volume = Box1.getVolume(); cout << "Volume of Box1 : " << volume <<endl; volume = Box2.getVolume(); cout << "Volume of Box2 : " << volume <<endl; Box3 = Box1 + Box2; return 0; } To get the volume of Box3, what code must be added to main?

INCORRECT ANSWER: Volume = ½(Box2.getVolume() + Box1.getVolume()); INCORRECT ANSWER: Volume = Box2.getVolume() + Box1.getVolume();

Use the following code segment for this question: #include <iostream> using namespace std; class Shape { public: // pure virtual function providing interface framework. void setWidth(int w) { width = w; } protected: int width; int height;}; class Rectangle { public: int getArea() { return (width * height); }}; class Triangle: public Shape { public: int getArea() { return (width * height)/2; }}; int main(void) { Rectangle Rect; Triangle Tri; Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object. cout << "Total Rectangle area: " << Rect.getArea() << endl; // Print the area of the object. cout << "Total Triangle area: " << Tri.getArea() << endl; return 0;} What method is missing in the base class?

INCORRECT ANSWER: a setWidth method that returns an int CORRECT ANSWER: a void setHeight method

Use the following code segment for this question: #include <iostream> using namespace std; class Shape { public: // pure virtual function providing interface framework. void setWidth(int w) { width = w; } protected: int width; int height;}; class Rectangle { public: int getArea() { return (width * height); }}; class Triangle: public Shape { public: int getArea() { return (width * height)/2; }}; int main(void) { Rectangle Rect; Triangle Tri; Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object. cout << "Total Rectangle area: " << Rect.getArea() << endl; // Print the area of the object. cout << "Total Triangle area: " << Tri.getArea() << endl; return 0;} A pure virtual function that needs to be in the Shape class should be declared as ______.

INCORRECT ANSWER: virtual int getArea(); CORRECT ANSWER B: virtual int getArea() = 0;

You are given the source code in HTML format consisting of lines. You have to answer queries. Each query asks you to print the value of the attribute specified. Print "Not Found!" if there is not any such attribute. Sample input: <tag1 value = "HelloWorld"> <tag2 name = "Name1"> </tag2> </tag1> tag1.tag2~name tag1~name tag1~value Sample output: Name1 Not Found! HelloWorld

The answer is NOT A: #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <map> using namespace std; vector<string> tag_stack; map<string, string> attrs; void insert_attr(string & name, string & val) { string full; for(string & str : tag_stack) full += str + "."; full.pop_back(); full += "~" + name; attrs[full] = val; } int main() { int n, q; cin >> n >> q; for(int i = 0; i < n; ++i) { char c; cin >> c; if(cin.peek() == '/') { // close string cn; cin >> cn; tag_stack.pop_back(); } else { //open' string name; cin >> name; if(name.back() == '>') { //no attrs name.pop_back(); tag_stack.push_back(name); } else { tag_stack.push_back(name); for(;;) { string attr_name, attr_val, eq; cin >> attr_name >> eq >> attr_val; if(attr_val.back() == '<') { //last attr attr_val.pop_back(); attr_val.pop_back(); attr_val = attr_val.substr(1); insert_attr(attr_name, attr_val); break; } else { attr_val.pop_back(); attr_val = attr_val.substr(1); insert_attr(attr_name, attr_val); } } } } } for(int i = 0; i < q; ++i) { string quer; cin >> quer; if(attrs.find(quer) != attrs.end()) cout << attrs[quer] << endl; else cout << "Not Found!" << endl; } return 0; } The answer is NOT B: #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <map> using namespace std; vector<string> tag_stack; map<string, string> attrs; void insert_attr(string & name, string & val) { string full; for(string & str : tag_stack) full += str + "."; full.pop_back(); full += "~" + name; attrs[full] = val; } int main() { int n, q; cin >> n >> q; for(int i = 0; i < n; ++i) { char c; cin >> c; if(cin.peek() == '/') { // close string cn; cin >> cn; tag_stack.pop_back(); } else { //open' string name; cin >> name; if(name.back() == '>') { //no attrs name.pop_back(); tag_stack.push_back(name); } else { tag_stack.push_back(name); for(;;) { string attr_name, attr_val, eq; cin >> attr_name >> eq >> attr_val; if(attr_val.back() == '>') { //last attr attr_val.pop_back(); attr_val.pop_back(); attr_val = attr_val.substr(1); insert_attr(attr_name, attr_val); break; } else { attr_val.pop_back(); attr_val = attr_val.substr(1); insert_attr(attr_name, attr_val); } } } } } for(int i = 1; i < q; ++i) { string quer; cin >> quer; if(attrs.find(quer) != attrs.end()) cout << attrs[quer] << endl; else cout << "Not Found!" << endl; } return 0; }

Consider an n-element array a where each index i contains a reference to an array of integers (where the value of varies from array to array). Given a, you must answer q queries. Each query is in the format (i,j), where i denotes an index in array a and j denotes an index in the array located at a[i]. For each query, find and print the value of element j in the array at location a[i] on a new line.

The answer is NOT A: #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { int n, q; cin >> n >> q; int *arr = new int*[n]; for ( int i = 0, k ; i < n ; ++ i ) { cin >> k; arr[i] = new int[k]; for ( int j = 0 ; j < k ; ++ j ) { cin >> arr[i][j]; } } for ( int i = 0, a, b ; i < q ; ++ i ) { cin >> a >> b; cout << arr[a][b] << endl; } return 0;}

Given a positive integer denoting n, do the following: If 1 <= n <= 9, then print the lowercase English word corresponding to the number (e.g., one for 1, two for 2). If n > 9, print Greater than 9 to the console.

The answer is NOT A: #include <iostream> using namespace std; int main() { string s[] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; int n; cin >> n; cout << ((1 <= n and n <= 9) ? s[n - 1] : "Greater than 9"); return 0;}


Related study sets

Section 10, Unit 1: Types of Defaults

View Set

Chapter 4 : Application Software : Vocabulary

View Set