TCS Papers: Sample Questions 280 - 281 of 502

Examrace Placement Series prepares you for the toughest placement exams to top companies.

Question number: 280

» Languages » C & C Plus Plus

Essay Question▾

Describe in Detail

What is the difference between functions abs () and fabs ()?

Explanation

  • These 2 functions perform the same action- get the absolute value of the given value.

  • Abs () is used for integer values, while fabs () is used for floating type numbers.

  • Prototype for abs () is under < tdlib. h > , while fabs () is under < math. h > .

  • Fabs () is slower, works only on floats, and throws an exception if used on a different type

Functions abs() and fabs()

Functions Abs () and Fabs ()

Given the image is defining the difference between functions abs () and fabs ()

Question number: 281

» Languages » C & C Plus Plus

MCQ▾

Question

Which of the following functions are performed by a constructor?

Choices

Choice (4) Response

a.

Initialize objects

b.

Construct a new function

c.

Construct a new object

d.

Construct a new class

Answer

a.

Explanation

  • Constructor initializes the objects of the class- that is typically give initial values to the function members.

  • Constructor is a member function with the same name as the name of a class is automatically called when an object is created without any return type.

  • Example:

  1. #include<iostream>
  2. using namespace std;
  3. class date
  4. {
  5.     private:
  6.     int ddmmyy;
  7.     public:
  8.     date ()
  9.     {
  10.         dd=10;
  11.         mm=01;
  12.         yy=2015;
  13.     }
  14.     void display()
  15.     {
  16.         cout<<dd<<"\"<<mm<<"\"<<yy;
  17.     }
  18. };
  19. int main()
  20. {
  21.     date date1;
  22.     date1.display();
  23. }
  • Output:

    1012015

  • In above example as soon as the object say date1 is created, the constructor will be called and the values dd, mm, yy initialized to the required date- 1012015.