Languages-C & C Plus Plus [TCS Placement]: Sample Questions 6 - 7 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 6

C & C Plus Plus

Question

MCQ▾

What happens when a class with parameterized constructors and having no default constructor is used in a program and we create an object that needs a zero-argument constructor?

Choices

Choice (4)

a.

Preprocessing error

b.

Compile-time error

c.

Runtime exception

d.

Runtime error

Answer

b.

Explanation

  • Compile time error is generated.
  • By default compiler provides zero argument constructor & copy constructor implicitly, if those are not defined explicitly.
  • However, compiler will not call default constructor (zero argument constructor) , if parameterized constructor is defined explicitly.
  • Thus creating class object without parameters will generate compile time error
  • Note: Default constructor is a constructor with no parameters (or) all parameters of the constructor are used with default arguments.
  1. #include<iostream>
  2. usingnamespacestd;
  3. classX
  4. {
  5.     private:
  6.     intx;
  7.     public:
  8.     X(intm) // Parameterized constructor
  9.     {
  10.         x =m;
  11.     }
  12. };
  13. intmain()
  14. {
  15.     Xp; // error
  16.     Xq(10); // No error
  17.     return0;
  18. }

Question 7

C & C Plus Plus
Edit

Describe in Detail

Essay▾

Which function sets first n characters of a string to a given character?

Explanation

  • strnset () function sets portion of characters in a string to given character
  • syntax for strnset () function is given below.

char ⚹ strnset (char ⚹ string, int c) ;

  • strnset () function is not standard function which may not available in standard library in C.
  • Example:
  1. #include<stdio.h>
  2. #include<string.h>
  3. intmain(void)
  4. {
  5.     char ⚹abc ="hellofriends";
  6.     charletter ='x';
  7.     printf("String before strnset:%s", abc);
  8.     strnset(abc, letter, 5);
  9.     printf("String after strnset:%s", abc);
  10.     return0;
  11. }
  • Output:

String before strnset: hellofriends

String after strnset: xxxxxfriends

Developed by: