This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Random Number Generation – 2”.
Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.
1. What will be the output of the following C code?
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("%d\n", rand() % 1000);
return 0;
}
a) Compile time error
b) An integer between 0-1000
c) An integer between 0-999 including 0 and 999
d) An integer between 0-1000 including 1000
View Answer
Explanation: None.
2. What will be the output of the following C code?
#include <stdio.h>
#include <stdlib.h>
int main()
{
srand(9000);
printf("%d\n", rand());
return 0;
}
a) Compile time error
b) An integer in the range 0 to RAND_MAX
c) A double in the range 0 to 1
d) A float in the range 0 to 1
View Answer
Explanation: None.
3. What will be the output of the following C code?
#include <stdio.h>
int main()
{
printf("%d\n", srand(9000));
return 0;
}
a) Compile time error
b) An integer in the range 0 to 9000
c) A float in the range 0 to 1
d) A double in the range 0 to 9000
View Answer
Explanation: None.
4. What will be the output of the following C code?
#include <stdio.h>
int main()
{
srand(time(NULL));
printf("%d\n", rand());
return 0;
}
a) Compile time error
b) An integer in the range 0 to RAND_MAX
c) A double in the range 0 to 1
d) A float in the range 0 to 1
View Answer
Explanation: None.
5. In the below C program, every time program is run different numbers are generated.
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("%d\n", rand());
return 0;
}
a) True
b) False
c) Depends on the platform
d) Depends on the compiler
View Answer
Explanation: None.
6. In the following C program, every time program is run different numbers are generated.
#include <stdio.h>
int main()
{
srand(time(NULL));
printf("%d\n", rand());
return 0;
}
a) True
b) False
c) Depends on the platform
d) Depends on the compiler
View Answer
Explanation: None.
7. Which of these is a correct way to generate numbers between 0 to 1(inclusive) randomly?
a) rand() / RAND_MAX
b) rand() % 2
c) rand(0, 1)
d) none of the mentioned
View Answer
Explanation: None.
Sanfoundry Global Education & Learning Series – C Programming Language.
To practice all areas of C language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Watch Advanced C Programming Videos
- Check C Books
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Apply for C Internship