Question 1 of 25
Hints left: 3
Q1
What will be the output of the following C++ code? #include <iostream> #include <string> using namespace std; class Box { int capacity; public: Box(int cap){ capacity = cap; } friend void show(); }; void Box::show() { Box b(10); cout<<"Value of capacity is: "<<b.capacity<<endl; } int main(int argc, char const *argv[]) { show(); return 0; }
Q2
What will be the output of the following C++ code? #include <iostream> #include <string> using namespace std; int main () { string str ("steve jobs is legend"); string::iterator it; str.erase (str.begin()+ 5, str.end()-7); cout << str << endl; return 0; }
Q3
What will be the output of the following C++ code? #include <iostream> using namespace std; class Distance { private: int feet; int inches; public: Distance() { feet = 0; inches = 0; } Distance(int f, int i) { feet = f; inches = i; } Distance operator()(int a, int b, int c) { Distance D; D.feet = a + c + 10; D.inches = b + c + 100 ; return D; } void displayDistance() { cout << feet << inches << endl; } }; int main() { Distance D1(11, 10), D2; cout << "First Distance : "; D1.displayDistance(); D2 = D1(10, 10, 10); cout << "Second Distance :"; D2.displayDistance(); return 0; }
Q4
What will be the output of the following C++ code? #include <iostream> using namespace std; class A { public: int x; A(int n = 0) : x(n) {}; int& operator[](int n) { cout << "0" ; return x; } int operator[](int n) const { cout << "1" ; return x; } }; void foo(const A& a) { int z = a[2]; } int main() { A a(7); a[3] = 8; int z = a[2]; foo(a); return 0; }
Q5
What will be the output of the following C++ code? #include <iostream> using namespace std; class sample { private: int var; public: void input() { cout << var; } void output() { cout << "Variable entered is "; cout << var << "\n"; } }; int main() { sample object; object.input(); object.output(); object.var(); return 0; }
Q6
What is Character-Array?
Q7
What will be the output of the following C++ code? #include <iostream> using namespace std; class sample { public: sample() { cout << "X::X()" << endl; } sample( sample const & ) { cout << "X::X( X const & )" << endl; } sample& operator=( sample const & ) { cout << "X::operator=(X const &)" << endl; } }; sample f() { sample tmp; return tmp; } int main() { sample x = f(); return 0; }
Q8
Give the function prototype of the operator function which we need to define in this program so that the program has no errors. #include <iostream> #include <string> using namespace std; class Box{ int capacity; public: Box(){} Box(double capacity){ this->capacity = capacity; } }; int main(int argc, char const *argv[]) { Box b1(10); Box b2 = Box(14); if(b1 == b2){ cout<<"Equal"; } else{ cout<<"Not Equal"; } return 0; }
Q9
What will be the output of the following C++ code? #include <iostream> using namespace std; int main () { int a, b, c; a = 2; b = 7; c = (a > b) ? a : b; cout << c; return 0; }
Q10
Where does the object is created?
Q11
Which of the following is not a modifier function in string class?
Q12
What will be the output of the following C++ code? #include <iostream> #include <string> using namespace std; class Box { int capacity; public: Box(int cap){ capacity = cap; } friend void show(); }; void show() { Box b(10); cout<<"Value of capacity is: "<<b.capacity<<endl; } int main(int argc, char const *argv[]) { show(); return 0; }
Q13
Which of the following statements is NOT valid about operator overloading?
Q14
What will be the output of the following C++ code? #include <iostream> using namespace std; class Box { double width; public: friend void printWidth( Box box ); void setWidth( double wid ); }; void Box::setWidth( double wid ) { width = wid; } void printWidth( Box box ) { box.width = box.width * 2; cout << "Width of box : " << box.width << endl; } int main( ) { Box box; box.setWidth(10.0); printWidth( box ); return 0; }
Q15
subscript operator is used to access which elements?
Q16
Which keyword is used to represent a friend function?
Q17
What will be the output of the following C++ code? #include <iostream> using namespace std; class Complex { private: float real; float imag; public: Complex():real(0), imag(0){} Complex operator ()(float re, float im) { real += re; imag += im; return *this; } Complex operator() (float re) { real += re; return *this; } void display() { cout << "(" << real << "," << imag << ")" << endl; } }; int main() { Complex c1, c2; c2 = c1(3.2, 5.3); c1(6.5, 2.7); c2(1.9); cout << "c2=";c1.display(); cout << "c2=";c2.display(); return 0; }
Q18
What will be the output of the following C++ code? #include <iostream> #include <complex> using namespace std; int main() { complex<int> i(2, 3); i = i * 6 / 3; cout << i; return 0; }
Q19
In the case of friend operator overloaded functions how many maximum object arguments a unary operator overloaded function can take?
Q20
What will be the output of the following C++ code? #include <iostream> #include <string> using namespace std; class Box { int capacity; Box(){} Box(double capacity){ this->capacity = capacity; } }; int main(int argc, char const *argv[]) { Box b1(10); Box b2 = Box(14); return 0; }
Q21
Which of the following operators can't be overloaded?
Q22
What will be the output of the following C++ code? #include <iostream> #include <string> using namespace std; class B { int b; public: B(){} B(int i){ b = i; } int show(){ return b; } }; class C { B b; public: C(int i){ b = B(i); } friend void show(){ C c(10); cout<<"value of b is: "<<c.b.show()<<endl; } }; int main(int argc, char const *argv[]) { show(); return 0; }
Q23
What will be the output of the following C++ code? #include <iostream> #include <string> using namespace std; class A { int a = 5; public: void change(int i){ a = i; } static void value_of_a(){ cout<<a; } }; int main(int argc, char const *argv[]) { A a1 = A(); a1.change(10); a1.value_of_a(); return 0; }
Q24
What does the dereference operator will return?
Q25
What will be the output of the following C++ code? #include <iostream> #include <complex> using namespace std; int main() { complex<double> c1(4.0, 3.0); complex<float> c2(polar(5.0, 0.75)); cout << (c1 += sqrt(c1)) << endl; return 0; }