Languages-C & C Plus Plus [3i Infotech Placement]: Sample Questions 305 - 307 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 305

C & C Plus Plus
Edit

Write in Short

Short Answer▾

What is the output of the following program?

  1. main ()
  2. {
  3.     inti =-1;
  4.     printf ( “i =%d, i =%d” ,i, -i);
  5. }

Explanation

In the program

Table Shows the Program
int i =-1;
  • Define the value of integer i =-1
printf ( “i =% d, i =% d” i, -i) ;
  • In printf first prints the value of i.
  • After that the value of the expression -i =- (-1) is printed.

Question 306

C & C Plus Plus
Edit

Write in Short

Short Answer▾
  1. voidmain ()
  2. {
  3.     charfar ⚹farther, ⚹farthest;
  4.     printf ( “%d. %d” sizeof (farther), sizeof (farthest) );
  5. }

Explanation

In a program

Table Shows the Program
char far ⚹ farther, ⚹ farthest;Here farther՚s type-FAR pointer to char

Farthest՚s type-near pointer to char

printf ( “% d. % d” sizeof (farther) , sizeof (farthest) ) ;Now the difference in size of those stems from the fact that far pointers consist of the segment and offset together, while near pointers just have the offset.

Near pointer thus have size of 2 (just the offset) , while far pointers- size of 4 bytes.

Question 307

C & C Plus Plus
Edit

Write in Short

Short Answer▾

Char inputString [100] = {0} ;

To get string input from the keyboard which one of the following is better?

  1. gets (inputString)
  2. fgets (inputString, sizeof (inputString) , fp)

Explanation

2nd one is better

Table Shows the Program
gets (inputString)
  • gets (inputString) doesn՚t know the size of the string passed and so, for very big inputs the characters will be written past the input string causing buffer overflow
fgets (inputString, sizeof (inputString) , fp)
  • fgets used with stdin performs the same operation as gets but is safe.

Developed by: