0% found this document useful (0 votes)
15 views7 pages

Test1 2022

Uploaded by

Mazwe Hlafuna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views7 pages

Test1 2022

Uploaded by

Mazwe Hlafuna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

INTERNAL TEST 1

SEMESTER 2, 2022 DATE: 16/09/2022


TIME: 120 min
SUBJECT: PROGRAMMING FOR ENGINEERS
TOTAL MARKS: 100
EXAMINER: PROF. PY TABAKOV FULL MARKS: 100
MODERATOR: PROF. HC MYBURGH

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%]

for ( int i = 100, i >= 1, i++ )


System.out.println( i );

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%]

for ( i = 19; i >= 1; i += 2 )


System.out.println( i );

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 );

4. What does the following program do? [2%]


1 public class Printing
2 {
3 public static void main ( String args [] )
4 {
5 for ( int i = 1; i <= 10; i ++ )
6 {
7 for ( int j = 1; j <= 5; j ++ )
8 System . out . print ( ’@ ’ ) ;
9 System . out . println () ;
10 } // end outer for
11 } // end main
12 } // end class Printing
13

5. Fill in the blanks in each of the following statements:

a) One-dimensional array p contains four elements. The names of those ele-


ments are , , and
. [1%]
b) Naming an array, stating its type and specifying the number of dimensions in
the array is called the array. [1%]

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%]

6. Consider a two-by-three integer array t.

a) Write a statement that declares and creates t. [1%]


b) How many rows does t have? [1%]
c) How many columns does t have? [1%]
d) How many elements does t have? [1%]
e) Write access expressions for all the elements in row 1 of t. [1%]
f) Write access expressions for all the elements in column 2 of t. [1%]
g) Write a single statement that sets the element of t in row 0 and column 1 to
zero. [1%]
h) Write a nested for statement that initializes each element of t to zero. [2%]
i) Write a series of statements that determines and displays the smallest value
in t. [2%]
j) Write a printf statement that displays the elements of the first row of t. Do
not use repetition. [2%]
k) Write a statement that totals the elements of the second column of t. Do not
use repetition. [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

3 public class ThreeDoors {


4

5 public static void main ( String [] args ) {


6 int winIfStay = 0;
7 int winIfSwitch = 0;
8 Random rnd = new Random () ;
9 int door [] = new int [4]; // use door [1] , door [2] and door [3]
10

11 for ( int k = 1; k < 1001; k ++) { // play the game 1000 times

12 for ( int j =1; j <4; j ++) door [ j ] = 0; // initialize the doors


13 // to zero ( nothing behind )
14 int car = rnd . nextInt (3) + 1; // randomly choose a door
15 door [ car ] = 1; // initialize it to 1 ( place the car behind it ) .
16 // player chooses a door (1 ,2 or 3)
17 int choice = rnd . nextInt (3) + 1;
18

19 // if the player chose the winning door ,


20 // then he wins if he stays put
21 if ( door [ choice ] == 1) {
22 winIfStay ++;
23 // if the player chose the empty door ,
24 // then he wins if he switches to another door
25 } else if ( door [ choice ] == 0) {
26 winIfSwitch ++;
27 }
28 }
29 System . out . printf ( " Win if stay : % d \ n " , winIfStay ) ;
30 System . out . printf ( " Win if switch : % d " , winIfSwitch ) ;
31 }
32 }

Output of this program looks like this:

Win if stay: 339


Win if switch: 661

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.

3. Errors: commas, infinite loop.

a) for ( int i = 100; i >= 1; i-- ) //possible variant


System.out.println( i ); // it prints numbers from 100 to 1

b) Error: the break; statement is missing

switch ( value % 2 )
{
case 0:
System.out.println( "Even integer" );
break;
case 1:
System.out.println( "Odd integer" );
break; // optional
}

c) Error: must be i-- decrement used

for ( int i = 19; i >= 1; i-=2 )


System.out.println( i );

v
d) Error: a capital letter (W) is used in keyword while

counter = 2;
do
{
System.out.println( counter );
counter += 2;
} while ( counter < 100 );

4. It prints a line (row) of 5 @ signs 10 times, i.e.:


@@@@@
@@@@@
. . . . . .
@@@@@

5. a) p[0], p[1], p[2], p[3]; b) declaring and creating; c) rows, columns;


d) m, n, m × n; e) d[2][4].

6. a) int t[][] = new int[2][3]


b) 2
c) 3
d) 6
e) t[1][0], t[1][1], t[1][2]
f) t[0][2], t[1][2]
g) t[0][1] = 0;
h) for (int i = 0; i < 2; i++)
for(int j = 0; j < 3; j++)
t[i][j] = 0;
i) int min = t[0][0];
for (int i = 0; i < 2; i++)
for(int j = 0; j < 3; j++)
if (t[i][j] < min)
min = t[i][j]
j) printf("%d %d %d", t[1][0], t[1][1], t[1][2]);
k) int s = t[0][2] + t[1][2]

7.1 class Absolute {


2 public static void main ( String args [ ]) {
3

4 int abs , num ;


5 num = 10; // or evaluated here
6 abs = num < 0 ? - num : num ; // get absolute
value of num
7 System . out . println ( " Absolute value of " + num + " is " +
abs ) ;

vi
8 } // end of main method
9 } // end of class Absolute

8. // N is entered/evaluated earlier on in the program


// the number can be very large
// possible variant is
. . . . . . . . . . . . . . . . . . . . . . . . .
long sum = 0;
long half = N / 2;
byte remainder = (byte)( N % 2);

if (remainder == 0)
sum = (N + 1) * half;
else
sum = N * half + N;
. . . . . . . . . . . . . . . . . . . . . . . . .

vii

You might also like