C++ Bank

1. Predict the output
main()
{
    float me = 1.1;
    double you = 1.1;
    if(me==you)
        cout << "C++ " ;
    else
        cout << "Programming";
}
Answer: Programming
Explanation: Double values takes the value more accurately than that of float.

2. Find the type of error in the following code snippet
main()
{
    extern int i;
    i=20;
    cout << i;
}
Answer: Compiler error
Explanation: The declaration of 'i' should be available out of main, because, 'extern int i' is not a declaration.

3. Predict the output
main()
{
    char *p;
    cout << sizeof(*p) << " " << sizeof(p);
}
Answer: The answer is compiler dependent, because, Turbo C/C++ is 16 bit based compiler.
g++            : 4 1
Turbo C++ : 2 1

4. Predict the output
main()
{
    cout << "\nab";
    cout << "\bsi" ;
    cout << "\rha";
}
Answer: hai 
Explanation: \n takes to the new line and then ab is printed
Result: ab
\b removes the last character in that line ( just as backspace does ) and si is printed
Result: asi
\r takes the cursor to the starting position of that line and ha is printed
Result: hai
Note that the existing characters are replaced

5. Predict the output
main()
{
    int i = -1;
    +i;
    cout << "i = " << i << ", +i = " << +i;
}
Answer: i = -1, +i = -1

6. Predict the output
void afunction(int *x)
{

   x = new int;
   *x = 12;
}

int main()

{

   int v = 10;
   afunction(&v);
   cout << v;
   return 0;
}

Answer:
10
Explanation:
Though the address of v is assigned to x and the content of x changes, value of v doesn't change because, x is then assigned a new memory location by calling the new operator.

7. What would be the output of cout << abs(-16);
Answer:
16

8. How long does this loop run ?Answer: Infinitely
Explanation: The condition is x=3, which assigns 3 to x each time and the condition is given as 3 ( non-zero value ) and so, the loop doesn't terminate.
9. Evaluate Answer: False 
10. What does non-global variables default to ? 
Answer: auto




  • Answer: 1
  • Explanation: Every non-zero value is treated as true and the equivalent integer for true is 1.
18. Will these functions cause ambiguity error?

  • Answer: Depends upon the function call
  • Explanation: Unless the function is called with no arguments, there is no ambiguity error.
  • function(2)  -->  Does not cause ambiguity error
  • function()  --> Causes ambiguity error
  • No function call --> Does not cause ambiguity error


  • Answer: Operator
  • Explanation: sizeof is an unoverloadable operator. 

  • Answer: Yes
  • Explanation: bool is also an integer data type, which can store only 0 or 1.

Answer: Function call can be in the left hand side of the assignment operator and a reference is more efficient than returning a copy

Answer: Friend keyword can be used on main()
Explanation: main() is not inside any class and hence friend can not be used for main.

23. How can we make a class abstract?
Answer: By making all member functions constant

 

}
Answer: 10 10 20 20
Explanation: Both (*p).x and p->x are the same.

 
}; 
int Sona::x = 0; 
int main() 
    Sona::SetData(33); 
    Sona::Display(); 
}
Answer: 33



  • 29. Which of the following term is used for a function defined inside a class?
  • Answer: Member function

  • Explanation: cout is an ostream object and cin is an istream object.

No comments:

Post a Comment