Languages-C & C Plus Plus [Redpine Infotech Placement]: Sample Questions 3 - 4 of 5

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

Question 3

C & C Plus Plus
Edit

Describe in Detail

Essay▾

What is the output of the following program?

  1. #include<stdio.h>
  2. main ()
  3. {
  4.     char ⚹q;
  5.     int ⚹ip;
  6.     q=(char ⚹) malloc (100);
  7.     ip=(int⚹)q;
  8.     free (ip);
  9. }

Explanation

Table Shows the Program
#include < tdio. h >Standard library function
main ()startup after initialization
char ⚹ q;

int ⚹ ip;

Declare character pointer q and integer pointer ip
q = (char ⚹) malloc (100) ;malloc allocates memory for 100 character.
ip = (int ⚹) q;Copy the pointer address of memory block char ⚹ q into int ⚹ ip
free (ip) ;Free the memory allocated and exit program.
  • So, result is nothing displayed on screen.
  • There is no need to declare main as returning an int.

Question 4

C & C Plus Plus

Question

MCQ▾

What is output for the following program?

  1. #include<stdio.h>
  2. main ()
  3. {
  4.     unsignedchari;
  5.     for(i=0; i<300; i++)
  6.     {
  7.         printf (“⚹”);
  8.     }
  9. }

Choices

Choice (4)

a.

Infinite

b.

299

c.

300

d.

Question does not provide sufficient data or is vague

Answer

a.

Explanation

Table Shows the Program
#include < tdio. h >Standard library function
main ()startup after initialization
unsigned char i;Size of character variable is 8 byte. Thus, the range of unsigned number in 8 bits is from 0 to
for (i = 0; i < 300; i ++)This loop upto 299 starting from 0, executing the statements in the loop 299 times. After executing these, the variable “i” is incremented by 1. However since the unsigned char cannot reach 300, the final incremented number is 0, resulting in re-execution of the loop. That is the loop runs infinite times.
printf ( “⚹” ) ;Print ⚹ infinite time.

Developed by: