Languages [TCS Placement]: Sample Questions 91 - 92 of 131

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 91

Edit

Write in Short

Short Answer▾

How to check whether a linked list is circular?

Explanation

  • Create two pointers, and set both to the start of the list. Update each as follows:
  • while (pointer1) {pointer1 = pointer1 ⇾ next; pointer2 = pointer2 ⇾ next; if (pointer2) pointer2 = pointer2 ⇾ next; if (pointer1 == pointer2) {print ( “circular” ) ;} }
  • If a list is circular, at some point pointer2 will wrap around and be either at the item just before pointer1, or the item before that. Either way, it՚s either 1 or 2 jumps until they meet.

Question 92

Question

MCQ▾

Which of the following function/type of function cannot be overloaded?

Choices

Choice (4)

a.

Virtual function

b.

Static function

c.

Member function

d.

All of the above

Answer

a.

Explanation

  • Virtual function cannot be overloaded
  • A virtual function is a member function expected to be redefined in derived classes.
  • When referencing a derived class object using pointer or a reference to the base class, virtual function for that object can be called to execute the derived class՚s version of the function.
  • Virtual functions ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used for function call.
  • Functions are declared with a virtual keyword in base class.
  • The resolving of function call is done at Run-time.
  • Example:
  1. #include<iostream>
  2. usingnamespacestd;
  3. classbase
  4. {
  5.     public:
  6.     virtualvoidprint ()
  7.     {
  8.         cout≪ "Print base class" ≪endl;
  9.     }
  10.     voidshow ()
  11.     {
  12.         cout≪ "Show base class" ≪endl;
  13.     }
  14. };
  15. classderived:publicbase
  16. {
  17.     public:
  18.     voidprint ()
  19.     {
  20.         cout≪ "Print derived class" ≪endl;
  21.     }
  22.     voidshow ()
  23.     {
  24.         cout≪ "Show derived class" ≪endl;
  25.     }
  26. };
  27. intmain()
  28. {
  29.     base ⚹bptr;
  30.     derivedd;
  31.     bptr =&d;
  32.     //virtual function, binded at runtime
  33.     bptr print();
  34.     // Non-virtual function, binded at compile time
  35.     bptr show();
  36. }
  • Output:

Print derived class

Show base class

Developed by: