250+ TOP MCQs on Trithemius Cipher and Answers

Data Structures & Algorithms Multiple Choice Questions on “Trithemius Cipher”.

1. Trithemius cipher is an example of ________________
a) mono-alphabetic cipher
b) poly-alphabetic cipher
c) transposition cipher
d) additive cipher

Answer: b
Clarification: Trithemius cipher is a substitution cipher. It falls under the category of poly alphabetic cipher as it uses multiple substitutions at different positions in order to cipher the plain text.

2. Encryption in trithemius cipher is done using _______________
a) trithemius table
b) vigenere cycle
c) tabula recta
d) any table provided by the person performing the encryption

Answer: c
Clarification: Encryption of plain text in trithemius cipher is done by making use of tabula recta. The same table is also used for encryption in vigenere cipher and running key cipher.

3. Which of the following is a modified version of Caesar cipher?
a) vigenere cipher
b) autokey cipher
c) running key cipher
d) trithemius cipher
View Answer

Answer: d
Clarification: If in caesar cipher we consider a shift that increases by 1 by each letter starting at 0 then it is equivalent to trithemius cipher. So trithemius cipher is a special case of caesar cipher.

4. Which of the following is a difference between trithemius cipher and vigenere cipher?
a) they use different tables for encryption
b) vigenere cipher is poly alphabetic whereas running key cipher is mono alphabetic
c) vigenere cipher uses a key whereas no key is required for using trithemius cipher
d) vigenere cipher is substitution cipher whereas trithemius cipher is transposition cipher

Answer: c
Clarification: Trithemius cipher is a special case of vigenere cipher. The difference is that vigenere cipher uses a different key every time but a fixed key is used by trithemius cipher.

5. Which of the following cipher require the use of tabula recta?
a) hill cipher
b) route cipher
c) rail fence cipher
d) trithemius cipher

Answer: d
Clarification: Ciphers like running key cipher, vigenere cipher, trithemius cipher, etc. makes use of tabula recta. Whereas hill cipher, rail fence cipher and route cipher does not require tabula recta for encryption of plain text.

6. Trithemius cipher is a special case of _______________
a) autokey cipher
b) vigenere cipher
c) hill cipher
d) route cipher

Answer: b
Clarification: Trithemius cipher is a special case of vigenere cipher. The difference is that vigenere cipher uses a different key every time but a fixed key is used by trithemius cipher.

7. Trithemius cipher is harder to crack than vigenere cipher.
a) true
b) false

Answer: b
Clarification: Trithemius cipher is a special case of vigenere cipher with ABCDEFGHIJKLMNOPQRSTUVWXYZ as key. So trithemius cipher is easier to crack as the key being used remains same every time.

8. What will be the plain text corresponding to cipher text “ACCFYX” if trithemius cipher is used?
a) ABACUS
b) ABROAD
c) ABRUPT
d) ABUSED
View Answer

Answer: a
Clarification: Running key cipher is a type of poly alphabetic substitution which uses tabula recta for making substitutions in the plain text. Using the table and key as ABCDEFGHIJKLMNOPQRSTUVWXYZ we find the plain text to be “ABACUS”.

9. Trithemius cipher is harder to crack than caesar cipher.
a) True
b) False
View Answer

Answer: a
Clarification: Trithemius cipher uses a more complex version of caesar cipher. So trithemius cipher is harder to crack as compared to caesar cipher.

10. Which of the following cipher is easiest to crack?
a) vigenere cipher
b) running key cipher
c) trithemius cipher
d) all are equally secure
View Answer

Answer: c
Clarification: Trithemius cipher is a special case of vigenere cipher and running key cipher is a variation of vigenere cipher. If one figures out that the cipher being used is trithemius then it is very easy to crack, unlike running key and vigenere ciphers as these use a secret key for encryption.

11. What will be the ciphered text corresponding to “” if trithemius cipher is used for encryption?
a) SBPISZTKZH
b) TCQJTAULAI
c) TBOGPVOESZ
d) SPBISZKTZH

Answer: a
Clarification: Encryption in trithemius cipher takes place exactly as in vigenere cipher if we consider the key to be ABCDEFGHIJKLMNOPQRSTUVWXYZ. So by using the tabula recta we can find the encrypted text which is “SBPISZTKZH”.

12. What will be the ciphered text corresponding to “ALGORITHM” if trithemius cipher is used for encryption?
a) BNJSWOAPV
b) BMHPSJUIN
c) AMIRVNZOU
d) MBPHJSNIU

Answer: c
Clarification: Encryption in trithemius cipher takes place exactly as in vigenere cipher if we consider the key to be ABCDEFGHIJKLMNOPQRSTUVWXYZ. So by using the tabula recta we can find the encrypted text which is “AMIRVNZOU”.

13. What will be the plain text corresponding to cipher text “RVUVMF” if trithemius cipher is used?
a) RABBIT
b) RUSSIA
c) RANGER
d) FRIEND

Answer: b
Clarification: Trithemius cipher is a type of poly alphabetic substitution which uses tabula recta for making substitutions in the plain text. Using the table and using the key as ABCDEFGHIJKLMNOPQRSTUVWXYZ we find the plain text to be “RUSSIA”.

contest

250+ TOP MCQs on Quickselect and Answers

Data Structures & Algorithms Multiple Choice Questions on “Quickselect”.

1. Which of the following is an alternative name of the quickselect algorithm?
a) quick sort
b) hoare’s selection algorithm
c) tony’s selection algorithm
d) kruskal’s algorithm
Answer: b
Clarification: Quick select is a selection algorithm. It was developed by Tony Hoare, thus it is also known as Hoare’s selection algorithm.

2. Quickselect is an example of ___________
a) sorting algorithm
b) selection algorithm
c) greedy algorithm
d) searching algorithm
Answer: b
Clarification: Quickselect is an example of a selection algorithm. It finds the kth smallest element from the given list.

3. What will be the output if quickselect algorithm is applied to the array arr={1,5,4,3,7} with k given as 4?
a) 1
b) 3
c) 4
d) 5
Answer: d
Clarification: Quickselect algorithm finds the kth smallest element from the given list. So as here the given value of k is 4 so we need to find the fourth smallest element which is 5 in the given array.

4. What is the auxiliary space requirement of the quickselect algorithm?
a) O(n2)
b) O(n)
c) O(n log n)
d) O(1)
Answer: d
Clarification: Quickselect algorithm requires no extra space in order to calculate the desired result. It performs manipulations in the given array itself so its auxiliary space requirement will be O(1).

5. Quickselect is an in-place algorithm?
a) true
b) false
Answer: a
Clarification: Quickselect’s auxiliary space requirement is O(1). So quickselect qualifies as an in-place algorithm.

6. What is the best case time complexity of quickselect?
a) O(n log n)
b) O(n2)
c) O(n)
d) O(log n)

Answer: c
Clarification: Best case time complexity of quickselect is O(n). It is observed in the case where good pivots are chosen consistently then the array size decreases exponentially and thus the required element is found in linear time.

7. Quickselect’s algorithm is similar to which of the following algorithm?
a) Merge sort
b) Quicksort
c) Selection sort
d) Counting sort
Answer: b
Clarification: Both quicksort and quickselect algorithms are closely related. They were developed by the same person. Like quicksort, quickselect also works by choosing a pivot and partitioning array.

8. What is the worst case time complexity of quickselect?
a) O(n log n)
b) O(n2)
c) O(n)
d) O(log n)

Answer: b
Clarification: Worst case complexity occurs in the case where bad pivots are chosen consistently due to which the size of the array decreases in the steps of 1 only. This leads to a time complexity of O(n2).

9. What is the average case time complexity of quickselect?
a) O(n log n)
b) O(n2)
c) O(n)
d) O(log n)

Answer: c
Clarification: In quickselect, we don’t recur for both portions of the array. Only that portion is considered where the smallest element lies. So this causes the average time complexity to be O(n).

10. Which of the following is a disadvantage of quickselect?
a) Poor space complexity
b) Poor best case time complexity
c) Poor average case time complexity
d) Poor worst case time complexity

Answer: d
Clarification: Quickselect has a poor worst case time complexity of O(n2). There are algorithms which have O(n) time complexity in the worst case.

11. Which of the following correctly represent the algorithm of quickselect?
a)

function quickSelect(list, left, right, k)
   if left = right
      return list[left]
   Select a pivotIndex between left and right
   pivotIndex := partition(list, left, right,  pivotIndex) 
   if k = pivotIndex
      return list[k]
   else if k < pivotIndex
      right := pivotIndex - 1
   else
      left := pivotIndex + 1

b)

function quickSelect(list, left, right, k)
   if left = right
      return list[right]
   Select a pivotIndex between left and right
   pivotIndex := partition(list, left, right,  pivotIndex)
   if k = pivotIndex
      return list[k]
   else if k > pivotIndex
      right := pivotIndex - 1
   else
      left := pivotIndex + 1

c)

function quickSelect(list, left, right, k)
   if left = right
      return list[left]
   Select a pivotIndex between left and right
   pivotIndex := partition(list, left, right,  pivotIndex)
   if k = pivotIndex
      return list[k]
   else if k < pivotIndex
      right := pivotIndex +1
   else
      left := pivotIndex -1

d)

function quickSelect(list, right,left, k)
   if left = right
      return list[left]
   Select a pivotIndex between left and right
   pivotIndex := partition(list, left, right,  pivotIndex)
   if k = pivotIndex
      return list[k]
   else if k < pivotIndex
      right := pivotIndex - 1
   else
      left := pivotIndex + 1

View Answer

Answer: a
Clarification: In quickselect algorithm if index of partitioned element is more than k, then we recur for left part. If index is same as k, we have found the kth smallest element and we return. If index is less than k, then we recur for right part.

 
 

250+ TOP MCQs on Interpolation Searching Algorithm and Answers

Data Structures & Algorithms Multiple Choice Questions & Answers (MCQs) on “Interpolation Searching Algorithm”.

1. Which of the following is the most desirable condition for interpolation search?
a) array should be sorted
b) array should not be sorted but the values should be uniformly distributed
c) array should have a less than 64 elements
d) array should be sorted and the values should be uniformly distributed
Answer: d
Clarification: Desirable condition for interpolation search is that the array should be sorted and the values should be uniformly distributed. The algorithm would fail to give the correct result if array is not sorted.

2. Interpolation search is a variation of?
a) Linear search
b) Binary search
c) Jump search
d) Exponential search

Answer: b
Clarification: Interpolation search is a variation of binary search which gives the best result when the array has uniformly distributed values. Interpolation search goes to different positions depending on the value being searched whereas binary search always goes to the middle element.

3. Interpolation search performs better than binary search when?
a) array has uniformly distributed values but is not sorted
b) array is sorted and has uniform distribution of values
c) array is sorted but the values are not uniformly distributed
d) array is not sorted

Answer: b
Clarification: Interpolation search is an improvement over a binary search for the case when array is sorted and has uniformly distributed values. Binary search performs better when the values are not distributed uniformly.

4. In which of the following case jump search performs better than interpolation search?
a) When array has uniformly distributed values but is not sorted
b) when array is sorted and has uniform distribution of values
c) when array is sorted but the values increases exponentially
d) when array is not sorted

Answer: c
Clarification: In case of non uniform distribution of values the time complexity of interpolation search is O(n) whereas the average time complexity of jump search is O(n1/2). So in such a case jump search has a better performance.

5. What is the time complexity of interpolation search when the input array has uniformly distributed values and is sorted?
a) O(n)
b) O(log log n)
c) O(n log n)
d) O(log n)
View Answer

Answer: b
Clarification: Interpolation search goes to different positions in the array depending on the value being searched. It is an improvement over binary search and has a time complexity of O(log log n).

6. What is the auxiliary space requirement of interpolation search?
a) O(n)
b) O(2n)
c) O(1)
d) O(log n)
Answer: c
Clarification: Interpolation search does not require any auxiliary space for finding the element being searched. So it has a constant auxiliary space O(1).

7. What is the time complexity of exponential search when the input array is sorted but the values are not uniformly distributed?
a) O(n1/2)
b) O(log log n)
c) O(n)
d) O(log n)

Answer: c
Clarification: When an array has non uniformly distributed values then in that case the algorithm of interpolation search fails to work efficiently. As a result, it has a time complexity of O(n) in such a case.

8. Which of the following searching algorithm is fastest when the input array is sorted and has uniformly distributed values?
a) jump search
b) exponential search
c) binary search
d) interpolation search
Answer: d
Clarification: Interpolation search has a time complexity of O( log log n) when the array is sorted and has uniformly distributed values. It has the least time complexity out of the given options for such a case.

9. Which of the following searching algorithm is fastest when the input array is sorted but has non uniformly distributed values?
a) jump search
b) linear search
c) binary search
d) interpolation search

Answer: c
Clarification: Interpolation search has a time complexity of O(n) when the array does not have uniformly distributed values. So in such a case binary search has the least time complexity out of the given options.

10. Which of the following searching algorithm is fastest when the input array is not sorted but has uniformly distributed values?
a) jump search
b) linear search
c) binary search
d) interpolation search
Answer: b
Clarification: Out of the given options linear search is the only searching algorithm which can be applied to arrays which are not sorted. It has a time complexity of O(n) in the worst case.

11. Interpolation search is an in place algorithm.
a) true
b) false
Answer: a
Clarification: Interpolation search has an auxiliary space complexity of O(1). So it qualifies as an in place algorithm.

12. Interpolation search has a better time complexity than exponential search for any given array.
a) True
b) False

Answer: b
Clarification: The worst case time complexity of interpolation search and exponential search are O(n) and O(log n) respectively. So exponential search is better when the worst case scenario is considered.

13. What is the formula used for calculating the position in interpolation search?
(x = element being searched, A[] = input array, low and high are the leftmost and rightmost index of A[] respectively)
a) ((x – A[low]) * (high – low)) / (A[high] – A[low])
b) high + ((x – A[low]) * (high – low)) / (A[high] – A[low])
c) low + ((x – A[low]) * (high – low)) / (A[high] – A[low])
d) x + ((x – A[low]) * (high – low)) / (A[high] – A[low])

Answer: c
Clarification: For calculating the position after each iteration in interpolation search we use the formula low + ((x – A[low]) * (high – low)) / (A[high] – A[low]). Then the value at the calculated position is compared with the element being searched.

14. What are the updated values of high and low in the array if the element being searched is greater than the value at calculated index in interpolation search? (pos = current position)
a) low = pos + 1, high remains unchanged
b) high = pos – 1, low remains unchanged
c) low = low +1, high = high – 1
d) low = pos +1, high = pos – 1

Answer: a
Clarification: When the element being searched is greater than the value at the calculated position then in that case we update low and high remains unaltered. Updated value of low is low = pos + 1.

15. What are the updated values of high and low in the array if the element being searched is lower than the value at calculated index in interpolation search? (pos = current position)
a) low = pos + 1, high remains unchanged
b) high = pos – 1, low remains unchanged
c) low = low +1, high = high – 1
d) low = pos +1, high = pos – 1

Answer: b
Clarification: When the element being searched is lower than the value at the calculated position then in that case we update high and low remains unaltered. Updated value of high is high = pos – 1.

and Answers.

contest

250+ TOP MCQs on Heapsort Multiple Choice Questions and Answers (MCQs) and Answers

Data Structures & Algorithms Multiple Choice Questions on “Heapsort – 1”.

1. On which algorithm is heap sort based on?
a) Fibonacci heap
b) Binary tree
c) Priority queue
d) FIFO
Answer: c
Clarification: Heap sort is based on the algorithm of priority queue and it gives the best sorting time.

2. In what time can a binary heap be built?
a) O(N)
b) O(N log N)
c) O(log N)
d) O(N2)

Answer: a
Clarification: The basic strategy is to build a binary heap of N elements which takes O(N) time.

3. Heap sort is faster than Shell sort.
a) true
b) false
Answer: b
Clarification: Heap sort is slower than Shell sort because Shell sort uses Sedgewick’s increment sequence.

4. Consider the following heap after buildheap phase. What will be its corresponding array?
heapsort-questions-answers-q4
a) 26,53,41,97,58,59,31
b) 26,31,41,53,58,59,97
c) 26,41,53,97,31,58,59
d) 97,53,59,26,41,58,31
Answer: d
Clarification: Constructing a max heap using the elements 97,53,59,26,41,58,31 will cause the heap to look like that.

5. In what position does the array for heap sort contains data?
a) 0
b) 1
c) -1
d) anywhere in the array

Answer: a
Clarification: The array for heap sort contains data at position 0 whereas for a binary heap, array begins at 1. This is the reason for its complexity.

6. In heap sort, after deleting the last minimum element, the array will contain elements in?
a) increasing sorting order
b) decreasing sorting order
c) tree inorder
d) tree preorder
Answer: b
Clarification: By logic, after deleting minimum element, the heap will contain elements in decreasing sorting order. We can change this by altering the ordering property.

7. What is the typical running time of a heap sort algorithm?
a) O(N)
b) O(N log N)
c) O(log N)
d) O(N2)
Answer: b
Clarification: The total running time of a heap sort algorithm is mathematically found to be O(N log N).

8. How many arrays are required to perform deletion operation in a heap?
a) 1
b) 2
c) 3
d) 4

Answer: b
Clarification: To perform deletion operation in a heap, we require 2 arrays and that occupies extra memory space and hence increase in running time.

9. What is the time taken to perform a delete min operation?
a) O(N)
b) O(N log N)
c) O(log N)
d) O(N2)

Answer: c
Clarification: The time taken to perform a deletion of a minimum element is mathematically found to be O( log N).

10. Heap sort is an extremely stable algorithm.
a) true
b) false
View Answer

Answer: a
Clarification: Heap sort uses fewer comparisons than other sorting algorithms and hence it is an extremely stable algorithm.

11. What is the average number of comparisons used in a heap sort algorithm?
a) N log N-O(N)
b) O(N log N)-O(N)
c) O(N log N)-1
d) 2N log N + O(N)
View Answer

Answer: d
Clarification: The average number of comparisons in a heapsort algorithm is mathematically found to be 2N log N + O(N).

12. What is the time taken to copy elements to and from two arrays created for deletion?
a) O(N)
b) O(N log N)
c) O(log N)
d) O(N2)

Answer: a
Clarification: The time taken to copy elements to and from the main array and extra array is found to be O(N).

13. What is the average number of comparisons used to heap sort a random permutation of N distinct items?
a) 2N log N-O(N)
b) 2N log N-O(N log N)
c) 2N log N-O(N log log N)
d) 2N log N-O(log N)
Answer: c
Clarification: According to a theorem, the average number of comparisons used to heap sort a random permutation of N distinct items is found to be 2N log N-O(N log log N).

contest

250+ TOP MCQs on MSD Radix Sort and Answers

Data Structures & Algorithms Multiple Choice Questions on “MSD Radix Sort”.

1. How many comparisons will be made to sort the array arr = {1, 5, 3, 8, 2} using MSD radix sort?
a) 5
b) 7
c) 9
d) 0
Answer: d
Clarification: As MSD radix sort is an example of non comparison sort so it is able to sort an array without making any comparison. So the answer should be 0.

2. What is the full form of MSD in MSD radix sort?
a) most significant digit
b) many significant digit
c) more significant digit
d) must significant digit
Answer: a
Clarification: MSD stands for Most Significant Digit. It is named so because in this algorithm the processing begins from the most significant digit.

3. Which of the following combines qualities of MSD radix sort and LSD radix sort?
a) in-place MSD radix sort
b) stable MSD radix sot
c) 3 way radix quick sort
d) forward radix sort

Answer: d
Clarification: Forward radix sort combines the qualities of MSD and LSD radix sort. The sorting is done by separating the strings into groups.

4. Which of the following is the most suitable definition of radix sort?
a) It is a non comparison based integer sort
b) It is a comparison based integer sort
c) It is a non comparison based non integer sort
d) It is a comparison based non integer sort

Answer: a
Clarification: Radix sort is a non-comparison based integer sort. It sorts the given data by grouping keys which share the same significant position value.

5. Which of the following is an alternate name of MSD radix sort?
a) bottom up radix sort
b) top down radix sort
c) forward radix sort
d) backward radix sort

Answer: b
Clarification: Top down radix sort is an alternate name of MSD radix sort. It is because in this algorithm the processing starts from the most significant digit and end at least significant digit.

6. Which of the following is not true about MSD radix sort?
a) its processing starts from the most significant digit
b) it is not a stable sort
c) it is an in place sorting algorithm
d) it is non comparison based sort

Answer: c
Clarification: MSD radix sort takes non constant time for sorting the input data. So it is not an in place sorting algorithm.

7. MSD radix sort should be preferred over LSD radix sort when we have to maintain the original relative order.
a) True
b) False

Answer: b
Clarification: MSD radix sort is not a stable sort whereas LSD radix sort is stable. So when we require to preserve the original order then in that case we should prefer LSD radix sort.

8. What is the average time complexity of MSD radix sort (w= bits required to store each key)?
a) O(n + w)
b) O(n.w)
c) O(n2)
d) O(n log n)

Answer: b
Clarification: Time complexity of radix sort is O(n.w). It performs better than quick sort when we have log n bits for every digit.

9. MSD radix sort is an in place sorting algorithm.
a) True
b) False
View Answer

Answer: b
Clarification: MSD radix sort takes non constant time for sorting the input data. So it is not an in place sorting algorithm.

10. Which of the following statement is not a stable sorting algorithm?
a) LSD radix sort
b) MSD radix sort
c) Counting sort
d) Pigeonhole sort
View Answer

Answer: b
Clarification: MSD radix sort is not a stable sort. It is because the elements with identical values do not appear in the same order in the output array as they were in the input array.

11. Which of the following is not true about radix sort?
a) Radix sort performs better than quick sort when we have log n bits for every digit
b) Radix sort has better cache performance than quick sort
c) Radix sort has higher values of constant factor in asymptotic notation
d) Radix sort takes more space than quick sort

Answer: b
Clarification: Quick sort has a better cache performance than radix sort. Radix sort also takes more space as compared to quick sort.

12. What is the advantage of radix sort over quick sort?
a) radix sort performs better than quick sort when we have log n bits for every digit
b) radix sort has lesser space complexity
c) radix sort is not a comparison based sorting technique
d) radix sort has better cache performance than quick sort

Answer: a
Clarification: Radix sort performs better than quick sort when we have log n bits for every digit. But radix sort takes more space as compared to quick sort.

13. What will be the order of elements of the array arr = {23, 67, 143, 654, 43} after first iteration of MSD sort is complete?
a) 23, 43, 67, 143, 654
b) 23, 67, 43, 143, 654
c) 23, 67, 143, 654, 43
d) 23, 143, 43, 654, 67

Answer: b
Clarification: In the first iteration the array is sorted according to the most significant digit I.e. hundreds place value. So the order of elements will be 23, 67, 43, 143, 654.

contest

250+ TOP MCQs on Pseudorandom Number Generators and Answers

Data Structures & Algorithms Multiple Choice Questions on “Pseudorandom Number Generators”.

1. What is pseudo random number generator?
a) an algorithm that generates random numbers with help of mathematical formula
b) an algorithm that generates random numbers according to user activity
c) an algorithm that generates random numbers according to time
d) an algorithm that generates random numbers with help of user input
Answer: a
Clarification: A pseudo random number generator generates random numbers with the help of a mathematical formula. We can seed the random number generator with a different value everytime if we want unique random numbers to be generated.

2. What should be the return type of rand() function?
a) int
b) float
c) long
d) double

Answer: a
Clarification: The return type of rand () is int. It can generate random numbers from 0 to RAND_MAX.

3. What is the purpose of using function srand()?
a) to set the seed of rand() function
b) to generate random numbers
c) to enable rand() function
d) to improve efficiency of rand()

Answer: a
Clarification: The function srand() sets the seed of rand(). It can be used to generate a unique set of random numbers every time.

4. What is the range of rand()?
a) 0 to RAND_MAX
b) 0 to infinity
c) 0 to 2147483647
d) 0 to 32767
View Answer

Answer: a
Clarification: The function rand() generates random numbers in the range 0 to RAND_MAX. The value of RAND_MAX is minimum 32,767.

5.What is the correct formula for generating random numbers in the range (lower,upper) using rand()?
a) rand() % (upper – lower)
b) rand() + lower
c) (rand()%(upper-lower)) + lower
d) (rand()%(upper-lower+1)) + lower

Answer: d
Clarification: The correct formula for generating random numbers in the range (lower,upper) using rand() is (rand()%(upper-lower+1)) + lower. The function rand() generates random numbers in the range 0 to RAND_MAX.

6. Which of the following will generate random numbers in the range 1-100 (both inclusive)?
a) rand() % 100
b) rand() % 101
c) (rand() % (101)) + 1
d) (rand() % (100)) + 1

Answer: d
Clarification: Formula for generating random numbers in the range (lower,upper) using rand() is (rand()%(upper-lower+1)) + lower. So the correct answer will be (rand() % (100)) + 1.

7. What is the minimum value of RAND_MAX possible in any implementation?
a) 0
b) 32767
c) 2147483647
d) 128

Answer: b
Clarification: The value of RAND_MAX varies according to implementation. But it is guaranteed to be at least 32767.

8. Function rand() generates unique random numbers every time.
a) true
b) false

Answer: b
Clarification: Function rand() does not generate unique random numbers every time. For achieving this we have to use srand() along with rand().

9. What is the default value of seed if function rand() is called before srand()?
a) srand(0)
b) srand(1)
c) srand(time(null))
d) srand(-1)

Answer: b
Clarification: If srand() is not called before the call to the function rand() then the value of seed is taken as srand(1) by default. We should use srand() before rand() in order to use our own seed.

10. Pseudo random number generators can be used for data encryption.
a) true
b) false

Answer: b
Clarification: Pseudo random number generators cannot be used for data encryption as the random numbers generated by them are predictable. For data encryption the numbers should be absolutely unpredictable.

11. Predict the output of the following code.

#include  
int main() 
{ 
     srand(0); 
     printf("%dn", rand()); 
     return 0; 
}

a) compilation error
b) random number between 0 to RAND_MAX
c) cannot be predicted
d) 0

Answer: b
Clarification: The function rand() generates random numbers in the range 0 to RAND_MAX. The function srand() seeds rand(). We get unique set of random numbers if rand() is seeded with a different value every time.

12. Which header file contains the function rand() in C language?
a) stdlib
b) iostream
c) stdio
d) time
Answer: a
Clarification: In C language the header file stdlib contains the function rand(). It generates pseudo random numbers.

13. Predict the output of the following code.

#include  
int main() 
{ 
     srand(0); 
     printf("%dn", rand()%50); 
     return 0; 
}

a) compilation error
b) random number between 0 to 50 (both inclusive)
c) random number between 0 to 51 (both inclusive)
d) random number between 0 to 49 (both inclusive)

Answer: d
Clarification: Formula for generating random numbers in the range (lower,upper) using rand() is (rand()%(upper-lower+1)) + lower. So the given code will generate random numbers between 0 to 49.

14. Which of the following code will generate unique random numbers every time?
a)

#include  
int main() 
{ 
     srand(0); 
     printf("%dn", rand()%50); 
     return 0; 
}

b)

#include  
int main() 
{ 
     srand(time(0)); 
     printf("%dn", rand()%50); 
     return 0; 
}

c)

#include  
int main() 
{ 
    srand(1); 
    printf("%dn", rand()%50); 
    return 0; 
}

d)

#include  
int main() 
{
     printf("%dn", rand()%50); 
     return 0; 
}

Answer: b
Clarification: The function rand() generates random numbers in the range 0 to RAND_MAX. The function srand() seeds rand(). We get a unique set of random numbers if rand() is seeded with a different value every time. This can be achieved by using srand(time(0)).

 
 

15. Which of the following code will give an error?
a)

#include  
int main() 
{ 
     printf("%dn", srand()); 
     return 0; 
}

b)

#include  
int main() 
{ 
     srand(time(0)); 
     printf("%dn", rand()%50); 
     return 0; 
}

c)

#include  
int main() 
{ 
     srand(1); 
     return 0; 
}

d)

#include  
int main() 
{
     printf("%dn", rand()%50); 
     return 0; 
}

Answer: a
Clarification: The function rand() generates random numbers in the range 0 to RAND_MAX. The function srand() seeds rand(). So we get an error if we try to print srand().