LIVE Course for free

Rated by 1 million+ students
Get app now
0 votes
18 views
in Information Technology by (48.1k points)
closed by

Consider the following ‘C’ Program:

# include <stdio.h>

int main ()

{

int max, i;

scanf (“%d”, & max);

int a[max];

for (i=1; i<max; i++)

{

scanf (“%d”, a[i]);

printf(“%d \n”, a[i]);

}

return 0;

}

 

Which of the following statements are correct about the ‘C’ program given above ?

(a) The code is correct and runs successfully

(b) The code is erroneous since the statement declaring array is invalid

(c) The code is erroneous since the subscript for array used in ‘for’ loop is in the range 1 to max - 1

(d) The code is erroneous since the type declaration statement int a [max]; is done after scanf()


1. (a) and (c)
2. (b) and (c)
3. (b) and (d)
4. (c) and (d)

1 Answer

0 votes
by (35.4k points)
selected by
 
Best answer
Correct Answer - Option 3 : (b) and (d)

Code explanation:

# include <stdio.h>

int main ()

{

int max, i;

scanf (“%d”, & max);

int a[max];          //  max in not initialized and int a[max] is declare after input of max at runtime

for (i=1; i<max; i++)

{

scanf (“%d”, a[i]);

printf(“%d \n”, a[i]);

}

return 0;

}

The code is erroneous since:

  • statement declaring array is invalid
  • the type declaration statement int a [max]; is done after scanf()

Important Note:

'i' was not declared in the original question by UGC, that has been added in the question.

Execute the input and check the result for max >1.

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...