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?
-
#include
-
#include
-
using namespace std;
-
int main()
-
{
-
int ran = rand();
-
cout << ran << endl;
-
}
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?
-
#include
-
#include
-
using namespace std;
-
int main()
-
{
-
cout << RAND_MAX << endl;
-
}
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?
-
#include
-
#include
-
#include
-
int main ()
-
{
-
srand (time(NULL));
-
printf ("Random number: %dn", rand() % 100);
-
srand (1);
-
return 0;
-
}
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?
-
#include
-
#include
-
#include
-
using namespace std;
-
int main()
-
{
-
srand((unsigned)time(0));
-
int ran;
-
for (int i = 0; i < 2; i++)
-
{
-
ran = (rand() % 10) + 1;
-
cout << ran;
-
}
-
}
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?
-
#include
-
#include
-
#include
-
using namespace std;
-
int main()
-
{
-
srand((unsigned)time(0));
-
int ran;
-
int low = 1, high = 10;
-
int range = (high - low) + 1;
-
for(int index = 0; index < 1; index++)
-
{
-
ran = low + int(range * rand() / (RAND_MAX + 1.0));
-
cout << ran << endl;
-
}
-
}
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.