Languages-C & C Plus Plus [3i Infotech Placement]: Sample Questions 120 - 122 of 354

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

Question 120

C & C Plus Plus
Edit

Describe in Detail

Essay▾

Write a program to interchange 2 variables without using the third one.

Explanation

  1. #include<stdio.h>
  2. #include<conio.h>
  3. main()
  4. {
  5.     inta=10,b=20;
  6.     clrscr();
  7.     printf(“beforeswapa=%db=%d”, a, b);
  8.     a=a+b
  9.     b=a-b
  10.     a=a-b
  11.     printf(Afterswapa=%db=%d”, a, b);
  12.     getch();
  13. }

OUTPUT

Before swap a = 10 b = 20

After swap a = 20 b = 10

Question 121

C & C Plus Plus
Edit

Write in Short

Short Answer▾

What is the output of the following program?

  1. main ()
  2. {
  3.     chari =0;
  4.     for (; i>= 0; i ++ );
  5.     printf ( “%d” i);
  6. }

Explanation

Behavior is implementation dependent.

In the program

Table Shows the Program
{

char i = 0;

for (; i >= 0; i ++) ;

printf ( “% d” i) ;

}

  • If the char is signed or unsigned by default is implementation dependent.
  • If the implementation treats the char to be signed by default the program will print -128 and terminate.
  • On the other hand if it considers char to be unsigned by default, it goes to infinite loop.

Rule: You can write programs that have implementation dependent behavior. But don՚t write programs that depend on such behavior.

Question 122

C & C Plus Plus
Edit

Describe in Detail

Essay▾

What is virtual class and friend class?

Explanation

Virtual Base Class:

  • Used in context of multiple inheritance in C ++ .
  • Used to derive two classes from a class, and further derive one class from the two classes in the second level (diamond hierarchy) .
  • We need to declare the uppermost base class as ‘virtual’ in the inherited classes (so that methods inherited from both class are implemented) .
  • This prevents multiple copies of the uppermost base class data members when an object of the class at the third level of hierarchy is created.

Friend class:

  • Friend class has complete access to data and methods including private and protected data methods.
  • It is not bi-directional-
  • If A declares B as its friend, it does not mean that A can access private data of B.
  • B can access all data of A.

Developed by: