All Exams  >   Class 6  >   C Programming for Beginners  >   All Questions

All questions of C Functions for Class 6 Exam

Will the following C code compile without any error?
#include <stdio.h>
    int main()
    {
        for (int k = 0; k < 10; k++);
            return 0;
    }
  • a)
    Yes
  • b)
    No
  • c)
    Depends on the C standard implemented by compilers
  • d)
    Error
Correct answer is option 'C'. Can you explain this answer?

Sarita Singh answered
Compilers implementing C90 do not allow this, but compilers implementing C99 allow it.
Output:
$ cc pgm4.c
pgm4.c: In function ‘main’:
pgm4.c:4: error: ‘for’ loop initial declarations are only allowed in C99 mode
pgm4.c:4: note: use option -std=c99 or -std=gnu99 to compile your code

What will happen after compiling and running following code?
main()

     printf("%p", main); 
}
  • a)
    Error
  • b)
    Will make an infinite loop.
  • c)
    Some address will be printed.
  • d)
    None of these.
Correct answer is option 'C'. Can you explain this answer?

Sarita Singh answered
Function names are just addresses (just like array names are addresses).
main() is also a function. So the address of function main will be printed. %p in printf specifies that the argument is an address. They will be printed as hexadecimal numbers.

What will be the output of the following program code?
main()
{
      static int var = 5;
      printf("%d ", var--);
      if(var)
            main();
}
  • a)
    5 5 5 5 5
  • b)
    5 4 3 2 1
  • c)
    Infinite Loop
  • d)
    Compilation Error
Correct answer is option 'B'. Can you explain this answer?

Sarita Singh answered
When static storage class is given, it is initialized once. The change in the value of a static variable is retained even between the function calls. Main is also treated like any other ordinary function, which can be called recursively.

What will be the output of the following C code?
#include <stdio.h>
    int main()
    {
        j = 10;
        printf("%d\n", j++);
        return 0;
    }
  • a)
    10
  • b)
    11
  • c)
    Compile time error
  • d)
    0
Correct answer is option 'C'. Can you explain this answer?

Shubham Gupta answered


Explanation:

The code provided will result in a compile-time error. This is because the variable 'j' is being used without declaration.

Reasoning:
- In the code, the variable 'j' is used without being declared. This will result in a compile-time error as the compiler does not know the data type of 'j'.

Fix:
To fix this issue, you can declare 'j' as an integer before using it.
Example:
c
#include
int main()
{
int j = 10;
printf("%d\n", j++);
return 0;
}


By declaring 'j' as an integer, the code will no longer have a compile-time error and will output the value of 'j' (which is 10) before incrementing it due to the postfix increment operator (++).

What is the result of compiling and running this code?
main()
{
      char string[] = "Hello World";
      display(string);
}
void display(char *string)
{
      printf("%s", string);
}
  • a)
    will print Hello World
  • b)
    Compilation Error
  • c)
    will print garbage value
  • d)
    None of these.
Correct answer is option 'B'. Can you explain this answer?

Sarita Singh answered
Compiler Error : Type mismatch in redeclaration of function display
As the function display() is not defined before use, compiler will assume the return type of the function which is int(default return type). But when compiler will see the actual definition of display(), mismatch occurs since the function display() is declared as void. Hence the error.

Chapter doubts & questions for C Functions - C Programming for Beginners 2025 is part of Class 6 exam preparation. The chapters have been prepared according to the Class 6 exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Class 6 2025 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of C Functions - C Programming for Beginners in English & Hindi are available as part of Class 6 exam. Download more important topics, notes, lectures and mock test series for Class 6 Exam by signing up for free.

Signup to see your scores go up within 7 days!

Study with 1000+ FREE Docs, Videos & Tests
10M+ students study on EduRev