C Programming Questions and Answers – Random Number Generation
C Programming Questions and Answers – Random Number Generation
1.The function srand(unsigned)
a) Sets the seed for rand
b) Doesn’t exist
c) Is an error
d) None of the mentioned
View Answer
Answer:a
2.Which is the best way to generate numbers between 0 to 99?
a) rand()-100
b) rand()%100
c) rand(100)
d) srand(100)
View Answer
Answer:b
3.The correct way to generate numbers between minimum and maximum(inclusive) is _____________________.
a) minimum + (rand() % (maximum – minimum));
b) minimum + (rand() % (maximum – minimum + 1));
c) minimum * (rand() % (maximum – minimum))
d) minimum – (rand() % (maximum+minimum));
View Answer
Answer:b
4.rand() and srand() functions are used
a) To find sqrt
b) For and operations
c) For or operations
d) To generate random numbers
View Answer
Answer:d
5.What is the return type of rand() function?
a) short
b) int
c) long
d) double
View Answer
Answer:b
6.Which of the following can be used for random number generation?
a) random()
b) rnd()
c) rndm()
d) None of the mentioned
View Answer
Answer:a
7.Which of the following snippet will effectively generate random numbers?
a) rand();
b) rand(10);
c) rand(time(NULL));
d) All of the mentioned
View Answer
Answer:a
8.Which among the following is correct function call for rand and random?
a) rand() and random();
b) rand() and random(1);
c) rand(1) and random(1);
d) rand(1) and random();
View Answer
Answer:a
9.For the function call time(), what type of parameter is accepted?
a) int
b) int *
c) time_t
d) time_t *
View Answer
Answer:d
10.What is the output of this 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
Answer:c
11.What is the output of this 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
Answer:b
12.What is the output of this 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
Answer:a
13.What is the output of this 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
Answer:b
14.In the below program everytime 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
Answer:b
15.In the below program everytime 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
Answer:a
16.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
Answer:a