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.

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!

advertisement
advertisement

Leave a Comment

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter

Check his free tutorial videos on Advanced C Programming. Join his Sanfoundry-Classes telegram channel for all videos and queries on Programming, Data-Structures, Kernel, Personality development and Mentoring.