C Programming Questions and Answers – Random Number Generation – 2

«
»

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.

advertisement

1. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     #include <stdlib.h>
  3.     int main()
  4.     {
  5.         printf("%d\n", rand() % 1000);
  6.         return 0;
  7.     }

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
Explanation: None.
advertisement
advertisement

2. What will be the output of the following C code?

advertisement
  1.     #include <stdio.h>
  2.     #include <stdlib.h>
  3.     int main()
  4.     {
  5.         srand(9000);
  6.         printf("%d\n", rand());
  7.         return 0;
  8.     }

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
Explanation: None.
advertisement

3. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         printf("%d\n", srand(9000));
  5.         return 0;
  6.     }

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
Explanation: None.
advertisement

4. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         srand(time(NULL));
  5.         printf("%d\n", rand());
  6.         return 0;
  7.     }

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
Explanation: None.

5. In the below C program, every time program is run different numbers are generated.

  1.     #include <stdio.h>
  2.     #include <stdlib.h>
  3.     int main()
  4.     {
  5.         printf("%d\n", rand());
  6.         return 0;
  7.     }

a) True
b) False
c) Depends on the platform
d) Depends on the compiler
View Answer

Answer: b
Explanation: None.

6. In the following C program, every time program is run different numbers are generated.

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         srand(time(NULL));
  5.         printf("%d\n", rand());
  6.         return 0;
  7.     }

a) True
b) False
c) Depends on the platform
d) Depends on the compiler
View Answer

Answer: a
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

Answer: a
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.

advertisement
advertisement

Leave a Comment

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.