TCS Papers: Sample Questions 430 - 431 of 502

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

Question number: 430

» Languages » C & C Plus Plus

Essay Question▾

Describe in Detail

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. int main(void)
  4. {
  5.     int val1 = 45678;
  6.     float val2 = 6.19;
  7.     char str1[20];
  8.     char str2[20];
  9.     itoa(val1str110);
  10.     printf("integer = %d string = %s  "val1str1);
  11.     sprintf(str2"%f"val2);
  12.     printf("float = %f string = %s"val2str2);
  13.     return 0;
  14. }
  • Output:

integer = 45678 string = 45678

float = 6.190000 string = 6.190000

Question number: 431

» Languages » C & C Plus Plus

MCQ▾

Question

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

Choices

Choice (4) Response

a.

Classic function

b.

Member variable

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. using namespace std;
  3. class Box
  4. {
  5.     public:
  6.     double length;
  7.     double breadth;
  8.     double height;
  9.     // Member functions declaration
  10.     double getVolume(void);
  11.     void setLengthdouble len);
  12.     void setBreadthdouble bre);
  13.     void setHeightdouble hei);
  14. };
  15. // Member functions definitions
  16. double Box::getVolume(void)
  17. {
  18.     return length * breadth * height;
  19. }
  20. void Box::setLengthdouble len )
  21. {
  22.     length = len;
  23. }
  24. void Box::setBreadthdouble bre )
  25. {
  26.     breadth = bre;
  27. }
  28. void Box::setHeightdouble hei )
  29. {
  30.     height = hei;
  31. }
  32. // Main function for the program
  33. int main()
  34. {
  35.     Box Box1;
  36.     Box Box2;
  37.     double volume = 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.     return 0;
  53. }
  • Output:

Volume of Box1: 224

Volume of Box2: 2880