Solutions of C Quiz 1

Want to participate in C Quiz 1, Click here

In this post, we will see the Answers and explanation of C Quiz 1.

Q.1. Which function would you use to convert 1.98 to 1?
ceil()
floor()
abs()
fabs()

Correct answer is floor(). We use floor function to convert values to the lower bound values. i.e. 1.98 to 1.

#include<stdio.h>
#include <math.h>

int main() {
    float m=1.98;
    printf("%f", floor(1.98));
    
    return 0;
}

Q.2. What will be the output of below C Program

#include<stdio.h>
int main() 
{
 int arr[2] = {1, 2};
 printf("%d", 1[arr]);
 return 0;
}

arr[1] is equal to 1[arr] in C. So the output of above code is 2.


Q.3. Guess the output of code below

#include<stdio.h>
int main() 
{
 int arr[2] = {1, 2};
 printf("%d", -1[arr]);
 return 0;
}

Error
-1
-2
2

The correct answer of above code is -2.
-1[arr] is same as -arr[1] .


Q.4. What will be the output of below C Program

#include<stdio.h>
int main() 
{
 int a, b;
 a=b=0;
 a++;
 b = b++;

 printf("%d %d", a, b);
 return 0;
}

0 0
1 1
0 1
1 0

Initially values of a & b are 0.
a++ means values of a incremented by 1. So the value of a becomes 1.
b = b++: b++ is post increment operator. Post increment operator first assigns the value and then increment it.
In this case b is assigned to b itself so the value of b remains same.

The output of above C program is 1 0


Q.5. Guess the output

#include<stdio.h>

int main() 
{
 int m=5, s=2;

 m += ++s;

 printf("%d %d", m, s);
 return 0;
}

8 2
8 3
3 3
3 2

Initially m = 5 and s = 2.
m += ++s: ++s is pre-increment operator. First it increments the value and then assign. So s becomes 2+1 i.e. 3
m += 3 which means m = m+3 so the value of m becomes 8.
Final output of above C Code is 8 3


Q.6. Which of the following is correct variable name/identifier in C?

abc
@a123
__a
_

Only option 2 is incorrect. Option 1, 2 & 4 are correct identifiers/variable names.


Q.7. Choose the correct output

#include
int main() 
{
 int m=5;
 {
 int m=10;
 }
 printf("%d", m);

 return 0;
}

10
5
0
15

Correct answer is 5.
We have already covered these type of questions on our Instagram Page.
Check out out Instagram Page.


Q.8. Why is a macro used in place of a function?

It reduces code size.
It increases code size.
It reduces execution time.
It increases execution time.

Macros simply helps to reduce the size of code. You can get more information about macros and quiz on macros on Guess The Output Series from cscode.tech.


Q.9. How many times will the following for loop execute?

#include
int main() 
{
 int i;

 for(i=0; i<5; i+=0.5)
 printf("1");

 return 0;
}

5
10
1
Infinite

The above for loop will execute infinite times.
i is an integer which does not store floating values.
i+=0.5 means i = i+0.5 & acts as i = i+0. So 1 is printed infinite times.


Q.10. Which of the following language is fast as compared to speed of execution

C
Python

Of-course C Programming Language is faster than Python Programming Language. For your information, NumPy, a Python Library is  written in C.


Admin

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top