Test1 2022
Test1 2022
PART 1. [60%]
Problem description: Imagine that you are on a game show, and you’re given the
choice of three doors: behind one is a car, and behind the other two, nothing. The host
asks you choose one of the doors. If you’re lucky, you win a car, if not, nothing.
You pick a door, say No. 1, and the host, who knows what’s behind the doors, opens
another door, say No. 3, which has nothing behind it. Then he says to you, “Do you
want to choose door No. 2 or stay where you’re?” Is it to your advantage to switch
your choice?
Assignment: You need to write a Java application that performs a numerical simu-
lation of the above problem. Repeat the simulation 1000 times, each time randomly
placing a car behind one of three doors. Also, each time the player randomly chooses
one door, and the host randomly (if there is a choice of two) opens an empty door .
Each time, check to see if you win if you stay and check to see what happens if you
change the door. Create two variables: stay and switch and count the number of “Wins”
for each choice.
Explain the obtained result.
PART 2. [40%]
1. Explain what happens when a Java program attempts to divide one integer by
another. What happens to the fractional part of the calculation? How can a
programmer avoid that outcome? [1%]
2. Describe the four basic elements of counter-controlled repetition. [2%]
3. Find and correct the error(s) in each of the following segments of code:
a) [2%]
i
b) The following code should print whether integer value is odd or even: [2%]
switch ( value % 2 )
{
case 0:
System.out.println( "Even integer" );
case 1:
System.out.println( "Odd integer" );
}
c) The following code should output the odd integers from 19 to 1: [2%]
d) The following code should output the even integers from 2 to 100: [1%]
counter = 2;
do
{
System.out.println( counter );
counter += 2;
} While ( counter < 100 );
ii
c) In a two-dimensional array, the first index identifies the of
an element and the second index identifies the of an element.
[1%]
d) An m-by-n array contains rows, columns
and elements. [1%]
e) The name of the element in row 3 and column 5 of array d is .
[1%]
7. Write a (short) programme using the ? ternary operator. The programme must
obtain the absolute value of a variable. [4%]
8. Write the code which finds the sum of first N natural numbers (1 + 2 + 3 + . . . + N ).
Assume that number N is already declared and the integer value is assigned to it.
[5%]
iii
Model answers
PART 1:
1 import java . util . Random ;
2
11 for ( int k = 1; k < 1001; k ++) { // play the game 1000 times
Explanation of the obtained result: When you choose a door, your chance to win
1 2
a car is while the probability to choose an empty door is . This means that you have
3 3
a better chance of picking an empty door, but when the host opens another empty door,
it just means that the probability that the car is behind the remaining door is higher,
iv
2 1
i.e. increases to . Therefore, if you stay, you have chance to win a car, if you switch
3 3
2
a door your chance becomes .
3
1
Alternatively, it can be explained like that: your original choice is probability to win
3
2
the car, the remaining doors are probability. When the hosts opens one of the two
3
2
doors, the probability stays unchanged.
3
PART 2:
1. When two integers are divided, the answer is a whole number (e.g. 10 / 2 = 5,
10 / 3 = 3). If there is a fractional part (remainder) of the calculations, then it
is lost. To obtain the remainder, the % sign is used for the division of two integer
numbers(e.g. 10 % 3 = 1 ).
2. A counter controlled loop is the definite repetition loop as the number of repetitions
is known before the loop begins executing.
Counter-controlled repetition requires: 1) a control variable (or loop counter); 2)
the initial value of the control variable; 3) the increment (or decrement) by which
the control variable is modified each time through the loop (also known as each iter-
ation of the loop); and 4) the loop-continuation condition that determines whether
looping should continue.
switch ( value % 2 )
{
case 0:
System.out.println( "Even integer" );
break;
case 1:
System.out.println( "Odd integer" );
break; // optional
}
v
d) Error: a capital letter (W) is used in keyword while
counter = 2;
do
{
System.out.println( counter );
counter += 2;
} while ( counter < 100 );
vi
8 } // end of main method
9 } // end of class Absolute
if (remainder == 0)
sum = (N + 1) * half;
else
sum = N * half + N;
. . . . . . . . . . . . . . . . . . . . . . . . .
vii