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

C & C Plus Plus

Question

MCQ▾

A constructor that accepts ________ parameters is called the default constructor.

Choices

Choice (4)

a.

One

b.

Two

c.

No

d.

Three

Answer

c.

Explanation

  • The correct answer is NO.
  • Default constructor is called when object is allocated memory but is not provided any values from the constructor call (that is constructor call is without any arguments) .
  • Example:
  1. #include<iostream>
  2. usingnamespacestd;
  3. classDef
  4. {
  5.     public:
  6.     inta;
  7.     intb;
  8.     Def()
  9.     {
  10.         a=b=0;
  11.     }
  12. };
  13. intmain()
  14. {
  15.     DefX;
  16.     cout ≪ "Default constructs a,b value:"X.a ≪" , "≪ X.b ≪ "";
  17.     return0;
  18. }
  • Output:

    Default constructer puts in a, b value: 0,0

  • In the above example a default constructor has the default value of “0” for both the parameters.

Question 60

C & C Plus Plus
Edit

Write in Short

Short Answer▾

Is it possible to initialize a variable at the time it was declared?

Explanation

  • Yes, a separate assignment statement is not required after the variable declaration, unless value is changed later on.
  • For example: char planet [15] = “Earth” does two things:
    • Declares a string variable named planet
    • Initializes it with the value “Earth” .

Question 61

C & C Plus Plus
Edit

Describe in Detail

Essay▾

Write a function to swap the values of two integers.

Explanation

Program:

  1. #include<stdio.h>
  2. intmain()
  3. {
  4.     intx=15, y=20;
  5.     x=x+y;
  6.     y=x-y;
  7.     x=x-y;
  8.     printf(“Afterswapping :x=%d, y=%d”, x, y);
  9.     return0;
  10. }
  • Output:

After swapping: x = 20, y = 15

Developed by: