Languages [3i Infotech Placement]: Sample Questions 407 - 408 of 546

Glide to success with Doorsteptutor material for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.

Question 407

Languages

Appeared in Year: 2004

Question

MCQ▾

What is the output of following piece of code?

  1. intx =2.
  2. switch (x)
  3. {
  4.     case1:System. Out. Println ( “1” );
  5.     case2:Case3:System. Out. Println ( “3” );
  6. case4:Case5:System. Out. Println ( “5” ); }

Choices

Choice (4)

a.

135

b.

35

c.

No output

d.

3

Answer

b.

Explanation

Dicleration of Integer Value X as 2
Int Declare integer value x as
Switch (x)The Switch expression should be an integer or a variable or an expression that evaluates to an integer.
case 1: System. Out. Println ( “1” ) ;In case output is printed But this will not be selected
case 2: case 3: System. Out. Println ( “3” ) ;In Case output is printed. Of course, this case would be selected, as the value of switch expression is .
case 4: Case 5: System. Out. Println ( “5” ) ;}In case output printed is would also be printed as there is no break inside 3: , so execution simply continues to Thus output will be

Question 408

Edit

Describe in Detail

Essay▾

Find the output of the following program

  1. classbase
  2. {
  3.     public:
  4.     VirtualvoidbaseFun ()
  5.     {
  6.         cout≪ “frombase” ≪endl;
  7.     };
  8. }
  9. classderi:Publicbase
  10. {
  11.     public:
  12.     VoidbaseFun ()
  13.     {
  14.         cout≪ “fromderived” ≪endl;
  15.     }
  16. };
  17. voidSomeFunc (base ⚹baseObj)
  18. {
  19.     baseObj ⇾ baseFun ();
  20. }
  21. intmain ()
  22. {
  23.     basebaseObject;
  24.     SomeFunc (&baseObject);
  25.     derideriObject;
  26.     SomeFunc (&deriObject);
  27. }

Explanation

  • In the program
Table Showing the Program
  1. classbase
  2. {
  3.     public:
  4.     VirtualvoidbaseFun ()
  5.     {
  6.         cout ≪ “frombase” ≪ endl;
  7.     };
  8. }
  • In base class defines the virtual baseFun () function
  • In a baseFun () cout prints “from base”
  1. classderi:Publicbase
  2. {
  3.     public:
  4.     VoidbaseFun ()
  5.     {
  6.         cout ≪ “fromderived” ≪ endl;
  7.     }
  8. };
  • In deri class define the void baseFun () function
  • In a baseFun () cout prints “from derived”
  1. voidSomeFunc (base ⚹baseObj)
  2. {
  3.     baseObj ⇾ baseFun ();
  4. }
  • SomeFunc expects a pointer to a base class.
  1. intmain ()
  2. {
  3.     basebaseObject;
  4.     SomeFunc (&baseObject);
  5.     derideriObject;
  6.     SomeFunc (&deriObject);
  7. }
  • Define the base () function object baseObject
  • SomeFunc () function pass the baseobject address
  • Define the deri () function object deriObject
  • SomeFunc () function pass the deriobject address
  • Since a pointer to a derived class object is passed.
  • Remember that baseFunc is a virtual function.
  • That means that it supports run-time polymorphism.
  • So the function corresponding to the derived class object is called

Developed by: