Question

please, Answer all questions and give a detailed explanation.

What is output by the following? class A public: AO { ++x; } static int x; int A::X - 1; int maino vector<A> vec(8): cout <<

What is output by the following? struct X{ x(int „a, int b) : a(a), b(b) () int a, b: int main x myx(1, 5): cout << myx.a <<

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer 1:

OUTPUT:

2

CODE (WITH COMMENTS):

#include <iostream>
#include <vector>
using namespace std;
class A{
public:
    A() { ++x; } // Constructor which increments x everytime a new object of A is created
    static int x; // static variable is common accross all objects of A
};
int A::x = 1; // Initialize x with 1
int main(){
    vector<A> vec(8);   // Reserves memory for 8 elements of type A and calls constructor of A only once
                        // to initialize all 8 objects and then after initialization destructor is called.
    cout << A::x; // Since, constructor has been called only once, therefore, A::x = 2 at this point of time
    return 0;
}

Answer 2:

OUTPUT:

1

CODE (WITH COMMENTS):

#include <iostream>
#include <vector>
using namespace std;
class A{
public:
    A() { ++x; } // Constructor which increments x everytime a new object of A is created
    ~A() { --x; } // Destructor which decrements x everytime an object goes out of scope
    static int x; // static variable is common accross all objects of A
};
int A::x = 1; // Initialize x with 1
int main(){
    vector<A> vec(5);   // Reserves memory for 5 elements of type A and calls constructor of A only once
                        // to initialize all 5 objects and then after initialization destructor is called.
    // Since, constructor and destructor have been called once, therefore, x remains 1 at this point of time
    
    // A::x < 10 is true because A::x = 1
    if (A::x < 10){
        A b, c, d, e, f, g; // Creates 6 objects of A and A::x becomes 1 + 6 = 7. These objects are local to
                            // this if block only and will be destroyed once goes out of the if block.
    }
    // Here, above 6 objects created goes out of scope therefore destructor will be called for each object
    // and A::x will be decremented 6 times in total. Hence, A::x will become 7 - 6 = 2 at this point of time.
    cout << vec.at(2).x; // vec.at(2).x is same as A::x. Therefore, 1 will be printed here.
    return 0;
}

Answer 3:

OUTPUT:

15

CODE (WITH COMMENTS):

#include <iostream>
#include <vector>
using namespace std;
struct X{
    // Constructor to initialize this->a = _a and this->b = b
    X(int _a, int b) : a(_a), b(b) {}
    int a, b;
};
int main(){
    X myX(1, 5); // Create object of struct with myX.a = 1 and myX.b = 5
    cout << myX.a << myX.b; // Here, myX.a = 1 and myX.b = 5. So, 15 will be printed
    return 0;
}

Answer 4:

OUTPUT:

welp

CODE (WITH COMMENTS):

#include <iostream>
#include <string>
using namespace std;
// This function takes a reference to the string object and an integer value
// and returns a reference to the character stored at i-th index in the string s.
// Reference to ith character is returned so that character can be changed by callee function
char& GetChar(string& s, int i){
    return s.at(i);
}
int main(){
    string message = "Help"; // Here, message[0] = 'H', message[1] = 'e', message[2] = 'l', and message[3] = 'p'
    GetChar(message, 0) = 'w'; // GetChar returns refernce to message[0] and then message[0] = 'w' is done
    // Therefore, message[0] = 'w', message[1] = 'e', message[2] = 'l', and message[3] = 'p'
    // message becomes "welp"
    cout << message; // Prints "welp"
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
please, Answer all questions and give a detailed explanation. What is output by the following? class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • output What is the output of the following: class Access public int x; private int y...

    output What is the output of the following: class Access public int x; private int y public void cal(int x, int y) { this.x = x + 1; this.y = y; } public void print() { System.out.print(""+y); } } class AccessSpecifier { public static void main(String args[]) { Access obj = new Access(); obj.cal(2, 3); System.out.print(obj.x); obj.print(); } } 33 Compilation error 23 None of the available choices Runtime error

  • What is the output for the following program codes? a) [ 0.5 Mark ] class A...

    What is the output for the following program codes? a) [ 0.5 Mark ] class A { int i; } class B extends A { int j; void display() { super.i = j + 1; System.out.println(j + " " + i); }} class inheritance { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); }} OUTPUT: b) [ 0.5 Mark ] class Parent { public void getBike(){ System.out.println("Suzuki Bike"); }} class Child extends Parent {...

  • Please help with the explanation of the of the following code's output: public class ArrayTest {...

    Please help with the explanation of the of the following code's output: public class ArrayTest { public static void main(String[] args) { int [][] myArray = {{3,4},{}}; System.out.print(myArray.length + " "); System.out.print(myArray[0].length + " "); System.out.print(myArray[1].length + " "); System.out.print(myArray[0][0] + " "); Output = 2 2 0 3 Please justfiy this code's output.

  • C++ output 6) What is the exact output of the following program? #include <iostream> using namespace...

    C++ output 6) What is the exact output of the following program? #include <iostream> using namespace stdi void zolo(int &a, int sb) int main int x = 5, y =8; zolo(x,y)i cout << "x " << x << endl ; cout << "y = "" << y << endl ; return o: void zolo(int &a, int &b) int v1 = b + a; ' int v2 = b-a; 3 a=v1;13

  • What is the output of the following code? Explain your answer. public class Test {public static...

    What is the output of the following code? Explain your answer. public class Test {public static void main (String [] args) {System.out. print((int) 5.6); System.out.println((int) 5.2);}}

  • What output is produced by the following program? public class MysteryNums public static void main(String[] args)...

    What output is produced by the following program? public class MysteryNums public static void main(String[] args) - int x = 12; int y = x - 3; 3, sentence (y, x + y); . public static void sentence (int numi, int num2) { 4: System.out.println(num1 + " + num2);

  • What does the program print, and if possible can you give an explanation of how you...

    What does the program print, and if possible can you give an explanation of how you reached the answer? . What will the following program print? class Quiz03 public static void main (String [] args) final int limit = 5; ( string line""i string line = "" ; for ( int j=1 ; j <= row; j++ ) for int row=1; row<=limit; row++ ) line += "*" ; System.out.println( line

  • Find Output. Just output. No explanation needed.. #include <iostream> #include <string> using namespace std; class baseClass...

    Find Output. Just output. No explanation needed.. #include <iostream> #include <string> using namespace std; class baseClass { public: void print() const; baseClass(string s = " ", int a = 0); //Postcondition: str = s; x = a; protected: int x; private: string str; }; class derivedClass: public baseClass { public: void print() const; derivedClass(string s = "", int a = 0, int b = 0); //Postcondition: str = s; x = a; y = b; private: int y; }; int...

  • C++ OBJECT ORIENTED PROGRAMMING 12. What will be the output of the following C++ code? #include...

    C++ OBJECT ORIENTED PROGRAMMING 12. What will be the output of the following C++ code? #include <iostream> #include <vector> using namespace std; int main() { vector<int> myvector; myvector.push_back(78); myvector.push_back(16); myvector.front() += myvector back(); cout << myvector.front() << '\n'; return 0; } a) 78 b) 16 c) 94 d) 86 13. What is the syntax of class template? a) template <paramaters> class declaration b) Template <paramaters> class declaration c) temp <paramaters> class declaration d) Temp <paramaters> class declaration sums the values...

  • What is the output of the expression 60%3, which makes use of the modulo operator? Answer:...

    What is the output of the expression 60%3, which makes use of the modulo operator? Answer: Answer When new data are to be inserted into a data structure, but there is no available space; this situation is usually called... Select one: a. Underfow b. Overfow c. None of the above d. Saturated e. Housefull What is the output of the following program? #include <iostream> using namespace std; class Student public: double mathematics; double physics; double biology; }; int main({ Student...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT