Languages [3i Infotech Placement]: Sample Questions 164 - 167 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 164

Edit

Describe in Detail

Essay▾

What is polymorphism? Explain with an example?

Explanation

Understanding of Polymorphism

A single function which is used for different purpose depending upon the parameters passes is a polymorphic function

Example:

  • Area is the function name which is used for calculating areas of the rectangle, triangle, and even more.
    • area (a) ; ◊ area of the square
    • area (l, b) ; ◊ area of the rectangle.
  • Area is thus polymorphic- exact function called depends on number of arguments.

Question 165

Edit

Write in Short

Short Answer▾
  1. #defineintchar
  2. main ()
  3. {
  4.     inti =65;
  5.     printf ( “sizeof (i) =%d” sizeof (i) );
  6. }

Explanation

In a program

Table Shows the Program
#define int char#define replaces the string int by the macro char
main ()

{

int i = 65;

printf ( “sizeof (i) =% d” sizeof (i) ) ;

}

Here , int value replace in char value

Then, print sizeof (i) = 1

Because, character variable minimum size is 1 byte or 1 character

Question 166

Edit

Write in Short

Short Answer▾

Are the variables argc and argv local to main?

Explanation

Yes, both argc and argv are local to main method.

Question 167

Edit

Describe in Detail

Essay▾

What is the output of the following program?

  1. #defineassert (cond) if ( (cond)! ) ⧵ (fprintf (stderr, “assertionfailed:%s, file %s, line %d” #cond, ⧵ __FILE__, __LINE__), abort () )
  2. voidmain ()
  3. {
  4.     inti =10;
  5.     if (i == 0)
  6.     assert (i <100);
  7.     else
  8.     printf ( “Thisstatementbecomeselseforifinassertmacro” );
  9. }

Explanation

  • In the program
Table Shows the Program
#define assert (cond) if ( (cond) !) ⧵ (fprintf (stderr, “assertion failed:% s, file % s, line % d” #cond, ⧵ ________FILE________, ________LINE________) , abort () )
  • Define macro condition
if (i == 0)

assert (i < 100) ;

  • The else with printf becomes the else for if in the assert macro.
  • Hence nothing is printed.
  • The solution is to use conditional operator instead of if statement
  • #define assert (cond) ( (cond) ? (0) : (fprintf (stderr, “assertion failed: ⧵ % s, file % s, line % d” , #cond, ________FILE________, ________LINE________) , abort () ) )
  • Note: This problem of “matching with nearest else” cannot be solved by the usual method of placing the if statement inside a block like this, #define assert (cond) {⧵if (! (cond) ) ⧵ (fprintf (stderr, “assertion failed:% s, file % s, line % d” , #cond, ⧵ ________FILE________, ________LINE________) , abort () ) ⧵}

Developed by: