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

C & C Plus Plus
Edit

Write in Short

Short Answer▾

Predict the output or error (s) for the following:

  1. main ()
  2. {
  3.     chara[4] =“HELL” ;
  4.     printf ( “%s” ,a);
  5. }

Explanation

HELL % @! ~@! @? ? ? @ ~ ~!

  • In this program
Table Showing the Program
char a [4] = “HELL” ;
  • The character array has the memory just enough to hold the string “HELL” and doesn՚t have enough space to store the terminating null character.
printf ( “% s” , a) ;
  • Printf prints the HELL correctly and continues to print garbage values till it accidentally comes across a NULL character.

Question 28

C & C Plus Plus

Question

MCQ▾

What will the following program do?

  1. voidmain ()
  2. {
  3.     inti;
  4.     chara[] =“String”;
  5.     char ⚹p =“NewSring”;
  6.     char ⚹Temp;
  7.     Temp =a;
  8.     a =malloc (strlen (p) +1);
  9.     strcpy (a, p);//Line number:9
  10.     //p =malloc (strlen (Temp) +1);
  11.     strcpy (p, Temp);
  12.     printf ( “(%s, %s)” a, p);
  13.     free (p);
  14.     free (a);
  15. }
  16. //Line number 15//

Choices

Choice (4)

a.

Generate compilation error in line number 7

b.

Generate compilation error in line number 8

c.

Generate compilation error in line number 1

d.

Generate compilation error in line number 5

Answer

b.

Explanation

  • In the program
Table Showing the Program
int i;
  • Define the integer i
char a [] = “String” ;
  • Define the character array a [] = “String”
char ⚹ p = “New Sring” ;
  • Define the character pointer p = “New String”
char ⚹ Temp;
  • Define the character pointer Temp
Temp = a;
  • Store the array value in pointer
a = malloc (strlen (p) + 1) ;
  • Statement tries to store the string length in pointer p in array “a”
  • But it is wrong, generating the error: assignment to expression with array type

Developed by: