250+ TOP MCQs on Random Numbers and Answers

This section on online C++ test on “Random Numbers”. One shall practice these test questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin 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 online C++ test questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of online C++ test 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
Answer: d
Clarification: 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 header file?
a) RAND_MAX
b) Rand
c) Srand
d) Blitz
Answer: a
Clarification: RAND_MAX is a constant defined in 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
Answer: a
Clarification: 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.

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

  1.     #include 
  2.     #include 
  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
Answer: c
Clarification: 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:

$ g++ rand.cpp
$ a.out
574

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

  1.     #include 
  2.     #include 
  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
Answer: c
Clarification: RAND_MAX is a function used by the compiler to create a maximum random number.
Output:

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

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

  1.     #include 
  2.     #include 
  3.     #include 
  4.     int main ()
  5.     {
  6.         srand (time(NULL));
  7.         printf ("Random number: %dn", rand() % 100);
  8.         srand (1);
  9.         return 0;
  10.     }

a) 12
b) 23
c) 33
d) Any number from 0 to 99
Answer: d
Clarification: 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 
  2.     #include 
  3.     #include 
  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
Answer: c
Clarification: 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 
  2.     #include 
  3.     #include 
  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
Answer: a
Clarification: 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
Answer: d
Clarification: 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
Answer: b
Clarification: Time can be used to create a random number without duplication.

  250+ TOP MCQs on Constructors and Destructors – 1 and Answers

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top