Programming in C

 Description: Programming in C Number of Questions: 5 Created by: Blackmamba Tags:
Attempted 0/5 Correct 0 Score 0

Find out the output of the given program or error if any.

static int i=10;
int main()
{
i=25;       // line 4
printf(%d,i);
return 0;
}

1. 10

2. 25

3. Garbage value

4. Error in the program

5. None of these

Correct Option: B
Explanation:

This is the correct option because the scope of static variable is throughout the program, hence the assignment at line 4 changes the value of static variable to 25. So this is the correct answer.

Find out the output of the given program or error if any.

int main()
{
void swap (int*,int*); //Line 4
int a=10,b=20;
swap(&a,&b);        //Line 6
printf(%d,%d,a,b);
getch();
}
void swap( int* x, int* y) //Line 9
{
int temp=*x;    //Line 10
*x=*y;            //Line 11
*y=temp;    //line 12
}

1. 10, 20

2. 20, 10

3. 20, 20

4. 10, 10

5. Error in the program

Correct Option: B
Explanation:

Here we are calling swap() by passing address of the parameters. This is called call by reference. Line 4 : void swap(int*,int*); <= this is the prototype of function and parameters are type of int* (Integer pointer), it stores the address of integer variable. Line 6 : swap(&a,&b); <= Calling swap function by passing address of  a and b using '&' operator. a | 10 |    b | 20 |  Line 9:  Now memory address of a and b are copied to x and y, so x and y too start pointing same memory as a and ba | 10 | <--xb | 20 | <--y Now values at memory location where x and y are pointing are swapped( Line 10, 11 and 12)[*x => value at memory location where x is pointing.]a | 20 | <--xb | 10 | <--y Now, you can see a and b memory location is same but values are changed. So this is the correct answer.

What will be the output of the given program or find out the error if any?

int main()
{
float  x=fun(2,3);
printf(%f,x);
}
int fun (int x,float y) //Line 7
{
x = 20.9;   //Line 9
return x;
}

1. 20.99000

2. 20.00000

3. 20

4. 20.9

5. Error in the program

Correct Option: E
Explanation:

There is an error in the program, so it will not show any output in the main function. Definition of func() is not known to compiler during compilation, so compiler assumed the return type and parameter type to int but in definition. When it sees the parameter type is float, it throws conflicting types error. So this is the correct choice.

Find out the output of the given program or error if any.

static int i=10;
i=20;
int main()
{
printf(%d,i);
return 0;
}

1. 10

2. 20

3. 0

4. Garbage value

5. Compilation error

Correct Option: E
Explanation:

There is an error in the program, it will not show any output as when we write i=20 in the global scope, it is considered as a declaration and not just an assignment and the compiler assumes it as int i=20. Hence the compiler reports an error that there is a non-static declaration of 'i' after the static declaration of i, which is not allowed. So this option is correct.

Find out the output of the given program or error if any.

int main()
{
register int n;
int i;
printf(Enter the no. of loops to run:);
scanf(%d,&n);
for (i=0;i < n;i++)
{
printf(%d\\n,i);
}
}

1. Prints 1 to n

2. Prints 1 to n-1

3. Prints n

4. Infinite loop

5. Compilation error

Correct Option: E
Explanation:

error: address of register variable ‘n’ requested The program will not print any output because there is an error in the program. We cannot refer to the address of a register variable because it is not stored in the memory but on the registers inside the CPU. Only variable stores in the memory have an address.S o there will be an error in the program. Hence this option is correct.

- Hide questions