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

C & C Plus Plus
Edit

Describe in Detail

Essay▾

What is the output of the following program?

  1. voidmain()
  2. {
  3.     int ⚹mptr,⚹cptr;
  4.     mptr=(int⚹)malloc(sizeof(int));
  5.     printf(“%d ”,⚹mptr );
  6.     cptr=(int⚹)calloc(sizeof(int),1);
  7.     printf(“%d”,⚹cptr);
  8. }

Explanation

In the program

Table Shows the Program
int ⚹ mptr, ⚹ cptr;
  • Given the integer pointer mptr and cptr.
mptr = (int ⚹) malloc (sizeof (int) ) ;

printf ( “% d” , ⚹ mptr) ;

  • The memory space allocated by malloc is uninitialized.
  • So it will print garbage value.
cptr = (int ⚹) calloc (sizeof (int) , 1) ;

printf ( “% d” , ⚹ cptr) ;

  • Calloc initializes the allocated memory space to zero.
Define the Calloc and Malloc Function

Question 148

C & C Plus Plus
Edit

Describe in Detail

Essay▾

What is the output of the following program?

  1. voidmain ()
  2. {
  3.     inti;
  4.     chara[] =“⧵0”;
  5.     if (printf ( “%s” a) )
  6.     printf ( “Okhere” );
  7.     else
  8.     printf ( “Forgetit” );
  9. }

Explanation

In the program

Table Shows the Program
int i;Define the integer variable i
char a [] = “⧵0” ;Given the character array ‘a’
if (printf ( “% s” a) )

printf ( “Ok here” ) ;

else

printf ( “Forget it” ) ;

Printf will return how many characters it prints.

Hence printing a null character returns 1 which makes the if statement true.

Thus “Ok here” is printed.

Developed by: