In The Following Program, If The Value of Guess Entered by The User Is 65, What Will Be The Expected Output(s)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

RANDOM NUMBERS

Each question carry 2 marks

1) In the following program, if the value of Guess entered by the user is 65, what will be the expected output(s)
from the following options (i), (ii), (iii) and (iv)?
#include <iostream.h>
#include <stdlib.h>
void main()
{
int Guess;
randomize();
cin>>Guess;
for (int I=1;I<=4;I++)
{
New=Guess+random(I);
cout<<(char)New;
}
}
(i) ABBC
(ii) ACBA
(iii) BCDA
(iv) CABD

Ans:

I random(I) New (char)New


Min Max Min Max Min Max
1 0 0 65 65 A A
2 0 1 65 66 A B
3 0 2 65 67 A C
4 0 3 65 68 A D

From the above table, it is clear that option (ii) will not be expected output (since when I =2, then C cannot be there in
the output. Similarly, option (iii) will not be expected as when I =1, 2 and 3 then output cannot be B, C and D
respectively.
Option (iv) is also not possible, when I =1 then output cannot be C.
So, only possible output is (i) ABBC
2)

1
Ans:-

C Sequence Select[Sequence]
Minimum Maximum
0 0 3 25/90/30/45
1 0 2 25/90/30
2 0 1 25/90
3 0 0 25

So option (i) will not be expected.

3) In the following program, find the correct possible output(s) from the options. Justify your answer.
#include<iostream.h>
#include<stdlib.h>
void main( )
{
randomize( );
int p=99,q=999;
int x=random(3)+4;
int y=random(2)+2;
for(int i=0;i<x;i++)
cout<<’#’;
cout<<p<<’-’;
for(i=0;i<y;i++)
cout<<’@’;
cout<<q<<endl;
}
i. ##99-@999
ii. ##99-@@999
iii. ######99-@@999
iv. ####99-@@@

Ans: option iii)

random(3) x i #
Min Max
0 4 0 3 ####
1 5 0 4 #####
2 6 0 5 ######

random(2) y i @
Min Max
0 2 0 1 @@
1 3 0 2 @@@
p=99, q= 999

possible output option (iii)– because according to the above tables six “#” can be generated if value of x is 6 and two
“@” can be generated if value of y is 2.
Option (i) Not possible – because two “##” symbol and one “@” symbol cannot be generated according to the above
table.
Option (ii) Not possible – because two “##” symbol cannot be generated according to the above table.
Option (iv) Not possible – because the value of q (999) is not present in the output.
4) The following code is from a game, which generates a set of 4 random numbers.
Yallav is playing this game, help him to identify the correct option(s) out of the four choices given below as the possible
set of such numbers generated from the program code so that he wins the game. Justify your answer.
#include <iostream.h>
#include <stdlib.h>

2
const int LOW=15;
void main ( )
{
randomize( ) ;
int POINT=5, Number;
for (int 1=1;I<=4;I++)
{
Number=LOW+random(POINT) ;
cout<<Number<<“:” ;
POINT--;
}
}
(i) 19:16:15:18:
(ii) 14:18:15:16:
(iii) 19:16:14:18:
(iv) 19:16:15:16:

5) Go through the C++ code shown below, and find out the possible output or
outputs from the suggested Output Options (i) to (iv). Also, write the least
value and highest value, which can be assigned to the variable Guess. 2
#include <iostream.h>
#include <stdlib.h>
void main ( )
{
randomize ( ) ;
int Guess, High=4;
Guess=random(High)+ 50 ;
for{int C=Guess ; C<=55 ; C++)
cout<<C<<"#" ;
}
(i) 50 # 51 # 52 # 53 # 54 # 55 #
(ii) 52 # 53 # 54 # 55
(iii) 53 # 54 #
(iv) 51 # 52 # 53 # 54 # 55

Ans: random(High) – minimum value – 0 and maximum value - 3


Guess - minimum value – 50 and maximum value – 53

Guess C
Min Max
50 50 55
51 51 55
52 52 55
53 53 55
Possible output option (i)

Option (ii) not possible as “#” is missing in 4th iteration.


Option (iii) not possible as the maximum value of C must be 55
Option (iv) not possible as “#” is missing in 5th iteration.

3
6) Observe the following program & Find out the correct possible outputs from the options .
( Assuming that all required header files are included in the program)
void main()
{
char serial[ ] = {‘E’, ‘X’,’A’,’M’,’\0’};
int number [ ]= { 69,66,67,68};
randomize();
cout<<number[random(3)];
for( int i=0;i<4;i++)
cout<<serial[sizeof(int) + random(2)-1];
}
(i) 67XXAX
(ii) 69AXXA
(iii) 66AAXA
(iv) 67AAAM
Ans:
random(3) = minimum value 0 and maximum value 2

random(3) number[random(3)]
0 69
1 66
2 67

random(2) = minimum value 0 and maximum value 1


random(2) sizeof(int) + random(2)-1 serial[sizeof(int) + random(2)-1]
0 2+0–1=1 X
1 2 + 1 -1 = 2 A
So, possible outputs

(i) 67XXAX
(ii) 69AXXA
(iii) 66AAXA

7) Based on the following C++ code, find out the expected correct output(s) from the options (i) to (iv). Also find out the
minimum and maximum value that can be assigned to the variable Trick used in the code at the time when value of
Count is 2:
#include<iostream.h>
#include<stdlib.h>
const int Max=3;
void main( )
{
randomize( );
char Status[] [20]= {"Jammu", "Jharkhand", "Maharashtra" };
int Turn=10, Trick;
for(int count=1;count<4;count++)
{
Trick=random(count);
cout<<Turn-Trick<<Status[Trick]<<"#";
}
}
i) 10Jammu#10Jammu#8Maharashtra#
ii) 10Jammu#8Maharashtra#9Jharkhand#
iii) 10Jammu#9Jharkhand#10Jammu#
iv) 10Jammu#10Jharkhand#8Maharashtra#

Answer:

Trick
count Turn-Trick Status
Min Max
1 0 0 10 Jammu
2 0 1 10 Or 9 Jammu Or Jharkhand
Jammu Or Jharkhand Or
3 0 2 10 or 9 Or 8
Maharashtra
So possible outputs:

i) 10Jammu#10Jammu#8Maharashtra#
iii) 10Jammu#9Jharkhand#10Jammu#

4
8) Study the following program and select the possible output from it
#include<stdlib.h>
#include<iostream.h>
void main()
{
randomize();
char city[ ][4]={“PKD”, “EKM”, ”TVM”, “KOL”, “CAL”};
int ZEN;
for(int i=0;i<3;i++);
{
ZEN=random(2)+1;
cout<<city[ZEN]<<”@”;
}
}
(i) PKD@ EKM @TVM@
(ii) EKM @TVM@ EKM @
(iii) TVM@KOL@CAL@
(iv) TVM@ EKM @TVM@
Answer:

random(2) = minimum value 0 and maximum value 1

ZEN = minimum value 1 and maximum value 2

i ZEN city[ZEN]
0 1 EKM
2 TVM
1 1 EKM
2 TVM
2 1 EKM
2 TVM

Possible outputs:

(ii) EKM @TVM@ EKM @


(iv) TVM@ EKM @TVM@

9) Study the following program and select the possible output(s) from the option (i) to (iv) following it.
Also write the maximum and the minimum values that can be assigned to the variable NUM.
Note: (2)
- Assume all required header files are already being included in the program.
- random(n) function generates an integer between 0 and n-1.

void main()
{
randomize();
int NUM;
NUM=random(3)+2;
char TEXT[]=”ABCDEFGHIJK”;
for (int I=1;I<=NUM; I++)
{
for (int J=NUM;J<=7;J++)
cout<<TEXT[J];
cout<<endl;
}
}
(i)FGHI (ii) BCDEFGH (iii) EFGH (iv) CDEFGH
FGHI BCDEFGH EFGH CDEFGH
FGHI EFGH
FGHI EFGH

5
Answer:

random(3) = minimum value 0 and maximum value 2

NUM = minimum value 2 and maximum value 4

If NUM = 2 then If NUM = 3 then If NUM = 4 then


For I = 1 and I = 2 For I = 1, I = 2 and I =3 For I = 1, I = 2, I =3 and I = 4

J TEXT[J]
J TEXT[J]
2 C J TEXT[J]
3 D
3 D 4 E
4 E
4 E 5 F
5 F
5 F 6 G
6 G
6 G 7 H
7 H
7 H

So possible outputs option (iii) and option (iv)

Option (i) – not possible as NUM value can never be 5

Option (ii) – not possible as NUM value can never be 1

10) Observe the following C++ code and find out, which out of the given options i) to iv) are the expected correct output.
Also assign the maximum and minimum value that can be assigned to the variable ‘Go’.

void main()
{ int X [4] ={100,75,10,125};
int Go = random(2)+2;
for (int i = Go; i< 4; i++)
cout<<X[i]<<”$$”;
}

i. 100$$75 ii. 75$$10$$125$$ iii. 75$$10$$ iv.10$$125$

Answer: random(2) = minimum value 0 and maximum value 1


Go = minimum value 2 and maximum value 3
Go i X[i]
2 2 10
3 125
3 3 125
So, from the above table it is clear Option (iv) is the only expected correct output.

Minimum value of Go = 2
Maximum value of Go = 3

11) Observe the following C++ code and find out, which out of the given options i) to iv) are the expected correct output.
Find the maximum and minimum value of L.
#include<stdlib.h>
#include<iostream.h>
#include<string.h>
void main()
{
randomize();
char P[ ] = “C++PROGRAM”;
long L;
for(int I =0; P[I]!=’R’;I++)
{
L=random(sizeof(L)) + 5;
cout<<P[ L ] <<”-“;
}
}
(i) R-P-O-R-
(ii) P-O-R-+-
(iii) O-R-A-G-

6
(iv) A-G-R-M-

Answer:

sizeof(L) = 4

random(sizeof(L)) = minimum value 0 and maximum value 3

L = minimum value 5 and maximum value 8

If value of I =0, Or value of I =1, Or value of I = 2 Or value of I =3 then following values will be generated for L and P[L]

L P[L]
5 O
6 G
7 R
8 A

So expected correct output option (iii)

Option (i) – not possible as value “P” can never be generated.


Option (ii) – not possible as value “P” and “+” can never be generated.
Option (iv) – not possible as value “M” can never be generated.

Minimum value of L = 5
Maximum value of L = 8

12) Consider the following C++ program code and choose the option(s) which are not possible as output. Also, print the
minimum & maximum value of variable Pick during complete execution of the program.(assume all necessary header
files are included in program):

(a) 5:6:6:6:
(b) 4:7:5:3:
(c) 8:6:1:2:
(d) 7:5:3:1

Ans:
Option (a) , (c) & (d)
Maximum value of Pick will be 8
Minimum value of Pick will be 1
(1/2 mark for each correct option)
(1 mark each giving correct values of both max & min)

7
13)

Ans:

Possible Outputs
(ii) and (iii)

Maximum value of R = 2
Maximum value of C = 3

You might also like