C++ Programming Questions and Answers – Random Numbers

This section on C++ interview questions and answers focuses on “Random Numbers”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ interview questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ questions on “Random Numbers” along with answers, explanations and/or solutions:

1. Which header file is used to create the pseudo random generator?
a) random
b) cstdlib
c) rand
d) both random and cstdlib
View Answer

Answer: d
Explanation: cstdlib header file is used to create pseudo random number. C++11 standard added random header file as well to generate random numbers.

2. Which is a constant defined in <cstdlib> header file?
a) RAND_MAX
b) Rand
c) Srand
d) Blitz
View Answer

Answer: a
Explanation: RAND_MAX is a constant defined in <cstdlib> for deciding the maximum random number that can be produced.

3. How many parameters are available in srand function?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: a
Explanation: There is one parameter available in srand function. That is an integer value to be used as seed by the pseudo-random number generator algorithm.
advertisement
advertisement

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

  1.     #include <cstdlib>
  2.     #include <iostream>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         int ran = rand();
  7.         cout << ran << endl;
  8.     }

a) Any number
b) 89
c) 0​ to RAND_MAX
d) 79
View Answer

Answer: c
Explanation: As the declared number is integer, It will produce the random number from 0​ to RAND_MAX. The value of RAND_MAX is library-dependent, but is guaranteed to be at least 32767 on any standard library implementation.
Output:

Note: Join free Sanfoundry classes at Telegram or Youtube
$ g++ rand.cpp
$ a.out
574

5. What will be the output of the following C++ code?

advertisement
  1.     #include <cstdlib>
  2.     #include <iostream>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         cout << RAND_MAX << endl;
  7.     }

a) 4385234
b) 12321412
c) Depends on the compiler
d) 32564789
View Answer

Answer: c
Explanation: RAND_MAX is a function used by the compiler to create a maximum random number.
Output:

advertisement
$ g++ rand1.cpp
$ a.out
2147483647

6. What will be the output of the following C++ code?

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

a) 12
b) 23
c) 33
d) Any number from 0 to 99
View Answer

Answer: d
Explanation: This program will create a random number based on time function using srand function.
Output:

$ g++ rand2.cpp
$ a.out
23

7. What will be the output of the following C++ code?

  1.     #include <cstdlib>
  2.     #include <ctime>
  3.     #include <iostream>
  4.     using namespace std;
  5.     int main()
  6.     {
  7.         srand((unsigned)time(0));
  8.         int ran;
  9.         for (int i = 0; i < 2; i++)
  10.         {
  11.             ran = (rand() % 10) + 1;
  12.             cout << ran;
  13.         }
  14.     }

a) 2 4
b) 10 20
c) Any two number from 1 to 10
d) 50 6
View Answer

Answer: c
Explanation: In this program, It will produce two numbers from 1 to 10 by using srand function.
Output:

$ g++ rand3.cpp
$ a.out
4 5

8. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     #include <ctime>
  3.     #include <cstdlib>
  4.     using namespace std;
  5.     int main()
  6.     {
  7.         srand((unsigned)time(0));
  8.         int ran;
  9.         int low = 1, high = 10;
  10.         int range = (high - low) + 1;
  11.         for(int index = 0; index < 1; index++)
  12.         {
  13.             ran = low + int(range * rand() / (RAND_MAX + 1.0));
  14.             cout << ran << endl;
  15.         }
  16.     }

a) 1
b) 2
c) 3
d) 4
View Answer

Answer: a
Explanation: In this program the output will always be 1 because as one can observe from the formula used in line #13 that the term inside int() will always be zero. Hence the output will always be 1.
Output:

$ g++ rand4.cpp
$ a.out
1

9. Which operator is used to produce a certain number in a specific range?
a) $
b) %
c) modulo operator
d) both % and modulo operator
View Answer

Answer: d
Explanation: In C++, modulo operator is denoted by %.

10. Which can be used to create a random number without duplication?
a) Character
b) Time
c) Both Character & Time
d) Date
View Answer

Answer: b
Explanation: Time can be used to create a random number without duplication.

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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
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 & discussions at Telegram SanfoundryClasses.