Languages-C & C Plus Plus [TCS Placement]: Sample Questions 34 - 35 of 119

Get unlimited access to the best preparation resource for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.

Question 34

C & C Plus Plus
Edit

Describe in Detail

Essay▾

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
Given the Image is Defining the Difference between Functions Abs () and Fabs ()

Question 35

C & C Plus Plus

Question

MCQ▾

Which of the following functions are performed by a constructor?

Choices

Choice (4)

a.

Construct a new object

b.

Initialize objects

c.

Construct a new function

d.

Construct a new class

Answer

b.

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. usingnamespacestd;
  3. classdate
  4. {
  5.     private:
  6.     intdd, mm, yy;
  7.     public:
  8.     date ()
  9.     {
  10.         dd=10;
  11.         mm=01;
  12.         yy=2015;
  13.     }
  14.     voiddisplay()
  15.     {
  16.         coutdd"⧵⧵"mm"⧵⧵"yy;
  17.     }
  18. };
  19. intmain()
  20. {
  21.     datedate1;
  22.     date1.display();
  23. }
  • Output:

    10⧵1⧵2015

  • 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- 10⧵1⧵2015.

Developed by: