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

All questions of C Functions for Class 6 Exam

What will be the output of the following C code?
#include <stdio.h>
    void foo(const int *);
    int main()
    {
        const int i = 10;
        printf("%d ", i);
        foo(&i);
        printf("%d", i);
 
    }
    void foo(const int *i)
    {
        *i = 20;
    }
  • a)
    Compile time error
  • b)
    10 20
  • c)
    Undefined value
  • d)
    10
Correct answer is option 'A'. Can you explain this answer?

Tanishq Mehra answered
Explanation:
- Compile time error: The code will throw a compile time error because of the attempt to modify a constant variable `i` inside the `foo` function.
- const int i = 10; This line declares a constant integer `i` with a value of 10.
- printf("%d ", i); This will print the value of `i`, which is 10.
- foo(&i); The address of `i` is passed to the `foo` function which expects a pointer to a constant integer.
- void foo(const int *i) This function takes a constant integer pointer as an argument.
- *i = 20; This line tries to change the value of `i` to 20 which is not allowed since `i` is a constant variable.
- printf("%d", i); This line will print the value of `i` which remains 10 since the `foo` function was not able to modify it.
Therefore, the code will not compile due to the attempt to modify a constant variable, resulting in a compile time error.

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

Mihir Menon answered

Explanation:

Scope of Variables:
- In the given code, there are two variables named 'k' declared in different scopes.
- The outer 'k' variable is declared in the main function, while the inner 'k' variable is declared inside the block enclosed by curly braces.

Inner Variable:
- The inner 'k' variable declared inside the block is a separate variable from the outer 'k' variable.
- This inner 'k' variable is local to the block and its scope is limited to that block only.

For Loop:
- Inside the inner block, there is a for loop that uses the inner 'k' variable to iterate from 0 to 9.
- This for loop does not have any statements inside the loop body, so it will simply increment the 'k' variable from 0 to 9 and then terminate.

Compilation:
- The code will compile without any error because each variable is declared in a separate scope, and there is no conflict between them.
- The inner 'k' variable is used only within the block where it is declared, and it does not affect the outer 'k' variable.

Therefore, the given C code will compile without any error.

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 printed when this program is executed?
int f(int x)
{
   if(x <= 4)
      return x;
   return f(--x);
}
void main()
{
   printf("%d ", f(7)); 
}
  • a)
    4 5 6 7
  • b)
    1 2 3 4
  • c)
    4
  • d)
    Syntax error
Correct answer is option 'C'. Can you explain this answer?

Priya Pillai answered
Understanding the Function
The function `f(int x)` is designed to return a specific value based on the input `x`.
- If `x` is less than or equal to 4, it directly returns `x`.
- If `x` is greater than 4, it decrements `x` by 1 and calls itself recursively with the new value.
Analyzing the Execution
Let's analyze what happens when `f(7)` is called:
1. First Call: `f(7)`
- Since 7 > 4, it calls `f(6)` after decrementing x.
2. Second Call: `f(6)`
- Since 6 > 4, it calls `f(5)` after decrementing x.
3. Third Call: `f(5)`
- Since 5 > 4, it calls `f(4)` after decrementing x.
4. Fourth Call: `f(4)`
- Since 4 <= 4,="" it="" returns="">
Returning the Value
Now let's trace back:
- `f(4)` returns 4.
- `f(5)` receives 4 from `f(4)`, but it doesn't change the return value, so it effectively returns 4.
- `f(6)` also receives 4 from `f(5)` and returns 4.
- Finally, `f(7)` receives 4 from `f(6)` and returns 4.
Final Output
Thus, when `main()` executes `printf("%d ", f(7));`, it prints `4`.
So, the correct answer is option C: 4.

Any C program
  • a)
    Must contain at least one function.
  • b)
    Need not contain any function.
  • c)
    Needs input data.
  • d)
    None of the above
Correct answer is option 'A'. Can you explain this answer?

Kiran Das answered
Explanation:

Function in a C program:
- A function is a block of code that performs a specific task. It helps in organizing the code and making it more readable and reusable.
- In C programming, at least one function is required in a program to execute the code.

Need for a function:
- Functions help in breaking down a program into smaller, manageable parts.
- They make the code modular and easier to understand.
- Functions can be called multiple times from different parts of the program, making it more efficient.

Explanation of options:
- Option A is correct because it states that a C program must contain at least one function, which is true.
- Option B is incorrect because a C program can contain functions, even though it is not mandatory.
- Option C is incorrect as input data can be provided in a C program regardless of the presence of functions.
- Option D is incorrect as the statement in option A is true.
Therefore, the correct answer is option A, as a C program must contain at least one function.

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?

Sarita Singh answered
Variable j is not defined.
Output:
$ cc pgm3.c
pgm3.c: In function ‘main’:
pgm3.c:4: error: ‘j’ undeclared (first use in this function)
pgm3.c:4: error: (Each undeclared identifier is reported only once
pgm3.c:4: error: for each function it appears in.)

What will be the output of the following C code?
#include <stdio.h>
    int main()
    {
        const int i = 10;
        int *ptr = &i;
        *ptr = 20;
        printf("%d\n", i);
        return 0;
    }
  • a)
    Compile time error
  • b)
    Compile time warning and printf displays 20
  • c)
    Undefined behaviour
  • d)
    10
Correct answer is option 'B'. Can you explain this answer?

Priya Pillai answered

Explanation:

Constant integer 'i' and pointer 'ptr'
- The code declares a constant integer 'i' with a value of 10.
- It then declares a pointer 'ptr' that points to the address of the constant integer 'i'.

Changing the value using pointer
- Since 'i' is declared as a constant integer, it cannot be modified directly.
- However, the code tries to change the value of 'i' through the pointer 'ptr' by assigning 20 to '*ptr'.

Compile time warning and output
- The code will give a compile-time warning because it is trying to modify a constant variable.
- However, the code will still compile and run, and the printf statement will display 20 instead of 10.

Therefore, the correct output will be: Compile time warning and printf displays 20.

Which keyword is used to prevent any changes in the variable within a C program?
  • a)
    immutable
  • b)
    mutable
  • c)
    const
  • d)
    volatile
Correct answer is option 'C'. Can you explain this answer?

Kunal Ghosh answered
The correct answer is option 'C': const.

Explanation:
In C programming, the keyword 'const' is used to declare a variable as a constant. When a variable is declared as 'const', it means that its value cannot be changed throughout the program execution. The 'const' keyword is used to indicate that a variable's value is read-only and cannot be modified.

The 'const' keyword is known as a type qualifier, which modifies the type of the variable. It can be applied to any data type such as int, float, char, etc. When a variable is declared as 'const', it must be assigned a value at the time of declaration, and this value cannot be modified later in the program.

Using the 'const' keyword has several benefits:
1. Readability: It makes the code more readable by clearly indicating that a variable is meant to be constant.
2. Avoid accidental modifications: It prevents accidental modifications of the variable's value, ensuring that the intended value remains constant throughout the program.
3. Optimization: The 'const' keyword allows the compiler to perform optimizations, as it knows that the value of the variable will not change. This can lead to improved performance in some cases.

Example:
```c
#include

int main() {
const int num = 5; // declaring 'num' as a constant

printf("num = %d\n", num);

// Trying to modify the value of 'num' will result in a compile-time error
num = 10; // Error: assignment of read-only variable 'num'

return 0;
}
```

In the above example, the variable 'num' is declared as a constant using the 'const' keyword. When we try to assign a new value to 'num', it will result in a compile-time error because the variable is read-only.

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.

Top Courses Class 6