TCS Papers: Sample Questions 240 - 242 of 502

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

Question number: 240

» Languages » C & C Plus Plus

Essay Question▾

Describe in Detail

Is there another function which can be used to convert an integer or a float to a string?

Explanation

  • Some implementations provide a nonstandard function called item (), which converts an integer to string.

  • #include char * item (int value, char * string, int radix);

  • DESCRIPTION: -The item () function constructs a string representation of an integer.

  • PARAMETERS: -

    • value: Is the integer to be converted to string representation.

    • string: Points to the buffer that holds resulting string. The resulting string may be as long as seventeen bytes.

    • radix: Is the base of the number; must be in the range 2 - 36.

  • A portable solution also exists. One can use sprintf (): char s [SOME_CONST]; int i = 10; float f = 10.20; sprintf (s, “%d % f ”, i, f);

Question number: 241

» Basic CS » Data Structures

Short Answer Question▾

Write in Short

Sorting in not possible by using which of the following methods? (Insertion, selection, exchange, deletion)

Explanation

  • Sorting is not possible with Deletion.

  • Using insertion - perform insertion sort.

  • Using selection- perform selection sort.

  • Using exchange- perform the bubble sort (or others).

  • No sorting method just uses deletion.

Question number: 242

» Languages » C & C Plus Plus

Essay Question▾

Describe in Detail

What is the difference between printf () and sprintf ()?

Explanation

printf ()

  • printf is used to print text (string/character stream) or values on standard output device.

  • Syntax: - int printf (const char * format, …);

  • “Format” provides the format of the text string to point on the output device with the help of specifier like % s, %d, %f etc.

  • “…” Provides the list of arguments to be print.

  • Return type int returns total number of printed characters on the output screen.

  • Example:

  1. #include<stdio.h>
  2. Int main()
  3. {
  4.     Printf(“hellohii”);
  5.     Return 0;
  6. }

Output: - hello, hii

sprintf ()

  • sprintf id used to send (copy) formatted text (string or charter stream) to string.

  • Syntax: - int sprintf (char * str, const char * format, …);

  • Here, char * str- is character array in which formatted text will be sent.

  • “Format” provides the formatted text with the help of format specifiers.

  • “…” provides the list of arguments to be print.

  • Return type int returns total number of copied characters into the char * str.

  • Example:

  1. #include<stdio.h>
  2. int main()
  3. {
  4.     Sprintf((char*)str, “My name is %sI am %d year old.”, “Krishna”, 20);
  5.     Printf(“Text is: %s ”, str);
  6.     Return 0;
  7. }

Output: - My name is Krishna, I am 20 years old.