TCS Placement: Sample Questions 386 - 387 of 502

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 386

Edit

Describe in Detail

Essay▾

Does there any function exist to convert the int or float to a string?

Explanation

  • Yes
    1. itoa () converts an integer to a string.
    2. ltoa () converts a long to a string.
    3. ultoa () converts an unsigned long to a string.
    4. sprintf () sends formatted output to a string, so it can be used to convert any type of values to string type.
  • Example:
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. intmain(void)
  4. {
  5.     intval1 =45678;
  6.     floatval2 =6.19;
  7.     charstr1[20];
  8.     charstr2[20];
  9.     itoa(val1, str1, 10);
  10.     printf("integer =%d string =%s ", val1, str1);
  11.     sprintf(str2, "%f", val2);
  12.     printf("float =%f string =%s", val2, str2);
  13.     return0;
  14. }
  • Output:

integer = 45678 string = 45678

float = 6.190000 string = 6.190000

Question 387

Question

MCQ▾

Which of the following term is used for a function defined inside a class?

Choices

Choice (4)

a.

Member variable

b.

Classic function

c.

Class function

d.

Member function

Answer

d.

Explanation

  • “Member functions” are the functions defined inside a class
  • Member function has definition or prototype within the class definition like other variables.
  • Operates on object of the class of which it is a member with access to all the members of a class for that object.
  • Member functions can be defined within the class definition or separately using scope resolution operator (:) . Defining a member function within the class definition declares the function inline, even if you do not use the inline specifier.
  • Example
  1. #include<iostream>
  2. usingnamespacestd;
  3. classBox
  4. {
  5.     public:
  6.     doublelength;
  7.     doublebreadth;
  8.     doubleheight;
  9.     // Member functions declaration
  10.     doublegetVolume(void);
  11.     voidsetLength( doublelen);
  12.     voidsetBreadth( doublebre);
  13.     voidsetHeight( doublehei);
  14. };
  15. // Member functions definitions
  16. doubleBox::getVolume(void)
  17. {
  18.     returnlength ⚹breadth ⚹height;
  19. }
  20. voidBox::setLength( doublelen )
  21. {
  22.     length =len;
  23. }
  24. voidBox::setBreadth( doublebre )
  25. {
  26.     breadth =bre;
  27. }
  28. voidBox::setHeight( doublehei )
  29. {
  30.     height =hei;
  31. }
  32. // Main function for the program
  33. intmain()
  34. {
  35.     BoxBox1;
  36.     BoxBox2;
  37.     doublevolume =0.0;
  38.     // box 1 specification
  39.     Box1.setLength(4.0);
  40.     Box1.setBreadth(7.0);
  41.     Box1.setHeight(8.0);
  42.     // box 2 specification
  43.     Box2.setLength(16.0);
  44.     Box2.setBreadth(18.0);
  45.     Box2.setHeight(10.0);
  46.     // volume of box 1
  47.     volume =Box1.getVolume();
  48.     cout ≪ "Volume of Box1 :" ≪ volume ≪endl;
  49.     // volume of box 2
  50.     volume =Box2.getVolume();
  51.     cout ≪ "Volume of Box2 :" ≪ volume ≪endl;
  52.     return0;
  53. }
  • Output:

Volume of Box1: 224

Volume of Box2: 2880

Developed by: