Here is a listing of C Objective Questions on “Random Number Generation” along with answers, explanations and/or solutions:
1. What will be the output of the following C code?
-
#include
-
#include
-
int main()
-
{
-
printf("%dn", 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
Answer: c
Clarification: None.
2. What will be the output of the following C code?
-
#include
-
#include
-
int main()
-
{
-
srand(9000);
-
printf("%dn", 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
Answer: b
Clarification: None.
3. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
printf("%dn", 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
Answer: a
Clarification: None.
4. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
srand(time(NULL));
-
printf("%dn", 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
Answer: b
Clarification: None.
5. In the below C program, every time program is run different numbers are generated.
-
#include
-
#include
-
int main()
-
{
-
printf("%dn", rand());
-
return 0;
-
}
a) True
b) False
c) Depends on the platform
d) Depends on the compiler
Answer: b
Clarification: None.
6. In the following C program, every time program is run different numbers are generated.
-
#include
-
int main()
-
{
-
srand(time(NULL));
-
printf("%dn", rand());
-
return 0;
-
}
a) True
b) False
c) Depends on the platform
d) Depends on the compiler
Answer: a
Clarification: 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
Answer: a
Clarification: None.